public void TestHttpMessageParser() { var httpResponse = @"HTTP/1.1 200 OK X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN Cache-Control: private, max-age=0 Content-Type: text/html; charset=UTF-8 Date: Fri, 13 Oct 2017 05:54:52 GMT Expires: -1 P3P: CP=""This is not a P3P policy! See g.co/p3phelp for more info."" Set-Cookie: 1P_JAR=2017-10-13-05; expires=Fri, 20-Oct-2017 05:54:52 GMT; path=/; domain=.google.co.jp,NID=114=Vzr79B7ISI0vlP54dhHQ1lyoyqxePhvy_k3w2ofp1oce73oG3m9ltBiUgdQNj4tSMkp-oWtzmhUi3rf314Fcrjy6J2DxtyEdA_suJlgfdN9973V2HO32OG9D3svImEJf; expires=Sat, 14-Apr-2018 05:54:52 GMT; path=/; domain=.google.co.jp; HttpOnly Server: gws Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding: chunked"; var parser = new HttpMessageParser(Encoding.ASCII.GetBytes(httpResponse)); parser.Append("foo: bar"); parser.StatusCode.Should().Be(HttpStatusCode.OK); parser.Reason.Should().Be("OK"); parser.Headers["X-XSS-Protection"].Should().Be("1; mode=block"); parser.Headers["X-Frame-Options"].Should().Be("SAMEORIGIN"); parser.Headers["Cache-Control"].Should().Be("private, max-age=0"); parser.Headers["P3P"].Should().Be("CP=\"This is not a P3P policy! See g.co/p3phelp for more info.\""); parser.Headers["Set-Cookie"].Should().Be( "1P_JAR=2017-10-13-05; expires=Fri, 20-Oct-2017 05:54:52 GMT; path=/; domain=.google.co.jp,NID=114=Vzr79B7ISI0vlP54dhHQ1lyoyqxePhvy_k3w2ofp1oce73oG3m9ltBiUgdQNj4tSMkp-oWtzmhUi3rf314Fcrjy6J2DxtyEdA_suJlgfdN9973V2HO32OG9D3svImEJf; expires=Sat, 14-Apr-2018 05:54:52 GMT; path=/; domain=.google.co.jp; HttpOnly"); parser.Headers["foo"].Should().Be("bar"); parser = new HttpMessageParser("HTTP/1.1 200 OK"); parser.StatusCode.Should().Be(HttpStatusCode.OK); parser.Reason.Should().Be("OK"); }
public void TestAppenNullToHttpMessageParser() { var httpResponse = @"HTTP/1.1 200 OK X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN Cache-Control: private, max-age=0 Content-Type: text/html; charset=UTF-8 Date: Fri, 13 Oct 2017 05:54:52 GMT Expires: -1 P3P: CP=""This is not a P3P policy! See g.co/p3phelp for more info."" Set-Cookie: 1P_JAR=2017-10-13-05; expires=Fri, 20-Oct-2017 05:54:52 GMT; path=/; domain=.google.co.jp,NID=114=Vzr79B7ISI0vlP54dhHQ1lyoyqxePhvy_k3w2ofp1oce73oG3m9ltBiUgdQNj4tSMkp-oWtzmhUi3rf314Fcrjy6J2DxtyEdA_suJlgfdN9973V2HO32OG9D3svImEJf; expires=Sat, 14-Apr-2018 05:54:52 GMT; path=/; domain=.google.co.jp; HttpOnly Server: gws Accept-Ranges: none Vary: Accept-Encoding Transfer-Encoding: chunked"; Exception e = null; var parser = new HttpMessageParser(Encoding.ASCII.GetBytes(httpResponse)); try { parser.Append(null); } catch (Exception ex) { e = ex; } e.Should().NotBeNull("because an exception is expected"); }
public void TestHttpMessageParserWithString() { var parser = new HttpMessageParser("HTTP/1.1 200 OK"); parser.StatusCode.Should().Be(HttpStatusCode.OK); parser.Reason.Should().Be("OK"); }
/// <summary>Creates new instance of DefaultBHttpClientConnection.</summary> /// <remarks>Creates new instance of DefaultBHttpClientConnection.</remarks> /// <param name="buffersize">buffer size. Must be a positive number.</param> /// <param name="fragmentSizeHint">fragment size hint.</param> /// <param name="chardecoder"> /// decoder to be used for decoding HTTP protocol elements. /// If <code>null</code> simple type cast will be used for byte to char conversion. /// </param> /// <param name="charencoder"> /// encoder to be used for encoding HTTP protocol elements. /// If <code>null</code> simple type cast will be used for char to byte conversion. /// </param> /// <param name="constraints"> /// Message constraints. If <code>null</code> /// <see cref="Org.Apache.Http.Config.MessageConstraints.Default">Org.Apache.Http.Config.MessageConstraints.Default /// </see> /// will be used. /// </param> /// <param name="incomingContentStrategy"> /// incoming content length strategy. If <code>null</code> /// <see cref="Org.Apache.Http.Impl.Entity.LaxContentLengthStrategy.Instance">Org.Apache.Http.Impl.Entity.LaxContentLengthStrategy.Instance /// </see> /// will be used. /// </param> /// <param name="outgoingContentStrategy"> /// outgoing content length strategy. If <code>null</code> /// <see cref="Org.Apache.Http.Impl.Entity.StrictContentLengthStrategy.Instance">Org.Apache.Http.Impl.Entity.StrictContentLengthStrategy.Instance /// </see> /// will be used. /// </param> /// <param name="requestWriterFactory"> /// request writer factory. If <code>null</code> /// <see cref="Org.Apache.Http.Impl.IO.DefaultHttpRequestWriterFactory.Instance">Org.Apache.Http.Impl.IO.DefaultHttpRequestWriterFactory.Instance /// </see> /// will be used. /// </param> /// <param name="responseParserFactory"> /// response parser factory. If <code>null</code> /// <see cref="Org.Apache.Http.Impl.IO.DefaultHttpResponseParserFactory.Instance">Org.Apache.Http.Impl.IO.DefaultHttpResponseParserFactory.Instance /// </see> /// will be used. /// </param> internal DefaultBHttpClientConnection(int buffersize, int fragmentSizeHint, CharsetDecoder chardecoder, CharsetEncoder charencoder, MessageConstraints constraints, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy, HttpMessageWriterFactory <IHttpRequest> requestWriterFactory, HttpMessageParserFactory <HttpResponse> responseParserFactory ) : base(buffersize, fragmentSizeHint, chardecoder, charencoder, constraints, incomingContentStrategy , outgoingContentStrategy) { this.requestWriter = (requestWriterFactory != null ? requestWriterFactory : DefaultHttpRequestWriterFactory .Instance).Create(GetSessionOutputBuffer()); this.responseParser = (responseParserFactory != null ? responseParserFactory : DefaultHttpResponseParserFactory .Instance).Create(GetSessionInputBuffer(), constraints); }
public void TestChunked_ContentLength() { 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-Modi"; var header2 = "fied: Wed, 22 Jul 2009 19:15:56 GMT\r\n" + "Content-Length: 5\r\n" + "Content-Type: text/plain\r\n\r\n"; var body = "ABCDE"; var parser = new HttpMessageParser(logger, true); Assert.Null(parser.Receive(new ByteArray(header1, Encoding.ASCII))); Assert.Null(parser.Receive(new ByteArray(header2, Encoding.ASCII))); var response = parser.Receive(new ByteArray(body, Encoding.ASCII)); Assert.NotNull(response); Assert.Equal("Apache", response.Headers["Server"]); Assert.Equal(body, Encoding.ASCII.GetString(response.BodyData.ReadBytes())); }
public void TestCheckHeader() { HttpMessageParser parser = new HttpMessageParser("HTTP/1.1 401 Unauthorized"); parser.Append("Content - Type: application / json"); parser.Append("Server: Couchbase Sync Gateway/2.0.0"); parser.Append("Www-Authenticate: Basic realm=\"Couchbase Sync Gateway\""); parser.Append("Date: Mon, 06 Aug 2018 17:44:51 GMT"); parser.Append("Content-Length: 50"); string key = "Connection"; string expectedValue = "Upgrade"; bool caseSens = false; // true var method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static); var res = method.Invoke(null, new object[4] { parser, key, expectedValue, caseSens }); res.Should().Be(false); parser = new HttpMessageParser("HTTP/1.1 101 Switching Protocols"); parser.Append("Upgrade: websocket"); parser.Append("Connection: Upgrade"); parser.Append("Sec-WebSocket-Accept: R3ztu/aZLI+izEEtS3Ao1kzub4s="); parser.Append("Sec-WebSocket-Protocol: BLIP_3+CBMobile_2"); key = "Connection"; expectedValue = "Upgrade"; caseSens = false; method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static); res = method.Invoke(null, new object[4] { parser, key, expectedValue, caseSens }); res.Should().Be(true); key = "Upgrade"; expectedValue = "websocket"; caseSens = false; method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static); res = method.Invoke(null, new object[4] { parser, key, expectedValue, caseSens }); res.Should().Be(true); key = "Sec-WebSocket-Accept"; expectedValue = "R3ztu/aZLI+izEEtS3Ao1kzub4s="; caseSens = true; method = WebSocketWrapperType.GetMethod("CheckHeader", BindingFlags.NonPublic | BindingFlags.Static); res = method.Invoke(null, new object[4] { parser, key, expectedValue, caseSens }); res.Should().Be(true); method = WebSocketWrapperType.GetMethod("Connected", BindingFlags.NonPublic | BindingFlags.Instance); //res = method.Invoke(webSocketWrapper, null); method = WebSocketWrapperType.GetMethod("ReceivedHttpResponse", BindingFlags.NonPublic | BindingFlags.Instance); //res = method.Invoke(webSocketWrapper, new object[1] { parser }); method = HttpLogicType.GetMethod("ReceivedResponse", BindingFlags.Public | BindingFlags.Instance); res = method.Invoke(hTTPLogic, new object[1] { parser }); method = HttpLogicType.GetMethod("Redirect", BindingFlags.NonPublic | BindingFlags.Instance); res = method.Invoke(hTTPLogic, new object[1] { parser }); }
public override ThenResponse Perform(EventInfo eventInfo) { if (eventInfo.Type == EventType.Message) { ThenResponse thenResult = ThenResponse.BreakRules; if (!eventInfo.Message.CheckedEntity(HttpMessage.EntityFlag)) { IVariable <byte[]> carryOverVar = eventInfo.Variables.GetOrDefault <byte[]>($"ThenDelimitHttp_CarryOverBytes") ?? eventInfo.Variables.Add <byte[]>($"ThenDelimitHttp_CarryOverBytes"); byte[] carryOverBytes = carryOverVar.Value ?? new byte[0]; carryOverVar.Value = new byte[0]; byte[] fullBytes = carryOverBytes.Combine(eventInfo.Message.RawBytes); List <EventInfo> childEvents = new List <EventInfo>(); Tuple <int, HttpMessage> messageInfo = null; IVariable <int> syncId = eventInfo.Variables.GetOrDefault <int>("ThenDelimitHttp_SyncId") ?? eventInfo.Variables.Add <int>("ThenDelimitHttp_SyncId"); do { HttpMessageParser parser = new HttpMessageParser() { TextEncoding = eventInfo.ProxyConnection.ProxyInfo.DefaultEncoding }; messageInfo = parser.Parse(fullBytes); if (messageInfo.Item2 != null) { carryOverBytes = new byte[0]; messageInfo.Item2.SyncId = syncId.Value++; childEvents.Add(eventInfo.Clone(message: messageInfo.Item2)); if (messageInfo.Item1 <= fullBytes.Length) { fullBytes = new Buffer <byte>(fullBytes, messageInfo.Item1, fullBytes.Length - messageInfo.Item1).GetBytes(); } else { fullBytes = new byte[0]; } } else { carryOverBytes = fullBytes; } }while (fullBytes.Length > 0 && messageInfo.Item2 != null); carryOverVar.Value = carryOverBytes; eventInfo.Engine.Queue.AddFirst(childEvents); eventInfo.Message.SetEntityFlag(HttpMessage.EntityFlag, false); } else if (eventInfo.Message is HttpMessage) { thenResult = ThenResponse.Continue; } return(thenResult); } else { return(ThenResponse.Continue); } }
public MessageValueHandler() { _httpHeadersParser = new HttpHeadersParser(); _httpStatusLineParser = new HttpStatusLineParser(); _httpMessageParser = new HttpMessageParser(); }