Class: SSE::Impl::HTTPResponseReader
- Inherits:
-
Object
- Object
- SSE::Impl::HTTPResponseReader
- Defined in:
- lib/ld-eventsource/impl/streaming_http.rb
Overview
Used internally to read the HTTP response, either all at once or as a stream of text lines. Incoming data is fed into an instance of HTTPTools::Parser, which gives us the header and chunks of the body via callbacks.
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
10000
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(socket, read_timeout) ⇒ HTTPResponseReader
constructor
A new instance of HTTPResponseReader.
- #read_all ⇒ Object
- #read_lines ⇒ Object
Constructor Details
#initialize(socket, read_timeout) ⇒ HTTPResponseReader
Returns a new instance of HTTPResponseReader.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 142 def initialize(socket, read_timeout) @socket = socket @read_timeout = read_timeout @parser = HTTPTools::Parser.new @buffer = "" @done = false @lock = Mutex.new # Provide callbacks for the Parser to give us the headers and body. This has to be done # before we start piping any data into the parser. have_headers = false @parser.on(:header) do have_headers = true end @parser.on(:stream) do |data| @lock.synchronize { @buffer << data } # synchronize because we're called from another thread in Socketry end @parser.on(:finish) do @lock.synchronize { @done = true } end # Block until the status code and headers have been successfully read. while !have_headers raise EOFError if !read_chunk_into_buffer end @headers = Hash[@parser.header.map { |k,v| [k.downcase, v] }] @status = @parser.status_code end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
140 141 142 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 140 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
140 141 142 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 140 def status @status end |
Instance Method Details
#read_all ⇒ Object
181 182 183 184 185 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 181 def read_all while read_chunk_into_buffer end @buffer end |
#read_lines ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/ld-eventsource/impl/streaming_http.rb', line 171 def read_lines Enumerator.new do |gen| loop do line = read_line break if line.nil? gen.yield line end end end |