private void InterpretHttpHeaders( HttpHeaders headers, string httpVersion ) { Contract.Requires(!string.IsNullOrEmpty(httpVersion)); int length = 0; string contentLength = headers["content-length"]; if ( contentLength != null ) { length = int.Parse( contentLength ); } string transferEncoding = headers["transfer-encoding"]; if ( transferEncoding != null && transferEncoding.ToLower().Contains( "chunked" ) ) { _context.State = new ReadChunkedHeaderState( _context ); if ( _bodyData.Length > 0 ) { _context.State.AcceptData( _bodyData.ToArray() ); } else { _context.OnAdditionalDataRequested(); } } else if ( length > 0 ) { _context.State = new ReadNormalBodyState( _context, length ); if ( _bodyData.Length > 0 ) { _context.State.AcceptData( _bodyData.ToArray() ); } else { _context.OnAdditionalDataRequested(); } } // HTTP 1.0 assumes non-persistent connection (connection=close unnecessary) // HTTP 1.1 assumes persistent connection. Unless the connection is explictly closed, assume no body // if content-length is not specified. else if ( contentLength == null && (httpVersion == "1.0" || headers["connection"] == "close" )) { _context.State = new ReadHttp10BodyState(_context); _context.State.AcceptData(_bodyData.ToArray()); } else { _context.OnMessageReadComplete(); // 0-byte message. Start reading the next header. _context.State = new ReadHeaderState(_context); _context.OnAdditionalDataRequested(); } }
public void IsSslTest() { List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>(); headerList.Add(new KeyValuePair<string, string>("Host", "www.yahoo.com")); headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive")); HttpHeaders headers = new HttpHeaders(headerList); var objectUnderTest = new HttpRequestHeaderEventArgs("1.1", headers, "cOnNeCt", "http://www.yahoo.com:443"); Assert.That(objectUnderTest.IsSsl, Is.True); }
public void PersistentConnectionTestHttp11() { List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>(); headerList.Add(new KeyValuePair<string, string>("Age", "0")); HttpHeaders headers = new HttpHeaders(headerList); HttpResponseHeaderEventArgs args = new HttpResponseHeaderEventArgs("1.1", headers, 200, "OK"); Assert.That(args.IsPersistent, Is.True, "Default is true with HTTP 1.1"); Assert.That(args.HasBody, Is.False); }
public void IndexTest() { List<KeyValuePair<string,string> > pairs = new List<KeyValuePair<string, string>>(); pairs.Add(new KeyValuePair<string, string>("foo","bar")); pairs.Add(new KeyValuePair<string, string>("cat", "dog")); HttpHeaders headers = new HttpHeaders( pairs ); Assert.That(headers["CAT"], Is.EqualTo("dog")); Assert.That(headers["bar"], Is.Null); }
public void RenameKeyTest() { List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>(); pairs.Add(new KeyValuePair<string, string>("foo", "bar")); pairs.Add(new KeyValuePair<string, string>("cat", "dog")); HttpHeaders headers = new HttpHeaders(pairs); headers.RenameKey("cat", "kitty"); Assert.That(headers.AsEnumerable().First().Key, Is.EqualTo("foo")); Assert.That(headers.AsEnumerable().ElementAt(1).Key, Is.EqualTo("kitty")); Assert.That(headers["cat"], Is.Null); }
public void CreateBufferTest() { List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>(); headerList.Add(new KeyValuePair<string, string>("Host", "www.yahoo.com")); headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive")); HttpHeaders headers = new HttpHeaders(headerList); var objectUnderTest = new HttpRequestHeaderEventArgs("1.1", headers, "PUT", "/"); byte[] headerBytes = objectUnderTest.GetBuffer(); string expectedHeader = "PUT / HTTP/1.1\r\nHost: www.yahoo.com\r\nConnection: keep-alive\r\n\r\n"; var expectedBytes = Encoding.UTF8.GetBytes(expectedHeader); Assert.That(headerBytes, Is.EqualTo(expectedBytes)); }
public void GetBufferTest() { List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>(); headerList.Add(new KeyValuePair<string, string>("Content-Length", "10")); headerList.Add(new KeyValuePair<string, string>("Age", "0")); HttpHeaders headers = new HttpHeaders(headerList); var objectUnderTest = new HttpResponseHeaderEventArgs("1.1", headers, 200, "OK"); byte[] headerBytes = objectUnderTest.GetBuffer(); string expectedHeader = "HTTP/1.1 200 OK\r\nContent-Length: 10\r\nAge: 0\r\n\r\n"; var expectedBytes = Encoding.UTF8.GetBytes(expectedHeader); Assert.That(headerBytes, Is.EqualTo(expectedBytes)); }
public void ConvertProxyConnectionHeaderInHttp11() { List<KeyValuePair<string, string>> headerList = new List<KeyValuePair<string, string>>(); headerList.Add(new KeyValuePair<string, string>("Host", "www.yahoo.com")); headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive")); headerList.Add(new KeyValuePair<string, string>("Foo", "bar")); HttpHeaders headers = new HttpHeaders(headerList); var objectUnderTest = new HttpRequestHeaderEventArgs("1.1", headers, "GET", "http://www.yahoo.com/foo.html"); Assert.That(objectUnderTest.Headers.Count, Is.EqualTo(3)); Assert.That(objectUnderTest.Headers["Connection"], Is.EqualTo("keep-alive")); var buffer = objectUnderTest.GetBuffer(); // This test uses three headers to verify that the Connection header is modified in-place // and not moved in the list. This is important because a proxy server cannot change // the header order according to HTTP spec. Assert.That(Encoding.UTF8.GetString(buffer), Is.EqualTo("GET /foo.html HTTP/1.1\r\nHost: www.yahoo.com\r\nConnection: keep-alive\r\nFoo: bar\r\n\r\n")); }
public void RemoveKeyValueTest() { List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>(); pairs.Add(new KeyValuePair<string, string>("content-encoding", "gzip;bark")); pairs.Add(new KeyValuePair<string, string>("count", "one;two;three")); pairs.Add(new KeyValuePair<string, string>("content-type", "text/html;charset=utf-8")); pairs.Add(new KeyValuePair<string, string>("foo", "bar")); HttpHeaders headers = new HttpHeaders(pairs); headers.RemoveKeyValue("content-encoding", "bark"); headers.RemoveKeyValue("count", "two"); headers.RemoveKeyValue("content-type", "text/html"); headers.RemoveKeyValue("foo", "bar"); Assert.That(headers.Count, Is.EqualTo(3)); Assert.That(headers["content-encoding"], Is.EqualTo("gzip")); Assert.That(headers["content-type"], Is.EqualTo("charset=utf-8")); Assert.That(headers["count"], Is.EqualTo("one;three")); }
private static HttpHeaders CreateHeaders( string[] headerLines ) { List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>( headerLines.Length - 1 ); for ( int i = 1; i < headerLines.Length; i++ ) { if ( headerLines[i].Trim().Length > 0 ) { int index = headerLines[i].IndexOf( ':' ); if ( index == -1 ) { throw new ArgumentException( "HTTP header line invalid or malformed: " + headerLines[i] ); } pairs.Add( new KeyValuePair<string, string>( headerLines[i].Substring( 0, index ), headerLines[i].Substring( index + 1 ).Trim() ) ); } } HttpHeaders headers = new HttpHeaders( pairs ); return headers; }
public void RemoveProxyConnectionHeaderInHttp10() { List<KeyValuePair<string,string> > headerList = new List<KeyValuePair<string, string>>(); headerList.Add(new KeyValuePair<string, string>("Proxy-Connection", "keep-alive")); HttpHeaders headers = new HttpHeaders(headerList); var objectUnderTest = new HttpRequestHeaderEventArgs( "1.0", headers, "GET", "http://www.yahoo.com/foo.html" ); Assert.That(objectUnderTest.Headers.Count, Is.EqualTo(0)); var buffer = objectUnderTest.GetBuffer(); Assert.That(Encoding.UTF8.GetString(buffer), Is.EqualTo("GET /foo.html HTTP/1.0\r\n\r\n") ); }
public void UpsertTest() { List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>(); pairs.Add(new KeyValuePair<string, string>("one", "1")); pairs.Add(new KeyValuePair<string, string>("two", "2")); pairs.Add(new KeyValuePair<string, string>("three", "3")); pairs.Add(new KeyValuePair<string, string>("two", "2")); HttpHeaders headers = new HttpHeaders(pairs); Assert.That(headers.Count, Is.EqualTo(4)); headers.UpsertKeyValue("two", "4"); Assert.That(headers.Count, Is.EqualTo(3), "The duplicate key matching the upsert key should have been removed"); Assert.That(headers.AsEnumerable().ElementAt(0).Key, Is.EqualTo("one")); Assert.That(headers.AsEnumerable().ElementAt(1).Key, Is.EqualTo("two")); Assert.That(headers.AsEnumerable().ElementAt(2).Key, Is.EqualTo("three")); Assert.That(headers["two"], Is.EqualTo("4")); }