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

Inherits:
Base
  • Object
show all
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.

Examples:

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.class # => PacketGen::Header::HTTP::Response

HTTP Response attributes

http_resp = PacketGen::Header::HTTP::Response.new
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

  • LemonTree55

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

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



69
70
71
72
# File 'lib/packetgen/header/http/response.rb', line 69

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

Instance Attribute Details

#bodyBinStruct::String

Response body

Returns:

  • (BinStruct::String)


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

define_attr :body, BinStruct::String

#headersHTTP::Headers

associated http/1.1 headers

Returns:



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

define_attr :headers, HTTP::Headers

#status_codeBinStruct::String

Response status code

Returns:

  • (BinStruct::String)


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

define_attr :status_code, BinStruct::String

#status_mesgBinStruct::String

Response status message

Returns:

  • (BinStruct::String)


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

define_attr :status_mesg, BinStruct::String

#versionBinStruct::String

HTTP version

Returns:

  • (BinStruct::String)


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.

Returns:

  • (Boolean)

Since:

  • 2.2.0



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.

Returns:

  • (self)

Since:

  • 2.2.0



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_sString

String representation of data.

Returns:

  • (String)

Since:

  • 2.2.0



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