Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 90 lines (73 sloc) 2.62 kb
7ed819a @chneukirchen Add Rack::Response and Rack::Utils
chneukirchen authored
1 require 'rack/utils'
2
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
3 module Rack
4 class Request
05ab0f9 @chneukirchen More convenience for Rack::Request
chneukirchen authored
5 attr_reader :env
6
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
7 def initialize(env)
8 @env = env
9 end
10
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
11 def body; @env["rack.input"] end
12 def scheme; @env["rack.url_scheme"] end
13 def script_name; @env["SCRIPT_NAME"].to_s end
14 def path_info; @env["PATH_INFO"].to_s end
15 def host; @env["HTTP_HOST"] || @env["SERVER_NAME"] end
16 def port; @env["SERVER_PORT"].to_i end
17 def request_method; @env["REQUEST_METHOD"] end
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
18 def query_string; @env["QUERY_STRING"].to_s end
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
19
20 def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
21 def path_info=(s); @env["PATH_INFO"] = s.to_s end
22
23 def get?; request_method == "GET" end
24 def post?; request_method == "POST" end
25 def put?; request_method == "PUT" end
26 def delete?; request_method == "DELETE" end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
27
28 def GET
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
29 if @env["rack.request.query_string"] == query_string
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
30 @env["rack.request.query_hash"]
31 else
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
32 @env["rack.request.query_string"] = query_string
33 @env["rack.request.query_hash"] =
34 Utils.parse_query(query_string)
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
35 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
36 end
37
38 def POST
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
39 if @env["rack.request.form_input"] == @env["rack.input"]
40 @env["rack.request.form_hash"]
41 else
42 @env["rack.request.form_input"] = @env["rack.input"]
43 @env["rack.request.form_vars"] = body.read
44 @env["rack.request.form_hash"] =
45 Utils.parse_query(@env["rack.request.form_vars"])
46 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
47 end
48
49 def params
50 self.GET.update(self.POST)
51 end
52
53 def cookies
b68c2d5 @chneukirchen Return empty hash on lack of cookies
chneukirchen authored
54 return {} unless @env["HTTP_COOKIE"]
55
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
56 if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
57 @env["rack.request.cookie_hash"]
58 else
59 @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
60 # XXX sure?
61 @env["rack.request.cookie_hash"] =
62 Utils.parse_query(@env["rack.request.cookie_string"], ';,')
63 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
64 end
65
66 def xhr?
67 @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
68 end
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
69
70 def url
71 url = scheme + "://"
72 url << host
73
74 if scheme == "https" && port != 443 ||
75 scheme == "http" && port != 80
76 url << ":#{port}"
77 end
78
79 url << script_name
80 url << path_info
81
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
82 unless query_string.empty?
83 url << "?" << query_string
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
84 end
85
86 url
87 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
88 end
89 end
Something went wrong with that request. Please try again.