Class: Rbkb::Http::Base

Inherits:
Object
  • Object
show all
Includes:
CommonInterface
Defined in:
lib/rbkb/http/base.rb

Overview

A base class containing some common features for Request and Response objects.

Don’t use this class directly, it’s intended for being overridden from its derived classes or mixins.

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommonInterface

#_common_init, #opts, #opts=

Constructor Details

#initialize(*args) ⇒ Base

Initializes a new Base object



16
17
18
# File 'lib/rbkb/http/base.rb', line 16

def initialize(*args)
  _common_init(*args)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



139
140
141
# File 'lib/rbkb/http/base.rb', line 139

def body
  @body
end

#headersObject

Returns the value of attribute headers.



139
140
141
# File 'lib/rbkb/http/base.rb', line 139

def headers
  @headers
end

Class Method Details

.parse(*args) ⇒ Object



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

def self.parse(*args)
  new(*args)
end

Instance Method Details

#attach_new_body(body_obj = nil) ⇒ Object



82
83
84
85
# File 'lib/rbkb/http/base.rb', line 82

def attach_new_body(body_obj=nil)
  self.body = body_obj
  return body_obj
end

#attach_new_header(hdr_obj = nil) ⇒ Object



77
78
79
80
# File 'lib/rbkb/http/base.rb', line 77

def attach_new_header(hdr_obj=nil)
  self.headers = hdr_obj
  return hdr_obj
end

#capture_body(bstr) ⇒ Object

This method parses just HTTP message body. Expects body to be split from the headers before-hand.



22
23
24
25
# File 'lib/rbkb/http/base.rb', line 22

def capture_body(bstr)
  self.body ||= default_body_obj
  @body.capture(bstr)
end

#capture_complete?Boolean

Indicates whether this object is ready to capture fresh data, or is waiting for additional data or a reset from a previous incomplete or otherwise broken capture. See also: reset_capture, reset_capture!

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
# File 'lib/rbkb/http/base.rb', line 130

def capture_complete?
  if( (@headers and not @headers.capture_complete?) or
      (@body and not @body.capture_complete?) )
    return false
  else
    true
  end
end

#capture_headers(hstr) ⇒ Object

This method parses only HTTP response headers. Expects headers to be split from the body before-hand.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbkb/http/base.rb', line 39

def capture_headers(hstr)
  self.headers ||= default_headers_obj

  if @body and not @body.capture_complete?
    return 
  elsif @headers.capture_complete?
    self.first_entity, @headers = default_headers_obj.capture_full_headers(hstr)
  else 
    @headers.capture(hstr)
  end
end

#content_length(hdrs = @headers) ⇒ Object

This method returns the content length from Headers. This is mostly useful if you are using a BoundBody object for the body.

Returns nil if no “Content-Length” is not found.

The opts parameter :ignore_content_length affects this method and will cause it always to return nil. This is useful, for example, for the responses to the HTTP HEAD request method, which return a Content-Length without actual content.



61
62
63
64
65
66
67
68
# File 'lib/rbkb/http/base.rb', line 61

def content_length(hdrs=@headers)
  raise "headers is nil?" if not hdrs
  if( (not @opts[:ignore_content_length]) and 
      hdrs.get_header_value("Content-Length").to_s =~ /^(\d+)$/ )

    $1.to_i
  end
end

#content_type(hdrs = @headers) ⇒ Object



70
71
72
73
74
75
# File 'lib/rbkb/http/base.rb', line 70

def content_type(hdrs=@headers)
  raise "headers is nil?" if not hdrs
  if ctype=hdrs.get_header_value("Content-Type")
    ctype.split(/\s*;\s*/).first
  end
end

#default_body_obj(*args) ⇒ Object

XXX doc override!



93
94
95
# File 'lib/rbkb/http/base.rb', line 93

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

#default_headers_obj(*args) ⇒ Object

XXX doc override!



88
89
90
# File 'lib/rbkb/http/base.rb', line 88

def default_headers_obj(*args)
  Header.new(*args)
end

#first_entityObject

XXX stub



28
29
30
# File 'lib/rbkb/http/base.rb', line 28

def first_entity
  @first_entity
end

#first_entity=(f) ⇒ Object

XXX stub



33
34
35
# File 'lib/rbkb/http/base.rb', line 33

def first_entity=(f)
  @first_entity=(f)
end

#reset_captureObject

This method will non-destructively reset the capture state on this object and all child entities. Note, however, If child entities are not defined, it may instantiate new ones. See also: capture_complete?, reset_capture!



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rbkb/http/base.rb', line 101

def reset_capture
  if @headers
    @headers.reset_capture if not @headers.capture_complete?
  else
    attach_new_header()
  end

  if @body
    @body.reset_capture if not @body.capture_complete? 
  else
    attach_new_body()
  end
  @capture_state = nil
  self
end

#reset_capture!Object

This method will destructively reset the capture state on this object. It does so by initializing fresh child entities and discarding the old ones. See also: capture_complete?, reset_capture



120
121
122
123
124
125
# File 'lib/rbkb/http/base.rb', line 120

def reset_capture!
  attach_new_header()
  attach_new_body()
  @capture_state = nil
  self
end