Class: PacketGen::Header::HTTP::Request
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- PacketGen::Header::HTTP::Request
- Defined in:
- lib/packetgen/header/http/request.rb
Overview
An HTTP/1.1 Request packet consists of:
-
the http verb (Types::String).
-
the path (Types::String).
-
the version (Types::String).
-
associated http headers (Headers).
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
Instance Attribute Summary collapse
- #body ⇒ Types::String
-
#headers ⇒ HTTP::Headers
associated http/1.1 headers.
- #path ⇒ Types::String
- #verb ⇒ Types::String
- #version ⇒ Types::String
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
- #parse? ⇒ Boolean
-
#read(str) ⇒ PacketGen::HTTP::Request
Read in the HTTP portion of the packet, and parse it.
-
#to_s ⇒ String
String representation of data.
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.
63 64 65 66 |
# File 'lib/packetgen/header/http/request.rb', line 63 def initialize(={}) super() self.headers ||= [:headers] end |
Instance Attribute Details
#body ⇒ Types::String
56 |
# File 'lib/packetgen/header/http/request.rb', line 56 define_field :body, Types::String |
#headers ⇒ HTTP::Headers
associated http/1.1 headers
53 |
# File 'lib/packetgen/header/http/request.rb', line 53 define_field :headers, HTTP::Headers |
#path ⇒ Types::String
46 |
# File 'lib/packetgen/header/http/request.rb', line 46 define_field :path, Types::String |
#verb ⇒ Types::String
43 |
# File 'lib/packetgen/header/http/request.rb', line 43 define_field :verb, Types::String |
#version ⇒ Types::String
49 |
# File 'lib/packetgen/header/http/request.rb', line 49 define_field :version, Types::String, default: 'HTTP/1.1' |
Instance Method Details
#parse? ⇒ Boolean
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.
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_s ⇒ String
String representation of data.
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 |