public static void SetUserAgent(this HttpRequestHeaders headers, string userAgent) { HttpHeaderValueCollection <ProductInfoHeaderValue> ua = headers.UserAgent; ua.Clear(); ua.ParseAdd(userAgent); }
public static void SetAcceptLanguage(this HttpRequestHeaders headers, string acceptLanguage) { HttpHeaderValueCollection <StringWithQualityHeaderValue> al = headers.AcceptLanguage; al.Clear(); al.ParseAdd(acceptLanguage); }
public static void SetAccept(this HttpRequestHeaders headers, string accept) { HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> a = headers.Accept; a.Clear(); a.ParseAdd(accept); }
// Prepares Content-Length and Transfer-Encoding headers. private Task <bool> PrepareHeadersAsync( HttpRequestMessage request, HttpResponseMessage response, IOwinResponse owinResponse, CancellationToken cancellationToken ) { Contract.Assert(response != null); HttpResponseHeaders responseHeaders = response.Headers; Contract.Assert(responseHeaders != null); HttpContent content = response.Content; bool isTransferEncodingChunked = responseHeaders.TransferEncodingChunked == true; HttpHeaderValueCollection <TransferCodingHeaderValue> transferEncoding = responseHeaders.TransferEncoding; if (content != null) { HttpContentHeaders contentHeaders = content.Headers; Contract.Assert(contentHeaders != null); if (isTransferEncodingChunked) { // According to section 4.4 of the HTTP 1.1 spec, HTTP responses that use chunked transfer // encoding must not have a content length set. Chunked should take precedence over content // length in this case because chunked is always set explicitly by users while the Content-Length // header can be added implicitly by System.Net.Http. contentHeaders.ContentLength = null; } else { // Copy the response content headers only after ensuring they are complete. // We ask for Content-Length first because HttpContent lazily computes this header and only // afterwards writes the value into the content headers. return(ComputeContentLengthAsync( request, response, owinResponse, cancellationToken )); } } // Ignore the Transfer-Encoding header if it is just "chunked"; the host will likely provide it when no // Content-Length is present (and if the host does not, there's not much better this code could do to // transmit the current response, since HttpContent is assumed to be unframed; in that case, silently drop // the Transfer-Encoding: chunked header). // HttpClient sets this header when it receives chunked content, but HttpContent does not include the // frames. The OWIN contract is to set this header only when writing chunked frames to the stream. // A Web API caller who desires custom framing would need to do a different Transfer-Encoding (such as // "identity, chunked"). if (isTransferEncodingChunked && transferEncoding.Count == 1) { transferEncoding.Clear(); } return(Task.FromResult(true)); }
public void Clear_AddValuesThenClear_NoElementsInCollection() { MockHeaders headers = new MockHeaders(); HttpHeaderValueCollection <Uri> collection = new HttpHeaderValueCollection <Uri>(knownUriHeader, headers); collection.Add(new Uri("http://www.example.org/1/")); collection.Add(new Uri("http://www.example.org/2/")); collection.Add(new Uri("http://www.example.org/3/")); Assert.Equal(3, collection.Count); collection.Clear(); Assert.Equal(0, collection.Count); }
public void Clear_AddValuesAndSpecialValueThenClear_EverythingCleared() { MockHeaders headers = new MockHeaders(); HttpHeaderValueCollection <Uri> collection = new HttpHeaderValueCollection <Uri>(knownUriHeader, headers, specialValue); collection.SetSpecialValue(); collection.Add(new Uri("http://www.example.org/1/")); collection.Add(new Uri("http://www.example.org/2/")); collection.Add(new Uri("http://www.example.org/3/")); Assert.Equal(4, collection.Count); Assert.True(collection.IsSpecialValueSet, "Special value not set."); collection.Clear(); Assert.Equal(0, collection.Count); Assert.False(collection.IsSpecialValueSet, "Special value was removed by Clear()."); }
public void Clear_AddValuesAndSpecialValueThenClear_EverythingCleared() { MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri))); HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers, specialValue); collection.SetSpecialValue(); collection.Add(new Uri("http://www.example.org/1/")); collection.Add(new Uri("http://www.example.org/2/")); collection.Add(new Uri("http://www.example.org/3/")); Assert.Equal(4, collection.Count); Assert.True(collection.IsSpecialValueSet, "Special value not set."); collection.Clear(); Assert.Equal(0, collection.Count); Assert.False(collection.IsSpecialValueSet, "Special value was removed by Clear()."); }
public void Clear_AddValuesThenClear_NoElementsInCollection() { MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri))); HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers); collection.Add(new Uri("http://www.example.org/1/")); collection.Add(new Uri("http://www.example.org/2/")); collection.Add(new Uri("http://www.example.org/3/")); Assert.Equal(3, collection.Count); collection.Clear(); Assert.Equal(0, collection.Count); }
internal static async Task <bool> PrepareHeadersAsync(HttpResponseBase responseBase, HttpRequestMessage request, HttpResponseMessage response, IExceptionLogger exceptionLogger, CancellationToken cancellationToken) { Contract.Assert(response != null); HttpResponseHeaders responseHeaders = response.Headers; Contract.Assert(responseHeaders != null); HttpContent content = response.Content; bool isTransferEncodingChunked = responseHeaders.TransferEncodingChunked == true; HttpHeaderValueCollection <TransferCodingHeaderValue> transferEncoding = responseHeaders.TransferEncoding; if (content != null) { HttpContentHeaders contentHeaders = content.Headers; Contract.Assert(contentHeaders != null); if (isTransferEncodingChunked) { // According to section 4.4 of the HTTP 1.1 spec, HTTP responses that use chunked transfer // encoding must not have a content length set. Chunked should take precedence over content // length in this case because chunked is always set explicitly by users while the Content-Length // header can be added implicitly by System.Net.Http. contentHeaders.ContentLength = null; } else { Exception exception = null; // Copy the response content headers only after ensuring they are complete. // We ask for Content-Length first because HttpContent lazily computes this // and only afterwards writes the value into the content headers. try { var unused = contentHeaders.ContentLength; } catch (Exception ex) { exception = ex; } if (exception != null) { ExceptionContext exceptionContext = new ExceptionContext(exception, WebHostExceptionCatchBlocks.HttpControllerHandlerComputeContentLength, request, response); await exceptionLogger.LogAsync(exceptionContext, cancellationToken); SetEmptyErrorResponse(responseBase); return(false); } } // Select output buffering based on the user-controlled buffering policy bool isBuffered = _bufferPolicySelector.Value != null? _bufferPolicySelector.Value.UseBufferedOutputStream(response) : true; responseBase.BufferOutput = isBuffered; } // Ignore the Transfer-Encoding header if it is just "chunked"; the host will provide it when no // Content-Length is present and BufferOutput is disabled (and this method guarantees those conditions). // HttpClient sets this header when it receives chunked content, but HttpContent does not include the // frames. The ASP.NET contract is to set this header only when writing chunked frames to the stream. // A Web API caller who desires custom framing would need to do a different Transfer-Encoding (such as // "identity, chunked"). if (isTransferEncodingChunked && transferEncoding.Count == 1) { transferEncoding.Clear(); // In the case of a conflict between a Transfer-Encoding: chunked header and the output buffering // policy, honor the Transnfer-Encoding: chunked header and ignore the buffer policy. // If output buffering is not disabled, ASP.NET will not write the TransferEncoding: chunked header. responseBase.BufferOutput = false; } return(true); }
/// <summary> /// Set an HTTP header to a single value, clearing any existing values /// </summary> /// <typeparam name="T"></typeparam> /// <param name="headerValue"></param> /// <param name="value"></param> public static void SetValue <T>(this HttpHeaderValueCollection <T> headerValue, string value) where T : class { headerValue.Clear(); headerValue.ParseAdd(value); }
static void CopyETags(ICollection<ETag> source, HttpHeaderValueCollection<EntityTagHeaderValue> target) { target.Clear(); foreach (var tag in source) target.Add(ToEntityTagHeaderValue(tag)); }