Example #1
0
    private HttpRequestMessage CreateRequest(string httpMethod, string absoluteUrl, object request)
    {
        this.PopulateRequestMetadata(request);

        var httpReq = new HttpRequestMessage(new HttpMethod(httpMethod), absoluteUrl);

        foreach (var name in Headers.AllKeys)
        {
            httpReq.Headers.Add(name, Headers[name]);
        }
        httpReq.Headers.Add(HttpHeaders.Accept, ContentType);

        if (HttpUtils.HasRequestBody(httpMethod) && request != null)
        {
            if (request is HttpContent httpContent)
            {
                httpReq.Content = httpContent;
            }
            else
            {
                if (request is string str)
                {
                    httpReq.Content = new StringContent(str);
                }
                else if (request is byte[] bytes)
                {
                    httpReq.Content = new ByteArrayContent(bytes);
                }
                else if (request is Stream stream)
                {
                    httpReq.Content = new StreamContent(stream);
                }
                else
                {
                    httpReq.Content = new StringContent(request.ToJson(), Encoding.UTF8, ContentType);
                }

                var compressor = StreamCompressors.Get(RequestCompressionType);
                if (compressor != null)
                {
                    httpReq.Content = new CompressContent(httpReq.Content, compressor);
                }
            }
        }

        ApplyWebRequestFilters(httpReq);

        Interlocked.Increment(ref activeAsyncRequests);

        return(httpReq);
    }
Example #2
0
        public static Stream GetInputStream(this IRequest req, Stream stream)
        {
            var enc        = req.GetContentEncoding();
            var compressor = enc != null
                ? StreamCompressors.Get(enc)
                : null;

            if (compressor != null)
            {
                return(compressor.Decompress(stream));
            }

            return(stream);
        }
Example #3
0
        public override string?GetCompressionType(IRequest request)
        {
            if (request.RequestPreferences.AcceptsDeflate && StreamCompressors.SupportsEncoding(CompressionTypes.Deflate))
            {
                return(CompressionTypes.Deflate);
            }

            if (request.RequestPreferences.AcceptsGzip && StreamCompressors.SupportsEncoding(CompressionTypes.GZip))
            {
                return(CompressionTypes.GZip);
            }

            return(null);
        }
Example #4
0
 public static byte[] DecompressBytes(this byte[] gzBuffer, string compressionType) =>
 StreamCompressors.GetRequired(compressionType).DecompressBytes(gzBuffer);
Example #5
0
 public static Stream Decompress(this Stream gzStream, string compressionType) =>
 !string.IsNullOrEmpty(compressionType)
         ? StreamCompressors.GetRequired(compressionType).Decompress(gzStream)
         : gzStream;
Example #6
0
 public static byte[] CompressBytes(this byte[] bytes, string compressionType) =>
 StreamCompressors.GetRequired(compressionType).Compress(bytes);
Example #7
0
 public static Stream CompressStream(this Stream stream, string compressionType) =>
 StreamCompressors.GetRequired(compressionType).Compress(stream);
Example #8
0
 public static byte[] Compress(this string text, string compressionType, Encoding?encoding = null) =>
 StreamCompressors.GetRequired(compressionType).Compress(text, encoding);
Example #9
0
        public static IStreamCompressor GetCompressor(this IRequest request)
        {
            var encoding = request.GetCompressionType();

            return(encoding != null?StreamCompressors.Get(encoding) : null);
        }
Example #10
0
 public void Can_zip_and_unzip_bytes_using_Gzip()
 {
     DoesCompress(StreamCompressors.GetRequired(CompressionTypes.GZip), "hello zip");
 }
Example #11
0
 public void Can_zip_and_unzip_bytes_using_DeflateStream()
 {
     DoesCompress(StreamCompressors.GetRequired(CompressionTypes.Deflate), "hello zip");
 }
Example #12
0
 public void Can_zip_and_unzip_bytes_using_BrotliStream()
 {
     DoesCompress(StreamCompressors.Assert("br"), "hello zip"); //CompressionTypes.Brotli
 }