public void Should_15_handle_http_get() { Profiling.Stamp("Creating HttpProxyClientStream..."); var cli = new Http.HttpProxyClientStream("localhost", _port).Track(this); Profiling.Stamp("Creating HttpProxyClientStream TestConnection..."); TestConnction(cli, Host); }
public void BaseClean() { Console.WriteLine("BaseTestCleanup..."); Profiling.Stamp(TraceCategory.Test, "BaseClean"); Explode(); // You can disable this in general and enable during fine object lifecycle profiling // this is safe, because ZZ_Clean test will wait for all tests at unce // WaitForObjects(); }
public override int Read(byte[] buffer, int offset, int count) { try { Profiling.Stamp("SS Reader Wait"); var b = _readerUnlock.WaitOne(); if (!b) { return(0); } Profiling.Stamp("SS Reader Unlocked"); if (_readTo > _readFrom) { var m = Math.Min(count, _readTo - _readFrom); Array.Copy(_readBuf, _readFrom, buffer, offset, m); _readFrom += m; if (_readFrom < _readTo) { // var rem = _readTo - _readFrom; // Array.Copy(_readBuf, _readFrom, _readBuf, 0, _readTo - _readFrom); // _readFrom = 0; // _readTo = rem; } else { _readFrom = 0; _readTo = 0; _readerUnlock.Reset(); _readerFinished.Set(); } return(m); } return(0); } catch { // reset _readFrom = 0; _readTo = 0; _readerUnlock.Reset(); _readerFinished.Set(); Dispose(); throw; } finally { } }
static void MainClient() { var client = new Socks4ClientStream("127.0.0.1", 1080, "www.google.com", 80); var readBuf = new byte[16 * 1024]; int readBufPos = 0; client.BeginRead(readBuf, 0, readBuf.Length, Read, null); // bool found = false; void Read(IAsyncResult ar) { var c = client.EndRead(ar); Profiling.Stamp(TraceCategory.Test, "Test Read Done = " + c); if (c == 0) { Console.WriteLine("Disconnected"); return; } var line = Encoding.UTF8.GetString(readBuf, readBufPos, c); Console.Write(line); // readBufPos += c; // var line = Encoding.UTF8.GetString(readBuf, 0, c); // Console.WriteLine(">>> " + line); // Profiling.Stamp(TraceCategory.Test, "Test Read..."); client.BeginRead(readBuf, readBufPos, readBuf.Length - readBufPos, Read, null); } string q = null; while (q != "q") { var request = Encoding.ASCII.GetBytes($"GET /ncr HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n"); client.Write(request, 0, request.Length); q = Console.ReadLine(); } }
// static string _etag = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpperInvariant(); private void HandleRequest(int end) { try { Profiling.Stamp(TraceCategory.Performance, "SS Writer - HandleRequest"); var str = _utf.GetString(_request, 0, end); // Console.WriteLine(str); var is1 = str.IndexOf(' '); var is2 = str.IndexOf(' ', is1 + 1); /* * var inmh = "\nIF-NONE-MATCH: "; * var inm = str.ToUpperInvariant().IndexOf(inmh); * if (inm > 0) * { * inm += inmh.Length; * } */ var ie = str.IndexOf('\r'); if (is2 < 0) { is2 = ie; // HTTP 0.9 } /* * * if (inm > 0) * { * var inme = str.IndexOf('\r', inm); * var etag = str.Substring(inm, inme - inm).Trim(); * if (etag.ToUpperInvariant() == _etag.ToUpperInvariant()) * { * /* * header = $@"HTTP/1.0 304 {msg} * Content-Type: {contentType} * Connection: keep-alive * Server: river * ETag: {_etag} * * "; * } * } * ETag: {_etag} */ var url = str.Substring(is1 + 1, is2 - is1 - 1).Trim(); var resp = GetResponse(url.Trim(), out var code, out var msg, out var contentType); var headerStr = $@"HTTP/1.1 {code} {msg} Content-Length: {resp.Length} Content-Type: {contentType} Connection: keep-alive Server: river "; // Console.WriteLine(url); // Console.WriteLine(headerStr); // Console.WriteLine(_utf.GetString(buf, 0, Math.Min(128, c))); // var buf = new byte[16 * 1024]; // ASCII bytes count match to buffer // var buf = new byte[headerStr.Length + resp.Length]; if (_readFrom != 0) { throw new Exception("_readFrom != 0"); } if (_readTo != 0) { throw new Exception("_readTo != 0"); } if (_readBuf.Length < headerStr.Length + resp.Length) { // entire byte blobs are already there anyway - just resize a transfer buffer // there is no streaming done anyway _readBuf = new byte[headerStr.Length + resp.Length]; } // header var hc = _ascii.GetBytes(headerStr, 0, headerStr.Length, _readBuf, _readTo); _readTo += hc; // resp Array.Copy(resp, 0, _readBuf, _readTo, resp.Length); _readTo += resp.Length; _readerUnlock.Set(); // _readerFinished.WaitOne(); /* * int transferred = 0; * do * { * Array.Copy(buf, 0, _readBuf, _readTo, c); * _readTo += c; * transferred += code; * Profiling.Stamp("SS Writer - Handled, unlocking"); * _auto.Set(); * } while (transferred < resp.Length); */ } catch { Dispose(); throw; } }
/// <summary> /// Test current connection to web server /// E.g. you can connect to httpbin.org to do this testing /// </summary> protected static string TestConnction(Stream client, string host, string expectedHost, int port, string url) { var expected = "</html>"; switch (expectedHost) { case "_river": expected = "Server: river"; break; case "www.google.com": url = "/ncr"; expected = "Location: http://www.google.com/"; break; default: break; } var readBuf = new byte[1024 * 1024]; var readBufPos = 0; var readBufSkip = 0; var are = new AutoResetEvent(false); var connected = true; var sw = Stopwatch.StartNew(); Profiling.Stamp(TraceCategory.Test, "Test Read..."); var response = ""; client.BeginRead(readBuf, 0, readBuf.Length, Read, null); // bool found = false; void Read(IAsyncResult ar) { var c = client.EndRead(ar); Profiling.Stamp(TraceCategory.Test, "Test Read Done = " + c); if (c == 0) { connected = false; return; } readBufPos += c; response = Encoding.UTF8.GetString(readBuf, readBufSkip, readBufPos); if (response.Contains(expected)) { // found = true; are.Set(); Console.WriteLine("Wait Done: " + sw.ElapsedMilliseconds); } // var line = Encoding.UTF8.GetString(readBuf, 0, c); // Console.WriteLine(">>> " + line); Profiling.Stamp(TraceCategory.Test, "Test Read..."); client.BeginRead(readBuf, readBufPos, readBuf.Length - readBufPos, Read, null); } var request = Encoding.ASCII.GetBytes($"GET {url} HTTP/1.1\r\nHost: {host}{(port==80?"":":"+port)}\r\nConnection: keep-alive\r\n\r\n"); Profiling.Stamp(TraceCategory.Test, "Test Write..."); client.Write(request, 0, request.Length); Profiling.Stamp(TraceCategory.Test, "Test Write Done"); // WaitFor(() => Encoding.UTF8.GetString(ms.ToArray()).Contains(expected) || !connected); sw = Stopwatch.StartNew(); Assert.IsTrue(are.WaitOneTest(5000)); Assert.IsTrue(connected); readBufSkip = readBufPos; client.Write(request, 0, request.Length); sw = Stopwatch.StartNew(); Assert.IsTrue(are.WaitOneTest(5000)); Assert.IsTrue(connected); return(response); // Encoding.UTF8.GetString(ms.ToArray()); }