Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (94 sloc) 3.439 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"]
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
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
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
27
c95b751 @chneukirchen Remove the port number of HTTP_HOST and SERVER_NAME
chneukirchen authored
28 def host
29 # Remove port number.
30 (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
31 end
32
a18fc32 @chneukirchen Rename Request#method to #request_method to not confuse stdlibs
chneukirchen authored
33 def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
34 def path_info=(s); @env["PATH_INFO"] = s.to_s end
35
36 def get?; request_method == "GET" end
37 def post?; request_method == "POST" end
38 def put?; request_method == "PUT" end
39 def delete?; request_method == "DELETE" end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
40
376fa1e @chneukirchen Add RDocs
chneukirchen authored
41 # Returns the data recieved in the query string.
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
42 def GET
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
43 if @env["rack.request.query_string"] == query_string
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
44 @env["rack.request.query_hash"]
45 else
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
46 @env["rack.request.query_string"] = query_string
47 @env["rack.request.query_hash"] =
48 Utils.parse_query(query_string)
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
49 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
50 end
51
376fa1e @chneukirchen Add RDocs
chneukirchen authored
52 # Returns the data recieved in the request body.
53 #
54 # This method support both application/x-www-form-urlencoded and
55 # multipart/form-data.
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
56 def POST
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
57 if @env["rack.request.form_input"] == @env["rack.input"]
58 @env["rack.request.form_hash"]
59 else
60 @env["rack.request.form_input"] = @env["rack.input"]
4fe5360 @chneukirchen Make Rack::Request read multipart form data
chneukirchen authored
61 unless @env["rack.request.form_hash"] =
62 Utils::Multipart.parse_multipart(env)
63 @env["rack.request.form_vars"] = @env["rack.input"].read
64 @env["rack.request.form_hash"] = Utils.parse_query(@env["rack.request.form_vars"])
65 end
66 @env["rack.request.form_hash"]
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
67 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
68 end
69
376fa1e @chneukirchen Add RDocs
chneukirchen authored
70 # The union of GET and POST data.
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
71 def params
72 self.GET.update(self.POST)
73 end
74
75 def cookies
b68c2d5 @chneukirchen Return empty hash on lack of cookies
chneukirchen authored
76 return {} unless @env["HTTP_COOKIE"]
77
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
78 if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
79 @env["rack.request.cookie_hash"]
80 else
81 @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
82 # XXX sure?
83 @env["rack.request.cookie_hash"] =
84 Utils.parse_query(@env["rack.request.cookie_string"], ';,')
85 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
86 end
87
88 def xhr?
89 @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
90 end
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
91
376fa1e @chneukirchen Add RDocs
chneukirchen authored
92 # Tries to return a remake of the original request URL as a string.
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
93 def url
94 url = scheme + "://"
95 url << host
96
97 if scheme == "https" && port != 443 ||
98 scheme == "http" && port != 80
99 url << ":#{port}"
100 end
101
102 url << script_name
103 url << path_info
104
166ab15 @qerub Add getter method for the query string (and use it internally)
qerub authored
105 unless query_string.empty?
106 url << "?" << query_string
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
107 end
108
109 url
110 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
111 end
112 end
Something went wrong with that request. Please try again.