Method: HTTPTools::Parser#finish
- Defined in:
- lib/http_tools/parser.rb
#finish ⇒ Object
:call-seq: parser.finish -> parser
Used to notify the parser that the request has finished in a case where it can not be determined by the request itself.
For example, when a server does not set a content length, and instead relies on closing the connection to signify the body end.
until parser.finished?
begin
parser << socket.sysread(1024 * 16)
rescue EOFError
parser.finish
break
end
end
This method can not be used to interrupt parsing from within a callback.
Will raise HTTPTools::MessageIncompleteError if called too early, or HTTPTools::EndOfMessageError if the message has already finished, unless a callback has been set for the :error event, in which case the callback will recieve the error insted.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/http_tools/parser.rb', line 183 def finish if @state == :body_on_close @buffer = @scanner @state = elsif @state == :body_chunked && @buffer.eos? && !@trailer_expected && @header.any? {|k,v| CONNECTION.casecmp(k) == 0 && CLOSE.casecmp(v) == 0} @state = elsif @state == :start && @buffer.string.length < 1 raise EmptyMessageError.new("Message empty") else raise MessageIncompleteError.new("Message ended early") end self end |