/// <summary> /// SAEA WebServer /// </summary> /// <param name="httpContentType">处理对象</param> /// <param name="root">根目录</param> /// <param name="port">监听端口</param> /// <param name="isStaticsCached">是否启用静态缓存</param> /// <param name="isZiped">是压启用内容压缩</param> /// <param name="bufferSize">http处理数据缓存大小</param> /// <param name="count">http连接数上限</param> /// <param name="timeOut">超时</param> /// <param name="isDebug">测试模式</param> public WebHost(Type httpContentType = null, string root = "wwwroot", int port = 39654, bool isStaticsCached = true, bool isZiped = true, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000, bool isDebug = false) { if (httpContentType != null && _httpContentType.GetInterface("SAEA.Http.Model.IHttpContext", true) != null) { _httpContentType = httpContentType; } else { _httpContentType = typeof(HttpContext); } WebConfig = new WebConfig() { Root = root, Port = port, IsStaticsCached = isStaticsCached, IsZiped = isZiped, HandleBufferSize = bufferSize, ClientCounts = count }; HttpUtility = new HttpUtility(WebConfig.Root); _serverSocket = new HttpSocket(port, bufferSize, count, timeOut, isDebug); _serverSocket.OnRequested += _serverSocket_OnRequested; }
static public void TestModifyHeader() { var vpc = "French"; var vh = "your.host.for.tonight"; var vc = "Canadian"; var msg = "Proxy-Connection: " + vpc + "\r\n" + "Host: " + vh + "\r\n" + "Connection: " + vc + "\r\n\r\n"; using (var hs = new HttpSocket( new EchoSocket(false, msg).socket)) { var hr = new HttpHeaders(hs); Assert.AreEqual(1, hr.Connection.Length); Assert.AreEqual(1, hr.ProxyConnection.Length); Assert.AreEqual(vpc.ToLower(), hr.ProxyConnection[0]); hr.ProxyConnection = new string[] { vc }; Assert.AreEqual(1, hr.ProxyConnection.Length); Assert.AreEqual(vc, hr.ProxyConnection[0]); Assert.AreEqual(vh.ToLower(), hr.Host); Assert.AreEqual(1, hr.Connection.Length); Assert.AreEqual(vc.ToLower(), hr.Connection[0]); hr.ProxyConnection = null; Assert.IsNull(hr.ProxyConnection); Assert.AreEqual(vh.ToLower(), hr.Host); Assert.AreEqual(1, hr.Connection.Length); Assert.AreEqual(vc.ToLower(), hr.Connection[0]); } }
static public void TestTunnelData() { // Test binary tunneling byte[] data = { 1, 2, 3 }; using (var hs = new HttpSocket( new EchoSocket(false, data).socket)) { // We tunnel data... to ourselves! hs.TunnelDataTo(hs, 3); uint r = hs.ReadBinary(); Assert.AreEqual(3, r); Assert.AreEqual(1, hs.Buffer[0]); Assert.AreEqual(2, hs.Buffer[1]); Assert.AreEqual(3, hs.Buffer[2]); } // Test the mixing of line-based and raw-based tunneling byte[] data2 = { (byte)'A', 13, 10, 42, 42, (byte)'B', 13, 10 }; using (var hs = new HttpSocket( new EchoSocket(false, data2).socket)) { // Ditto hs.TunnelDataTo(hs, (uint)data2.Length); string msgA = hs.ReadAsciiLine(); Assert.AreEqual("A", msgA); hs.TunnelDataTo(hs, 2); string msgB = hs.ReadAsciiLine(); Assert.AreEqual("B", msgB); } }
public RouteHandle(Route r, HttpServer s, HttpRequest req, HttpSocket so) { Route = r; Server = s; Request = req; Socket = so; }
public void ConnectToNonExistingEndPoint() { using (var socket = new HttpSocket()) { var endPoint = new IPEndPoint(1, 1); Assert.Throws <TimeoutException>(() => socket.Connect(endPoint, 0)); } }
private void SocketInit() { _socket = new HttpSocket(); _message.Socket = _socket; _connectTimestamp = 0; _isConnected = false; _isConnecting = false; _isFaulted = false; EndPoint = null; }
/// <summary> /// Parse a HTTP header after sending it through the TCP channel /// </summary> /// <returns>A parsed version of <param>rq_line</param></returns> /// <remarks> /// This method must be called between Setup() and TearDown(). /// </remarks> static public HttpRequestLine ParseHttpRequestLine(string rq_line) { byte[] to_send = System.Text.Encoding.ASCII.GetBytes(rq_line); var es = new EchoSocket(bUseIPv6, to_send); using (var hs = new HttpSocket(es.socket)) { es.socket.Shutdown(SocketShutdown.Send); return(new HttpRequestLine(hs)); } }
/// <summary> /// Parse a HTTP header after sending it through the TCP channel /// </summary> /// <returns>A parsed version of <param>header</param></returns> /// <remarks> /// This method must be called between Setup() and TearDown(). /// </remarks> static public HttpHeaders ParseHttpHeaders(string header) { byte[] to_send = System.Text.Encoding.ASCII.GetBytes(header); var es = new EchoSocket(bUseIPv6, to_send); using (var hs = new HttpSocket(es.socket)) { es.socket.Shutdown(SocketShutdown.Send); return(new TrotiNet.HttpHeaders(hs)); } }
/// <summary> /// Send a message through the TCP channel, and expect it to /// return unchanged /// </summary> /// <remarks> /// Useful for testing I/O levels 1 and 2. /// This method must be called between Setup() and TearDown(). /// </remarks> static public void DoMsgRoundTrip(string msg_send, string msg_expect) { byte[] to_send = System.Text.Encoding.ASCII.GetBytes(msg_send); using (var hs = new HttpSocket( new EchoSocket(bUseIPv6, to_send).socket)) { var msg_receive = hs.ReadAsciiLine(); Assert.AreEqual(msg_expect, msg_receive); hs.CloseSocket(); } }
internal bool ReportBug(string msg, string email) { HttpSocket s = new HttpSocket(); // send data string resp = s.Post(URL, string.Format("message={0}&email={1}&identifier={2}&hide=1", HttpUtility.UrlEncode(msg), HttpUtility.UrlEncode(email), IDENTIFIER)); if (resp.Contains("Input successful.")) { return(true); } return(false); }
internal static void Submit() { StringBuilder sb = new StringBuilder(); foreach (MappedMob mb in mMobs) { sb.AppendLine(mb.ToString()); } // send data HttpSocket s = new HttpSocket(); s.Post(SUBMIT_URL, string.Format("mobs={0}", HttpUtility.UrlEncode(sb.ToString()))); mSubmitted.AddRange(mMobs); mMobs.Clear(); }
static public void TestHttpResponseLine() { // Test the mixing of structured and unstructured // line-based reads var msg = "Captain Copyright"; var http_msg = "HTTP/1.0 200 OK\r\n\r\n" + msg + "\r\n"; using (var hs = new HttpSocket( new EchoSocket(false, http_msg).socket)) { var hsl = new HttpStatusLine(hs); Assert.AreEqual(200, hsl.StatusCode); /*var hh = */ new HttpHeaders(hs); var line = hs.ReadAsciiLine(); Assert.AreEqual(msg, line); } }
public static void TestHttpResponseLine() { // Test the mixing of structured and unstructured // line-based reads var msg = "Captain Copyright"; var http_msg = "HTTP/1.0 200 OK\r\n\r\n" + msg + "\r\n"; using (var hs = new HttpSocket( new EchoSocket(false, http_msg).socket)) { var hsl = new HttpStatusLine(hs); Assert.AreEqual(200, hsl.StatusCode); /*var hh = */new HttpHeaders(hs); var line = hs.ReadAsciiLine(); Assert.AreEqual(msg, line); } }
/// <summary> /// SAEA WebServer /// </summary> /// <param name="invoker">处理对象</param> /// <param name="root">根目录</param> /// <param name="port">监听端口</param> /// <param name="isStaticsCached">是否启用静态缓存</param> /// <param name="isZiped">是压启用内容压缩</param> /// <param name="bufferSize">http处理数据缓存大小</param> /// <param name="count">http连接数上限</param> /// <param name="timeOut">超时</param> /// <param name="isDebug">测试模式</param> public WebHost(IInvoker invoker, string root = "wwwroot", int port = 39654, bool isStaticsCached = true, bool isZiped = true, int bufferSize = 1024 * 10, int count = 10000, int timeOut = 120 * 1000, bool isDebug = false) { Invoker = invoker; WebConfig = new WebConfig() { Root = root, Port = port, IsStaticsCached = isStaticsCached, IsZiped = isZiped, HandleBufferSize = bufferSize, ClientCounts = count }; _serverSocket = new HttpSocket(port, bufferSize, count, timeOut, isDebug); _serverSocket.OnRequested += _serverSocket_OnRequested; }
static public void TestHttpResponseBinary() { // Test the mixing of line-based and raw-based reads var msg = "123"; var http_msg = "HTTP/1.0 200 OK\r\n\r\n" + msg; using (var hs = new HttpSocket( new EchoSocket(false, http_msg).socket)) { var hsl = new HttpStatusLine(hs); Assert.AreEqual(200, hsl.StatusCode); /*var hh = */ new HttpHeaders(hs); uint r = hs.ReadBinary(); Assert.AreEqual(r, 3); Assert.AreEqual('1', hs.Buffer[0]); Assert.AreEqual('2', hs.Buffer[1]); Assert.AreEqual('3', hs.Buffer[2]); } }
public static void TestHttpResponseBinary() { // Test the mixing of line-based and raw-based reads var msg = "123"; var http_msg = "HTTP/1.0 200 OK\r\n\r\n" + msg; using (var hs = new HttpSocket( new EchoSocket(false, http_msg).socket)) { var hsl = new HttpStatusLine(hs); Assert.AreEqual(200, hsl.StatusCode); /*var hh = */new HttpHeaders(hs); uint r = hs.ReadBinary(); Assert.AreEqual(r, 3); Assert.AreEqual('1', hs.Buffer[0]); Assert.AreEqual('2', hs.Buffer[1]); Assert.AreEqual('3', hs.Buffer[2]); } }
static void Main(string[] args) { // Making a request to a nonexistent domain. HttpSocket.MakeRequest(new Uri("http://www.tanzaniatouristboard.com/"), callbackState => { if (callbackState.RequestTimedOut) { Console.WriteLine("Timed out!"); } else if (callbackState.Exception != null) { throw callbackState.Exception; } else { Console.WriteLine(GetResponseText(callbackState.ResponseStream)); } }); Thread.Sleep(100000); }
public static void TestPortAlreadyAssigned() { int port = 20000; bool bUseIPv6 = false; TcpServer s1 = new TcpServer(port, bUseIPv6); TcpServer s2 = new TcpServer(port, bUseIPv6); s1.Start(OnConnectionTalk); s1.InitListenFinished.WaitOne(); if (s1.InitListenException != null) { throw s1.InitListenException; } Assert.IsTrue(s1.IsListening); // Starting a second server on the same port should fail Console.WriteLine("Note: SocketException expected"); s2.Start(OnConnectionFail); s2.InitListenFinished.WaitOne(); Assert.IsNotNull(s2.InitListenException); var e = s2.InitListenException as SocketException; Assert.IsNotNull(e); Assert.AreEqual(10048, e.ErrorCode); // 10048 means "Only one usage of each socket address ..." Assert.IsFalse(s2.IsListening); // Check that the first server still works using (var hs = new HttpSocket( new EchoSocket(port, bUseIPv6).socket)) { string s = hs.ReadAsciiLine(); Assert.AreEqual(ss, s); } Assert.IsTrue(s1.IsListening); s1.Stop(); // stop the server in case we want to re-run the test Assert.IsFalse(s1.IsListening); // while we are here... }
public TransparentProxy CreateProxy(HttpSocket clientSocket) { var proxy = new TransparentProxy(clientSocket, this); IPAddress ip = GetIP(); int tries = 0; while (ip == null && tries < 5) { Thread.Sleep(500); ++tries; ip = GetIP(); } if (ip != null) { proxy.InterfaceToBind = ip; return(proxy); } return(null); }
public void Init() { encoder = new Encoder(); decoder = new Decoder(); //parser = new Parser(); //handlers = new Dictionary<string, List<Action<SocketIOEvent>>>(); // //sync,改用同步方式....... hs = new HttpSocket(false); hs.OnMessage += OnMessage; hs.OnError += OnError; timeCacl = 0.0f; queryTicks = 1.0f; #if SOCKET_IO_DEBUG if (debugMethod == null) { debugMethod = Debug.Log; } ; #endif }
/// <summary> /// Called when [next]. /// </summary> /// <param name="value">The value.</param> public void OnNext(EmbedlyRequest value) { Interlocked.Increment(ref _count); Log.DebugFormat("Http request for {0} urls: {1}", value.UrlRequests.Count, value.EmbedlyUrl); HttpSocket.GetAsync(value.EmbedlyUrl.AbsoluteUri, _timeout, callbackState => { var state = (EmbedlyRequest)callbackState.State; if (callbackState.Exception == null) { Response[] responses = Deserialize(callbackState.ResponseStream); for (int i = 0; i < state.UrlRequests.Count; i++) { Log.DebugFormat("Response for url: {0} was {1} from {2}", state.UrlRequests[i].Url, responses[i].Type, state.UrlRequests[i].Provider.Name); _cache.Put(state.UrlRequests[i], responses[i]); _results.OnNext(new Result(state.UrlRequests[i], responses[i])); } } else { foreach (UrlRequest urlRequest in state.UrlRequests) { _results.OnNext(new Result(urlRequest, callbackState.Exception)); } } // signal when we're done so consumers can finish if (Interlocked.Decrement(ref _count) == 0 && _complete) { _results.OnCompleted(); } }, value); }
public void Test_Full_Response() { SocketWatcher watcher = new SocketWatcher(); Address a = new Address("127.0.0.1", 7002); a.Resolve(); ServerListener server = new ServerListener(); AsyncSocket server_sock = watcher.CreateListenSocket(server, a); server_sock.RequestAccept(); ResponseListener resp = new ResponseListener(); HttpSocket sock = new HttpSocket(resp); Uri u = new Uri("http://localhost:7002/"); byte[] buf = ENC.GetBytes("11111"); HttpSocket s = (HttpSocket)sock; s.Execute("GET", u, buf, 0, buf.Length, "text/plain"); resp.Event.WaitOne(); Assert.AreEqual("1234567890", resp.Last); resp.Last = null; buf = ENC.GetBytes("22222"); s.Execute("GET", u, buf, 0, buf.Length, "text/plain"); resp.Event.WaitOne(); Assert.AreEqual("1234567890", resp.Last); resp.Last = null; buf = ENC.GetBytes("33333"); s.Execute("GET", u, buf, 0, buf.Length, "text/plain"); resp.Event.WaitOne(); Assert.AreEqual("12345678901234567890", resp.Last); }
public static void TestPortAlreadyAssigned() { int port = 20000; bool bUseIPv6 = false; TcpServer s1 = new TcpServer(port, bUseIPv6); TcpServer s2 = new TcpServer(port, bUseIPv6); s1.Start(OnConnectionTalk); s1.InitListenFinished.WaitOne(); if (s1.InitListenException != null) throw s1.InitListenException; Assert.IsTrue(s1.IsListening); // Starting a second server on the same port should fail Console.WriteLine("Note: SocketException expected"); s2.Start(OnConnectionFail); s2.InitListenFinished.WaitOne(); Assert.IsNotNull(s2.InitListenException); var e = s2.InitListenException as SocketException; Assert.IsNotNull(e); Assert.AreEqual(10048, e.ErrorCode); // 10048 means "Only one usage of each socket address ..." Assert.IsFalse(s2.IsListening); // Check that the first server still works using (var hs = new HttpSocket( new EchoSocket(port, bUseIPv6).socket)) { string s = hs.ReadAsciiLine(); Assert.AreEqual(ss, s); } Assert.IsTrue(s1.IsListening); s1.Stop(); // stop the server in case we want to re-run the test Assert.IsFalse(s1.IsListening); // while we are here... }
/// <summary> /// Static constructor with <c>PrintEchoPrefix = true</c> /// </summary> public static AbstractProxyLogic CreateEchoProxy(HttpSocket socketBP) { return new ProxyDummyEcho(socketBP, true); }
static new public GreenProxy CreateProxy(HttpSocket clientSocket) { return(new GreenProxy(clientSocket)); }
public ProxyHandler(HttpSocket clientSocket) : base(clientSocket) { }
public static void TestModifyHeader() { var vpc = "French"; var vh = "your.host.for.tonight"; var vc = "Canadian"; var msg = "Proxy-Connection: " + vpc + "\r\n" + "Host: " + vh + "\r\n" + "Connection: " + vc + "\r\n\r\n"; using (var hs = new HttpSocket( new EchoSocket(false, msg).socket)) { var hr = new HttpHeaders(hs); Assert.AreEqual(1, hr.Connection.Length); Assert.AreEqual(1, hr.ProxyConnection.Length); Assert.AreEqual(vpc.ToLower(), hr.ProxyConnection[0]); hr.ProxyConnection = new string[] { vc }; Assert.AreEqual(1, hr.ProxyConnection.Length); Assert.AreEqual(vc, hr.ProxyConnection[0]); Assert.AreEqual(vh.ToLower(), hr.Host); Assert.AreEqual(1, hr.Connection.Length); Assert.AreEqual(vc.ToLower(), hr.Connection[0]); hr.ProxyConnection = null; Assert.IsNull(hr.ProxyConnection); Assert.AreEqual(vh.ToLower(), hr.Host); Assert.AreEqual(1, hr.Connection.Length); Assert.AreEqual(vc.ToLower(), hr.Connection[0]); } }
/// <summary> /// Static constructor with <c>PrintEchoPrefix = false</c> /// </summary> static public AbstractProxyLogic CreateMirrorProxy(HttpSocket socketBP) { return(new ProxyDummyEcho(socketBP, false)); }
/// <summary> /// Instantiate a dummy proxy that echoes what it reads on the /// socket back to it /// </summary> /// <param name="socketBP">Client socket</param> /// <param name="PrintEchoPrefix">If true, the proxy will add an /// "Echo" prefix for each message</param> public ProxyDummyEcho(HttpSocket socketBP, bool PrintEchoPrefix) : base(socketBP) { bPrintEchoPrefix = PrintEchoPrefix; }
static AbstractProxyLogic OnConnectionFail(HttpSocket hh) { // Should never be called Assert.Fail(); return null; }
public static ProxyHandler OnConnection(HttpSocket clientSocket) { return(new ProxyHandler(clientSocket)); }
public static new TransparentProxy CreateProxy(HttpSocket clientSocket) { return new TransparentProxy(clientSocket); }
/// <summary> /// Static constructor with <c>PrintEchoPrefix = false</c> /// </summary> public static AbstractProxyLogic CreateMirrorProxy(HttpSocket socketBP) { return new ProxyDummyEcho(socketBP, false); }
public RewritingProxy(HttpSocket clientSocket) : base(clientSocket) { }
public static void TestHttpSocket() { using var socket = new HttpSocket("124.228.42.248", 80); Console.WriteLine(socket.GetHttp("/")); }
public RedirectingProxy(HttpSocket clientSocket) : base(clientSocket) { }
public static new RedirectingProxy CreateProxy(HttpSocket clientSocket) { return new RedirectingProxy(clientSocket); }
static AbstractProxyLogic OnConnectionTalk(HttpSocket hh) { hh.WriteAsciiLine(ss); return null; }
/// <summary> /// Static constructor with <c>PrintEchoPrefix = true</c> /// </summary> static public AbstractProxyLogic CreateEchoProxy(HttpSocket socketBP) { return(new ProxyDummyEcho(socketBP, true)); }
static new public RedirectingProxy CreateProxy(HttpSocket clientSocket) { return(new RedirectingProxy(clientSocket)); }
public TransparentProxy(HttpSocket clientSocket) : base(clientSocket) { }
public static void TestTunnelData() { // Test binary tunneling byte[] data = { 1, 2, 3 }; using (var hs = new HttpSocket( new EchoSocket(false, data).socket)) { // We tunnel data... to ourselves! hs.TunnelDataTo(hs, 3); uint r = hs.ReadBinary(); Assert.AreEqual(3, r); Assert.AreEqual(1, hs.Buffer[0]); Assert.AreEqual(2, hs.Buffer[1]); Assert.AreEqual(3, hs.Buffer[2]); } // Test the mixing of line-based and raw-based tunneling byte[] data2 = { (byte)'A', 13, 10, 42, 42, (byte)'B', 13, 10 }; using (var hs = new HttpSocket( new EchoSocket(false, data2).socket)) { // Ditto hs.TunnelDataTo(hs, (uint)data2.Length); string msgA = hs.ReadAsciiLine(); Assert.AreEqual("A", msgA); hs.TunnelDataTo(hs, 2); string msgB = hs.ReadAsciiLine(); Assert.AreEqual("B", msgB); } }