Class: PacketGen::Header::HTTP::Response
- Inherits:
-
Base
- Object
- Types::Fields
- Base
- PacketGen::Header::HTTP::Response
- Defined in:
- lib/packetgen/header/http/response.rb
Overview
An HTTP/1.1 Response packet consists of:
-
the version (Types::String).
-
the status code (Types::String).
-
the status message (Types::String).
-
associated http headers (Headers).
-
the actual http payload body (Types::String).
Create a HTTP Response header
# standalone
http_resp = PacketGen::Header::HTTP::Response.new
# in a packet
pkt = PacketGen.gen("IP").add("TCP").add("HTTP::Response")
# access to HTTP Response header
pkt.http_response # => PacketGen::Header::HTTP::Response
Note: When creating a HTTP Response packet, sport
and dport
attributes of TCP header are not set.
HTTP Response attributes
http_resp.version = "HTTP/1.1"
http_resp.status_code = "200"
http_resp.status_mesg = "OK"
http_resp.body = "this is a body"
http_resp.headers = "Host: tcpdump.org" # string or
http_resp.headers = { "Host": "tcpdump.org" } # even a hash
Instance Attribute Summary collapse
- #body ⇒ HTTP::PHeaders
-
#headers ⇒ Types::String
associated http/1.1 headers.
- #status_code ⇒ Types::String
- #status_mesg ⇒ Types::String
- #version ⇒ Types::String
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Response
constructor
A new instance of Response.
- #parse? ⇒ Boolean
-
#read(str) ⇒ PacketGen::HTTP::Response
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 = {}) ⇒ Response
Returns a new instance of Response.
63 64 65 66 |
# File 'lib/packetgen/header/http/response.rb', line 63 def initialize(={}) super() self.headers ||= [:headers] end |
Instance Attribute Details
#body ⇒ HTTP::PHeaders
55 |
# File 'lib/packetgen/header/http/response.rb', line 55 define_field :body, Types::String |
#headers ⇒ Types::String
associated http/1.1 headers
52 |
# File 'lib/packetgen/header/http/response.rb', line 52 define_field :headers, HTTP::Headers |
#status_code ⇒ Types::String
45 |
# File 'lib/packetgen/header/http/response.rb', line 45 define_field :status_code, Types::String |
#status_mesg ⇒ Types::String
48 |
# File 'lib/packetgen/header/http/response.rb', line 48 define_field :status_mesg, Types::String |
#version ⇒ Types::String
42 |
# File 'lib/packetgen/header/http/response.rb', line 42 define_field :version, Types::String, default: 'HTTP/1.1' |
Instance Method Details
#parse? ⇒ Boolean
82 83 84 |
# File 'lib/packetgen/header/http/response.rb', line 82 def parse? version.start_with?('HTTP/1.') end |
#read(str) ⇒ PacketGen::HTTP::Response
Read in the HTTP portion of the packet, and parse it.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/packetgen/header/http/response.rb', line 70 def read(str) headers, data = collect_headers_and_data(str) unless headers.empty? extract_info_from_first_line(headers) self[:headers].read(headers.join("\n")) end self[:body].read data.join("\n") self end |
#to_s ⇒ String
String representation of data.
88 89 90 91 92 93 94 95 |
# File 'lib/packetgen/header/http/response.rb', line 88 def to_s raise_on_bad_version_status str = +'' str << self.version << ' ' << self.status_code << ' ' << self.status_mesg << "\r\n" str << self[:headers].to_s if self[:headers].given? str << self.body end |