Exemple #1
0
        public static void HttpServerConnectionTest(string[] args)
        {
            HttpListener server = new HttpListener(new IPEndPoint(IPAddress.Any, 420));

            server.OnConnection += (connection) => Console.WriteLine("connection");
            server.OnRequest     = (req, res) =>
            {
                Console.WriteLine("request");
                if (req.Path == "/favicon.ico")
                {
                    res.End(404, "Not Found");
                }
                else
                {
                    Stopwatch sw = Stopwatch.StartNew();
                    res.OnFinish            += () => Console.WriteLine("{0}ms for onfinish fire", sw.Elapsed.TotalMilliseconds.ToString("F2"));
                    res["Transfer-Encoding"] = "chunked";
                    res["Content-Type"]      = "text/plain";
                    res["Connection"]        = "close";
                    res.SendHead(200, "OK");
                    for (int i = 0; i < 1000000; i++)
                    {
                        res.Write(ASCII.GetBytes(i.ToString() + " "));
                    }
                    res.End();
                    Console.WriteLine("{0}ms for req end", sw.Elapsed.TotalMilliseconds.ToString("F2"));
                    sw.Stop();
                }
            };
            server.Start();
            Console.WriteLine("started");
            Console.ReadKey();
            server.Stop();
            Console.WriteLine("stopped");
            Console.ReadKey();
        }
Exemple #2
0
        public static void HttpClientConnectionTest(string[] args)
        {
            IPEndPoint       clientEndPoint = new IPEndPoint(IPAddress.Loopback, 420);
            IPEndPoint       serverEndPoint = new IPEndPoint(IPAddress.Any, 420);
            HttpListener     listener       = new HttpListener(serverEndPoint);
            ServerConnection server         = null;

            listener.OnRequest = (sreq, res) =>
            {
                Console.WriteLine("Server request");
                server = sreq.Connection;
                if (sreq.Path != "/echo")
                {
                    res["Content-Length"] = "0";
                    res.End(404, "Not Found"); return;
                }
                switch (sreq.Method)
                {
                case "GET":
                    Console.WriteLine("Server GET request");
                    res["Content-Type"]      = "text/plain";
                    res["Transfer-Encoding"] = "chunked";
                    res.SendHead(200, "OK");
                    res.Write(UTF8.GetBytes("GET on /echo\r\nMake a POST request to echo its body"));
                    res.End();
                    Console.WriteLine("Server response finish");
                    break;

                case "POST":
                    Console.WriteLine("Server POST request");
                    sreq.OnFinish += () =>
                    {
                        Console.WriteLine("Server POST request finish");
                        if (sreq.BufferedReadable == 0)
                        {
                            res.End(400, "Bad Request"); return;
                        }
                        res["Content-Type"]      = "text/plain";
                        res["Transfer-Encoding"] = "chunked";
                        res.SendHead(200, "OK");
                        res.Write(UTF8.GetBytes("/ path on POST!\r\n"));
                        res.Write(UTF8.GetBytes("Body is as follows:\r\n\r\n"));
                        byte[] data = sreq.Read();
                        Console.WriteLine(UTF8.GetString(data).Replace("\r", "\\r").Replace("\n", "\\n"));
                        res.Write(data);
                        res.End();
                        Console.WriteLine("Server response finish");
                    };
                    break;

                default: res.End(400, "Bad Request"); break;
                }
            };
            listener.Start();
            ClientConnection client = null;

            client = new ClientConnection(clientEndPoint, () =>
            {
                OutgoingRequest creq = client.Enqueue("HTTP/1.1", "POST", "/echo");
                creq.OnResponse     += (res) =>
                {
                    Console.WriteLine("Client response {0} {1}", res.StatusCode, res.StatusDescription);
                    res.OnFinish += () =>
                    {
                        Console.WriteLine("Client response finish\r\n{0}", UTF8.GetString(res.Read()));
                        client.End();
                        client.Base.End();
                    };
                };
                creq["Transfer-Encoding"] = "chunked";
                creq.SendHead();
                creq.Write(UTF8.GetBytes("I am a body"));
                creq.End();
                Console.WriteLine("Client request finish");
            });
            Console.ReadKey();
            listener.Stop();
            Console.ReadKey();
            //client.Terminate();
        }