Class: Cybertrain::Request
- Inherits:
-
Object
- Object
- Cybertrain::Request
- Defined in:
- cybertrain/http/request.rb
Overview
One incoming HTTP request. Header names are expected lowercased (as HttpParser produces them), but #header also finds mixed-case keys so hand-built requests in tests behave the same.
Controller#request in an action. Read parameters through
Controller#params; the request holds the raw parts. There is no
remote_ip (no X-Forwarded-For handling), xhr?, format or url.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The raw body (
""when there is none). -
#headers ⇒ Hash{String => String}
readonly
The headers, by lowercase name.
-
#method ⇒ String
readonly
The method, upper case, after a form’s
_methodoverride (so"PATCH"for an edit form). -
#path ⇒ String
readonly
The path as sent, without the query string and not percent-decoded.
-
#query_string ⇒ String
readonly
The raw query string, without the
?(""when there is none). -
#remote_addr ⇒ String
readonly
The peer’s IP address (the reverse proxy’s, behind one).
Instance Method Summary collapse
-
#content_length ⇒ Integer
The Content-Length header as an Integer.
-
#content_type ⇒ String
Media type only: “text/html; charset=utf-8” -> “text/html”.
-
#cookie_header ⇒ String
The raw Cookie header; parse it with Cookies.parse.
-
#delete? ⇒ Boolean
-
#form? ⇒ Boolean
True for an
application/x-www-form-urlencodedbody, the only kind parsed into params. -
#get? ⇒ Boolean
-
#head? ⇒ Boolean
-
#header(name) ⇒ String?
-
#json? ⇒ Boolean
True for an
application/jsonbody. -
#patch? ⇒ Boolean
-
#post? ⇒ Boolean
-
#put? ⇒ Boolean
Instance Attribute Details
#body ⇒ String (readonly)
The raw body ("" when there is none).
41 42 43 |
# File 'cybertrain/http/request.rb', line 41 def body @body end |
#headers ⇒ Hash{String => String} (readonly)
The headers, by lowercase name. #header looks one up.
36 37 38 |
# File 'cybertrain/http/request.rb', line 36 def headers @headers end |
#method ⇒ String (readonly)
The method, upper case, after a form’s _method override (so
"PATCH" for an edit form).
21 22 23 |
# File 'cybertrain/http/request.rb', line 21 def method @method end |
#path ⇒ String (readonly)
The path as sent, without the query string and not percent-decoded.
26 27 28 |
# File 'cybertrain/http/request.rb', line 26 def path @path end |
#query_string ⇒ String (readonly)
The raw query string, without the ? ("" when there is none).
31 32 33 |
# File 'cybertrain/http/request.rb', line 31 def query_string @query_string end |
#remote_addr ⇒ String (readonly)
The peer’s IP address (the reverse proxy’s, behind one).
46 47 48 |
# File 'cybertrain/http/request.rb', line 46 def remote_addr @remote_addr end |
Instance Method Details
#content_length ⇒ Integer
The Content-Length header as an Integer.
0 when the header is absent or not a plain non-negative integer. An overlong all-digit value saturates to 9223372036854775807 under Spinel (CRuby would return a Bignum); the Server’s 413 limit rejects it.
96 97 98 99 100 101 102 |
# File 'cybertrain/http/request.rb', line 96 def content_length value = header("content-length") return 0 if value.nil? || value.empty? return 0 unless value.bytes.all? { |b| b >= 48 && b <= 57 } value.to_i end |
#content_type ⇒ String
Media type only: “text/html; charset=utf-8” -> “text/html”.
Lowercase; "" when absent.
108 109 110 111 112 113 114 115 |
# File 'cybertrain/http/request.rb', line 108 def content_type value = header("content-type") return "" if value.nil? semi = value.index(";") value = value[0, semi] unless semi.nil? value.strip.downcase end |
#cookie_header ⇒ String
The raw Cookie header; parse it with Cookies.parse.
323 324 325 |
# File 'cybertrain/http/request.rb', line 323 def header("cookie") || "" end |
#delete? ⇒ Boolean
167 168 169 |
# File 'cybertrain/http/request.rb', line 167 def delete? @method == "DELETE" end |
#form? ⇒ Boolean
Returns true for an application/x-www-form-urlencoded
body, the only kind parsed into params.
174 175 176 |
# File 'cybertrain/http/request.rb', line 174 def form? content_type == "application/x-www-form-urlencoded" end |
#get? ⇒ Boolean
137 138 139 |
# File 'cybertrain/http/request.rb', line 137 def get? @method == "GET" end |
#head? ⇒ Boolean
149 150 151 |
# File 'cybertrain/http/request.rb', line 149 def head? @method == "HEAD" end |
#header(name) ⇒ String?
79 80 81 82 83 84 85 86 87 |
# File 'cybertrain/http/request.rb', line 79 def header(name) value = @headers[HeaderName.lower(name)] return value unless value.nil? @headers.each do |k, v| return v if HeaderName.same?(k, name) end nil end |
#json? ⇒ Boolean
Returns true for an application/json body.
180 181 182 |
# File 'cybertrain/http/request.rb', line 180 def json? content_type == "application/json" end |
#patch? ⇒ Boolean
155 156 157 |
# File 'cybertrain/http/request.rb', line 155 def patch? @method == "PATCH" end |
#post? ⇒ Boolean
143 144 145 |
# File 'cybertrain/http/request.rb', line 143 def post? @method == "POST" end |
#put? ⇒ Boolean
161 162 163 |
# File 'cybertrain/http/request.rb', line 161 def put? @method == "PUT" end |