Class: Beaver::Parsers::HTTP

Inherits:
Request
  • Object
show all
Defined in:
lib/beaver/parsers/http.rb

Overview

Parser for HTTP Common Log entries. See the Request class for more log entry attributes.

Constant Summary collapse

FORMAT =

The Combined Log Format

'%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"'
FORMAT_SYMBOLS =

The Combined Log Format as an array of symbols

FORMAT.split(' ').map(&:to_sym)
REGEX_TIME_FIX =
/([0-9]{4}):([0-9]{2})/
MATCHERS =

Regex matchers keyed by opening quotes, etc.

{'[' => /^\[([^\]]+)\] ?/, '"' => /^"([^"]+)" ?/}
REGEX_MATCH =

Matches an HTTP Log entry

%r{\[[0-9]{2}/\w+/[0-9:]+ }
REGEX_REQUEST =

Matches the request method, url and params

/^([A-Z]+) (\/[^\?]+)\??(.*) HTTP\/1/
REGEX_DATE =

Matches the request date

%r{^([0-9]{2}/[a-z]+/[0-9]+)}i

Constants inherited from Request

Request::BLANK_HASH, Request::BLANK_STR

Instance Method Summary collapse

Methods inherited from Request

#<<, #completed?, #date, #final!, #final?, for, inherited, #invalid?, #ip, match?, #params, #skip!, #status, #time, #to_s

Constructor Details

#initialize(data) ⇒ HTTP

Partially parse the request



24
25
26
27
# File 'lib/beaver/parsers/http.rb', line 24

def initialize(data)
  super
  pre_parse!
end

Instance Method Details

#methodObject

Returns the HTTP method as a symbol



30
31
32
33
# File 'lib/beaver/parsers/http.rb', line 30

def method
  parse_request! if @method.nil?
  @method
end

#params_strObject

Returns the url query string



54
55
56
57
# File 'lib/beaver/parsers/http.rb', line 54

def params_str
  parse_request! if @params_str.nil?
  @params_str
end

#parse_paramsObject

Parses and returns the request parameters as a Hash



42
43
44
45
46
47
48
49
50
51
# File 'lib/beaver/parsers/http.rb', line 42

def parse_params
  params_str.empty? ? BLANK_HASH : CGI::parse(params_str).inject({}) do |hash, (param, value)|
    hash[param] = if value.is_a?(Array) and value.size == 1 and param !~ /\[\d*\]$/
      value[0]
    else
      value
    end
    hash
  end
end

#pathObject

Returns the request path



36
37
38
39
# File 'lib/beaver/parsers/http.rb', line 36

def path
  parse_request! if @path.nil?
  @path
end

#refererObject Also known as: referrer

Returns the REFERER [sic]



65
66
67
# File 'lib/beaver/parsers/http.rb', line 65

def referer
  @request[FREFERER]
end

#sizeObject

Returns the response size (in bytes)



60
61
62
# File 'lib/beaver/parsers/http.rb', line 60

def size
  @size ||= @request[FSIZE].to_i
end

#user_agentObject

Returns the user agent string



71
72
73
# File 'lib/beaver/parsers/http.rb', line 71

def user_agent
  @request[FUA]
end