Class: Rbkb::Http::Request

Inherits:
Base
  • Object
show all
Defined in:
lib/rbkb/http/request.rb

Overview

A Request encapsulates all the entities in a HTTP request message including the action header, general headers, and body.

Instance Attribute Summary

Attributes inherited from Base

#body, #headers

Instance Method Summary collapse

Methods inherited from Base

#attach_new_body, #attach_new_header, #capture_body, #capture_complete?, #capture_headers, #content_length, #content_type, #initialize, parse, #reset_capture, #reset_capture!

Methods included from CommonInterface

#_common_init, #opts, #opts=

Constructor Details

This class inherits a constructor from Rbkb::Http::Base

Instance Method Details

#actionObject Also known as: first_entity



6
7
8
# File 'lib/rbkb/http/request.rb', line 6

def action
  @action ||= RequestAction.new
end

#action=(a) ⇒ Object Also known as: first_entity=



10
11
12
# File 'lib/rbkb/http/request.rb', line 10

def action=(a)
  @action = a
end

#action_parametersObject



17
18
19
# File 'lib/rbkb/http/request.rb', line 17

def action_parameters
  action.parameters
end

#body_parametersObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rbkb/http/request.rb', line 21

def body_parameters
  ctype, ct_parms = @headers.get_parameterized_value('Content-Type')
  case ctype
  when /^application\/(?:x-)?(?:www-form-url|url-)encoded(?:\W|$)/
    FormUrlencodedParams.new(@body)
  when /^multipart\/form-data$/
    MultipartFormParams.new(@body, :boundary => ct_parms.get_value_for('boundary'))
  when /^text\/plain$/
    # safari just gives us url-encoded parameters for text/plain.
    # Joy!
    if @headers.get_value_for('User-Agent') =~ /\WSafari\W/
      FormUrlencodedParams.new(@body)
    else
      TextPlainFormParams.new(@body)
    end
  end
end

#capture(str) ⇒ Object

Parses a raw HTTP request and captures data into the current instance.



71
72
73
74
75
76
77
78
# File 'lib/rbkb/http/request.rb', line 71

def capture(str)
  raise "arg 0 must be a string" unless String === str
  hstr, bstr = str.split(/\s*\r?\n\r?\n/, 2)
  capture_headers(hstr)
  self.body = content_length ? BoundBody.new : Body.new
  capture_body(bstr)
  return self
end

#default_body_obj(*args) ⇒ Object

Returns a new BoundBody object. This is the default object which will be used when composing fresh Request body entities.



48
49
50
# File 'lib/rbkb/http/request.rb', line 48

def default_body_obj(*args)
  Body.new(*args)
end

#default_headers_obj(*args) ⇒ Object

Returns a new Headers object extended as RequestHeaders. This is the default object which will be used when composing fresh Request header entities.



42
43
44
# File 'lib/rbkb/http/request.rb', line 42

def default_headers_obj(*args)
  Headers.new(*args).extend(RequestHeaders)
end

#to_rawObject

Returns a raw HTTP request for this instance.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbkb/http/request.rb', line 53

def to_raw()
  self.headers ||= default_headers_obj()
  self.body ||= default_body_obj()

  if len=@opts[:static_length]
    @body = Body.new(@body, @body.opts) {|x| x.base = self}
    @headers.set_header("Content-Length", len.to_i)
  elsif @opts[:ignore_content_length]
    @headers.delete_header("Content-Length")
  end

  bstr = @body.to_raw
  hdrs = @headers.to_raw_array.unshift(first_entity.to_raw)
  return "#{hdrs.join("\r\n")}\r\n\r\n#{bstr}"
end