public void EnsureResponseMessagePublished() { // If the response message hasn't been published yet, do any final processing of it before it is. if (!Task.IsCompleted) { // On Windows, if the response was automatically decompressed, Content-Encoding and Content-Length // headers are removed from the response. Do the same thing here. DecompressionMethods dm = _handler.AutomaticDecompression; if (dm != DecompressionMethods.None) { HttpContentHeaders contentHeaders = _responseMessage.Content.Headers; IEnumerable <string> encodings; if (contentHeaders.TryGetValues(HttpKnownHeaderNames.ContentEncoding, out encodings)) { foreach (string encoding in encodings) { if (((dm & DecompressionMethods.GZip) != 0 && string.Equals(encoding, EncodingNameGzip, StringComparison.OrdinalIgnoreCase)) || ((dm & DecompressionMethods.Deflate) != 0 && string.Equals(encoding, EncodingNameDeflate, StringComparison.OrdinalIgnoreCase))) { contentHeaders.Remove(HttpKnownHeaderNames.ContentEncoding); contentHeaders.Remove(HttpKnownHeaderNames.ContentLength); break; } } } } } // Now ensure it's published. bool result = TrySetResult(_responseMessage); Debug.Assert(result || Task.Status == TaskStatus.RanToCompletion, "If the task was already completed, it should have been completed successfully; " + "we shouldn't be completing as successful after already completing as failed."); }
public static byte[] HandleGzipCompression(HttpContentHeaders contentHeaders, byte[] decodedBodyArray) { if (decodedBodyArray is null || !decodedBodyArray.Any()) { return(decodedBodyArray); } if (contentHeaders?.ContentEncoding != null && contentHeaders.ContentEncoding.Contains("gzip")) { using (var src = new MemoryStream(decodedBodyArray)) using (var unzipStream = new GZipStream(src, CompressionMode.Decompress)) { using var targetStream = new MemoryStream(); unzipStream.CopyTo(targetStream); decodedBodyArray = targetStream.ToArray(); } contentHeaders.ContentEncoding.Remove("gzip"); if (!contentHeaders.ContentEncoding.Any()) { contentHeaders.Remove("Content-Encoding"); } } return(decodedBodyArray); }
public void EnsureResponseMessagePublished() { // If the response message hasn't been published yet, do any final processing of it before it is. if (!Task.IsCompleted) { EventSourceTrace("Publishing response message"); // On Windows, if the response was automatically decompressed, Content-Encoding and Content-Length // headers are removed from the response. Do the same thing here. DecompressionMethods dm = _handler.AutomaticDecompression; if (dm != DecompressionMethods.None) { HttpContentHeaders contentHeaders = _responseMessage.Content.Headers; IEnumerable <string> encodings; if (contentHeaders.TryGetValues(HttpKnownHeaderNames.ContentEncoding, out encodings)) { foreach (string encoding in encodings) { if (((dm & DecompressionMethods.GZip) != 0 && string.Equals(encoding, EncodingNameGzip, StringComparison.OrdinalIgnoreCase)) || ((dm & DecompressionMethods.Deflate) != 0 && string.Equals(encoding, EncodingNameDeflate, StringComparison.OrdinalIgnoreCase))) { contentHeaders.Remove(HttpKnownHeaderNames.ContentEncoding); contentHeaders.Remove(HttpKnownHeaderNames.ContentLength); break; } } } } } // Now ensure it's published. bool completedTask = TrySetResult(_responseMessage); Debug.Assert(completedTask || Task.IsCompletedSuccessfully, "If the task was already completed, it should have been completed successfully; " + "we shouldn't be completing as successful after already completing as failed."); // If we successfully transitioned it to be completed, we also handed off lifetime ownership // of the response to the owner of the task. Transition our reference on the EasyRequest // to be weak instead of strong, so that we don't forcibly keep it alive. if (completedTask) { Debug.Assert(_selfStrongToWeakReference != null, "Expected non-null wrapper"); _selfStrongToWeakReference.MakeWeak(); } }
public static void Set(this HttpContentHeaders headers, string name, string value) { if (headers.Contains(name)) { headers.Remove(name); } headers.Add(name, value); }
private static void AddOrUpdateHeader(this HttpContentHeaders headers, string headerKey, string headerValue) { lock (headers) { headers.Remove(headerKey); headers.Add(headerKey, headerValue); } }
public static void Set(this HttpContentHeaders headers, string name, IEnumerable <string> values) { if (headers == null) { throw new ArgumentNullException(nameof(headers)); } if (headers.Contains(name)) { headers.Remove(name); } headers.Add(name, values); }
/// <summary> /// Add a name value pair to the headers. If the name already exists then the /// value will be updated. /// </summary> /// <param name="source">Headers collection to perform the operation on.</param> /// <param name="name">The header to add or update in the collection.</param> /// <param name="value">The content of the header.</param> /// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> public static void AddOrUpdate(this HttpContentHeaders source, string name, string value) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (source.Contains(name)) { source.Remove(name); } source.Add(name, value); }
private void ReplaceHeaders(HttpContentHeaders content, IEnumerable <KeyValuePair <string, string[]> > headersToAdd) { foreach (var header in headersToAdd) { content.Remove(header.Key); if (header.Value.Count() > 1) { content.Add(header.Key, header.Value); } else { content.Add(header.Key, header.Value.First()); } } }
public static void AssertValidHeaders(HttpHeaders messageHeaders, HttpContentHeaders contentHeaders) { if (messageHeaders != null && messageHeaders.Contains("Transfer-Encoding")) { if (contentHeaders != null && contentHeaders.Contains("Content-Length")) { contentHeaders.Remove("Content-Length"); } } // Any Content-Length field value greater than or equal to zero is valid. if (contentHeaders != null && contentHeaders.Contains("Content-Length")) { if (contentHeaders.ContentLength < 0) throw new HttpRequestException("Content-Length MUST be larger than zero."); } }