Class: TDiary::CGICompat

Inherits:
Object
  • Object
show all
Includes:
RequestExtension
Defined in:
lib/tdiary/cgi_compat.rb

Overview

class CGICompat provides the CGI compatible interface consumed by plugins as @cgi on top of TDiary::Request. Behaviour is locked by the shared examples in spec/support/cgi_compat_shared_examples.rb, which are also applied to RackCGI, the subclass the dispatcher hands to plugins on the Rack path.

Direct Known Subclasses

RackCGI

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestExtension

#mobile_agent?, #smartphone?

Constructor Details

#initialize(request) ⇒ CGICompat

Returns a new instance of CGICompat.



14
15
16
17
18
19
20
# File 'lib/tdiary/cgi_compat.rb', line 14

def initialize( request )
	@request = request
	# built eagerly so that a clone (Object#clone copies instance
	# variables by reference) shares the same params Hash, like a
	# cloned CGI instance does. Plugins may still rely on this.
	@params = build_params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



12
13
14
# File 'lib/tdiary/cgi_compat.rb', line 12

def request
  @request
end

Instance Method Details

#auth_typeObject



62
63
64
# File 'lib/tdiary/cgi_compat.rb', line 62

def auth_type
	env_table['AUTH_TYPE']
end

#base_urlObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tdiary/cgi_compat.rb', line 105

def base_url
	return '' unless script_name
	begin
		script_dirname = script_name.empty? ? '' : File::dirname(script_name)
		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

#cookiesObject



28
29
30
31
32
# File 'lib/tdiary/cgi_compat.rb', line 28

def cookies
	# CGI::Cookie keeps the multi-value cookie behaviour (00default.rb
	# reads name and mail from the two values of the tdiary cookie)
	@cookies ||= CGI::Cookie.parse( env_table['HTTP_COOKIE'] )
end

#env_tableObject



34
35
36
# File 'lib/tdiary/cgi_compat.rb', line 34

def env_table
	@request.env
end

#gateway_interfaceObject



66
67
68
# File 'lib/tdiary/cgi_compat.rb', line 66

def gateway_interface
	env_table['GATEWAY_INTERFACE']
end

#https?Boolean

the URL helpers below come from the CGI patches that used to live in core_ext.rb

Returns:

  • (Boolean)


80
81
82
83
84
# File 'lib/tdiary/cgi_compat.rb', line 80

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

#redirect_urlObject



101
102
103
# File 'lib/tdiary/cgi_compat.rb', line 101

def redirect_url
	env_table['REDIRECT_URL']
end

#refererObject



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

def referer
	env_table['HTTP_REFERER']
end

#remote_addrObject



46
47
48
# File 'lib/tdiary/cgi_compat.rb', line 46

def remote_addr
	env_table['REMOTE_ADDR']
end

#remote_userObject



58
59
60
# File 'lib/tdiary/cgi_compat.rb', line 58

def remote_user
	env_table['REMOTE_USER']
end

#request_methodObject



50
51
52
# File 'lib/tdiary/cgi_compat.rb', line 50

def request_method
	env_table['REQUEST_METHOD']
end

#request_uriObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tdiary/cgi_compat.rb', line 86

def request_uri
	_request_uri = env_table['REQUEST_URI']
	_script_name = env_table['SCRIPT_NAME']
	if !_request_uri || _request_uri == '' || _request_uri == _script_name then
		_path_info    = env_table['PATH_INFO'] || ''
		_query_string = env_table['QUERY_STRING'] || ''
		# Workaround for IIS-style PATH_INFO ('/dir/script.cgi/path', not '/path')
		# See http://support.microsoft.com/kb/184320/
		_request_uri = _path_info.include?(_script_name) ? '' : _script_name.dup
		_request_uri << _path_info
		_request_uri << '?' + _query_string if _query_string != ''
	end
	_request_uri
end

#script_nameObject



54
55
56
# File 'lib/tdiary/cgi_compat.rb', line 54

def script_name
	env_table['SCRIPT_NAME']
end

#server_nameObject



70
71
72
# File 'lib/tdiary/cgi_compat.rb', line 70

def server_name
	env_table['SERVER_NAME']
end

#server_portObject



74
75
76
# File 'lib/tdiary/cgi_compat.rb', line 74

def server_port
	env_table['SERVER_PORT'].to_i
end

#user_agentObject



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

def user_agent
	env_table['HTTP_USER_AGENT']
end

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/tdiary/cgi_compat.rb', line 24

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