From 66881d7dd196eb7a588b576e6e4654362c326cf4 Mon Sep 17 00:00:00 2001 From: "Robert G. Jakabosky" Date: Sat, 1 Sep 2018 02:25:05 +0800 Subject: [PATCH] If buffer is empty, read more data before calling parser. --- src/client/parser.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/client/parser.rs b/src/client/parser.rs index b6f4ea3f7..5fd81da25 100644 --- a/src/client/parser.rs +++ b/src/client/parser.rs @@ -39,6 +39,23 @@ impl HttpResponseParser { T: IoStream, { loop { + // Don't call parser until we have data to parse. + if !buf.is_empty() { + match HttpResponseParser::parse_message(buf) + .map_err(HttpResponseParserError::Error)? + { + Async::Ready((msg, decoder)) => { + self.decoder = decoder; + return Ok(Async::Ready(msg)); + } + Async::NotReady => { + if buf.capacity() >= MAX_BUFFER_SIZE { + return Err(HttpResponseParserError::Error(ParseError::TooLarge)); + } + // Parser needs more data. + } + } + } // Read some more data into the buffer for the parser. match io.read_available(buf) { Ok(Async::Ready((false, true))) => { @@ -50,22 +67,6 @@ impl HttpResponseParser { return Err(HttpResponseParserError::Error(err.into())) } } - - // Call HTTP response parser. - match HttpResponseParser::parse_message(buf) - .map_err(HttpResponseParserError::Error)? - { - Async::Ready((msg, decoder)) => { - self.decoder = decoder; - return Ok(Async::Ready(msg)); - } - Async::NotReady => { - if buf.capacity() >= MAX_BUFFER_SIZE { - return Err(HttpResponseParserError::Error(ParseError::TooLarge)); - } - // Parser needs more data. Loop and read more data. - } - } } }