Class: PacketGen::Header::HTTP::Response
- Defined in:
- lib/packetgen/header/http/response.rb
Overview
An HTTP/1.1 Response packet consists of:
-
the version (
BinStruct::String
). -
the status code (
BinStruct::String
). -
the status message (
BinStruct::String
). -
associated HTTP headers (Headers).
-
the actual HTTP payload body (
BinStruct::String
).
Note: When creating a HTTP Response packet, sport
and dport
attributes of TCP header are not set.
Instance Attribute Summary collapse
-
#body ⇒ BinStruct::String
Response body.
-
#headers ⇒ HTTP::Headers
associated http/1.1 headers.
-
#status_code ⇒ BinStruct::String
Response status code.
-
#status_mesg ⇒ BinStruct::String
Response status message.
-
#version ⇒ BinStruct::String
HTTP version.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Response
constructor
A new instance of Response.
-
#parse? ⇒ Boolean
May be parsed as a HTTP response if version is
HTTP/1.x
. -
#read(str) ⇒ self
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
Constructor Details
#initialize(options = {}) ⇒ Response
Returns a new instance of Response.
69 70 71 72 |
# File 'lib/packetgen/header/http/response.rb', line 69 def initialize(={}) super self.headers ||= [:headers] end |
Instance Attribute Details
#body ⇒ BinStruct::String
Response body
61 |
# File 'lib/packetgen/header/http/response.rb', line 61 define_attr :body, BinStruct::String |
#headers ⇒ HTTP::Headers
associated http/1.1 headers
57 |
# File 'lib/packetgen/header/http/response.rb', line 57 define_attr :headers, HTTP::Headers |
#status_code ⇒ BinStruct::String
Response status code
49 |
# File 'lib/packetgen/header/http/response.rb', line 49 define_attr :status_code, BinStruct::String |
#status_mesg ⇒ BinStruct::String
Response status message
53 |
# File 'lib/packetgen/header/http/response.rb', line 53 define_attr :status_mesg, BinStruct::String |
#version ⇒ BinStruct::String
HTTP version
45 |
# File 'lib/packetgen/header/http/response.rb', line 45 define_attr :version, BinStruct::String, default: 'HTTP/1.1' |
Instance Method Details
#parse? ⇒ Boolean
May be parsed as a HTTP response if version is HTTP/1.x
.
90 91 92 |
# File 'lib/packetgen/header/http/response.rb', line 90 def parse? version.start_with?('HTTP/1.') end |
#read(str) ⇒ self
Read in the HTTP portion of the packet, and parse it.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/packetgen/header/http/response.rb', line 76 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.
96 97 98 99 100 101 102 103 |
# File 'lib/packetgen/header/http/response.rb', line 96 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 |