コード例 #1
0
ファイル: WebResponse.cs プロジェクト: drazenzadravec/nequeo
        /// <summary>
        /// Write the response status to the stream.
        /// </summary>
        /// <param name="writeEndOfHeaders">Write the end of the header bytes, carrige return line feed.</param>
        /// <param name="writeResponseStatus">Write the response status (HTTP/1.1 200 OK)</param>
        public virtual void WriteWebResponseHeaders(bool writeEndOfHeaders = true, bool writeResponseStatus = true)
        {
            byte[] buffer = null;
            string data   = "";

            // If chunked is used.
            if (SendChunked)
            {
                AddHeader("Transfer-Encoding", "Chunked");
            }

            // If the server has been specified.
            if (!String.IsNullOrEmpty(Server))
            {
                AddHeader("Server", Server);
            }

            // If content length has been specified.
            if (ContentLength > 0)
            {
                AddHeader("Content-Length", ContentLength.ToString());
            }

            // If the allow has been specified.
            if (!String.IsNullOrEmpty(Allow))
            {
                AddHeader("Allow", Allow);
            }

            // If the content type has been specified.
            if (!String.IsNullOrEmpty(ContentType))
            {
                AddHeader("Content-Type", ContentType);
            }

            // If the Upgrade has been specified.
            if (!String.IsNullOrEmpty(Upgrade))
            {
                // If an upgrade is required
                // then set the connection to upgrade
                // and set the upgrade to the protocol (e.g. WebSocket, HTTP/2.0 .. etc).
                AddHeader("Connection", "Upgrade");
                AddHeader("Upgrade", Upgrade);
            }
            else
            {
                // If the connection is open.
                if (KeepAlive)
                {
                    AddHeader("Connection", "Keep-Alive");
                }
            }

            // If the content encoding has been specified.
            if (!String.IsNullOrEmpty(ContentEncoding))
            {
                AddHeader("Content-Encoding", ContentEncoding);
            }

            // If the content lanaguage has been specified.
            if (!String.IsNullOrEmpty(ContentLanguage))
            {
                AddHeader("Content-Language", ContentLanguage);
            }

            // If authenticate type is other than none.
            if (AuthorizationType != Nequeo.Security.AuthenticationType.None)
            {
                AddHeader("WWW-Authenticate", AuthorizationType.ToString());
            }

            // Write response status.
            if (writeResponseStatus)
            {
                // If protocol version http/2 is used.
                if ((ProtocolVersion.ToLower().Equals("http/2")) || (ProtocolVersion.ToLower().Equals("http/2.0")))
                {
                    // Send the http response status.
                    data   = ":status = " + StatusCode.ToString() + (StatusSubcode > 0 ? "." + StatusSubcode.ToString() : "") + _deli;
                    buffer = Encoding.Default.GetBytes(data);
                    Write(buffer, 0, buffer.Length);
                }
                else
                {
                    // Send the http response status.
                    data   = ProtocolVersion + " " + StatusCode.ToString() + (StatusSubcode > 0 ? "." + StatusSubcode.ToString() : "") + " " + StatusDescription + _deli;
                    buffer = Encoding.Default.GetBytes(data);
                    Write(buffer, 0, buffer.Length);
                }
            }

            // If headers exists.
            if (Headers != null)
            {
                // For each header found.
                foreach (string header in Headers.AllKeys)
                {
                    // If protocol version http/2 is used.
                    if ((ProtocolVersion.ToLower().Equals("http/2")) || (ProtocolVersion.ToLower().Equals("http/2.0")))
                    {
                        // Add each header.
                        data   = header.ToLower() + " = " + Headers[header] + _deli;
                        buffer = Encoding.Default.GetBytes(data);
                        Write(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        // Add each header.
                        data   = header + ": " + Headers[header] + _deli;
                        buffer = Encoding.Default.GetBytes(data);
                        Write(buffer, 0, buffer.Length);
                    }
                }
            }

            // If cookies exists.
            if (Cookies != null)
            {
                // For each cookie found.
                foreach (Cookie cookie in Cookies)
                {
                    // Make shore the cookie has been set.
                    if (!String.IsNullOrEmpty(cookie.Name) && !String.IsNullOrEmpty(cookie.Value))
                    {
                        // Get the cookie details.
                        data = "Set-Cookie" + ": " + cookie.Name + "=" + cookie.Value +
                               (cookie.Expires != null ? "; Expires=" + cookie.Expires.ToUniversalTime().ToLongDateString() + " " + cookie.Expires.ToUniversalTime().ToLongTimeString() + " GMT" : "") +
                               (!String.IsNullOrEmpty(cookie.Path) ? "; Path=" + cookie.Path : "") +
                               (!String.IsNullOrEmpty(cookie.Domain) ? "; Domain=" + cookie.Domain : "") +
                               (cookie.Version > 0 ? "; Version=" + cookie.Version : "") +
                               (cookie.Secure ? "; Secure" : "") +
                               (cookie.HttpOnly ? "; HttpOnly" : "") +
                               _deli;

                        // Write to the stream.
                        buffer = Encoding.Default.GetBytes(data);
                        Write(buffer, 0, buffer.Length);
                    }
                }
            }

            // Write the end of the headers.
            if (writeEndOfHeaders)
            {
                // Send the header end space.
                data   = _deli;
                buffer = Encoding.Default.GetBytes(data);
                Write(buffer, 0, buffer.Length);
            }
        }
コード例 #2
0
        /// <summary>
        /// Write the request to the stream.
        /// </summary>
        /// <param name="writeEndOfHeaders">Write the end of the header bytes, carrige return line feed.</param>
        public virtual void WriteNetRequestHeaders(bool writeEndOfHeaders = true)
        {
            byte[] buffer = null;
            string data   = "";

            // If content length has been specified.
            if (ContentLength > 0)
            {
                AddHeader("Content-Length", ContentLength.ToString());
            }

            // If the Host been specified.
            if (!String.IsNullOrEmpty(Host))
            {
                AddHeader("Host", Host);
            }

            // If the content type has been specified.
            if (!String.IsNullOrEmpty(ContentType))
            {
                AddHeader("Content-Type", ContentType);
            }

            // If the content encoding has been specified.
            if (AcceptEncoding != null)
            {
                AddHeader("Accept-Encoding", String.Join(",", AcceptEncoding));
            }

            // If the content lanaguage has been specified.
            if (AcceptLanguages != null)
            {
                AddHeader("Accept-Language", String.Join(",", AcceptLanguages));
            }

            // If the content accept types has been specified.
            if (AcceptTypes != null)
            {
                AddHeader("Accept", String.Join(",", AcceptTypes));
            }

            // If the UrlReferrer has been specified.
            if (UrlReferrer != null)
            {
                AddHeader("Referer", UrlReferrer.OriginalString);
            }

            // If the upgrade has been specified.
            if (!String.IsNullOrEmpty(Upgrade))
            {
                // If an upgrade is required
                // then set the connection to upgrade
                // and set the upgrade to the protocol (e.g. WebSocket, HTTP/2.0 .. etc).
                AddHeader("Connection", "Upgrade");
                AddHeader("Upgrade", Upgrade);
            }
            else
            {
                // If the connection is open.
                if (KeepAlive)
                {
                    AddHeader("Connection", "Keep-Alive");
                }
            }

            // If the credentials has been specified.
            if (Credentials != null)
            {
                string userAndPassword  = Credentials.UserName + ":" + Credentials.Password;
                byte[] credentialBytes  = Encoding.Default.GetBytes(userAndPassword);
                string credentialString = Convert.ToBase64String(credentialBytes);
                AddHeader("Authorization", AuthorizationType.ToString() + " " + credentialString);
            }

            // If cookies exist.
            if (Cookies != null)
            {
                string cookieNameValueCol = "";

                // For each cookie found.
                foreach (Cookie cookie in Cookies)
                {
                    // Make shore the cookie has been set.
                    if (!String.IsNullOrEmpty(cookie.Name) && !String.IsNullOrEmpty(cookie.Value))
                    {
                        // Set the name value cookie collection.
                        cookieNameValueCol += cookie.Name + "=" + cookie.Value + ";";
                    }
                }

                // Only set the cookies if a name value exists.
                if (!String.IsNullOrEmpty(cookieNameValueCol))
                {
                    // Add the cookie header.
                    AddHeader("Cookie", cookieNameValueCol.TrimEnd(';'));
                }
            }

            // If protocol version http/2 is used.
            if ((ProtocolVersion.ToLower().Equals("http/2")) || (ProtocolVersion.ToLower().Equals("http/2.0")))
            {
                // Send the http request.
                data   = ":method = " + Method + _deli + ":scheme = " + Scheme + _deli + ":path = " + Path + _deli;
                buffer = Encoding.Default.GetBytes(data);
                Write(buffer, 0, buffer.Length);
            }
            else
            {
                // Send the http request.
                data   = Method + " " + Path + " " + ProtocolVersion + _deli;
                buffer = Encoding.Default.GetBytes(data);
                Write(buffer, 0, buffer.Length);
            }

            // If headers exists.
            if (Headers != null)
            {
                // For each header found.
                foreach (string header in Headers.AllKeys)
                {
                    // If protocol version http/2 is used.
                    if ((ProtocolVersion.ToLower().Equals("http/2")) || (ProtocolVersion.ToLower().Equals("http/2.0")))
                    {
                        // Add each header.
                        data   = header.ToLower() + " = " + Headers[header] + _deli;
                        buffer = Encoding.Default.GetBytes(data);
                        Write(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        // Add each header.
                        data   = header + ": " + Headers[header] + _deli;
                        buffer = Encoding.Default.GetBytes(data);
                        Write(buffer, 0, buffer.Length);
                    }
                }
            }

            // Write the end of the headers.
            if (writeEndOfHeaders)
            {
                // Send the header end space.
                data   = _deli;
                buffer = Encoding.Default.GetBytes(data);
                Write(buffer, 0, buffer.Length);
            }
        }