Class: GRHttp::HTTPRequest

Inherits:
Hash
  • Object
show all
Defined in:
lib/grhttp/http_request.rb,
lib/grhttp/http_cookies.rb

Overview

This class is the part of the GRHttp server. The request object is a Hash and the HTTPRequest provides simple shortcuts and access to the request' Hash data.

Defined Under Namespace

Classes: Cookies

Constant Summary collapse

HTTP_GET =

method recognition

'GET'
HTTP_HEAD =
'HEAD'
HTTP_POST =
'POST'
HTTP_PUT =
'PUT'
HTTP_DELETE =
'DELETE'
HTTP_TRACE =
'TRACE'
HTTP_OPTIONS =
'OPTIONS'
HTTP_CONNECT =
'CONNECT'
HTTP_PATCH =
'PATCH'
HTTP_CTYPE =
'content-type'
HTTP_JSON =
/application\/json/
HTTP_XML =
/text\/xml/
HTTP_UPGRADE =
'upgrade'
HTTP_UPGRADE_REGEX =
/upg/i
HTTP_WEBSOCKET =
'websocket'
HTTP_CONNECTION =
'connection'

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ HTTPRequest

Returns a new instance of HTTPRequest


10
11
12
13
14
15
# File 'lib/grhttp/http_request.rb', line 10

def initialize io = nil
  super()
  self[:io] = io if io
  self[:cookies] = Cookies.new
  self[:params] = {}
end

Instance Method Details

#base_url(switch_protocol = nil) ⇒ Object

the base url ([http/https]://host)


59
60
61
# File 'lib/grhttp/http_request.rb', line 59

def base_url switch_protocol = nil
  "#{switch_protocol || self[:requested_protocol]}://#{self[:host_name]}#{self[:port]? ":#{self[:port]}" : ''}"
end

#connect?Boolean

returns true of the method == CONNECT

Returns:

  • (Boolean)

124
125
126
# File 'lib/grhttp/http_request.rb', line 124

def connect?
  self[:method] == HTTP_CONNECT
end

#cookiesObject

the cookies sent by the client.


36
37
38
# File 'lib/grhttp/http_request.rb', line 36

def cookies
  self[:cookies]
end

#delete?Boolean

returns true of the method == DELETE

Returns:

  • (Boolean)

109
110
111
# File 'lib/grhttp/http_request.rb', line 109

def delete?
  self[:method] == HTTP_DELETE
end

#get?Boolean

returns true of the method == GET

Returns:

  • (Boolean)

88
89
90
# File 'lib/grhttp/http_request.rb', line 88

def get?
  self[:method] == HTTP_GET
end

#head?Boolean

returns true of the method == HEAD

Returns:

  • (Boolean)

94
95
96
# File 'lib/grhttp/http_request.rb', line 94

def head?
  self[:method] == HTTP_HEAD
end

#headersObject

the request's headers


20
21
22
# File 'lib/grhttp/http_request.rb', line 20

def headers
  self.select {|k,v| k.is_a? String }
end

#ioBasicIO, SSLBasicIO

Returns the io used for the request.

Returns:

  • (BasicIO, SSLBasicIO)

    the io used for the request.


80
81
82
# File 'lib/grhttp/http_request.rb', line 80

def io
  self[:io]      
end

#json?Boolean

returns true if the request is of type JSON.

Returns:

  • (Boolean)

134
135
136
# File 'lib/grhttp/http_request.rb', line 134

def json?
  self[HTTP_CTYPE].match HTTP_JSON
end

#options?Boolean

returns true of the method == OPTIONS

Returns:

  • (Boolean)

119
120
121
# File 'lib/grhttp/http_request.rb', line 119

def options?
  self[:method] == HTTP_OPTIONS
end

#original_pathObject

the original (frozen) path (resource requested).


46
47
48
# File 'lib/grhttp/http_request.rb', line 46

def original_path
  self[:original_path]
end

#paramsObject

the parameters sent by the client.


32
33
34
# File 'lib/grhttp/http_request.rb', line 32

def params
  self[:params]
end

#patch?Boolean

returns true of the method == PATCH

Returns:

  • (Boolean)

129
130
131
# File 'lib/grhttp/http_request.rb', line 129

def patch?
  self[:method] == HTTP_PATCH
end

#pathObject

the requested path (rewritable).


51
52
53
# File 'lib/grhttp/http_request.rb', line 51

def path
  self[:path]
end

#path=(new_path) ⇒ Object


54
55
56
# File 'lib/grhttp/http_request.rb', line 54

def path=(new_path)
  self[:path] = new_path
end

#post?Boolean

returns true of the method == POST

Returns:

  • (Boolean)

99
100
101
# File 'lib/grhttp/http_request.rb', line 99

def post?
  self[:method] == HTTP_POST
end

#protocolObject

the protocol managing this request


69
70
71
# File 'lib/grhttp/http_request.rb', line 69

def protocol
  self[:requested_protocol]
end

#put?Boolean

returns true of the method == PUT

Returns:

  • (Boolean)

104
105
106
# File 'lib/grhttp/http_request.rb', line 104

def put?
  self[:method] == HTTP_PUT
end

#queryObject

the query string


41
42
43
# File 'lib/grhttp/http_request.rb', line 41

def query
  self[:query]
end

#request_methodObject

the request's method (GET, POST… etc').


24
25
26
# File 'lib/grhttp/http_request.rb', line 24

def request_method
  self[:method]
end

#request_method=(value) ⇒ Object

set request's method (GET, POST… etc').


28
29
30
# File 'lib/grhttp/http_request.rb', line 28

def request_method= value
  self[:method] = value
end

#request_url(switch_protocol = nil) ⇒ Object

the request's url, without any GET parameters ([http/https]://host/path)


64
65
66
# File 'lib/grhttp/http_request.rb', line 64

def request_url switch_protocol = nil
  "#{base_url switch_protocol}#{self[:original_path]}"
end

#ssl?true, false Also known as: secure?

Returns true if the requested was an SSL protocol (true also if the connection is clear-text behind an SSL Proxy, such as with some PaaS providers).

Returns:

  • (true, false)

    returns true if the requested was an SSL protocol (true also if the connection is clear-text behind an SSL Proxy, such as with some PaaS providers).


74
75
76
# File 'lib/grhttp/http_request.rb', line 74

def ssl?
  io.ssl? || self[:requested_protocol] == 'https' || self[:requested_protocol] == 'wss'
end

#trace?Boolean

returns true of the method == TRACE

Returns:

  • (Boolean)

114
115
116
# File 'lib/grhttp/http_request.rb', line 114

def trace?
  self[:method] == HTTP_TRACE
end

#upgrade?Boolean

returns true if this is a websocket upgrade request

Returns:

  • (Boolean)

144
145
146
# File 'lib/grhttp/http_request.rb', line 144

def upgrade?
  self[HTTP_UPGRADE] && self[HTTP_UPGRADE].to_s.downcase == HTTP_WEBSOCKET &&  self[HTTP_CONNECTION].to_s.match(HTTP_UPGRADE_REGEX) && true
end

#xml?Boolean

returns true if the request is of type XML.

Returns:

  • (Boolean)

139
140
141
# File 'lib/grhttp/http_request.rb', line 139

def xml?
  self[HTTP_CTYPE].match HTTP_XML
end