public void TestValidPartialValueResponse() { var header1 = "HTTP/1.1 200 OK\r\n" + "Date: Mon, 27 Jul 2009 12:28:53 GMT\r\n" + "Server: Apache\r\n" + "Last-Modified: Wed, 22 Jul 2"; var header2 = header1 + "009 19:15:56 GMT\r\n" + "ETag: \"34aa387-d-1568eb00\"\r\n" + "Accept-Ranges: bytes\r\n" + "Content-Length: 0\r\n" + "Vary: Accept-Encoding\r\n" + "Content-Type: text/plain\r\n\r\n"; var parser = new HttpHeaderParser(); var stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(header1)); parser.Parse(stream, true); Assert.Equal("Mon, 27 Jul 2009 12:28:53 GMT", parser.GetHeader("Date")); Assert.Equal("Apache", parser.GetHeader("Server")); Assert.Null(parser.GetHeader("Last-Modi")); Assert.Null(parser.GetHeader("Last-Modified")); stream = new ByteArray(ASCIIEncoding.ASCII.GetBytes(header2)); parser.Parse(stream, true); Assert.Equal("Apache", parser.GetHeader("Server")); Assert.Equal("Wed, 22 Jul 2009 19:15:56 GMT", parser.GetHeader("Last-Modified")); Assert.Equal("\"34aa387-d-1568eb00\"", parser.GetHeader("ETag")); var data = parser.Create(stream, EmptyData.Instance); }
public void BasicDoc() { var slize = CreateSlice(@"GET / HTTP/1.1 Connection: Keep-Alive HOST: localhost Content-Length: 0 "); _parser.Parse(new SliceStream(slize, slize.Count)); Assert.Equal("Keep-Alive", _request.Headers["Connection"].Value); Assert.Equal("localhost", _request.Headers["HOST"].Value); Assert.Equal("0", _request.Headers["Content-Length"].Value); }
public void ParseHeader_SkipBody() { const string HttpPost = @"POST / HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 11 Origin: http://localhost:8080 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Accept: */* Referer: http://localhost:8080/ajaxPost.html Accept-Encoding: gzip,deflate,sdch Accept-Language: sv,en;q=0.8,en-US;q=0.6 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF hello=world"; string actual = ""; var buffer = new BufferSlice(Encoding.ASCII.GetBytes(HttpPost), 0, HttpPost.Length); var stream = new SliceStream(buffer, buffer.Count); var parser = new HttpHeaderParser(); parser.HeaderParsed += (sender, args) => actual = args.Value; parser.Parse(stream); Assert.Equal("ASP.NET_SessionId=5vkr4tfivb1ybu1sm4u4kahy; GriffinLanguageSwitcher=sv-se; __RequestVerificationToken=LiTSJATsiqh8zlcft_3gZwvY8HpcCUkirm307njxIZLdsJSYyqaV2st1tunH8sMvMwsVrj3W4dDoV8ECZRhU4s6DhTvd2F-WFkgApDBB-CA1; .ASPXAUTH=BF8BE1C246428B10B49AE867BEDF9748DB3842285BC1AF1EC44AD80281C4AE084B75F0AE13EAF1BE7F71DD26D0CE69634E83C4846625DC7E4D976CA1845914E2CC7A7CF2C522EA5586623D9B73B0AE433337FC59CF6AF665DC135491E78978EF", actual); Assert.Equal('h', (char)buffer.Buffer[stream.Position]); }
public void Parse() { var buffer = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nSERVER: LOCALHOST\r\n\r\n"); var slice = new BufferSlice(buffer, 0, buffer.Length); var reader = new SliceStream(slice); var parser = new HttpHeaderParser(); parser.HeaderParsed += (sender, args) => Console.WriteLine(args.Name + ": " + args.Value); parser.Parse(reader); }
/// <summary> /// Handle an message /// </summary> /// <param name="context">Context unique for this handler instance</param> /// <param name="message">Message to process</param> public void HandleUpstream(IPipelineHandlerContext context, IPipelineMessage message) { if (message is Closed) { _bodyBytesLeft = 0; _headerParser.Reset(); } else if (message is Received) { var msg = (Received)message; // complete the body if (_bodyBytesLeft > 0) { var bytesToSend = Math.Min(_bodyBytesLeft, msg.BufferReader.RemainingLength); _bodyBytesLeft -= bytesToSend; context.SendUpstream(message); return; } _headerParser.Parse(msg.BufferReader); if (_headerCompleted) { var request = (IRequest)_message; var ourRequest = _message as HttpRequest; if (ourRequest != null) { ourRequest.RemoteEndPoint = msg.RemoteEndPoint as IPEndPoint; } request.AddHeader("RemoteAddress", msg.RemoteEndPoint.ToString()); var receivedHttpRequest = new ReceivedHttpRequest(request); _headerParser.Reset(); _headerCompleted = false; context.SendUpstream(receivedHttpRequest); if (msg.BufferReader.RemainingLength > 0) { context.SendUpstream(msg); } } return; } context.SendUpstream(message); }
/// <summary> /// Append more bytes to your message building /// </summary> /// <param name="reader">Contains bytes which was received from the other end</param> /// <returns><c>true</c> if a complete message has been built; otherwise <c>false</c>.</returns> /// <remarks>You must handle/read everything which is available in the buffer</remarks> public bool Append(IBufferReader reader) { _headerParser.Parse(reader); if (_bodyBytestLeft > 0) { var bytesToRead = Math.Min(reader.RemainingLength, _bodyBytestLeft); reader.CopyTo(_bodyStream, bytesToRead); _bodyBytestLeft -= bytesToRead; if (_bodyBytestLeft == 0) { _bodyStream.Position = 0; _messages.Enqueue(_message); _message = null; } if (reader.RemainingLength > 0) { _headerParser.Parse(reader); } } return(_messages.Count > 0); }
public HttpData Receive(IDataStream s) { lock (contentLock) { if (useContentData) { contentData.Add(s); } dataBuilder.Append(s); if (state == ParsingState.Header) { try { if (parser.Parse(contentData, isResponse)) { state = ParsingState.Body; try { var code = parser.StatusCode; // header parsing is finished if (LastRequestMethod == HttpRequestMethod.HEAD) { info = new HttpBodyLengthInfo((long)0); } else if (LastRequestMethod == HttpRequestMethod.CONNECT && code > 199 && code < 300) { info = new HttpBodyLengthInfo((long)0); } else { info = parser.GetBodyLengthInfo(!isResponse); } useContentData = info.Type == HttpBodyLengthInfo.LengthType.Chunked; headerLength = parser.HeaderLength; if (contentData.Length - parser.HeaderLength > 0) { var tmpList = new StreamList(); var contentStart = new ByteArray(contentData, parser.HeaderLength); tmpList.Add(contentStart); contentData = tmpList; } else { contentData = new StreamList(); } } catch (BadRequestException) { throw new HttpInvalidMessageException(); } } } catch (HttpHeaderParserException) { throw new HttpInvalidMessageException(); } } if (state == ParsingState.Body) { if (info.Type == HttpBodyLengthInfo.LengthType.Exact && info.Length <= dataBuilder.Length - headerLength) { var resStream = dataBuilder.Close(); return(parser.Create(new StreamSegment(resStream, 0, headerLength), new StreamSegment(resStream, headerLength, info.Length), resStream)); } else if (info.Type == HttpBodyLengthInfo.LengthType.Chunked) { parseChunkedBody(); } } } if (state == ParsingState.CheckingChunedTrailer) { if (contentData.Length >= 2) { // check CRLF bool chunkedEnd = contentData.ReadByte(0) == 13 && contentData.ReadByte(1) == 10; if (chunkedEnd) { contentData = new StreamList(); var resStream = dataBuilder.Close(); return(parser.Create(new StreamSegment(resStream, 0, headerLength), new StreamSegment(resStream, headerLength), resStream)); } else { parser.ParseChunkedTrailer(); state = ParsingState.ParsingChunkedTrailer; } } } if (state == ParsingState.ParsingChunkedTrailer) { if (parser.Parse(contentData, isResponse)) { contentData = new StreamList(); var resStream = dataBuilder.Close(); return(parser.Create(new StreamSegment(resStream, 0, headerLength), new StreamSegment(resStream, headerLength), resStream)); } } return(null); }