Example #1
0
        private static void ClientRequest(HttpClientHandler client, HttpRequest req)
        {
            HttpResponse resp = null;

            //Console.WriteLine("--- REQUEST ---");
            //Console.WriteLine(req.ToString());
            if (req["Request"] == "GET")
            {
                string uri = req["URI"];
                if (uri == "/")
                {
                    resp = new HttpResponse("HTTP/1.1", "200", "OK");
                    resp.AddProperty("Date", DateTime.Now.ToShortDateString());
                    resp.AddProperty("Server", "WunderVision");
                    resp.AddProperty("Content-Type", "text/html;charset=UTF-8");
                    resp.SetData(Site);
                }
                else
                {
                    if (req.ContainsKey("Sec-WebSocket-Key"))
                    {
                        resp = new HttpResponse("HTTP/1.1", "101", "Switching Protocols");
                        resp.AddProperty("Upgrade", "websocket");
                        resp.AddProperty("Connection", "Upgrade");
                        //Console.WriteLine(req["Sec-WebSocket-Key"]);
                        resp.AddProperty("Sec-WebSocket-Accept", HttpTools.ComputeWebSocketKeyHash(req["Sec-WebSocket-Key"]));
                        client.UpgradeToWebsocket();
                    }
                    else
                    {
                        Uri requestedfile = new Uri(Root + uri);
                        //Console.WriteLine(requestedfile.LocalPath);
                        if (File.Exists(requestedfile.LocalPath))
                        {
                            string mime = HttpTools.GetFileMimeType(uri);
                            //Console.WriteLine(mime);
                            byte[] data;
                            if (HttpTools.IsFileBinary(uri))
                            {
                                data = File.ReadAllBytes(requestedfile.LocalPath);
                            }
                            else
                            {
                                data = System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(requestedfile.LocalPath));
                            }
                            resp = new HttpResponse("HTTP/1.1", "200", "OK");
                            resp.AddProperty("Date", DateTime.Now.ToShortDateString());
                            resp.AddProperty("Server", "WunderVision");
                            resp.AddProperty("Content-Type", mime);
                            resp.SetData(data);
                        }
                    }
                }
            }
            if (resp == null)
            {
                resp = new HttpResponse("HTTP/1.1", "404", "NOT FOUND");
                resp.AddProperty("Date", DateTime.Now.ToShortDateString());
                resp.AddProperty("Server", "WunderVision");
                resp.AddProperty("Content-Type", "text/html;charset=UTF-8");
                resp.SetData("SORRY CAN'T DO WHAT YOU WANT ME TO");
            }
            //Console.WriteLine("--- RESPONSE ---");
            //Console.WriteLine(resp.ToString());
            client.Send(resp);
        }