Class: PacketGen::Header::HTTP::Response

Inherits:
Base show all
Defined in:
lib/packetgen/header/http/response.rb

Overview

An HTTP/1.1 Response packet consists of:

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

Author:

  • Kent 'picat' Gruber

Since:

  • 2.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :version (String)
  • :status_code (String)
  • :status_mesg (String)
  • :body (String)
  • :headers (Hash)

Since:

  • 2.2.0


62
63
64
65
# File 'lib/packetgen/header/http/response.rb', line 62

def initialize(options={})
  super(options)
  self.headers ||= options[:headers]
end

Instance Attribute Details

#bodyHTTP::PHeaders

Returns:

  • (HTTP::PHeaders)

54
# File 'lib/packetgen/header/http/response.rb', line 54

define_field :body, Types::String

#headersTypes::String

associated http/1.1 headers

Returns:


51
# File 'lib/packetgen/header/http/response.rb', line 51

define_field :headers, HTTP::Headers

#status_codeTypes::String

Returns:


44
# File 'lib/packetgen/header/http/response.rb', line 44

define_field :status_code, Types::String

#status_mesgTypes::String

Returns:


47
# File 'lib/packetgen/header/http/response.rb', line 47

define_field :status_mesg, Types::String

#versionTypes::String

Returns:


41
# File 'lib/packetgen/header/http/response.rb', line 41

define_field :version,     Types::String, default: 'HTTP/1.1'

Instance Method Details

#parse?Boolean

Returns:

  • (Boolean)

Since:

  • 2.2.0


81
82
83
# File 'lib/packetgen/header/http/response.rb', line 81

def parse?
  version.start_with?('HTTP/1.')
end

#read(str) ⇒ PacketGen::HTTP::Response

Read in the HTTP portion of the packet, and parse it.

Returns:

  • (PacketGen::HTTP::Response)

Since:

  • 2.2.0


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/packetgen/header/http/response.rb', line 69

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_sString

String representation of data.

Returns:

  • (String)

Since:

  • 2.2.0


87
88
89
90
91
92
93
94
# File 'lib/packetgen/header/http/response.rb', line 87

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