Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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