Skip to content
This repository
Newer
Older
100644 135 lines (112 sloc) 3.882 kb
7ed819ad » chneukirchen
2007-02-16 Add Rack::Response and Rack::Utils
1 require 'rack/utils'
2
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
3 module Rack
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
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"]
230d62c7 » chneukirchen
2007-05-16 Fix trailing whitespace. Sigh.
11
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
12 class Request
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
13 # The environment of the request.
05ab0f90 » chneukirchen
2007-02-18 More convenience for Rack::Request
14 attr_reader :env
230d62c7 » chneukirchen
2007-05-16 Fix trailing whitespace. Sigh.
15
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
16 def initialize(env)
17 @env = env
18 end
19
a18fc325 » chneukirchen
2007-02-18 Rename Request#method to #request_method to not confuse stdlibs
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
166ab15f » qerub
2007-02-21 Add getter method for the query string (and use it internally)
26 def query_string; @env["QUERY_STRING"].to_s end
a18fc325 » chneukirchen
2007-02-18 Rename Request#method to #request_method to not confuse stdlibs
27
c95b7516 » chneukirchen
2007-02-28 Remove the port number of HTTP_HOST and SERVER_NAME
28 def host
29 # Remove port number.
30 (@env["HTTP_HOST"] || @env["SERVER_NAME"]).gsub(/:\d+\z/, '')
31 end
32
a18fc325 » chneukirchen
2007-02-18 Rename Request#method to #request_method to not confuse stdlibs
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
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
40
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
41 # Returns the data recieved in the query string.
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
42 def GET
166ab15f » qerub
2007-02-21 Add getter method for the query string (and use it internally)
43 if @env["rack.request.query_string"] == query_string
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
44 @env["rack.request.query_hash"]
45 else
166ab15f » qerub
2007-02-21 Add getter method for the query string (and use it internally)
46 @env["rack.request.query_string"] = query_string
47 @env["rack.request.query_hash"] =
48 Utils.parse_query(query_string)
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
49 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
50 end
51
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
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.
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
56 def POST
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
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"]
4fe5360a » chneukirchen
2007-02-28 Make Rack::Request read multipart form data
61 unless @env["rack.request.form_hash"] =
230d62c7 » chneukirchen
2007-05-16 Fix trailing whitespace. Sigh.
62 Utils::Multipart.parse_multipart(env)
4fe5360a » chneukirchen
2007-02-28 Make Rack::Request read multipart form data
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"]
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
67 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
68 end
69
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
70 # The union of GET and POST data.
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
71 def params
72 self.GET.update(self.POST)
73 end
74
de7411c5 » manveru
2007-05-15 add some features to Request and the corresponding tests for them
75 # shortcut for request.params[key]
76 def [](key)
77 params[key.to_s]
78 end
79
80 # shortcut for request.params[key] = value
81 def []=(key, value)
82 params[key.to_s] = value
83 end
84
85 # like Hash#values_at
86 def values_at(*keys)
87 keys.map{|key| params[key] }
88 end
89
90 # the referer of the client or '/'
91 def referer
92 @env['HTTP_REFERER'] || '/'
93 end
94 alias referrer referer
95
96
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
97 def cookies
b68c2d51 » chneukirchen
2007-02-19 Return empty hash on lack of cookies
98 return {} unless @env["HTTP_COOKIE"]
99
6c80c6cf » chneukirchen
2007-02-19 Cache the parsed things in Rack::Request
100 if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
101 @env["rack.request.cookie_hash"]
102 else
103 @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
104 # XXX sure?
105 @env["rack.request.cookie_hash"] =
106 Utils.parse_query(@env["rack.request.cookie_string"], ';,')
107 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
108 end
109
110 def xhr?
111 @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
112 end
fd51d363 » chneukirchen
2007-02-21 Add Request#url
113
376fa1e3 » chneukirchen
2007-03-01 Add RDocs
114 # Tries to return a remake of the original request URL as a string.
fd51d363 » chneukirchen
2007-02-21 Add Request#url
115 def url
116 url = scheme + "://"
117 url << host
118
119 if scheme == "https" && port != 443 ||
120 scheme == "http" && port != 80
121 url << ":#{port}"
122 end
123
234c062f » chneukirchen
2007-03-30 Add Request#fullpath
124 url << fullpath
fd51d363 » chneukirchen
2007-02-21 Add Request#url
125
126 url
127 end
234c062f » chneukirchen
2007-03-30 Add Request#fullpath
128
129 def fullpath
130 path = script_name + path_info
131 path << "?" << query_string unless query_string.empty?
132 path
133 end
5f13e3d0 » chneukirchen
2007-02-16 Add Rack::Request
134 end
135 end
Something went wrong with that request. Please try again.