Skip to content

Latest commit

 

History

History
59 lines (45 loc) · 3.09 KB

DecompressionHandler.md

File metadata and controls

59 lines (45 loc) · 3.09 KB

Decompression Handler

A middleware component that requests, detects and decompresses response bodies.

Requirements

  • Add Accept-encoding: gzip to request headers.
  • Decompress response bodies that have the header Content-Encoding: gzip
  • Compression handler should be installed as a default handler by GraphClientFactory
  • Additional observability requirements in Observability

Remarks

For request payload compression, refer to the compression handler specification.

State of automatic decompression

The following table describes the state of automatic decompression of responses. When automatic decompression is supported, we don't need to implement it manually.

LanguageClientAutomatic decompressionSourceNotes
CSharpHttpClientYeslinkOnly if the handler AutomaticDecompression is set to All (not the default!)
Gonet/httpYeslinkOnly gzip is supported. Ensure DisableCompression is false (default) on the transport.
JavaOkHttpYeslink
PHPGuzzleYeslinkDoes NOT add the accept encoding header automatically.
PythonHTTPXYesNoneCouldn't find any source, but tested with 0.27.0 and httpbin
TypeScript/JavaScriptfetchYeslinkAutomatically adds the accept encoding header and decompresses the response. Make sure compress is set to true (default) in the request options.

Example

publicclassDecompressionHandler:DelegatingHandler{protectedasyncoverrideTask<HttpResponseMessage>SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken){// Declare compression support to servervargzipQString=newStringWithQualityHeaderValue("gzip");if(!request.Headers.AcceptEncoding.Contains(gzipQString)){request.Headers.AcceptEncoding.Add(gzipQString);}// Record feature usage for telemetryvarrequestContext=request.GetRequestContext();requestContext.FeatureUsage|=FeatureFlag.CompressionHandler;// Send Requestvarresponse=awaitbase.SendAsync(request,cancellationToken);// Decompress gzipped responsesif(response.Content!=null&&response.Content.Headers.ContentEncoding.Contains("gzip")){response.Content=newStreamContent(newGZipStream(awaitresponse.Content.ReadAsStreamAsync(),CompressionMode.Decompress));}returnresponse;}}
close