using System.IO; using System.IO.Compression; using System.Text; using Swan.Logging; namespace EmbedIO { partial class HttpContextExtensions { /// /// Wraps the request input stream and returns a that can be used directly. /// Decompression of compressed request bodies is implemented if specified in the web server's options. /// /// The on which this method is called. /// /// A that can be used to write response data. /// This stream MUST be disposed when finished writing. /// /// /// public static Stream OpenRequestStream(this IHttpContext @this) { var stream = @this.Request.InputStream; var encoding = @this.Request.Headers[HttpHeaderNames.ContentEncoding]?.Trim(); switch (encoding) { case CompressionMethodNames.Gzip: if (@this.SupportCompressedRequests) return new GZipStream(stream, CompressionMode.Decompress); break; case CompressionMethodNames.Deflate: if (@this.SupportCompressedRequests) return new DeflateStream(stream, CompressionMode.Decompress); break; case CompressionMethodNames.None: case null: return stream; } $"[{@this.Id}] Unsupported request content encoding \"{encoding}\", sending 400 Bad Request..." .Warn(nameof(OpenRequestStream)); throw HttpException.BadRequest($"Unsupported content encoding \"{encoding}\""); } /// /// Wraps the request input stream and returns a that can be used directly. /// Decompression of compressed request bodies is implemented if specified in the web server's options. /// /// The on which this method is called. /// /// A that can be used to read the request body as text. /// This reader MUST be disposed when finished reading. /// /// /// public static TextReader OpenRequestText(this IHttpContext @this) => new StreamReader(OpenRequestStream(@this), @this.Request.ContentEncoding); } }