/// <summary>Read the contents of an entity and return it as a byte array.</summary> /// <remarks>Read the contents of an entity and return it as a byte array.</remarks> /// <param name="entity">the entity to read from=</param> /// <returns> /// byte array containing the entity content. May be null if /// <see cref="Org.Apache.Http.HttpEntity.GetContent()">Org.Apache.Http.HttpEntity.GetContent() /// </see> /// is null. /// </returns> /// <exception cref="System.IO.IOException">if an error occurs reading the input stream /// </exception> /// <exception cref="System.ArgumentException">if entity is null or if content length > Integer.MAX_VALUE /// </exception> public static byte[] ToByteArray(HttpEntity entity) { Args.NotNull(entity, "Entity"); InputStream instream = entity.GetContent(); if (instream == null) { return(null); } try { Args.Check(entity.GetContentLength() <= int.MaxValue, "HTTP entity too large to be buffered in memory" ); int i = (int)entity.GetContentLength(); if (i < 0) { i = 4096; } ByteArrayBuffer buffer = new ByteArrayBuffer(i); byte[] tmp = new byte[4096]; int l; while ((l = instream.Read(tmp)) != -1) { buffer.Append(tmp, 0, l); } return(buffer.ToByteArray()); } finally { instream.Close(); } }
/// <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"); } } }
/// <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()); } } }
/// <summary> /// Get the entity content as a String, using the provided default character set /// if none is found in the entity. /// </summary> /// <remarks> /// Get the entity content as a String, using the provided default character set /// if none is found in the entity. /// If defaultCharset is null, the default "ISO-8859-1" is used. /// </remarks> /// <param name="entity">must not be null</param> /// <param name="defaultCharset">character set to be applied if none found in the entity /// </param> /// <returns> /// the entity content as a String. May be null if /// <see cref="Org.Apache.Http.HttpEntity.GetContent()">Org.Apache.Http.HttpEntity.GetContent() /// </see> /// is null. /// </returns> /// <exception cref="Org.Apache.Http.ParseException">if header elements cannot be parsed /// </exception> /// <exception cref="System.ArgumentException">if entity is null or if content length > Integer.MAX_VALUE /// </exception> /// <exception cref="System.IO.IOException">if an error occurs reading the input stream /// </exception> /// <exception cref="Sharpen.UnsupportedCharsetException"> /// Thrown when the named charset is not available in /// this instance of the Java virtual machine /// </exception> public static string ToString(HttpEntity entity, Encoding defaultCharset) { Args.NotNull(entity, "Entity"); InputStream instream = entity.GetContent(); if (instream == null) { return(null); } try { Args.Check(entity.GetContentLength() <= int.MaxValue, "HTTP entity too large to be buffered in memory" ); int i = (int)entity.GetContentLength(); if (i < 0) { i = 4096; } Encoding charset = null; try { ContentType contentType = ContentType.Get(entity); if (contentType != null) { charset = contentType.GetCharset(); } } catch (UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.Message); } if (charset == null) { charset = defaultCharset; } if (charset == null) { charset = HTTP.DefContentCharset; } StreamReader reader = new InputStreamReader(instream, charset); CharArrayBuffer buffer = new CharArrayBuffer(i); char[] tmp = new char[1024]; int l; while ((l = reader.Read(tmp)) != -1) { buffer.Append(tmp, 0, l); } return(buffer.ToString()); } finally { instream.Close(); } }
/// <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); } } } }
/// <summary>Creates a new buffered entity wrapper.</summary> /// <remarks>Creates a new buffered entity wrapper.</remarks> /// <param name="entity">the entity to wrap, not null</param> /// <exception cref="System.ArgumentException">if wrapped is null</exception> /// <exception cref="System.IO.IOException"></exception> public BufferedHttpEntity(HttpEntity entity) : base(entity) { if (!entity.IsRepeatable() || entity.GetContentLength() < 0) { this.buffer = EntityUtils.ToByteArray(entity); } else { this.buffer = null; } }
/// <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"); } } } }
/// <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.ContainsHeader(HTTP.ExpectDirective)) { if (request is HttpEntityEnclosingRequest) { ProtocolVersion ver = request.GetRequestLine().GetProtocolVersion(); HttpEntity entity = ((HttpEntityEnclosingRequest)request).GetEntity(); // Do not send the expect header if request body is known to be empty if (entity != null && entity.GetContentLength() != 0 && !ver.LessEquals(HttpVersion .Http10)) { bool active = request.GetParams().GetBooleanParameter(CoreProtocolPNames.UseExpectContinue , this.activeByDefault); if (active) { request.AddHeader(HTTP.ExpectDirective, HTTP.ExpectContinue); } } } } }
/// <exception cref="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.ContainsHeader(HTTP.ExpectDirective)) { if (request is HttpEntityEnclosingRequest) { ProtocolVersion ver = request.GetRequestLine().GetProtocolVersion(); HttpEntity entity = ((HttpEntityEnclosingRequest)request).GetEntity(); // Do not send the expect header if request body is known to be empty if (entity != null && entity.GetContentLength() != 0 && !ver.LessEquals(HttpVersion .Http10)) { HttpClientContext clientContext = ((HttpClientContext)HttpClientContext.Adapt(context )); RequestConfig config = clientContext.GetRequestConfig(); if (config.IsExpectContinueEnabled()) { request.AddHeader(HTTP.ExpectDirective, HTTP.ExpectContinue); } } } } }
public virtual long GetContentLength() { return(wrappedEntity.GetContentLength()); }