private async void ProcessHttpRequest(System.Net.Http.HttpListenerContext context, CancellationToken cancellationToken) { bool isHandled = false; foreach (var routeHandler in RestServerRouteHandlers) { if (cancellationToken.IsCancellationRequested) { break; } isHandled = await routeHandler.HandleRouteAsync(context); if (isHandled) { break; } } if (!isHandled && _restServerRoute404Handler != null) { isHandled = await _restServerRoute404Handler.HandleRouteAsync(context); } if (!context.Response.IsClosed) { if (!isHandled) { context.Response.NotFound(); } context.Response.Close(); } }
private async Task ProcessHttpRequest(System.Net.Http.HttpListenerContext context) { foreach (var routeHandler in _restServerRouteHandlers) { var isHandled = await routeHandler.HandleRouteAsync(context); if (isHandled && !context.Response.IsClosed) { context.Response.Close(); return; } } if (!context.Response.IsClosed) { context.Response.NotFound(); context.Response.Close(); } }
public void HandleContext(System.Net.Http.HttpListenerContext context) { Task.Factory.StartNew(async() => await ProcessHttpRequest(context)); }
public async Task <bool> HandleRouteAsync(System.Net.Http.HttpListenerContext context) { string correlationId = Guid.NewGuid().ToString(); string route = MakeRoute(StripPort(context.Request.Url.AbsolutePath)); var restServerAction = GetActionForRoute(route); if (restServerAction == null) { return(false); } Log.i($"Handling route {route} - Correlation: {correlationId}"); object inputParameter = null; // make request parameter object. try { if (restServerAction.InputType != null) { string requestString = await context.Request.ReadContentAsStringAsync(); if (!string.IsNullOrEmpty(requestString)) { inputParameter = _converter.DeserializeObject(requestString, restServerAction.InputType); } } } catch (ConverterException ex) { Log.i($"{correlationId} Routing failed, Bad Request."); context.Response.ReasonPhrase = "Bad Request - " + ex.Message; context.Response.StatusCode = 400; context.Response.Close(); return(true); } // make response. context.Response.Headers.ContentType.Clear(); context.Response.Headers.ContentType.Add(_converter.ContentType); object result = null; try { result = await restServerAction.Execute(context, inputParameter); } catch (Exception ex) { Log.i($"{correlationId} Routing failed, {route} action failed. Message: {ex.Message}"); context.Response.InternalServerError(); context.Response.Close(); return(true); } if (result != null) { try { string json = _converter.SerializeObject(result); await context.Response.WriteContentAsync(json); } catch (ConverterException ex) { Log.i($"{correlationId} Routing failed, {route} result serialization failed Type={result?.GetType().FullName ?? "NULL"}. Message: {ex.Message}"); context.Response.InternalServerError(); context.Response.Close(); return(true); } } Log.i($"{correlationId} Routing ended gracefully. {route}"); return(true); }
public async Task <bool> HandleRouteAsync(System.Net.Http.HttpListenerContext context) { string correlationId = Guid.NewGuid().ToString(); string route = MakeRoute(StripPort(context.Request.Url.AbsolutePath)); var restServerAction = GetActionForRoute(route); if (restServerAction == null) { return(false); } _logger.Info($"Handling route {route} - Correlation: {correlationId}"); object[] inputParameter = null; try { if (restServerAction.InputType != null) { string requestString = await context.Request.ReadContentAsStringAsync(); if (restServerAction.IsBodyRequested && restServerAction.IsParameterized) { inputParameter = new object[2] { null, requestString } } ; else { inputParameter = new object[1]; } if (!string.IsNullOrEmpty(requestString)) { if (restServerAction.InputType == typeof(string)) { inputParameter[0] = requestString; } else if (!restServerAction.IsBodyRequested) { inputParameter[0] = JsonConvert.DeserializeObject(requestString, restServerAction.InputType); } } if (restServerAction.IsParameterized) { if (inputParameter[0] == null) { inputParameter[0] = Activator.CreateInstance(restServerAction.InputType); } ExposedRestServerActionCompiledParameters.FillParameters(inputParameter[0], route, restServerAction.CompiledParameters); } } } catch (FormatException ex) // <-- CHANGE EXCEPTION TYPE!!!!1!11 { _logger.Info($"{correlationId} Routing failed, Bad Request."); context.Response.ReasonPhrase = "Bad Request - " + ex.Message; context.Response.StatusCode = 400; context.Response.Close(); return(true); } catch (JsonException ex) { _logger.Info($"{correlationId} Routing failed, Bad Request."); context.Response.ReasonPhrase = "Bad Request - " + ex.Message; context.Response.StatusCode = 400; context.Response.Close(); return(true); } object result = null; // make json response. context.Response.Headers.ContentType.Clear(); context.Response.Headers.ContentType.Add("application/json"); try { result = await restServerAction.Execute(context, inputParameter); } catch (Exception ex) { _logger.Info($"{correlationId} Routing failed, {route} action failed. Message: {ex.Message}"); context.Response.InternalServerError(); context.Response.Close(); return(true); } if (result != null) { try { string json = JsonConvert.SerializeObject(result); await context.Response.WriteContentAsync(json); } catch (JsonException ex) { _logger.Info($"{correlationId} Routing failed, {route} result serialization failed Type={result?.GetType().FullName ?? "NULL"}. Message: {ex.Message}"); context.Response.InternalServerError(); context.Response.Close(); return(true); } } _logger.Info($"{correlationId} Routing ended gracefully. {route}"); return(true); }
public void HandleContext(System.Net.Http.HttpListenerContext context, CancellationToken cancellationToken) { Task.Factory.StartNew(() => ProcessHttpRequest(context, cancellationToken)); }