Class: HTTP::Response::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/http/response/parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

NOTE(ixti): This class is a subject of future refactoring, thus don't expect this class API to be stable until this message disappears and class is not marked as private anymore.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.



15
16
17
18
19
20
# File 'lib/http/response/parser.rb', line 15

def initialize
  @state  = HttpParser::Parser.new_instance { |i| i.type = :response }
  @parser = HttpParser::Parser.new(self)

  reset
end

Instance Attribute Details

#headersObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



13
14
15
# File 'lib/http/response/parser.rb', line 13

def headers
  @headers
end

Instance Method Details

#add(data) ⇒ self Also known as: <<

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (self)

Raises:

  • (IOError)


23
24
25
26
27
28
29
30
31
32
# File 'lib/http/response/parser.rb', line 23

def add(data)
  # XXX(ixti): API doc of HttpParser::Parser is misleading, it says that
  #   it returns boolean true if data was parsed successfully, but instead
  #   it's response tells if there was an error; So when it's `true` that
  #   means parse failed, and `false` means parse was successful.
  #   case of success.
  return self unless @parser.parse(@state, data)

  raise IOError, "Could not parse data"
end

#finished?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


103
104
105
# File 'lib/http/response/parser.rb', line 103

def finished?
  @finished[:message]
end

#headers?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


35
36
37
# File 'lib/http/response/parser.rb', line 35

def headers?
  @finished[:headers]
end

#http_versionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
# File 'lib/http/response/parser.rb', line 39

def http_version
  @state.http_version
end

#on_body(_response, chunk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
# File 'lib/http/response/parser.rb', line 66

def on_body(_response, chunk)
  if @chunk
    @chunk << chunk
  else
    @chunk = chunk
  end
end

#on_header_field(_response, field) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

HTTP::Parser callbacks



51
52
53
54
# File 'lib/http/response/parser.rb', line 51

def on_header_field(_response, field)
  append_header if @reading_header_value
  @field << field
end

#on_header_value(_response, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
# File 'lib/http/response/parser.rb', line 56

def on_header_value(_response, value)
  @reading_header_value = true
  @field_value << value
end

#on_headers_complete(_reposse) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
# File 'lib/http/response/parser.rb', line 61

def on_headers_complete(_reposse)
  append_header if @reading_header_value
  @finished[:headers] = true
end

#on_message_complete(_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
# File 'lib/http/response/parser.rb', line 88

def on_message_complete(_response)
  @finished[:message] = true
end

#read(size) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/http/response/parser.rb', line 74

def read(size)
  return if @chunk.nil?

  if @chunk.bytesize <= size
    chunk  = @chunk
    @chunk = nil
  else
    chunk = @chunk.byteslice(0, size)
    @chunk[0, size] = ""
  end

  chunk
end

#resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
95
96
97
98
99
100
101
# File 'lib/http/response/parser.rb', line 92

def reset
  @state.reset!

  @finished             = Hash.new(false)
  @headers              = HTTP::Headers.new
  @reading_header_value = false
  @field                = +""
  @field_value          = +""
  @chunk                = nil
end

#status_codeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/http/response/parser.rb', line 43

def status_code
  @state.http_status
end