Class: HTTPcap::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/httpcap/message.rb

Overview

Message is a abstract class for Request and Response

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data) ⇒ Message

Returns a new instance of Message.

Parameters:

  • type (Integer)

    request or response



13
14
15
16
17
18
19
20
21
22
# File 'lib/httpcap/message.rb', line 13

def initialize(type, data)
  @parser = HTTP::Parser.new(type)
  @headers = Headers.new
  @body = ''

  %i[on_message_complete on_url on_header_field on_header_value on_headers_complete on_body].each do |name|
    @parser.send(name, &method(name))
  end
  receive_data(data)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/httpcap/message.rb', line 10

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/httpcap/message.rb', line 10

def headers
  @headers
end

Instance Method Details

#on_body(data) ⇒ Object



44
45
46
# File 'lib/httpcap/message.rb', line 44

def on_body(data)
  @body += data
end

#on_header_field(value) ⇒ Object



32
33
34
# File 'lib/httpcap/message.rb', line 32

def on_header_field(value)
  @headers.stream(Headers::TYPE_FIELD, value)
end

#on_header_value(value) ⇒ Object



36
37
38
# File 'lib/httpcap/message.rb', line 36

def on_header_value(value)
  @headers.stream(Headers::TYPE_VALUE, value)
end

#on_headers_completeObject



40
41
42
# File 'lib/httpcap/message.rb', line 40

def on_headers_complete
  @headers.stream_complete
end

#on_message_completeObject



28
# File 'lib/httpcap/message.rb', line 28

def on_message_complete; end

#on_urlObject



30
# File 'lib/httpcap/message.rb', line 30

def on_url; end

#receive_data(data) ⇒ Object



24
25
26
# File 'lib/httpcap/message.rb', line 24

def receive_data(data)
  @parser << data
end