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, 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.

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

#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
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_sString

String representation of data.

Returns:

  • (String)

Raises:

Since:

  • 2.2.0



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