private async Task <InterceptedResponse> GetResponseFromLocalTargetAsync(IOnPremiseTargetConnector connector, string url, IOnPremiseTargetRequest onPremiseTargetRequest, string relayedRequestHeader) { var request = new InterceptedRequest(onPremiseTargetRequest); try { var requestInterceptor = _onPremiseInterceptorFactory.CreateOnPremiseRequestInterceptor(); requestInterceptor?.HandleRequest(request); } catch (Exception ex) { _logger?.Error(ex, "Error during intercepting the request. request-id={RequestId}", request.RequestId); } var onPremiseTargetResponse = await connector.GetResponseFromLocalTargetAsync(url, request, relayedRequestHeader).ConfigureAwait(false); var response = new InterceptedResponse(onPremiseTargetResponse); try { var responseInterceptor = _onPremiseInterceptorFactory.CreateOnPremiseResponseInterceptor(); responseInterceptor?.HandleResponse(request, response); } catch (Exception ex) { _logger?.Error(ex, "Error during intercepting the response. request-id={RequestId}", request.RequestId); } return(response); }
private async Task RequestLocalTargetAsync(RequestContext ctx, string key, IOnPremiseTargetConnector connector, IOnPremiseTargetRequestInternal request, CancellationToken cancellationToken) { _logger?.Debug("Relaying request to local target. request-id={RequestId}, request-url={RequestUrl}", request.RequestId, request.Url); var url = (request.Url.Length > key.Length) ? request.Url.Substring(key.Length + 1) : String.Empty; if (request.Body != null) { if (request.Body.Length == 0) { // a length of 0 indicates that there is a larger body available on the server _logger?.Verbose("Requesting body. request-id={RequestId}", request.RequestId); // request the body from the RelayServer (because SignalR cannot handle large messages) var webResponse = await GetToRelay("/request/" + request.RequestId, cancellationToken).ConfigureAwait(false); request.Stream = await webResponse.Content.ReadAsStreamAsync().ConfigureAwait(false); // this stream should not be disposed (owned by the Framework) } else { // the body is small enough to be used directly request.Stream = new MemoryStream(request.Body); } } else { // no body available (e.g. GET request) request.Stream = Stream.Null; } using (var response = await connector.GetResponseFromLocalTargetAsync(url, request, RelayedRequestHeader).ConfigureAwait(false)) { if (response.Stream == null) { response.Stream = Stream.Null; } _logger?.Debug("Sending response. request-id={RequestId}", request.RequestId); try { // transfer the result to the RelayServer (need POST here, because SignalR does not handle large messages) await PostResponseAsync(ctx, response, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger?.Error(ex, "Error during posting local target response to relay. request-id={RequestId}", request.RequestId); } } }