/// <summary>
        /// Checks the headers of a response message
        /// </summary>
        /// <param name="response"></param>
        internal protected void CheckRequiredResponseHeaders(HttpResponse response)
        {
            // make sure the server header is filled in
            if (HttpUtils.IsEmptryString(response.Server))
            {
                response.Server = new CarbonServerProtocolVersion().ToString();
            }

            // if there is no body, or that body is zero length we are finished
            if (response.Body == null)
            {
                return;
            }

            // if the body is zero length no problem
            if (response.Body.Length == 0)
            {
                return;
            }

            // however if there is a body with X length, we must ensure that the response's content-length header is filled in properly
            if (response.ContentLength != response.Body.Length)
            {
                response.ContentLength = response.Body.Length;
            }

            // lastly, warn if we are going to send a response with no content-type, it's just not cool
            if (HttpUtils.IsEmptryString(response.ContentType))
            {
                Debug.WriteLine(string.Format("The response '{0}' contains a body, but does not contain a valid 'content-type'. Please set the 'content-type' header field, or use the SetBody(XXX) methods of the HttpMessage class when setting the message body.", response.FirstLine), MY_TRACE_CATEGORY);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write the value to the specified header, optionally deletes the header if the value is empty or null
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="deleteIfEmptyOrNull"></param>
        public virtual void WriteHeaderValue(string name, string value, bool deleteIfNullOrEmpty)
        {
            if (deleteIfNullOrEmpty)
            {
                if (HttpUtils.IsEmptryString(value) || HttpUtils.IsNullString(value))
                {
                    _headers.Remove(name);
                    return;
                }
            }

            // validate the header value, not empty, null, or contains CR or LF
            HttpUtils.ValidateToken(name, value);

            // otherwise if it doesn't exist
            if (!_headers.Contains(name))
            {
                // add it in with the value specified
                _headers.Add(new HttpHeader(name, value));
                return;
            }

            // or finally just update the one that exists
            _headers[name].Value = value;
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            // append each header
            foreach (HttpHeader header in base.InnerList)
            {
                if (!HttpUtils.IsEmptryString(header.Value))
                {
                    sb.Append(header.ToString());
                }
            }

            // wrap up the headers with another crlf combo
            sb.Append(HttpControlChars.CRLF);

            return(sb.ToString());
        }
        /// <summary>
        /// Checks the headers of a request message
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="request"></param>
        internal protected void CheckRequiredRequestHeaders(Socket socket, HttpRequest request)
        {
            // make sure the host header is filled in
            if (HttpUtils.IsEmptryString(request.Host))
            {
//				lock(socket)
                request.Host = this.GetHostFromEndPointString(socket.LocalEndPoint.ToString());
            }

            // make sure the user-agent header is filled in
            if (HttpUtils.IsEmptryString(request.UserAgent))
            {
                request.UserAgent = new CarbonClientProtocolVersion().ToString();
            }

            // if there is no body, or that body is zero length we are finished
            if (request.Body == null)
            {
                return;
            }

            // if the body is zero length no problem
            if (request.Body.Length == 0)
            {
                return;
            }

            // however if there is a body with X length, we must ensure that the request's content-length header is filled in properly
            if (request.ContentLength != request.Body.Length)
            {
                request.ContentLength = request.Body.Length;
            }

            // lastly, warn if we are going to send a request with no content-type, it's just not cool
            if (HttpUtils.IsEmptryString(request.ContentType))
            {
                Debug.WriteLine(string.Format("The request '{0}' contains a body, but does not contain a valid 'content-type'. Please set the 'content-type' header field, or use the SetBody(XXX) methods of the HttpMessage class when setting the message body.", request.FirstLine), MY_TRACE_CATEGORY);
            }
        }