public void StatusLineParserNullBuffer() { HttpUnsortedResponse statusLine = new HttpUnsortedResponse(); HttpStatusLineParser parser = new HttpStatusLineParser(statusLine, ParserData.MinStatusLineSize); Assert.NotNull(parser); int bytesConsumed = 0; Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer"); }
public void StatusLineParserMinimumBuffer() { byte[] data = CreateBuffer("HTTP/1.1", "200", ""); HttpUnsortedResponse statusLine = new HttpUnsortedResponse(); HttpStatusLineParser parser = new HttpStatusLineParser(statusLine, ParserData.MinStatusLineSize); Assert.NotNull(parser); int bytesConsumed = 0; ParserState state = parser.ParseBuffer(data, data.Length, ref bytesConsumed); Assert.Equal(ParserState.Done, state); Assert.Equal(data.Length, bytesConsumed); ValidateResult(statusLine, new Version("1.1"), HttpStatusCode.OK, ""); }
private static ParserState ParseBufferInSteps(HttpStatusLineParser parser, byte[] buffer, int readsize, out int totalBytesConsumed) { ParserState state = ParserState.Invalid; totalBytesConsumed = 0; while (totalBytesConsumed <= buffer.Length) { int size = Math.Min(buffer.Length - totalBytesConsumed, readsize); byte[] parseBuffer = new byte[size]; Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size); int bytesConsumed = 0; state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed); totalBytesConsumed += bytesConsumed; if (state != ParserState.NeedMoreData) { return state; } } return state; }
private static ParserState ParseBufferInSteps(HttpStatusLineParser parser, byte[] buffer, int readsize, out int totalBytesConsumed) { ParserState state = ParserState.Invalid; totalBytesConsumed = 0; while (totalBytesConsumed <= buffer.Length) { int size = Math.Min(buffer.Length - totalBytesConsumed, readsize); byte[] parseBuffer = new byte[size]; Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size); int bytesConsumed = 0; state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed); totalBytesConsumed += bytesConsumed; if (state != ParserState.NeedMoreData) { return(state); } } return(state); }
public ParserState ParseBuffer( byte[] buffer, int bytesReady, ref int bytesConsumed) { if (buffer == null) { throw new ArgumentNullException("buffer"); } ParserState parseStatus = ParserState.NeedMoreData; ParserState subParseStatus = ParserState.NeedMoreData; switch (_responseStatus) { case HttpResponseState.StatusLine: try { subParseStatus = _statusLineParser.ParseBuffer(buffer, bytesReady, ref bytesConsumed); } catch (Exception) { subParseStatus = ParserState.Invalid; } if (subParseStatus == ParserState.Done) { _responseStatus = HttpResponseState.ResponseHeaders; subParseStatus = ParserState.NeedMoreData; goto case HttpResponseState.ResponseHeaders; } else if (subParseStatus != ParserState.NeedMoreData) { // Report error - either Invalid or DataTooBig parseStatus = subParseStatus; break; } break; // read more data case HttpResponseState.ResponseHeaders: if (bytesConsumed >= bytesReady) { // we already can tell we need more data break; } try { subParseStatus = _headerParser.ParseBuffer(buffer, bytesReady, ref bytesConsumed); } catch (Exception) { subParseStatus = ParserState.Invalid; } if (subParseStatus == ParserState.Done) { parseStatus = subParseStatus; } else if (subParseStatus != ParserState.NeedMoreData) { parseStatus = subParseStatus; break; } break; // need more data } return(parseStatus); }