Sha256: 32f9204ffc1adcab454a5c2e90939325c4feeb8b7dec72ad17e7423c0401f3ba

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

module Wukong

  # Represents a generic HTTP request.
  class HttpRequest
    
    include Gorillib::Model

    field :timestamp,  Integer, :doc => "Timestamp at which the HTTP request was received"
    field :verb,       String,  :doc => "HTTP verb of the request"
    field :path,       String,  :doc => "Absolute path to the resource requested"
    field :params,     Hash,    :doc => "Query parameters contained in the request", :default => {}
    field :headers,    Hash,    :doc => "HTTP headers of the request", :default => {}
    field :ip_address, String,  :doc => "IP address of the client"
    field :body,       String,  :doc => "Body of the request"

    # Return the URL of this request.
    #
    # @return [String]
    def url
      File.join(headers['Host'] || '', (path || ''))
    end

    # Return the HTTP Referer of this request.
    #
    # @return [String]
    def referer
      headers['Referer']
    end
    alias_method :referrer, :referer

    # Return the HTTP User-Agent of this request.
    #
    # @return [String]
    def user_agent
      headers['User-Agent']
    end

    # Return the HTTP Cookie of this request.
    #
    # @return [String]
    def cookie
      headers['Cookie']
    end

    # Return the "best" IP address from this request.
    #
    # Will return the first IP address in the HTTP X-Forwarded-For chain
    # if present, otherwise will return the IP address of the request
    # itself.
    #
    # @return [String]
    def best_ip_address
      ip_string = headers['X-Forwarded-For']
      return ip_address if ip_string.blank?
      ips = ip_string.split(/\s*,\s*/)
      ips.empty? ? ip_address : ips.first # client comes first, then proxies in order
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wukong-load-0.1.1 lib/wukong-load/models/http_request.rb
wukong-load-0.1.0 lib/wukong-load/models/http_request.rb