Module: TDiary::RequestExtension

Included in:
CGICompat, Request
Defined in:
lib/tdiary/core_ext.rb

Overview

CGI-semantics readers shared by TDiary::Request and the CGICompat facade (which forwards env to the request it wraps, so state memoized in the env is visible through either object).

Instance Method Summary collapse

Instance Method Details

#cgi_cookiesObject

multi-value cookies, unlike Rack::Request#cookies: CGI::Cookie splits each value on '&' (the tdiary cookie carries 'name&mail')



48
49
50
# File 'lib/tdiary/core_ext.rb', line 48

def cgi_cookies
	@cgi_cookies ||= CGI::Cookie.parse( env['HTTP_COOKIE'] )
end

#cgi_paramsObject

params with CGI semantics, unlike Rack::Request#params: POST reads only the body, every value is an Array, unknown keys yield [], duplicated keys collect all values, broken UTF-8 input is retried as Shift_JIS and multipart uploads are normalized to their tempfiles. Memoized so the request and its CGICompat facade (which delegates here) share one Hash; plugins mutate it through



33
34
35
# File 'lib/tdiary/core_ext.rb', line 33

def cgi_params
	@cgi_params ||= build_cgi_params
end

#hide_referer!Object

TDiaryView hides the referer for the rest of the request when the referer filters reject it; plugins observe nil through the @cgi facade too, which delegates both methods to the request



55
56
57
# File 'lib/tdiary/core_ext.rb', line 55

def hide_referer!
	@referer_hidden = true
end

#https?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/tdiary/core_ext.rb', line 71

def https?
	return true if env['HTTP_X_FORWARDED_PROTO'] == 'https'
	return false if env['HTTPS'].nil? or /off/i =~ env['HTTPS'] or env['HTTPS'] == ''
	true
end

#mobile_agent?Boolean

backward compatibility, returns NOT mobile phone always

Returns:

  • (Boolean)


9
10
11
# File 'lib/tdiary/core_ext.rb', line 9

def mobile_agent?
	false
end

#param(key, idx = 0) ⇒ Object

single-value accessor over cgi_params; nil when the key is absent



38
39
40
# File 'lib/tdiary/core_ext.rb', line 38

def param( key, idx = 0 )
	cgi_params[key][idx]
end

#refererObject



59
60
61
# File 'lib/tdiary/core_ext.rb', line 59

def referer
	@referer_hidden ? nil : env['HTTP_REFERER']
end

#remote_addrObject



63
64
65
# File 'lib/tdiary/core_ext.rb', line 63

def remote_addr
	env['REMOTE_ADDR']
end

#remote_userObject



67
68
69
# File 'lib/tdiary/core_ext.rb', line 67

def remote_user
	env['REMOTE_USER']
end

#smartphone?Boolean

backward compatibility, returns NOT smartphone always

Returns:

  • (Boolean)


14
15
16
# File 'lib/tdiary/core_ext.rb', line 14

def smartphone?
	false
end

#static_assets?Boolean

true when the endpoint runs behind a web server (CGI/FCGI hosting, which sets tdiary.static_assets in the env) that serves the js/ and theme/ directories itself; false on the Rack path, where TDiary::Application serves them under assets/

Returns:

  • (Boolean)


22
23
24
# File 'lib/tdiary/core_ext.rb', line 22

def static_assets?
	env['tdiary.static_assets'] ? true : false
end

#tdiary_base_urlObject

not Rack::Request#base_url (scheme and host only): the CGI flavour with the SCRIPT_NAME dirname and a trailing '/'



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tdiary/core_ext.rb', line 79

def tdiary_base_url
	script_name = env['SCRIPT_NAME']
	return '' unless script_name
	begin
		script_dirname = script_name.empty? ? '' : File::dirname( script_name )
		server_name = env['SERVER_NAME']
		server_port = env['SERVER_PORT'].to_i
		if https?
			port = (server_port == 443) ? '' : ':' + server_port.to_s
			"https://#{server_name}#{port}#{script_dirname}/"
		else
			port = (server_port == 80) ? '' : ':' + server_port.to_s
			"http://#{server_name}#{port}#{script_dirname}/"
		end.sub(%r|/+$|, '/')
	rescue SecurityError
		''
	end
end

#valid?(param, idx = 0) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tdiary/core_ext.rb', line 42

def valid?( param, idx = 0 )
	cgi_params[param] and cgi_params[param][idx] and cgi_params[param][idx].length > 0
end