public async Task TryDispatchHttpRequestAsync(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } try { (string username, string password) = ParseBasicAuthenticationHeader(httpContext.Request); if (username != null) { await _authorizationService.AuthorizeUser(httpContext, username, password).ConfigureAwait(false); } var channelIdentifier = await _authorizationService.GetChannelIdentifier(httpContext).ConfigureAwait(false); if (channelIdentifier == null) { httpContext.Response.Redirect("/Cloud/Account/Login"); return; } var requestMessage = await _cloudMessageFactory.Create(httpContext.Request).ConfigureAwait(false); var responseMessage = await Invoke(channelIdentifier, requestMessage, httpContext.RequestAborted).ConfigureAwait(false); var responseContent = _cloudMessageSerializer.Unpack <HttpResponseCloudMessageContent>(responseMessage.Payload); await PatchHttpResponseWithResponseFromDevice(httpContext.Response, responseContent).ConfigureAwait(false); } catch (OperationCanceledException) { } catch (Exception exception) { DefaultExceptionFilter.HandleException(exception, httpContext); } }
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); } }