Class: ServerSide::HTTP::Request

Inherits:
Object
  • Object
show all
Includes:
Parsing
Defined in:
lib/serverside/http/request.rb

Constant Summary collapse

HOST_PORT_RE =
/^([^:]+):(.+)$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ Request

Returns a new instance of Request.



11
12
13
14
15
16
# File 'lib/serverside/http/request.rb', line 11

def initialize(conn)
  @conn = conn
  @headers = {}
  @header_count = 0
  @cookies = {}
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/serverside/http/request.rb', line 9

def body
  @body
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



8
9
10
# File 'lib/serverside/http/request.rb', line 8

def content_length
  @content_length
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



9
10
11
# File 'lib/serverside/http/request.rb', line 9

def cookies
  @cookies
end

#header_countObject (readonly)

Returns the value of attribute header_count.



8
9
10
# File 'lib/serverside/http/request.rb', line 8

def header_count
  @header_count
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/serverside/http/request.rb', line 8

def headers
  @headers
end

#http_versionObject (readonly)

Returns the value of attribute http_version.



7
8
9
# File 'lib/serverside/http/request.rb', line 7

def http_version
  @http_version
end

#methodObject (readonly)

Returns the value of attribute method.



7
8
9
# File 'lib/serverside/http/request.rb', line 7

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/serverside/http/request.rb', line 7

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/serverside/http/request.rb', line 7

def path
  @path
end

#persistentObject (readonly)

Returns the value of attribute persistent.



8
9
10
# File 'lib/serverside/http/request.rb', line 8

def persistent
  @persistent
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/serverside/http/request.rb', line 7

def query
  @query
end

#request_lineObject (readonly)

Returns the value of attribute request_line.



7
8
9
# File 'lib/serverside/http/request.rb', line 7

def request_line
  @request_line
end

Instance Method Details

#accept?(re) ⇒ Boolean

Returns true if the accept header contains the supplied pattern.

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/serverside/http/request.rb', line 64

def accept?(re)
  re = Regexp.new(re) unless Regexp === re
  (h = @headers[:accept]) && (h =~ re) && true
end

#client_nameObject

Returns the client name. The client name is either the value of the X-Forwarded-For header, or the result of get_peername.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/serverside/http/request.rb', line 51

def client_name
  unless @client_name
    @client_name = @headers[:x_forwarded_for]
    unless @client_name
      if addr = @conn.get_peername
        p, @client_name = Socket.unpack_sockaddr_in(addr)
      end
    end
  end
  @client_name
end

#content_typeObject



73
74
75
76
77
# File 'lib/serverside/http/request.rb', line 73

def content_type
  if t = @headers[:content_type]
    t =~ /(.*);/ ? $1.strip : t
  end
end

#encrypted?Boolean

Returns true if the request was received on port 443

Returns:

  • (Boolean)


31
32
33
# File 'lib/serverside/http/request.rb', line 31

def encrypted?
  port == 443
end

#hostObject

Returns the host specified in the Host header.



19
20
21
22
# File 'lib/serverside/http/request.rb', line 19

def host
  parse_host_header unless @host_header_parsed
  @host
end

#parse_host_headerObject

Parses the Host header.



38
39
40
41
42
43
44
45
46
47
# File 'lib/serverside/http/request.rb', line 38

def parse_host_header
  h = @headers[:host]
  if h =~ HOST_PORT_RE
    @host = $1
    @port = $2.to_i
  else
    @host = h
  end
  @host_header_parsed = true
end

#portObject

Returns the port number if specified in the Host header.



25
26
27
28
# File 'lib/serverside/http/request.rb', line 25

def port
  parse_host_header unless @host_header_parsed
  @port
end

#user_agentObject



69
70
71
# File 'lib/serverside/http/request.rb', line 69

def user_agent
  @headers[:user_agent]
end