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 method (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.method = "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.
- #method ⇒ Types::String
- #path ⇒ Types::String
- #version ⇒ Types::String
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Request
constructor
A new instance of Request.
-
#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, known_headers, #ll_header
Methods included from PacketGen::Headerable
#added_to_packet, included, #method_name, #packet, #packet=, #parse?, #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.
59 60 61 62 |
# File 'lib/packetgen/header/http/request.rb', line 59 def initialize(={}) super() self.headers ||= [:headers] end |
Instance Attribute Details
#body ⇒ Types::String
52 |
# File 'lib/packetgen/header/http/request.rb', line 52 define_field :body, Types::String |
#headers ⇒ HTTP::Headers
associated http/1.1 headers
49 |
# File 'lib/packetgen/header/http/request.rb', line 49 define_field :headers, HTTP::Headers |
#method ⇒ Types::String
39 |
# File 'lib/packetgen/header/http/request.rb', line 39 define_field :method, Types::String |
#path ⇒ Types::String
42 |
# File 'lib/packetgen/header/http/request.rb', line 42 define_field :path, Types::String |
#version ⇒ Types::String
45 |
# File 'lib/packetgen/header/http/request.rb', line 45 define_field :version, Types::String, default: 'HTTP/1.1' |
Instance Method Details
#read(str) ⇒ PacketGen::HTTP::Request
Read in the HTTP portion of the packet, and parse it.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/packetgen/header/http/request.rb', line 66 def read(str) str = str.bytes.map!(&:chr).join unless str.valid_encoding? vrb = HTTP::VERBS.detect { |verb| str.include?(verb) } str = vrb + str.split(vrb)[-1] str = str.split("\n").map(&:chomp) first_line = str.shift.split self[:method].read first_line[0] self[:path].read first_line[1] self[:version].read first_line[2] # requests can sometimes have a payload if (data_index = str.find_index('')) data = str[data_index + 1..-1].join("\n") headers = str[0..data_index - 1].join("\n") else headers = str.join("\n") end self[:headers].read(headers) self[:body].read data self end |
#to_s ⇒ String
String representation of data.
89 90 91 92 93 94 95 96 |
# File 'lib/packetgen/header/http/request.rb', line 89 def to_s raise FormatError, 'Missing #method.' if self.method.empty? raise FormatError, 'Missing #path.' if self.path.empty? raise FormatError, 'Missing #version.' if self.version.empty? str = ''.dup # build 'dat string str << self[:method] << ' ' << self[:path] << ' ' << self[:version] << "\r\n" << self[:headers].to_s << self[:body] end |