Class: Cybertrain::Request

Inherits:
Object
  • Object
show all
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.

Examples:

request.header("user-agent")
JSON.parse(request.body) if request.json?   # JSON bodies are not parsed into params

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#body ⇒ String (readonly)

The raw body ("" when there is none).

Returns:

  • (String)


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.

Returns:

  • (Hash{String => String})


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).

Returns:

  • (String)


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.

Returns:

  • (String)


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).

Returns:

  • (String)


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).

Returns:

  • (String)


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.

Returns:

  • (Integer)


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.

Returns:

  • (String)


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

The raw Cookie header; parse it with Cookies.parse.

Returns:

  • (String) —

    "" when absent



323
324
325
# File 'cybertrain/http/request.rb', line 323

def cookie_header
  header("cookie") || ""
end

#delete? ⇒ Boolean

Returns:

  • (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.

Returns:

  • (Boolean) —

    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

Returns:

  • (Boolean)


137
138
139
# File 'cybertrain/http/request.rb', line 137

def get?
  @method == "GET"
end

#head? ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'cybertrain/http/request.rb', line 149

def head?
  @method == "HEAD"
end

#header(name) ⇒ String?

Examples:

request.header("Referer")

Parameters:

  • name (String) —

    any case

Returns:

  • (String, nil)


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.

Returns:

  • (Boolean) —

    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

Returns:

  • (Boolean)


155
156
157
# File 'cybertrain/http/request.rb', line 155

def patch?
  @method == "PATCH"
end

#post? ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'cybertrain/http/request.rb', line 143

def post?
  @method == "POST"
end

#put? ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'cybertrain/http/request.rb', line 161

def put?
  @method == "PUT"
end