Sha256: a7edbf7dda0421985a6290ac3597460d1d174bd0eac247768b17b6eff8c898a5

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

module Rbkb::Http

  # A Request encapsulates all the entities in a HTTP request message
  # including the action header, general headers, and body.
  class Request < Base
    attr_accessor :action

    alias first_entity action
    alias first_entity= action=

    def request_parameters
      @action.parameters
    end

    # Returns a new Headers object extended as RequestHeaders. This is the 
    # default object which will be used when composing fresh Request header
    # entities.
    def default_headers_obj(*args)
      Headers.new(*args).extend(RequestHeaders)
    end

    # Returns a new BoundBody object. This is the default object which will 
    # be used when composing fresh Request body entities.
    def default_body_obj(*args)
      Body.new(*args)
    end

    # Returns a raw HTTP request for this instance. The instance must have 
    # an action element defined at the bare minimum.
    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


    # Parses a raw HTTP request and captures data into the current instance.
    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
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
emonti-rbkb-0.6.6 lib/rbkb/http/request.rb
emonti-rbkb-0.6.7 lib/rbkb/http/request.rb
emonti-rbkb-0.6.8 lib/rbkb/http/request.rb
emonti-rbkb-0.6.9.1 lib/rbkb/http/request.rb
emonti-rbkb-0.6.9 lib/rbkb/http/request.rb
rbkb-0.6.10 lib/rbkb/http/request.rb