Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 89 lines (72 sloc) 2.61 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
18
19 def script_name=(s); @env["SCRIPT_NAME"] = s.to_s end
20 def path_info=(s); @env["PATH_INFO"] = s.to_s end
21
22 def get?; request_method == "GET" end
23 def post?; request_method == "POST" end
24 def put?; request_method == "PUT" end
25 def delete?; request_method == "DELETE" end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
26
27 def GET
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
28 if @env["rack.request.query_string"] == @env["QUERY_STRING"]
29 @env["rack.request.query_hash"]
30 else
31 @env["rack.request.query_string"] = @env["QUERY_STRING"]
32 @env["rack.request.query_hash"] =
33 Utils.parse_query(@env["QUERY_STRING"])
34 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
35 end
36
37 def POST
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
38 if @env["rack.request.form_input"] == @env["rack.input"]
39 @env["rack.request.form_hash"]
40 else
41 @env["rack.request.form_input"] = @env["rack.input"]
42 @env["rack.request.form_vars"] = body.read
43 @env["rack.request.form_hash"] =
44 Utils.parse_query(@env["rack.request.form_vars"])
45 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
46 end
47
48 def params
49 self.GET.update(self.POST)
50 end
51
52 def cookies
b68c2d5 @chneukirchen Return empty hash on lack of cookies
chneukirchen authored
53 return {} unless @env["HTTP_COOKIE"]
54
6c80c6c @chneukirchen Cache the parsed things in Rack::Request
chneukirchen authored
55 if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"]
56 @env["rack.request.cookie_hash"]
57 else
58 @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"]
59 # XXX sure?
60 @env["rack.request.cookie_hash"] =
61 Utils.parse_query(@env["rack.request.cookie_string"], ';,')
62 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
63 end
64
65 def xhr?
66 @env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
67 end
fd51d36 @chneukirchen Add Request#url
chneukirchen authored
68
69 def url
70 url = scheme + "://"
71 url << host
72
73 if scheme == "https" && port != 443 ||
74 scheme == "http" && port != 80
75 url << ":#{port}"
76 end
77
78 url << script_name
79 url << path_info
80
81 if @env["QUERY_STRING"] && !@env["QUERY_STRING"].empty?
82 url << "?" << @env["QUERY_STRING"]
83 end
84
85 url
86 end
5f13e3d @chneukirchen Add Rack::Request
chneukirchen authored
87 end
88 end
Something went wrong with that request. Please try again.