コード例 #1
0
        /// <summary>Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
        ///     </summary>
        /// <remarks>Processes the response (possibly updating or inserting) Content-Length and Transfer-Encoding headers.
        ///     </remarks>
        /// <param name="response">The HttpResponse to modify.</param>
        /// <param name="context">Unused.</param>
        /// <exception cref="Org.Apache.Http.ProtocolException">If either the Content-Length or Transfer-Encoding headers are found.
        ///     </exception>
        /// <exception cref="System.ArgumentException">If the response is null.</exception>
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(HttpResponse response, HttpContext context)
        {
            Args.NotNull(response, "HTTP response");
            if (this.overwrite)
            {
                response.RemoveHeaders(HTTP.TransferEncoding);
                response.RemoveHeaders(HTTP.ContentLen);
            }
            else
            {
                if (response.ContainsHeader(HTTP.TransferEncoding))
                {
                    throw new ProtocolException("Transfer-encoding header already present");
                }
                if (response.ContainsHeader(HTTP.ContentLen))
                {
                    throw new ProtocolException("Content-Length header already present");
                }
            }
            ProtocolVersion ver    = response.GetStatusLine().GetProtocolVersion();
            HttpEntity      entity = response.GetEntity();

            if (entity != null)
            {
                long len = entity.GetContentLength();
                if (entity.IsChunked() && !ver.LessEquals(HttpVersion.Http10))
                {
                    response.AddHeader(HTTP.TransferEncoding, HTTP.ChunkCoding);
                }
                else
                {
                    if (len >= 0)
                    {
                        response.AddHeader(HTTP.ContentLen, System.Convert.ToString(entity.GetContentLength
                                                                                        ()));
                    }
                }
                // Specify a content type if known
                if (entity.GetContentType() != null && !response.ContainsHeader(HTTP.ContentType))
                {
                    response.AddHeader(entity.GetContentType());
                }
                // Specify a content encoding if known
                if (entity.GetContentEncoding() != null && !response.ContainsHeader(HTTP.ContentEncoding
                                                                                    ))
                {
                    response.AddHeader(entity.GetContentEncoding());
                }
            }
            else
            {
                int status = response.GetStatusLine().GetStatusCode();
                if (status != HttpStatus.ScNoContent && status != HttpStatus.ScNotModified && status
                    != HttpStatus.ScResetContent)
                {
                    response.AddHeader(HTTP.ContentLen, "0");
                }
            }
        }
コード例 #2
0
        /// <exception cref="Org.Apache.Http.HttpException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(HttpResponse response, HttpContext context)
        {
            Args.NotNull(response, "HTTP response");
            HttpCoreContext corecontext = HttpCoreContext.Adapt(context);
            // Always drop connection after certain type of responses
            int status = response.GetStatusLine().GetStatusCode();

            if (status == HttpStatus.ScBadRequest || status == HttpStatus.ScRequestTimeout ||
                status == HttpStatus.ScLengthRequired || status == HttpStatus.ScRequestTooLong ||
                status == HttpStatus.ScRequestUriTooLong || status == HttpStatus.ScServiceUnavailable ||
                status == HttpStatus.ScNotImplemented)
            {
                response.SetHeader(HTTP.ConnDirective, HTTP.ConnClose);
                return;
            }
            Header _explicit = response.GetFirstHeader(HTTP.ConnDirective);

            if (_explicit != null && Sharpen.Runtime.EqualsIgnoreCase(HTTP.ConnClose, _explicit
                                                                      .GetValue()))
            {
                // Connection persistence _explicitly disabled
                return;
            }
            // Always drop connection for HTTP/1.0 responses and below
            // if the content body cannot be correctly delimited
            HttpEntity entity = response.GetEntity();

            if (entity != null)
            {
                ProtocolVersion ver = response.GetStatusLine().GetProtocolVersion();
                if (entity.GetContentLength() < 0 && (!entity.IsChunked() || ver.LessEquals(HttpVersion
                                                                                            .Http10)))
                {
                    response.SetHeader(HTTP.ConnDirective, HTTP.ConnClose);
                    return;
                }
            }
            // Drop connection if requested by the client or request was <= 1.0
            IHttpRequest request = corecontext.GetRequest();

            if (request != null)
            {
                Header header = request.GetFirstHeader(HTTP.ConnDirective);
                if (header != null)
                {
                    response.SetHeader(HTTP.ConnDirective, header.GetValue());
                }
                else
                {
                    if (request.GetProtocolVersion().LessEquals(HttpVersion.Http10))
                    {
                        response.SetHeader(HTTP.ConnDirective, HTTP.ConnClose);
                    }
                }
            }
        }
コード例 #3
0
 /// <exception cref="Org.Apache.Http.HttpException"></exception>
 /// <exception cref="System.IO.IOException"></exception>
 public virtual void Process(IHttpRequest request, HttpContext context)
 {
     Args.NotNull(request, "HTTP request");
     if (request is HttpEntityEnclosingRequest)
     {
         if (this.overwrite)
         {
             request.RemoveHeaders(HTTP.TransferEncoding);
             request.RemoveHeaders(HTTP.ContentLen);
         }
         else
         {
             if (request.ContainsHeader(HTTP.TransferEncoding))
             {
                 throw new ProtocolException("Transfer-encoding header already present");
             }
             if (request.ContainsHeader(HTTP.ContentLen))
             {
                 throw new ProtocolException("Content-Length header already present");
             }
         }
         ProtocolVersion ver    = request.GetRequestLine().GetProtocolVersion();
         HttpEntity      entity = ((HttpEntityEnclosingRequest)request).GetEntity();
         if (entity == null)
         {
             request.AddHeader(HTTP.ContentLen, "0");
             return;
         }
         // Must specify a transfer encoding or a content length
         if (entity.IsChunked() || entity.GetContentLength() < 0)
         {
             if (ver.LessEquals(HttpVersion.Http10))
             {
                 throw new ProtocolException("Chunked transfer encoding not allowed for " + ver);
             }
             request.AddHeader(HTTP.TransferEncoding, HTTP.ChunkCoding);
         }
         else
         {
             request.AddHeader(HTTP.ContentLen, System.Convert.ToString(entity.GetContentLength
                                                                            ()));
         }
         // Specify a content type if known
         if (entity.GetContentType() != null && !request.ContainsHeader(HTTP.ContentType))
         {
             request.AddHeader(entity.GetContentType());
         }
         // Specify a content encoding if known
         if (entity.GetContentEncoding() != null && !request.ContainsHeader(HTTP.ContentEncoding
                                                                            ))
         {
             request.AddHeader(entity.GetContentEncoding());
         }
     }
 }
コード例 #4
0
 public virtual bool IsChunked()
 {
     return(wrappedEntity.IsChunked());
 }