Class: PacketGen::Header::HTTP::Request

Inherits:
Base show all
Defined in:
lib/packetgen/header/http/request.rb

Overview

An HTTP/1.1 Request packet consists of:

Create a HTTP Request header

# standalone
http_rqst = PacketGen::Header::HTTP::Request.new
# in a packet
pkt = PacketGen.gen("IP").add("TCP").add("HTTP::Request")
# access to HTTP Request header
pkt.http_request # => PacketGen::Header::HTTP::Request

Note: When creating a HTTP Request packet, sport and dport attributes of TCP header are not set.

HTTP Request attributes

http_rqst.version = "HTTP/1.1"
http_rqst.verb  = "GET"
http_rqst.path    = "/meow.html"
http_rqst.headers = "Host: tcpdump.org"     # string or
http_rqst.headers = { "Host": "tcpdump.org" } # even a hash

Author:

  • Kent ‘picat’ Gruber

  • Sylvain Daubert

Since:

  • 3.1.0 Rename #method into #verb to not mask Object#method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #ip_header, #ll_header

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #protocol_name

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, update_field

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :verb (String)
  • :path (String)
  • :version (String)
  • :headers (Hash)

Since:

  • 3.1.0 Rename #method into #verb to not mask Object#method.



63
64
65
66
# File 'lib/packetgen/header/http/request.rb', line 63

def initialize(options={})
  super(options)
  self.headers ||= options[:headers]
end

Instance Attribute Details

#bodyTypes::String

Returns:



56
# File 'lib/packetgen/header/http/request.rb', line 56

define_field :body, Types::String

#headersHTTP::Headers

associated http/1.1 headers

Returns:



53
# File 'lib/packetgen/header/http/request.rb', line 53

define_field :headers, HTTP::Headers

#pathTypes::String

Returns:



46
# File 'lib/packetgen/header/http/request.rb', line 46

define_field :path,    Types::String

#verbTypes::String

Returns:

Since:

  • 3.1.0



43
# File 'lib/packetgen/header/http/request.rb', line 43

define_field :verb, Types::String

#versionTypes::String

Returns:



49
# File 'lib/packetgen/header/http/request.rb', line 49

define_field :version, Types::String, default: 'HTTP/1.1'

Instance Method Details

#parse?Boolean

Returns:

  • (Boolean)

Since:

  • 3.1.0 Rename #method into #verb to not mask Object#method.



85
86
87
# File 'lib/packetgen/header/http/request.rb', line 85

def parse?
  VERBS.include?(self.verb) && self.version.start_with?('HTTP/1.')
end

#read(str) ⇒ PacketGen::HTTP::Request

Read in the HTTP portion of the packet, and parse it.

Returns:

  • (PacketGen::HTTP::Request)

Since:

  • 3.1.0 Rename #method into #verb to not mask Object#method.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/packetgen/header/http/request.rb', line 70

def read(str)
  lines = lines(str)
  first_line_words = lines.shift.split
  self[:verb].read first_line_words[0]
  self[:path].read first_line_words[1]
  self[:version].read first_line_words[2]

  # requests can sometimes have a payload
  headers, data = headers_and_payload_from_lines(lines)
  self[:headers].read(headers)
  self[:body].read(data)

  self
end

#to_sString

String representation of data.

Returns:

  • (String)

Raises:

Since:

  • 3.1.0 Rename #method into #verb to not mask Object#method.



91
92
93
94
95
96
97
# File 'lib/packetgen/header/http/request.rb', line 91

def to_s
  raise FormatError, 'Missing #verb.' if self.verb.empty?
  raise FormatError, 'Missing #path.'    if self.path.empty?
  raise FormatError, 'Missing #version.' if self.version.empty?

  "#{self.verb.dup} #{self.path} #{self.version}\r\n#{self[:headers]}#{self.body}"
end