Ejemplo n.º 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");
                }
            }
        }
Ejemplo n.º 2
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());
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Handles the following
        /// <code>Content-Encoding</code>
        /// s by
        /// using the appropriate decompressor to wrap the response Entity:
        /// <ul>
        /// <li>gzip - see
        /// <see cref="Apache.Http.Client.Entity.GzipDecompressingEntity">Apache.Http.Client.Entity.GzipDecompressingEntity
        ///     </see>
        /// </li>
        /// <li>deflate - see
        /// <see cref="Apache.Http.Client.Entity.DeflateDecompressingEntity">Apache.Http.Client.Entity.DeflateDecompressingEntity
        ///     </see>
        /// </li>
        /// <li>identity - no action needed</li>
        /// </ul>
        /// </summary>
        /// <param name="response">the response which contains the entity</param>
        /// <param name="context">not currently used</param>
        /// <exception cref="Apache.Http.HttpException">
        /// if the
        /// <code>Content-Encoding</code>
        /// is none of the above
        /// </exception>
        /// <exception cref="System.IO.IOException"></exception>
        public virtual void Process(HttpResponse response, HttpContext context)
        {
            HttpEntity entity = response.GetEntity();

            // entity can be null in case of 304 Not Modified, 204 No Content or similar
            // check for zero length entity.
            if (entity != null && entity.GetContentLength() != 0)
            {
                Header ceheader = entity.GetContentEncoding();
                if (ceheader != null)
                {
                    HeaderElement[] codecs       = ceheader.GetElements();
                    bool            uncompressed = false;
                    foreach (HeaderElement codec in codecs)
                    {
                        string codecname = codec.GetName().ToLower(CultureInfo.InvariantCulture);
                        if ("gzip".Equals(codecname) || "x-gzip".Equals(codecname))
                        {
                            response.SetEntity(new GzipDecompressingEntity(response.GetEntity()));
                            uncompressed = true;
                            break;
                        }
                        else
                        {
                            if ("deflate".Equals(codecname))
                            {
                                response.SetEntity(new DeflateDecompressingEntity(response.GetEntity()));
                                uncompressed = true;
                                break;
                            }
                            else
                            {
                                if ("identity".Equals(codecname))
                                {
                                    return;
                                }
                                else
                                {
                                    throw new HttpException("Unsupported Content-Coding: " + codec.GetName());
                                }
                            }
                        }
                    }
                    if (uncompressed)
                    {
                        response.RemoveHeaders("Content-Length");
                        response.RemoveHeaders("Content-Encoding");
                        response.RemoveHeaders("Content-MD5");
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public virtual Header GetContentEncoding()
 {
     return(wrappedEntity.GetContentEncoding());
 }