コード例 #1
0
        internal void FinishInitialization()
        {
            string host = UserHostName;

            if (version > HttpVersion.Version10 && (host == null || host.Length == 0))
            {
                context.ErrorMessage = "Invalid host name";
                return;
            }

            string path;
            Uri    raw_uri = null;

            if (Uri.MaybeUri(raw_url) && Uri.TryCreate(raw_url, UriKind.Absolute, out raw_uri))
            {
                path = raw_uri.PathAndQuery;
            }
            else
            {
                path = raw_url;
            }

            if ((host == null || host.Length == 0))
            {
                host = UserHostAddress;
            }

            if (raw_uri != null)
            {
                host = raw_uri.Host;
            }

            int colon = host.IndexOf(':');

            if (colon >= 0)
            {
                host = host.Substring(0, colon);
            }

            string base_uri = String.Format("{0}://{1}:{2}",
                                            (IsSecureConnection) ? "https" : "http",
                                            host, LocalEndPoint.Port);

            if (!Uri.TryCreate(base_uri + path, UriKind.Absolute, out url))
            {
                context.ErrorMessage = "Invalid url: " + base_uri + path;
                return;
            }

            CreateQueryString(url.Query);

            if (version >= HttpVersion.Version11)
            {
                string t_encoding = Headers ["Transfer-Encoding"];
                is_chunked = (t_encoding != null && String.Compare(t_encoding, "chunked", StringComparison.OrdinalIgnoreCase) == 0);
                // 'identity' is not valid!
                if (t_encoding != null && !is_chunked)
                {
                    context.Connection.SendError(null, 501);
                    return;
                }
            }

            if (!is_chunked && !cl_set)
            {
                if (String.Compare(method, "POST", StringComparison.OrdinalIgnoreCase) == 0 ||
                    String.Compare(method, "PUT", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    context.Connection.SendError(null, 411);
                    return;
                }
            }

            if (String.Compare(Headers ["Expect"], "100-continue", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ResponseStream output = context.Connection.GetResponseStream();
                output.InternalWrite(_100continue, 0, _100continue.Length);
            }
        }
コード例 #2
0
        internal void FinishInitialization()
        {
            string text = this.UserHostName;

            if (this.version > HttpVersion.Version10 && (text == null || text.Length == 0))
            {
                this.context.ErrorMessage = "Invalid host name";
                return;
            }
            System.Uri uri;
            string     pathAndQuery;

            if (System.Uri.MaybeUri(this.raw_url) && System.Uri.TryCreate(this.raw_url, System.UriKind.Absolute, out uri))
            {
                pathAndQuery = uri.PathAndQuery;
            }
            else
            {
                pathAndQuery = this.raw_url;
            }
            if (text == null || text.Length == 0)
            {
                text = this.UserHostAddress;
            }
            if (uri != null)
            {
                text = uri.Host;
            }
            int num = text.IndexOf(':');

            if (num >= 0)
            {
                text = text.Substring(0, num);
            }
            string text2 = string.Format("{0}://{1}:{2}", (!this.IsSecureConnection) ? "http" : "https", text, this.LocalEndPoint.Port);

            if (!System.Uri.TryCreate(text2 + pathAndQuery, System.UriKind.Absolute, out this.url))
            {
                this.context.ErrorMessage = "Invalid url: " + text2 + pathAndQuery;
                return;
            }
            this.CreateQueryString(this.url.Query);
            string text3 = null;

            if (this.version >= HttpVersion.Version11)
            {
                text3 = this.Headers["Transfer-Encoding"];
                if (text3 != null && text3 != "chunked")
                {
                    this.context.Connection.SendError(null, 501);
                    return;
                }
            }
            this.is_chunked = (text3 == "chunked");
            foreach (string strB in HttpListenerRequest.no_body_methods)
            {
                if (string.Compare(this.method, strB, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    return;
                }
            }
            if (!this.is_chunked && !this.cl_set)
            {
                this.context.Connection.SendError(null, 411);
                return;
            }
            if (this.is_chunked || this.content_length > 0L)
            {
                this.input_stream = this.context.Connection.GetRequestStream(this.is_chunked, this.content_length);
            }
            if (this.Headers["Expect"] == "100-continue")
            {
                ResponseStream responseStream = this.context.Connection.GetResponseStream();
                responseStream.InternalWrite(HttpListenerRequest._100continue, 0, HttpListenerRequest._100continue.Length);
            }
        }
コード例 #3
0
        internal void FinishInitialization()
        {
            string host = UserHostName;

            if (version > HttpVersion.Version10 && (host == null || host.Length == 0))
            {
                context.ErrorMessage = "Invalid host name";
                return;
            }

            if (host == null || host.Length == 0)
            {
                host = UserHostAddress;
            }

            int colon = host.IndexOf(':');

            if (colon >= 0)
            {
                host = host.Substring(0, colon);
            }

            string base_uri = String.Format("{0}://{1}:{2}",
                                            (IsSecureConnection) ? "https" : "http",
                                            host,
                                            LocalEndPoint.Port);

            try {
                url = new Uri(base_uri + raw_url);
            } catch {
                context.ErrorMessage = "Invalid url";
                return;
            }

            CreateQueryString(url.Query);

            string t_encoding = null;

            if (version >= HttpVersion.Version11)
            {
                t_encoding = Headers ["Transfer-Encoding"];
                // 'identity' is not valid!
                if (t_encoding != null && t_encoding != "chunked")
                {
                    context.Connection.SendError(null, 501);
                    return;
                }
            }

            is_chunked = (t_encoding == "chunked");

            foreach (string m in no_body_methods)
            {
                if (string.Compare(method, m, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    return;
                }
            }

            if (!is_chunked && !cl_set)
            {
                context.Connection.SendError(null, 411);
                return;
            }

            if (is_chunked || content_length > 0)
            {
                input_stream = context.Connection.GetRequestStream(is_chunked, content_length);
            }

            if (Headers ["Expect"] == "100-continue")
            {
                ResponseStream output = context.Connection.GetResponseStream();
                output.InternalWrite(_100continue, 0, _100continue.Length);
            }
        }
コード例 #4
0
        internal void SendHeaders(bool closing)
        {
            //TODO: When do we send KeepAlive?
            MemoryStream ms       = new MemoryStream();
            Encoding     encoding = content_encoding;

            if (encoding == null)
            {
                encoding = Encoding.Default;
            }

            if (content_type != null)
            {
                if (content_encoding != null && content_type.IndexOf("charset=") == -1)
                {
                    string enc_name = content_encoding.WebName;
                    headers.SetInternal("Content-Type", content_type + "; charset=" + enc_name);
                }
                else
                {
                    headers.SetInternal("Content-Type", content_type);
                }
            }

            if (headers ["Server"] == null)
            {
                headers.SetInternal("Server", "Mono-HTTPAPI/1.0");
            }

            CultureInfo inv = CultureInfo.InvariantCulture;

            if (headers ["Date"] == null)
            {
                headers.SetInternal("Date", DateTime.UtcNow.ToString("r", inv));
            }

            if (!chunked)
            {
                if (!cl_set && closing)
                {
                    cl_set         = true;
                    content_length = 0;
                }

                if (cl_set)
                {
                    headers.SetInternal("Content-Length", content_length.ToString(inv));
                }
            }

            Version v = context.Request.ProtocolVersion;

            if (!cl_set && !chunked && v >= HttpVersion.Version11)
            {
                chunked = true;
            }

            /* Apache forces closing the connection for these status codes:
             *	HttpStatusCode.BadRequest       400
             *	HttpStatusCode.RequestTimeout       408
             *	HttpStatusCode.LengthRequired       411
             *	HttpStatusCode.RequestEntityTooLarge    413
             *	HttpStatusCode.RequestUriTooLong    414
             *	HttpStatusCode.InternalServerError  500
             *	HttpStatusCode.ServiceUnavailable   503
             */
            bool conn_close = (status_code == 400 || status_code == 408 || status_code == 411 ||
                               status_code == 413 || status_code == 414 || status_code == 500 ||
                               status_code == 503);

            if (conn_close == false)
            {
                conn_close = (context.Request.Headers ["connection"] == "close");
            }

            // They sent both KeepAlive: true and Connection: close!?
            if (!chunked || conn_close)
            {
                headers.SetInternal("Connection", "close");
            }

            if (chunked)
            {
                headers.SetInternal("Transfer-Encoding", "chunked");
            }

            int chunked_uses = context.Connection.ChunkedUses;

            if (chunked_uses >= 100)
            {
                force_close_chunked = true;
                if (!conn_close)
                {
                    headers.SetInternal("Connection", "close");
                }
            }

            if (location != null)
            {
                headers.SetInternal("Location", location);
            }

            if (cookies != null)
            {
                bool          firstDone = false;
                StringBuilder cookieSB  = new StringBuilder();
                foreach (Cookie cookie in cookies)
                {
                    if (firstDone)
                    {
                        cookieSB.Append(",");
                    }
                    firstDone = true;
                    cookieSB.Append(cookie.ToClientString());
                }
                headers.SetInternal("Set-Cookie2", cookieSB.ToString());
            }

            StreamWriter writer = new StreamWriter(ms, encoding);

            writer.Write("HTTP/{0} {1} {2}\r\n", version, status_code, status_description);
            string headers_str = headers.ToString();

            writer.Write(headers_str);
            writer.Flush();
            // Perf.: use TCP_CORK if we're writing more?
            int preamble = encoding.GetPreamble().Length;

            if (output_stream == null)
            {
                output_stream = context.Connection.GetResponseStream();
            }

            output_stream.InternalWrite(ms.GetBuffer(), 0 + preamble, (int)ms.Length - preamble);
            HeadersSent = true;
        }