상속: IDisposable
예제 #1
0
        protected HttpRequestParser(HttpClient client, int contentLength)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            Client = client;
            ContentLength = contentLength;
        }
예제 #2
0
        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>();
        }
예제 #3
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;
        }
예제 #4
0
        private void RegisterClient(HttpClient client)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            lock (_syncLock)
            {
                _clients.Add(client, true);

                _clientsChangedEvent.Set();
            }
        }
예제 #5
0
        private void AcceptTcpClientCallback(IAsyncResult asyncResult)
        {
            if (_state != HttpServerState.Started)
                return;

            try
            {
                var listener = _listener; // Prevent race condition.

                if (listener == null)
                    return;

                var tcpClient = listener.EndAcceptTcpClient(asyncResult);

                var client = new HttpClient(this, tcpClient);

                RegisterClient(client);

                client.BeginRequest();

                BeginAcceptTcpClient();
            }
            catch (ObjectDisposedException)
            {
                // EndAcceptTcpClient will throw a ObjectDisposedException
                // when we're shutting down. This can safely be ignored.
            }
            catch (Exception ex)
            {
                Log.Info("Failed to accept TCP client", ex);
            }
        }
예제 #6
0
        internal void UnregisterClient(HttpClient client)
        {
            if (client == null)
                throw new ArgumentNullException("client");

            lock (_syncLock)
            {
                Debug.Assert(_clients.ContainsKey(client));

                _clients.Remove(client);

                _clientsChangedEvent.Set();
            }
        }
예제 #7
0
 internal HttpContext(HttpClient client)
 {
     Server = client.Server.ServerUtility;
     Request = new HttpRequest(client);
     Response = new HttpResponse(this);
 }
예제 #8
0
 public HttpUrlEncodedRequestParser(HttpClient client, int contentLength)
     : base(client, contentLength)
 {
     _stream = new MemoryStream();
 }
예제 #9
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);
                }
            }
        }
예제 #10
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;
        }
예제 #11
0
        private void ParseRemoteEndPoint(HttpClient client)
        {
            var endPoint = (IPEndPoint)client.TcpClient.Client.RemoteEndPoint;

            UserHostName = UserHostAddress = endPoint.Address.ToString();
        }
예제 #12
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());
        }
예제 #13
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)
                {
                    Log.Info("Received multipart item without name");
                    continue;
                }

                if (item.Value != null)
                {
                    Form[name] = item.Value;
                }
                else
                {
                    Files.AddFile(name, new HttpPostedFile((int)item.Stream.Length, contentType, fileName, item.Stream));
                }
            }
        }
예제 #14
0
 public HttpUnknownRequestParser(HttpClient client, int contentLength)
     : base(client, contentLength)
 {
     Client.InputStream = new MemoryStream();
 }