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.
-
#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, 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 = {}) ⇒ Response
Returns a new instance of Response.
62 63 64 65 |
# File 'lib/packetgen/header/http/response.rb', line 62 def initialize(={}) super() self.headers ||= [:headers] end |
Instance Attribute Details
#body ⇒ HTTP::PHeaders
54 |
# File 'lib/packetgen/header/http/response.rb', line 54 define_field :body, Types::String |
#headers ⇒ Types::String
associated http/1.1 headers
51 |
# File 'lib/packetgen/header/http/response.rb', line 51 define_field :headers, HTTP::Headers |
#status_code ⇒ Types::String
44 |
# File 'lib/packetgen/header/http/response.rb', line 44 define_field :status_code, Types::String |
#status_mesg ⇒ Types::String
47 |
# File 'lib/packetgen/header/http/response.rb', line 47 define_field :status_mesg, Types::String |
#version ⇒ Types::String
41 |
# File 'lib/packetgen/header/http/response.rb', line 41 define_field :version, Types::String, default: 'HTTP/1.1' |
Instance Method Details
#read(str) ⇒ PacketGen::HTTP::Response
Read in the HTTP portion of the packet, and parse it.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/packetgen/header/http/response.rb', line 69 def read(str) str = str.bytes.map!(&:chr).join unless str.valid_encoding? arr = str.split("\r\n") headers = [] # header stream data = [] # data stream switch = false arr.each do |line| if line.empty? data << line if switch # already done switch = true next end case switch when true data << line else headers << line end end unless headers.empty? first_line = headers.shift.split self[:version].read first_line[0] self[:status_code].read first_line[1] self[:status_mesg].read first_line[2..-1].join(' ') self[:headers].read(headers.join("\n")) end self[:body].read data.join("\n") self end |
#to_s ⇒ String
String representation of data.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/packetgen/header/http/response.rb', line 101 def to_s raise FormatError, 'Missing #status_code.' if self.status_code.empty? raise FormatError, 'Missing #status_mesg.' if self.status_mesg.empty? raise FormatError, 'Missing #version.' if self.version.empty? str = ''.dup # build 'dat string str << self[:version] << ' ' << self[:status_code] << ' ' << self[:status_mesg] << "\r\n" str << self[:headers].to_s if self[:headers].given? str << self.body end |