public async Task ForwardHttpRequestAsync(HttpContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } try { context.Response.Headers.Add("Wirehome-Cloud-Enter", new StringValues(DateTime.UtcNow.ToString("O"))); var authorizationContext = _authorizationService.AuthorizeHttpCall(context); var requestContent = new HttpRequestMessageContent { Method = context.Request.Method, Uri = context.Request.Path + context.Request.QueryString, Content = LoadContent(context.Request) }; if (!string.IsNullOrEmpty(context.Request.ContentType)) { requestContent.Headers.Add("Content-Type", context.Request.ContentType); } var requestMessage = _messageFactory.CreateMessage(CloudMessageType.HttpInvoke, requestContent); var responseMessage = await Invoke(authorizationContext, requestMessage, context.RequestAborted).ConfigureAwait(false); var responseContent = responseMessage.Content.ToObject <HttpResponseMessageContent>(); context.Response.StatusCode = responseContent.StatusCode; foreach (var header in responseContent.Headers) { context.Response.Headers.Add(header.Key, new StringValues(header.Value)); } context.Response.Headers.Add("Wirehome-Cloud-Exit", new StringValues(DateTime.UtcNow.ToString("O"))); if (responseContent.Content?.Length > 0) { context.Response.Body.Write(responseContent.Content); } } catch (OperationCanceledException) { } catch (UnauthorizedAccessException) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } catch (SessionNotFoundException) { context.Response.StatusCode = (int)HttpStatusCode.GatewayTimeout; } }
public async Task TryDispatchHttpRequestAsync(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } try { var deviceSessionIdentifier = httpContext.GetDeviceSessionIdentifier(); if (deviceSessionIdentifier == null) { httpContext.Response.Redirect("/Cloud/Account/Login"); return; } var requestContent = new HttpRequestMessageContent { Method = httpContext.Request.Method, Uri = httpContext.Request.Path + httpContext.Request.QueryString, Content = LoadContent(httpContext.Request) }; if (!string.IsNullOrEmpty(httpContext.Request.ContentType)) { requestContent.Headers = new Dictionary <string, string> { ["Content-Type"] = httpContext.Request.ContentType }; } var requestMessage = new CloudMessage { Type = CloudMessageType.HttpInvoke }; requestMessage.SetContent(requestContent); var responseMessage = await Invoke(deviceSessionIdentifier, requestMessage, httpContext.RequestAborted).ConfigureAwait(false); var responseContent = responseMessage.GetContent <HttpResponseMessageContent>(); httpContext.Response.StatusCode = responseContent.StatusCode ?? 200; if (responseContent.Headers?.Any() == true) { foreach (var header in responseContent.Headers) { httpContext.Response.Headers.Add(header.Key, new StringValues(header.Value)); } } if (responseContent.Content?.Length > 0) { httpContext.Response.Body.Write(responseContent.Content); } } catch (OperationCanceledException) { } catch (Exception exception) { DefaultExceptionFilter.HandleException(exception, httpContext); } }