Example #1
0
        internal HttpRequest(HTTPClient client)
        {
            ParseHeaders(client);

            Form = client.PostParameters;

            ParseMultiPartItems(client);

            HttpMethod = RequestType = client.Method;

            ParsePath(client);

            ParseRemoteEndPoint(client);

            BuildServerVariables(client);

            BuildParams();

            InputStream = client.InputStream;

            if (InputStream != null)
            {
                InputStream.Position = 0;
            }
        }
Example #2
0
        private void ParseMultiPartItems(HTTPClient client)
        {
            Files = new HttpFileCollection();

            if (client.MultiPartItems == null)
            {
                return;
            }

            foreach (var item in client.MultiPartItems)
            {
                string contentType = null;
                string name        = null;
                string fileName    = null;

                string header;

                if (item.Headers.TryGetValue("Content-Disposition", out header))
                {
                    string[] parts = header.Split(';');

                    for (int i = 0; i < parts.Length; i++)
                    {
                        string part = parts[i].Trim();

                        if (part.StartsWith("name="))
                        {
                            name = ParseContentDispositionItem(part.Substring(5));
                        }
                        else if (part.StartsWith("filename="))
                        {
                            fileName = ParseContentDispositionItem(part.Substring(9));
                        }
                    }
                }

                if (item.Headers.TryGetValue("Content-Type", out header))
                {
                    contentType = header;
                }

                if (name == null)
                {
                    continue;
                }

                if (item.Value != null)
                {
                    Form[name] = item.Value;
                }
                else
                {
                    Files.AddFile(name, new HttpPostedFile((int)item.Stream.Length, contentType, fileName, item.Stream));
                }
            }
        }
        protected HttpRequestParser(HTTPClient client, int contentLength)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }

            Client        = client;
            ContentLength = contentLength;
        }
Example #4
0
 private void RegisterClient(HTTPClient client)
 {
     if (client == null)
     {
         throw new ArgumentNullException("HTTPClient argument provided is null.");
     }
     lock (_syncLock)
     {
         _clients.Add(client, false);
         _clientsChangedEvent.Set();
     }
 }
        public HttpMultiPartRequestParser(HTTPClient client, int contentLength, string boundary)
            : base(client, contentLength)
        {
            if (boundary == null)
            {
                throw new ArgumentNullException("boundary");
            }

            _firstBoundary     = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
            _separatorBoundary = Encoding.ASCII.GetBytes("\r\n--" + boundary);

            Client.MultiPartItems = new List <HttpMultiPartItem>();
        }
Example #6
0
        internal void UnregisterClient(HTTPClient client)
        {
            if (client == null)
            {
                throw new ArgumentNullException("Client argument in HttpServer.UnregisterClient() method is null");
            }

            lock (_syncLock)
            {
                Debug.Assert(_clients.ContainsKey(client));
                _clients.Remove(client);
                _clientsChangedEvent.Set();
            }
        }
Example #7
0
        private void BuildServerVariables(HTTPClient client)
        {
            ServerVariables = new NameValueCollection();

            // Add all headers.

            var allHttp = new StringBuilder();
            var allRaw  = new StringBuilder();

            foreach (var item in client.Headers)
            {
                ServerVariables[item.Key] = item.Value;

                string httpKey = "HTTP_" + (item.Key.Replace('-', '_')).ToUpperInvariant();

                ServerVariables[httpKey] = item.Value;

                allHttp.Append(httpKey);
                allHttp.Append('=');
                allHttp.Append(item.Value);
                allHttp.Append("\r\n");

                allRaw.Append(item.Key);
                allRaw.Append('=');
                allRaw.Append(item.Value);
                allRaw.Append("\r\n");
            }

            ServerVariables["ALL_HTTP"] = allHttp.ToString();
            ServerVariables["ALL_RAW"]  = allRaw.ToString();

            ServerVariables["CONTENT_LENGTH"] = ContentLength.ToString(CultureInfo.InvariantCulture);
            ServerVariables["CONTENT_TYPE"]   = ContentType;

            ServerVariables["LOCAL_ADDR"] = client.Server.EndPoint.Address.ToString();
            ServerVariables["PATH_INFO"]  = Path;

            string[] parts = client.Request.Split(new[] { '?' }, 2);

            ServerVariables["QUERY_STRING"]    = parts.Length == 2 ? parts[1] : "";
            ServerVariables["REMOTE_ADDR"]     = UserHostAddress;
            ServerVariables["REMOTE_HOST"]     = UserHostName;
            ServerVariables["REMOTE_PORT"]     = null;
            ServerVariables["REQUEST_METHOD"]  = RequestType;
            ServerVariables["SCRIPT_NAME"]     = Path;
            ServerVariables["SERVER_NAME"]     = client.Server.ServerUtility.MachineName;
            ServerVariables["SERVER_PORT"]     = client.Server.EndPoint.Port.ToString(CultureInfo.InvariantCulture);
            ServerVariables["SERVER_PROTOCOL"] = client.Protocol;
            ServerVariables["URL"]             = Path;
        }
Example #8
0
 private void AcceptTcpClientCallback(IAsyncResult asyncResult)
 {
     try
     {
         var listener = _listener;
         if (listener == null)
         {
             return;
         }
         var tcpClient = listener.EndAcceptTcpClient(asyncResult);
         if (State == HTTPServerState.STATE.STOPPED)
         {
             tcpClient.Close();
         }
         var client = new HTTPClient(this, tcpClient);
         RegisterClient(client);
         client.BeginRequest();
         ////////listener.BeginAcceptTcpClient(AcceptTcpClientCallback, listener);
         BeginAcceptTcpClient();
     }
     catch (ObjectDisposedException) { }
     catch (Exception ex) { Console.WriteLine(ex.ToString()); }
 }
 public HttpUnknownRequestParser(HTTPClient client, int contentLength)
     : base(client, contentLength)
 {
     Client.InputStream = new MemoryStream();
 }
Example #10
0
 internal HttpContext(HTTPClient client)
 {
     Server   = client.Server.ServerUtility;
     Request  = new HttpRequest(client);
     Response = new HttpResponse(this);
 }
 public HttpUrlEncodedRequestParser(HTTPClient client, int contentLength)
     : base(client, contentLength)
 {
     _stream = new MemoryStream();
 }
Example #12
0
        private void ParseRemoteEndPoint(HTTPClient client)
        {
            var endPoint = (IPEndPoint)client.TcpClient.Client.RemoteEndPoint;

            UserHostName = UserHostAddress = endPoint.Address.ToString();
        }
Example #13
0
        private void ParsePath(HTTPClient client)
        {
            RawUrl = client.Request;

            string[] parts = client.Request.Split(new[] { '?' }, 2);

            Path = parts[0];

            QueryString = new NameValueCollection();
            if (parts.Length == 2)
            {
                HttpUtil.UrlDecodeTo(parts[1], QueryString);
            }

            string host;
            string port;
            string hostHeader;

            if (client.Headers.TryGetValue("Host", out hostHeader))
            {
                parts = hostHeader.Split(new[] { ':' }, 2);

                host = parts[0];

                if (parts.Length == 2)
                {
                    port = parts[1];
                }
                else
                {
                    port = null;
                }
            }
            else
            {
                var endPoint = client.Server.EndPoint;

                host = endPoint.Address.ToString();

                if (endPoint.Port == 80)
                {
                    port = null;
                }
                else
                {
                    port = endPoint.Port.ToString(CultureInfo.InvariantCulture);
                }
            }

            var sb = new StringBuilder();

            sb.Append("http://");
            sb.Append(host);

            if (port != null)
            {
                sb.Append(':');
                sb.Append(port);
            }

            sb.Append(client.Request);

            Url = new Uri(sb.ToString());
        }
Example #14
0
        private void ParseHeaders(HTTPClient client)
        {
            Headers = CreateCollection(client.Headers);

            string header;

            // Parse Accept.

            if (client.Headers.TryGetValue("Accept", out header))
            {
                string[] parts = header.Split(',');

                HttpUtil.TrimAll(parts);

                AcceptTypes = parts;
            }
            else
            {
                AcceptTypes = EmptyStringArray;
            }

            // Parse Content-Type.

            if (client.Headers.TryGetValue("Content-Type", out header))
            {
                string[] parts = header.Split(new[] { ';' }, 2);

                ContentType = parts[0].Trim();

                if (parts.Length == 2)
                {
                    string[] encoding = parts[1].Trim().Split(new[] { '=' }, 2);

                    if (encoding.Length == 2 && String.Equals(encoding[0], "charset", StringComparison.OrdinalIgnoreCase))
                    {
                        ContentEncoding = Encoding.GetEncoding(encoding[1]);
                    }
                }
            }

            // Parse Content-Length.

            if (client.Headers.TryGetValue("Content-Length", out header))
            {
                int contentLength;

                if (int.TryParse(header, out contentLength))
                {
                    ContentLength = contentLength;
                }
            }

            // Parse Referer.

            if (client.Headers.TryGetValue("Referer", out header))
            {
                UrlReferer = new Uri(header);
            }

            // Parse User-Agent.

            if (client.Headers.TryGetValue("User-Agent", out header))
            {
                UserAgent = header;
            }

            // Parse Accept-Language.

            if (client.Headers.TryGetValue("Accept-Language", out header))
            {
                string[] parts = header.Split(',');

                HttpUtil.TrimAll(parts);

                UserLanguages = parts;
            }
            else
            {
                UserLanguages = EmptyStringArray;
            }

            // Parse Cookie.

            Cookies = new HttpCookieCollection();

            if (client.Headers.TryGetValue("Cookie", out header))
            {
                string[] parts = header.Split(';');

                foreach (string part in parts)
                {
                    string[] partParts = part.Split(new[] { '=' }, 2);

                    string name  = partParts[0].Trim();
                    string value = partParts.Length == 1 ? null : partParts[1];

                    Cookies.AddCookie(new HttpCookie(name, value), true);
                }
            }
        }