private static Task SendResponseMessageAsync(IDictionary <string, object> environment, HttpResponseMessage response) { environment[OwinConstants.ResponseStatusCodeKey] = response.StatusCode; environment[OwinConstants.ResponseReasonPhraseKey] = response.ReasonPhrase; // Copy non-content headers IDictionary <string, string[]> responseHeaders = environment.GetOwinValue <IDictionary <string, string[]> >(OwinConstants.ResponseHeadersKey); foreach (KeyValuePair <string, IEnumerable <string> > header in response.Headers) { responseHeaders[header.Key] = header.Value.ToArray(); } if (response.Content == null) { return(TaskHelpers.Completed()); } else { // Trigger delayed content-length calculations before enumerating the headers. response.Content.Headers.ContentLength = response.Content.Headers.ContentLength; // Copy content headers foreach (KeyValuePair <string, IEnumerable <string> > contentHeader in response.Content.Headers) { responseHeaders[contentHeader.Key] = contentHeader.Value.ToArray(); } // Copy body Stream responseBody = environment.GetOwinValue <Stream>(OwinConstants.ResponseBodyKey); return(response.Content.CopyToAsync(responseBody)); } }
private static HttpRequestMessage CreateRequestMessage(IDictionary <string, object> environment, Stream requestBody) { string requestMethod = environment.GetOwinValue <string>(OwinConstants.RequestMethodKey); IDictionary <string, string[]> requestHeaders = environment.GetOwinValue <IDictionary <string, string[]> >(OwinConstants.RequestHeadersKey); string requestPathBase; Uri requestUri = CreateRequestUri(environment, requestHeaders, out requestPathBase); // Create the request HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(requestMethod), requestUri); // Set the body HttpContent content = new StreamContent(requestBody); request.Content = content; // Copy the headers foreach (KeyValuePair <string, string[]> header in requestHeaders) { if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value)) { bool success = request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); Contract.Assert(success, "Every header can be added either to the request headers or to the content headers"); } } // Map the OWIN environment keys to the request properties keys that Web API expects MapRequestProperties(request, environment, requestPathBase); return(request); }
// Implements the algorithm for reconstructing a URI according to section 5.4 of the OWIN specification private static Uri CreateRequestUri(IDictionary <string, object> environment, IDictionary <string, string[]> requestHeaders, out string requestPathBase) { StringBuilder uriBuilder = new StringBuilder(); // Append request scheme string requestScheme = environment.GetOwinValue <string>(OwinConstants.RequestSchemeKey); uriBuilder.Append(requestScheme); uriBuilder.Append("://"); // Append host and port string[] hostHeaderValues; if (requestHeaders.TryGetValue("Host", out hostHeaderValues) && hostHeaderValues.Length > 0) { uriBuilder.Append(hostHeaderValues[0]); } else { throw Error.InvalidOperation(SRResources.CreateRequestURI_MissingHostHeader); } // Append request path requestPathBase = environment.GetOwinValue <string>(OwinConstants.RequestPathBaseKey); uriBuilder.Append(requestPathBase); string requestPath = environment.GetOwinValue <string>(OwinConstants.RequestPathKey); uriBuilder.Append(requestPath); // Append query string string requestQueryString = environment.GetOwinValue <string>(OwinConstants.RequestQueryStringKey); if (requestQueryString.Length > 0) { uriBuilder.Append('?'); uriBuilder.Append(requestQueryString); } return(new Uri(uriBuilder.ToString(), UriKind.Absolute)); }
public async Task Invoke(IDictionary<string, object> environment) { if (environment == null) { throw new ArgumentNullException("environment"); } Stream requestBody = environment.GetOwinValue<Stream>(OwinConstants.RequestBodyKey); HttpRequestMessage request = CreateRequestMessage(environment, requestBody); if (!requestBody.CanSeek && _bufferPolicySelector.UseBufferedInputStream(hostContext: environment)) { await BufferRequestBodyAsync(environment, request.Content); } CancellationToken cancellationToken = environment.GetOwinValue<CancellationToken>(OwinConstants.CallCancelledKey); SetPrincipal(environment); HttpResponseMessage response = null; bool callNext = false; try { response = await _messageInvoker.SendAsync(request, cancellationToken); // Handle null responses if (response == null) { throw Error.InvalidOperation(SRResources.SendAsync_ReturnedNull); } // Handle soft 404s where no route matched - call the next component if (IsSoftNotFound(request, response)) { callNext = true; } else { if (response.Content != null && _bufferPolicySelector.UseBufferedOutputStream(response)) { response = await BufferResponseBodyAsync(request, response); } await SendResponseMessageAsync(environment, response); } } finally { // Note that the HttpRequestMessage is explicitly NOT disposed. Disposing it would close the input stream // and prevent cascaded components from accessing it. The server MUST handle any necessary cleanup upon // request completion. request.DisposeRequestResources(); if (response != null) { response.Dispose(); } } // Call the next component if no route matched if (callNext) { await _next.Invoke(environment); } }
private static Task SendResponseMessageAsync(IDictionary<string, object> environment, HttpResponseMessage response) { environment[OwinConstants.ResponseStatusCodeKey] = response.StatusCode; environment[OwinConstants.ResponseReasonPhraseKey] = response.ReasonPhrase; // Copy non-content headers IDictionary<string, string[]> responseHeaders = environment.GetOwinValue<IDictionary<string, string[]>>(OwinConstants.ResponseHeadersKey); foreach (KeyValuePair<string, IEnumerable<string>> header in response.Headers) { responseHeaders[header.Key] = header.Value.ToArray(); } if (response.Content == null) { return TaskHelpers.Completed(); } else { // Trigger delayed content-length calculations before enumerating the headers. response.Content.Headers.ContentLength = response.Content.Headers.ContentLength; // Copy content headers foreach (KeyValuePair<string, IEnumerable<string>> contentHeader in response.Content.Headers) { responseHeaders[contentHeader.Key] = contentHeader.Value.ToArray(); } // Copy body Stream responseBody = environment.GetOwinValue<Stream>(OwinConstants.ResponseBodyKey); return response.Content.CopyToAsync(responseBody); } }
// Implements the algorithm for reconstructing a URI according to section 5.4 of the OWIN specification private static Uri CreateRequestUri(IDictionary<string, object> environment, IDictionary<string, string[]> requestHeaders, out string requestPathBase) { StringBuilder uriBuilder = new StringBuilder(); // Append request scheme string requestScheme = environment.GetOwinValue<string>(OwinConstants.RequestSchemeKey); uriBuilder.Append(requestScheme); uriBuilder.Append("://"); // Append host and port string[] hostHeaderValues; if (requestHeaders.TryGetValue("Host", out hostHeaderValues) && hostHeaderValues.Length > 0) { uriBuilder.Append(hostHeaderValues[0]); } else { throw Error.InvalidOperation(SRResources.CreateRequestURI_MissingHostHeader); } // Append request path requestPathBase = environment.GetOwinValue<string>(OwinConstants.RequestPathBaseKey); uriBuilder.Append(requestPathBase); string requestPath = environment.GetOwinValue<string>(OwinConstants.RequestPathKey); uriBuilder.Append(requestPath); // Append query string string requestQueryString = environment.GetOwinValue<string>(OwinConstants.RequestQueryStringKey); if (requestQueryString.Length > 0) { uriBuilder.Append('?'); uriBuilder.Append(requestQueryString); } return new Uri(uriBuilder.ToString(), UriKind.Absolute); }
private static HttpRequestMessage CreateRequestMessage(IDictionary<string, object> environment, Stream requestBody) { string requestMethod = environment.GetOwinValue<string>(OwinConstants.RequestMethodKey); IDictionary<string, string[]> requestHeaders = environment.GetOwinValue<IDictionary<string, string[]>>(OwinConstants.RequestHeadersKey); string requestPathBase; Uri requestUri = CreateRequestUri(environment, requestHeaders, out requestPathBase); // Create the request HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(requestMethod), requestUri); // Set the body HttpContent content = new StreamContent(requestBody); request.Content = content; // Copy the headers foreach (KeyValuePair<string, string[]> header in requestHeaders) { if (!request.Headers.TryAddWithoutValidation(header.Key, header.Value)) { bool success = request.Content.Headers.TryAddWithoutValidation(header.Key, header.Value); Contract.Assert(success, "Every header can be added either to the request headers or to the content headers"); } } // Map the OWIN environment keys to the request properties keys that Web API expects MapRequestProperties(request, environment, requestPathBase); return request; }
public async Task Invoke(IDictionary <string, object> environment) { if (environment == null) { throw new ArgumentNullException("environment"); } Stream requestBody = environment.GetOwinValue <Stream>(OwinConstants.RequestBodyKey); HttpRequestMessage request = CreateRequestMessage(environment, requestBody); if (!requestBody.CanSeek && _bufferPolicySelector.UseBufferedInputStream(hostContext: environment)) { await BufferRequestBodyAsync(environment, request.Content); } CancellationToken cancellationToken = environment.GetOwinValue <CancellationToken>(OwinConstants.CallCancelledKey); SetPrincipal(environment); HttpResponseMessage response = null; bool callNext = false; try { response = await _messageInvoker.SendAsync(request, cancellationToken); // Handle null responses if (response == null) { throw Error.InvalidOperation(SRResources.SendAsync_ReturnedNull); } // Handle soft 404s where no route matched - call the next component if (IsSoftNotFound(request, response)) { callNext = true; } else { if (response.Content != null && _bufferPolicySelector.UseBufferedOutputStream(response)) { response = await BufferResponseBodyAsync(request, response); } await SendResponseMessageAsync(environment, response); } } finally { // Note that the HttpRequestMessage is explicitly NOT disposed. Disposing it would close the input stream // and prevent cascaded components from accessing it. The server MUST handle any necessary cleanup upon // request completion. request.DisposeRequestResources(); if (response != null) { response.Dispose(); } } // Call the next component if no route matched if (callNext) { await _next.Invoke(environment); } }
private static Task SendResponseMessageAsync(IDictionary<string, object> environment, HttpResponseMessage response) { environment[OwinConstants.ResponseStatusCodeKey] = response.StatusCode; environment[OwinConstants.ResponseReasonPhraseKey] = response.ReasonPhrase; // Copy non-content headers IDictionary<string, string[]> responseHeaders = environment.GetOwinValue<IDictionary<string, string[]>>(OwinConstants.ResponseHeadersKey); foreach (KeyValuePair<string, IEnumerable<string>> header in response.Headers) { responseHeaders[header.Key] = header.Value.ToArray(); } HttpContent responseContent = response.Content; if (responseContent == null) { // Set the content-length to 0 to prevent the server from sending back the response chunked responseHeaders["Content-Length"] = new string[] { "0" }; return TaskHelpers.Completed(); } else { // Copy content headers foreach (KeyValuePair<string, IEnumerable<string>> contentHeader in responseContent.Headers) { responseHeaders[contentHeader.Key] = contentHeader.Value.ToArray(); } // Copy body Stream responseBody = environment.GetOwinValue<Stream>(OwinConstants.ResponseBodyKey); return responseContent.CopyToAsync(responseBody); } }