Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 198 lines (169 sloc) 6.551 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
376fa1e @chneukirchen Add RDocs
chneukirchen authored
4 # Rack::Request provides a convenient interface to a Rack
5 # environment. It is stateless, the environment +env+ passed to the
6 # constructor will be directly modified.
7 #
8 # req = Rack::Request.new(env)
9 # req.post?
10 # req.params["data"]
230d62c @chneukirchen Fix trailing whitespace. Sigh.
chneukirchen authored
11
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
12 class Request
376fa1e @chneukirchen Add RDocs
chneukirchen authored
13 # The environment of the request.
05ab0f9 @chneukirchen More convenience for Rack::Request
chneukirchen authored
14 attr_reader :env
230d62c @chneukirchen Fix trailing whitespace. Sigh.
chneukirchen authored
15
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
16 def initialize(env)
17 @env = env
18 end
19
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
20 def body; @env["rack.input"] end
21 def scheme; @env["rack.url_scheme"] end
22 def script_name; @env["SCRIPT_NAME"].to_s end
23 def path_info; @env["PATH_INFO"].to_s end
24 def port; @env["SERVER_PORT"].to_i end
25 def request_method; @env["REQUEST_METHOD"] end
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
26 def query_string; @env["QUERY_STRING"].to_s end
fe40ec2 @chneukirchen Fix that Request assumes form-data even when Content-Type says otherwise
chneukirchen authored
27 def content_length; @env['CONTENT_LENGTH'] end
28 def content_type; @env['CONTENT_TYPE'] end
29
30 # The media type (type/subtype) portion of the CONTENT_TYPE header
31 # without any media type parameters. e.g., when CONTENT_TYPE is
32 # "text/plain;charset=utf-8", the media-type is "text/plain".
33 #
34 # For more information on the use of media types in HTTP, see:
35 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
36 def media_type
37 content_type && content_type.split(/\s*[;,]\s*/, 2)[0].downcase
38 end
39
40 # The media type parameters provided in CONTENT_TYPE as a Hash, or
41 # an empty Hash if no CONTENT_TYPE or media-type parameters were
42 # provided. e.g., when the CONTENT_TYPE is "text/plain;charset=utf-8",
43 # this method responds with the following Hash:
44 # { 'charset' => 'utf-8' }
45 def media_type_params
46 return {} if content_type.nil?
47 content_type.split(/\s*[;,]\s*/)[1..-1].
48 collect { |s| s.split('=', 2) }.
49 inject({}) { |hash,(k,v)| hash[k.downcase] = v ; hash }
50 end
51
52 # The character set of the request body if a "charset" media type
53 # parameter was given, or nil if no "charset" was specified. Note
54 # that, per RFC2616, text/* media types that specify no explicit
55 # charset are to be considered ISO-8859-1.
56 def content_charset
57 media_type_params['charset']
58 end
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
59
c95b751 @chneukirchen Remove the port number of HTTP_HOST and SERVER_NAME
chneukirchen authored
60 def host
61 # Remove port number.
62 (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
63 end
64
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
65 def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
66 def path_info=(s); @env["PATH_INFO"] = s.to_s end
67
68 def get?; request_method == "GET" end
69 def post?; request_method == "POST" end
70 def put?; request_method == "PUT" end
71 def delete?; request_method == "DELETE" end
fe40ec2 @chneukirchen Fix that Request assumes form-data even when Content-Type says otherwise
chneukirchen authored
72 def head?; request_method == "HEAD" end
73
74 # The set of form-data media-types. Requests that do not indicate
75 # one of the media types presents in this list will not be eligible
76 # for form-data / param parsing.
77 FORM_DATA_MEDIA_TYPES = [
78 nil,
79 'application/x-www-form-urlencoded',
80 'multipart/form-data'
81 ]
82
83 # Determine whether the request body contains form-data by checking
84 # the request media_type against registered form-data media-types:
85 # "application/x-www-form-urlencoded" and "multipart/form-data". The
86 # list of form-data media types can be modified through the
87 # +FORM_DATA_MEDIA_TYPES+ array.
88 def form_data?
89 FORM_DATA_MEDIA_TYPES.include?(media_type)
90 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
91
376fa1e @chneukirchen Add RDocs
chneukirchen authored
92 # Returns the data recieved in the query string.
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
93 def GET
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
94 if @env["rack.request.query_string"] == query_string
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
95 @env["rack.request.query_hash"]
96 else
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
97 @env["rack.request.query_string"] = query_string
98 @env["rack.request.query_hash"] =
99 Utils.parse_query(query_string)
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
100 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
101 end
102
376fa1e @chneukirchen Add RDocs
chneukirchen authored
103 # Returns the data recieved in the request body.
104 #
105 # This method support both application/x-www-form-urlencoded and
106 # multipart/form-data.
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
107 def POST
7f34329 Bug fix for Tempfile POST bodies under Ruby 1.8
Adam Harper authored
108 if @env["rack.request.form_input"].eql? @env["rack.input"]
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
109 @env["rack.request.form_hash"]
fe40ec2 @chneukirchen Fix that Request assumes form-data even when Content-Type says otherwise
chneukirchen authored
110 elsif form_data?
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
111 @env["rack.request.form_input"] = @env["rack.input"]
4fe5360 @chneukirchen Make Rack::Request read multipart form data
chneukirchen authored
112 unless @env["rack.request.form_hash"] =
230d62c @chneukirchen Fix trailing whitespace. Sigh.
chneukirchen authored
113 Utils::Multipart.parse_multipart(env)
4fe5360 @chneukirchen Make Rack::Request read multipart form data
chneukirchen authored
114 @env["rack.request.form_vars"] = @env["rack.input"].read
115 @env["rack.request.form_hash"] = Utils.parse_query(@env["rack.request.form_vars"])
116 end
117 @env["rack.request.form_hash"]
fe40ec2 @chneukirchen Fix that Request assumes form-data even when Content-Type says otherwise
chneukirchen authored
118 else
119 {}
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
120 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
121 end
122
376fa1e @chneukirchen Add RDocs
chneukirchen authored
123 # The union of GET and POST data.
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
124 def params
125 self.GET.update(self.POST)
67a7507 @chneukirchen handle EOFError exception in Request#params
chneukirchen authored
126 rescue EOFError => e
127 self.GET
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
128 end
129
de7411c @manveru add some features to Request and the corresponding tests for them
manveru authored
130 # shortcut for request.params[key]
131 def [](key)
132 params[key.to_s]
133 end
134
135 # shortcut for request.params[key] = value
136 def []=(key, value)
137 params[key.to_s] = value
138 end
139
140 # like Hash#values_at
141 def values_at(*keys)
142 keys.map{|key| params[key] }
143 end
144
145 # the referer of the client or '/'
146 def referer
147 @env['HTTP_REFERER'] || '/'
148 end
149 alias referrer referer
150
151
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
152 def cookies
b68c2d5 @chneukirchen Return empty hash on lack of cookies
chneukirchen authored
153 return {} unless @env["HTTP_COOKIE"]
154
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
155 if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
156 @env["rack.request.cookie_hash"]
157 else
158 @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
4a08f8a Conform to RFC 2109 regarding multiple values for same cookie
Aman Gupta authored
159 # According to RFC 2109:
160 # If multiple cookies satisfy the criteria above, they are ordered in
161 # the Cookie header such that those with more specific Path attributes
162 # precede those with less specific. Ordering with respect to other
163 # attributes (e.g., Domain) is unspecified.
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
164 @env["rack.request.cookie_hash"] =
4a08f8a Conform to RFC 2109 regarding multiple values for same cookie
Aman Gupta authored
165 Utils.parse_query(@env["rack.request.cookie_string"], ';,').inject({}) {|h,(k,v)|
acb8695 @chneukirchen Fix cookie parsing
chneukirchen authored
166 h[k] = Array === v ? v.first : v
4a08f8a Conform to RFC 2109 regarding multiple values for same cookie
Aman Gupta authored
167 h
168 }
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
169 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
170 end
171
172 def xhr?
173 @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
174 end
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
175
376fa1e @chneukirchen Add RDocs
chneukirchen authored
176 # Tries to return a remake of the original request URL as a string.
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
177 def url
178 url = scheme + "://"
179 url << host
180
181 if scheme == "https" && port != 443 ||
182 scheme == "http" && port != 80
183 url << ":#{port}"
184 end
185
234c062 @chneukirchen Add Request#fullpath
chneukirchen authored
186 url << fullpath
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
187
188 url
189 end
234c062 @chneukirchen Add Request#fullpath
chneukirchen authored
190
191 def fullpath
192 path = script_name + path_info
193 path << "?" << query_string unless query_string.empty?
194 path
195 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
196 end
197 end
Something went wrong with that request. Please try again.