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 collapse

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 Attribute Details

#actionObject Also known as: first_entity

Returns the value of attribute action.



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

def action
  @action
end

Instance Method Details

#action_parametersObject



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

def action_parameters
  @action.parameters
end

#body_parametersObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rbkb/http/request.rb', line 15

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.



67
68
69
70
71
72
73
74
# File 'lib/rbkb/http/request.rb', line 67

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.



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

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.



36
37
38
# File 'lib/rbkb/http/request.rb', line 36

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

#to_raw(tmp_body = @body) ⇒ Object

Returns a raw HTTP request for this instance. The instance must have an action element defined at the bare minimum.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbkb/http/request.rb', line 48

def to_raw(tmp_body=@body)
  raise "this request has no action entity" unless first_entity()
  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 = tmp_body.to_raw
  hdrs = (@headers).to_raw_array.unshift(first_entity.to_raw)
  return "#{hdrs.join("\r\n")}\r\n\r\n#{bstr}"
end