Ejemplo n.º 1
0
        void ServeHTTP(TcpClient tc)
        {
            var stream = tc.GetStream();
            var line   = ReadLine(stream);

            if (line == null)
            {
                return;
            }
            var top = line.Trim().Split(' ');

            if (top.Length != 3)
            {
                return;
            }

            var req = new HttpRequest()
            {
                HttpMethod = top [0], RawUrl = top [1], protocol = top [2]
            };

            if (req.RawUrl.StartsWith("http://"))
            {
                req.Url = new Uri(req.RawUrl);
            }
            else
            {
                req.Url = new Uri("http://" + System.Net.IPAddress.Any + ":" + port + req.RawUrl);
            }

            while (true)
            {
                var headerline = ReadLine(stream);
                if (headerline.Length == 0)
                {
                    break;
                }
                req.Headers.AddHeaderLine(headerline);
            }


            req.InputStream = stream;
            string contentLength = req.Headers.Get("Content-Length");

            if (contentLength != null)
            {
                var count  = int.Parse(contentLength);
                var bytes  = new byte[count];
                var offset = 0;
                while (count > 0)
                {
                    offset = stream.Read(bytes, offset, count);
                    count -= offset;
                }
                req.body = System.Text.Encoding.UTF8.GetString(bytes);
            }

            string[] contentTypes = req.Headers.GetValues("Content-Type");
            if (contentTypes != null && Array.IndexOf(contentTypes, "multipart/form-data") >= 0)
            {
                req.formData = MultiPartEntry.Parse(req);
            }

            if (processRequestsInMainThread)
            {
                lock (mainThreadRequests) {
                    mainThreadRequests.Enqueue(req);
                }
            }
            else
            {
                var response = new HttpResponse();
                ProcessRequest(req, response);
            }
        }
Ejemplo n.º 2
0
        void ServeHTTP(Socket sock)
        {
            var stream = new HttpStream(sock);
            var line   = ReadLine(stream);

            if (line == null)
            {
                return;
            }
            var top = line.Trim().Split(' ');

            if (top.Length != 3)
            {
                return;
            }

            var req = new HttpRequest(stream)
            {
                HttpMethod = top [0], RawUrl = top [1], protocol = top [2]
            };

            // XXX: Should use a regex to check for the scheme, could also be e.g. https://
            if (req.RawUrl.StartsWith("http://"))
            {
                // .NET API specifies that the RawURL starts with the path
                req.RawUrl = req.RawUrl.Remove(0, "http://".Length);
                req.Url    = new Uri(req.RawUrl);
            }
            else
            {
                req.Url = new Uri("http://" + System.Net.IPAddress.Any + ":" + port + req.RawUrl);
            }

            while (true)
            {
                var headerline = ReadLine(stream);
                if (headerline.Length == 0)
                {
                    break;
                }
                req.Headers.AddHeaderLine(headerline);
            }

            string contentLength = req.Headers.Get("Content-Length");

            if (contentLength != null)
            {
                var count  = int.Parse(contentLength);
                var bytes  = new byte[count];
                var offset = 0;
                int len    = -1;
                do
                {
                    len     = stream.Read(bytes, offset, count);
                    offset += len;
                    count  -= len;
                } while (len > 0 && count > 0);
                req.body = System.Text.Encoding.UTF8.GetString(bytes);
            }

            string[] contentTypes = req.Headers.GetValues("Content-Type");
            if (contentTypes != null && Array.IndexOf(contentTypes, "multipart/form-data") >= 0)
            {
                req.formData = MultiPartEntry.Parse(req);
            }

            if (processRequestsInMainThread)
            {
                lock (mainThreadRequests) {
                    mainThreadRequests.Enqueue(req);
                }
            }
            else
            {
                ProcessRequest(req);
            }
        }