public void ResponseHeaderParserAcceptsCustomStatusCodes(HttpStatusCode status) { byte[] data = CreateBuffer( "HTTP/1.1", ((int)status).ToString(), "Reason", ParserData.ValidHeaders ); for (var cnt = 1; cnt <= data.Length; cnt++) { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result); Assert.NotNull(parser); int totalBytesConsumed = 0; ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(ParserState.Done, state); Assert.Equal(data.Length, totalBytesConsumed); ValidateResult( result, new Version("1.1"), status, "Reason", ParserData.ValidHeaders ); } }
private static ParserState ParseBufferInSteps( HttpResponseHeaderParser 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 void ResponseHeaderParserAcceptsValidVersion(Version version) { byte[] data = CreateBuffer( String.Format("HTTP/{0}", version.ToString(2)), "200", "Reason", ParserData.ValidHeaders ); for (var cnt = 1; cnt <= data.Length; cnt++) { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result); Assert.NotNull(parser); int totalBytesConsumed = 0; ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(data.Length, totalBytesConsumed); ValidateResult( result, version, HttpStatusCode.OK, "Reason", ParserData.ValidHeaders ); } }
public void ResponseHeaderParserNullBuffer() { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize); Assert.NotNull(parser); int bytesConsumed = 0; Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer"); }
public void ResponseHeaderParserNullBuffer() { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize); Assert.NotNull(parser); int bytesConsumed = 0; Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed); }, "buffer"); }
public void ResponseHeaderParserMinimumBuffer() { byte[] data = CreateBuffer("HTTP/1.1", "200", "", null); HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize); 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(result, new Version("1.1"), HttpStatusCode.OK, "", null); }
public void ResponseHeaderParserRejectsInvalidVersion(string invalid) { byte[] data = CreateBuffer(invalid, "200", "Reason", ParserData.ValidHeaders); for (var cnt = 1; cnt <= data.Length; cnt++) { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result); Assert.NotNull(parser); int totalBytesConsumed = 0; ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(ParserState.Invalid, state); } }
public void HttpResponseHeaderParserConstructorTest() { HttpUnsortedResponse result = new HttpUnsortedResponse(); Assert.NotNull(result); Assert.ThrowsArgumentGreaterThanOrEqualTo(() => new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize - 1, ParserData.MinHeaderSize), "maxStatusLineSize", ParserData.MinStatusLineSize.ToString(), ParserData.MinStatusLineSize - 1); Assert.ThrowsArgumentGreaterThanOrEqualTo(() => new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize - 1), "maxHeaderSize", ParserData.MinHeaderSize.ToString(), ParserData.MinHeaderSize - 1); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize); Assert.NotNull(parser); Assert.ThrowsArgumentNull(() => { new HttpResponseHeaderParser(null); }, "httpResponse"); }
public void ResponseHeaderParserMinimumBuffer() { byte[] data = CreateBuffer("HTTP/1.1", "200", "", null); HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize); 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(result, new Version("1.1"), HttpStatusCode.OK, "", null); }
public void HttpResponseHeaderParserConstructorTest() { HttpUnsortedResponse result = new HttpUnsortedResponse(); Assert.NotNull(result); Assert.ThrowsArgumentGreaterThanOrEqualTo( () => new HttpResponseHeaderParser( result, ParserData.MinStatusLineSize - 1, ParserData.MinHeaderSize ), "maxStatusLineSize", ParserData.MinStatusLineSize.ToString(), ParserData.MinStatusLineSize - 1 ); Assert.ThrowsArgumentGreaterThanOrEqualTo( () => new HttpResponseHeaderParser( result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize - 1 ), "maxHeaderSize", ParserData.MinHeaderSize.ToString(), ParserData.MinHeaderSize - 1 ); HttpResponseHeaderParser parser = new HttpResponseHeaderParser( result, ParserData.MinStatusLineSize, ParserData.MinHeaderSize ); Assert.NotNull(parser); Assert.ThrowsArgumentNull( () => { new HttpResponseHeaderParser(null); }, "httpResponse" ); }
public void ResponseHeaderParserAcceptsStandardStatusCodes(HttpStatusCode status) { byte[] data = CreateBuffer("HTTP/1.1", ((int)status).ToString(), "Reason", ParserData.ValidHeaders); for (var cnt = 1; cnt <= data.Length; cnt++) { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result); Assert.NotNull(parser); int totalBytesConsumed = 0; ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(ParserState.Done, state); Assert.Equal(data.Length, totalBytesConsumed); ValidateResult(result, new Version("1.1"), status, "Reason", ParserData.ValidHeaders); } }
private static ParserState ParseBufferInSteps(HttpResponseHeaderParser 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 async Task<HttpResponseMessage> ReadAsHttpResponseMessageAsyncCore(this HttpContent content, int bufferSize, int maxHeaderSize, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); Stream stream = await content.ReadAsStreamAsync(); HttpUnsortedResponse httpResponse = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(httpResponse, HttpResponseHeaderParser.DefaultMaxStatusLineSize, maxHeaderSize); ParserState parseStatus; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; int headerConsumed = 0; while (true) { try { bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); } catch (Exception e) { throw new IOException(Properties.Resources.HttpMessageErrorReading, e); } try { parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed); } catch (Exception) { parseStatus = ParserState.Invalid; } if (parseStatus == ParserState.Done) { // Create and return parsed HttpResponseMessage return CreateHttpResponseMessage(httpResponse, stream, bytesRead - headerConsumed); } else if (parseStatus != ParserState.NeedMoreData) { throw Error.InvalidOperation(Properties.Resources.HttpMessageParserError, headerConsumed, buffer); } else if (bytesRead == 0) { throw new IOException(Properties.Resources.ReadAsHttpMessageUnexpectedTermination); } } }
public void ResponseHeaderParserRejectsInvalidVersion(string invalid) { byte[] data = CreateBuffer(invalid, "200", "Reason", ParserData.ValidHeaders); for (var cnt = 1; cnt <= data.Length; cnt++) { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result); Assert.NotNull(parser); int totalBytesConsumed = 0; ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(ParserState.Invalid, state); } }
public void ResponseHeaderParserAcceptsValidVersion(Version version) { byte[] data = CreateBuffer(String.Format("HTTP/{0}", version.ToString(2)), "200", "Reason", ParserData.ValidHeaders); for (var cnt = 1; cnt <= data.Length; cnt++) { HttpUnsortedResponse result = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(result); Assert.NotNull(parser); int totalBytesConsumed = 0; ParserState state = ParseBufferInSteps(parser, data, cnt, out totalBytesConsumed); Assert.Equal(data.Length, totalBytesConsumed); ValidateResult(result, version, HttpStatusCode.OK, "Reason", ParserData.ValidHeaders); } }
public static Task<HttpResponseMessage> ReadAsHttpResponseMessageAsync(this HttpContent content, int bufferSize) { if (content == null) { throw new ArgumentNullException("content"); } if (bufferSize < MinBufferSize) { throw new ArgumentOutOfRangeException("bufferSize", bufferSize, RS.Format(Properties.Resources.ArgumentMustBeGreaterThanOrEqualTo, MinBufferSize)); } HttpMessageContent.ValidateHttpMessageContent(content, false, true); return content.ReadAsStreamAsync().Then(stream => { HttpUnsortedResponse httpResponse = new HttpUnsortedResponse(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(httpResponse); ParserState parseStatus; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; int headerConsumed = 0; while (true) { try { bytesRead = stream.Read(buffer, 0, buffer.Length); } catch (Exception e) { throw new IOException(Properties.Resources.HttpMessageErrorReading, e); } try { parseStatus = parser.ParseBuffer(buffer, bytesRead, ref headerConsumed); } catch (Exception) { parseStatus = ParserState.Invalid; } if (parseStatus == ParserState.Done) { // Create and return parsed HttpResponseMessage return CreateHttpResponseMessage(httpResponse, stream, bytesRead - headerConsumed); } else if (parseStatus != ParserState.NeedMoreData) { throw new IOException(RS.Format(Properties.Resources.HttpMessageParserError, headerConsumed, buffer)); } } }); }