class Mallory::Request

Attributes

protocol[RW]

Public Class Methods

new(data, logger) click to toggle source
# File lib/mallory/request.rb, line 8
def initialize(data, logger)
  @logger = logger
  line = data.match(/([A-Z]{3,8})\s(?:(http\w*):\/\/)*(?:(\w*):*(\d{2,5})*)(\/{0,1}.*)\sHTTP/)
  method = line[1]
  @protocol = "http"
  host = line[3] || data.match(/Host:\s(.*)\n/)[1]
  port = line[4]
  path = line[5]
  data.sub!("#{method} #{line[3]}:#{port}", "#{method} #{line[3]}/")
  data.sub!("Host: #{line[3]}:#{port}", "Host: #{line[3]}")
  @request = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
  @request.parse(StringIO.new(data))
rescue WEBrick::HTTPStatus::BadRequest
  raise
end

Public Instance Methods

body() click to toggle source
# File lib/mallory/request.rb, line 42
def body
  @request.body
rescue WEBrick::HTTPStatus::LengthRequired
  nil
end
headers() click to toggle source
# File lib/mallory/request.rb, line 36
def headers
  headers = {}
  @request.each { |head| headers[head] = @request[head] }
  headers
end
host() click to toggle source
# File lib/mallory/request.rb, line 28
def host
  @request['host'].split(":")[0]
end
method() click to toggle source
# File lib/mallory/request.rb, line 32
def method
  @request.request_method.downcase
end
uri() click to toggle source
# File lib/mallory/request.rb, line 24
def uri
  "#{@protocol}://#{@request['host']}#{@request.path}"
end