| 1 |
module ActionController |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
class AbstractRequest |
|---|
| 5 |
cattr_accessor :relative_url_root |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
attr_reader :env |
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
def parameters |
|---|
| 13 |
@parameters ||= request_parameters.update(query_parameters).update(path_parameters).with_indifferent_access |
|---|
| 14 |
end |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
def method |
|---|
| 20 |
@request_method ||= (!parameters[:_method].blank? && @env['REQUEST_METHOD'] == 'POST') ? |
|---|
| 21 |
parameters[:_method].to_s.downcase.to_sym : |
|---|
| 22 |
@env['REQUEST_METHOD'].downcase.to_sym |
|---|
| 23 |
|
|---|
| 24 |
@request_method == :head ? :get : @request_method |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
def get? |
|---|
| 29 |
method == :get |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
def post? |
|---|
| 34 |
method == :post |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
def put? |
|---|
| 39 |
method == :put |
|---|
| 40 |
end |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
def delete? |
|---|
| 44 |
method == :delete |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
def head? |
|---|
| 50 |
@env['REQUEST_METHOD'].downcase.to_sym == :head |
|---|
| 51 |
end |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
def content_type |
|---|
| 59 |
@content_type ||= |
|---|
| 60 |
begin |
|---|
| 61 |
content_type = @env['CONTENT_TYPE'].to_s.downcase |
|---|
| 62 |
|
|---|
| 63 |
if x_post_format = @env['HTTP_X_POST_DATA_FORMAT'] |
|---|
| 64 |
case x_post_format.to_s.downcase |
|---|
| 65 |
when 'yaml' |
|---|
| 66 |
content_type = 'application/x-yaml' |
|---|
| 67 |
when 'xml' |
|---|
| 68 |
content_type = 'application/xml' |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
Mime::Type.lookup(content_type) |
|---|
| 73 |
end |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
def accepts |
|---|
| 78 |
@accepts ||= |
|---|
| 79 |
if @env['HTTP_ACCEPT'].to_s.strip.empty? |
|---|
| 80 |
[ content_type, Mime::ALL ] |
|---|
| 81 |
else |
|---|
| 82 |
Mime::Type.parse(@env['HTTP_ACCEPT']) |
|---|
| 83 |
end |
|---|
| 84 |
end |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
def xml_http_request? |
|---|
| 90 |
not /XMLHttpRequest/i.match(@env['HTTP_X_REQUESTED_WITH']).nil? |
|---|
| 91 |
end |
|---|
| 92 |
alias xhr? :xml_http_request? |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
def remote_ip |
|---|
| 101 |
return @env['HTTP_CLIENT_IP'] if @env.include? 'HTTP_CLIENT_IP' |
|---|
| 102 |
|
|---|
| 103 |
if @env.include? 'HTTP_X_FORWARDED_FOR' then |
|---|
| 104 |
remote_ips = @env['HTTP_X_FORWARDED_FOR'].split(',').reject do |ip| |
|---|
| 105 |
ip =~ /^unknown$|^(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\./i |
|---|
| 106 |
end |
|---|
| 107 |
|
|---|
| 108 |
return remote_ips.first.strip unless remote_ips.empty? |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
@env['REMOTE_ADDR'] |
|---|
| 112 |
end |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
def domain(tld_length = 1) |
|---|
| 117 |
return nil if !/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(host).nil? or host.nil? |
|---|
| 118 |
|
|---|
| 119 |
host.split('.').last(1 + tld_length).join('.') |
|---|
| 120 |
end |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
def subdomains(tld_length = 1) |
|---|
| 126 |
return [] unless host |
|---|
| 127 |
parts = host.split('.') |
|---|
| 128 |
parts[0..-(tld_length+2)] |
|---|
| 129 |
end |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
def raw_post |
|---|
| 135 |
@env['RAW_POST_DATA'] |
|---|
| 136 |
end |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
def request_uri |
|---|
| 141 |
if uri = @env['REQUEST_URI'] |
|---|
| 142 |
|
|---|
| 143 |
(%r{^\w+\://[^/]+(/.*|$)$} =~ uri) ? $1 : uri |
|---|
| 144 |
else |
|---|
| 145 |
|
|---|
| 146 |
script_filename = @env['SCRIPT_NAME'].to_s.match(%r{[^/]+$}) |
|---|
| 147 |
uri = @env['PATH_INFO'] |
|---|
| 148 |
uri = uri.sub(/ |
|---|
| 149 |
unless (env_qs = @env['QUERY_STRING']).nil? || env_qs.empty? |
|---|
| 150 |
uri << '?' << env_qs |
|---|
| 151 |
end |
|---|
| 152 |
@env['REQUEST_URI'] = uri |
|---|
| 153 |
end |
|---|
| 154 |
end |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
def protocol |
|---|
| 158 |
ssl? ? 'https://' : 'http://' |
|---|
| 159 |
end |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
def ssl? |
|---|
| 163 |
@env['HTTPS'] == 'on' || @env['HTTP_X_FORWARDED_PROTO'] == 'https' |
|---|
| 164 |
end |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
def path |
|---|
| 168 |
path = (uri = request_uri) ? uri.split('?').first : '' |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
path.sub!(%r/^ |
|---|
| 172 |
path || '' |
|---|
| 173 |
end |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
def relative_url_root |
|---|
| 180 |
@@relative_url_root ||= case |
|---|
| 181 |
when @env["RAILS_RELATIVE_URL_ROOT"] |
|---|
| 182 |
@env["RAILS_RELATIVE_URL_ROOT"] |
|---|
| 183 |
when server_software == 'apache' |
|---|
| 184 |
@env["SCRIPT_NAME"].to_s.sub(/\/dispatch\.(fcgi|rb|cgi)$/, '') |
|---|
| 185 |
else |
|---|
| 186 |
'' |
|---|
| 187 |
end |
|---|
| 188 |
end |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
def port |
|---|
| 192 |
@port_as_int ||= @env['SERVER_PORT'].to_i |
|---|
| 193 |
end |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
def standard_port |
|---|
| 197 |
case protocol |
|---|
| 198 |
when 'https://' then 443 |
|---|
| 199 |
else 80 |
|---|
| 200 |
end |
|---|
| 201 |
end |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
def port_string |
|---|
| 206 |
(port == standard_port) ? '' : ":#{port}" |
|---|
| 207 |
end |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
def host_with_port |
|---|
| 212 |
host + port_string |
|---|
| 213 |
end |
|---|
| 214 |
|
|---|
| 215 |
def path_parameters=(parameters) |
|---|
| 216 |
@path_parameters = parameters |
|---|
| 217 |
@symbolized_path_parameters = @parameters = nil |
|---|
| 218 |
end |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
def symbolized_path_parameters |
|---|
| 222 |
@symbolized_path_parameters ||= path_parameters.symbolize_keys |
|---|
| 223 |
end |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
def path_parameters |
|---|
| 231 |
@path_parameters ||= {} |
|---|
| 232 |
end |
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
def server_software |
|---|
| 236 |
(@env['SERVER_SOFTWARE'] && /^([a-zA-Z]+)/ =~ @env['SERVER_SOFTWARE']) ? $1.downcase : nil |
|---|
| 237 |
end |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
def query_parameters |
|---|
| 243 |
end |
|---|
| 244 |
|
|---|
| 245 |
def request_parameters |
|---|
| 246 |
end |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
def host |
|---|
| 250 |
end |
|---|
| 251 |
|
|---|
| 252 |
def cookies |
|---|
| 253 |
end |
|---|
| 254 |
|
|---|
| 255 |
def session |
|---|
| 256 |
end |
|---|
| 257 |
|
|---|
| 258 |
def session=(session) |
|---|
| 259 |
@session = session |
|---|
| 260 |
end |
|---|
| 261 |
|
|---|
| 262 |
def reset_session |
|---|
| 263 |
end |
|---|
| 264 |
end |
|---|
| 265 |
end |
|---|