private async Task HandleRequest(string json, ILogger logger) { var request = RequestPacket.Parse(json); if (logger.IsEnabled(LogLevel.Debug)) { LogRequest(json, logger, LogLevel.Debug); } var response = request.Reply(); try { if (!request.Command.StartsWith("/")) { request.Command = $"/{request.Command}"; } // hand off request to next layer if (_endpointHandlers.TryGetValue(request.Command, out var handler)) { var result = await handler.Value.Handle(request); response.Body = result; return; } throw new NotSupportedException($"Command '{request.Command}' is not supported."); } catch (Exception e) { if (e is AggregateException aggregateEx) { e = aggregateEx.Flatten().InnerException; } // updating the response object here so that the ResponseStream // prints the latest state when being closed response.Success = false; response.Message = JsonConvert.ToString(e.ToString(), '"', StringEscapeHandling.Default); } finally { // response gets logged when Debug or more detailed log level is enabled // or when we have unsuccessful response (exception) if (logger.IsEnabled(LogLevel.Debug) || !response.Success) { // if logging is at Debug level, request would have already been logged // however not for higher log levels, so we want to explicitly log the request too if (!logger.IsEnabled(LogLevel.Debug)) { LogRequest(json, logger, LogLevel.Warning); } LogResponse(response.ToString(), logger, response.Success); } // actually write it _writer.WriteLine(response); } }
private async Task HandleRequest <TContext>(string json, IHttpApplication <TContext> application) { var request = RequestPacket.Parse(json); var response = request.Reply(); using (var inputStream = request.ArgumentsStream) using (var outputStream = new MemoryStream()) { try { var features = new FeatureCollection(); var requestFeature = new RequestFeature(); var responseFeature = new ResponseFeature(); requestFeature.Path = request.Command; requestFeature.Body = inputStream; requestFeature.Headers["Content-Type"] = new[] { "application/json" }; responseFeature.Body = outputStream; features.Set <IHttpRequestFeature>(requestFeature); features.Set <IHttpResponseFeature>(responseFeature); var context = application.CreateContext(features); // hand off request to next layer await application.ProcessRequestAsync(context); if (responseFeature.StatusCode != 200) { response.Success = false; } // HttpResponse stream becomes body as is var buffer = outputStream.ToArray(); if (buffer.Length > 0) { response.Body = new JRaw(new String(Encoding.UTF8.GetChars(buffer, 0, buffer.Length))); } } catch (Exception e) { // updating the response object here so that the ResponseStream // prints the latest state when being closed response.Success = false; response.Message = JsonConvert.ToString(e.ToString(), '"', StringEscapeHandling.Default); } finally { // actually write it _writer.WriteLine(response); } } }
private async Task HandleRequest(string json) { var request = RequestPacket.Parse(json); var response = request.Reply(); using (var inputStream = request.ArgumentsStream) using (var outputStream = new MemoryStream()) { try { var httpRequest = new RequestFeature(); httpRequest.Path = request.Command; httpRequest.Body = inputStream; httpRequest.Headers["Content-Type"] = new[] { "application/json" }; var httpResponse = new ResponseFeature(); httpResponse.Body = outputStream; var collection = new FeatureCollection(); collection[typeof(IHttpRequestFeature)] = httpRequest; collection[typeof(IHttpResponseFeature)] = httpResponse; // hand off request to next layer await _next(collection); if (httpResponse.StatusCode != 200) { response.Success = false; } // HttpResponse stream becomes body as is var buffer = outputStream.ToArray(); if (buffer.Length > 0) { response.Body = new JRaw(new String(Encoding.UTF8.GetChars(buffer, 0, buffer.Length))); } } catch (Exception e) { // updating the response object here so that the ResponseStream // prints the latest state when being closed response.Success = false; response.Message = e.ToString(); } finally { // actually write it _writer.WriteLine(response); } } }
private async Task HandleRequest(string json, ILogger logger) { var request = RequestPacket.Parse(json); if (logger.IsEnabled(LogLevel.Debug)) { LogRequest(json, logger); } var response = request.Reply(); try { if (!request.Command.StartsWith("/")) { request.Command = $"/{request.Command}"; } // hand off request to next layer if (_endpointHandlers.TryGetValue(request.Command, out var handler)) { var result = await handler.Value.Handle(request); response.Body = result; return; } throw new NotSupportedException($"Command '{request.Command}' is not supported."); } catch (Exception e) { if (e is AggregateException aggregateEx) { e = aggregateEx.Flatten().InnerException; } // updating the response object here so that the ResponseStream // prints the latest state when being closed response.Success = false; response.Message = JsonConvert.ToString(e.ToString(), '"', StringEscapeHandling.Default); } finally { if (logger.IsEnabled(LogLevel.Debug)) { LogResponse(response.ToString(), logger); } // actually write it _writer.WriteLine(response); } }