Example #1
0
        private static void ProcessRequest(EndpointHandler endpointHandler, EndpointHandlerRegister handlerRegister, TcpClient client)
        {
            try
            {
                NetworkStream requestStream = client.GetStream();

                Console.WriteLine($"Nachricht erhalten um: {DateTime.Now} Client: {client.Client.RemoteEndPoint}");
                Console.WriteLine("-----------------------------------------------------------------------------");

                HttpRequestParser requestParser = new HttpRequestParser();
                RequestContext    context;

                context = requestParser.ParseRequestStream(requestStream);

                Console.WriteLine("-----------------------------------------------------------------------------");
                Console.WriteLine();

                RouteMatch match = handlerRegister.GetEndPointHandler(context);

                if (match != null)
                {
                    IActionResult result = endpointHandler.Invoke(match, client, context);
                    result.Execute();
                }
                else
                {
                    HttpStatusCodeResult.NotFound(client)
                    .Execute();
                }

                client.Close();
            }
            catch (HttpRequestParserException parserEx)
            {
                string parseErrorMessage = $"Ungültiger Request, parsen nicht möglich: {parserEx.Message}";

                Console.WriteLine(parseErrorMessage);
                HttpStatusCodeResult.BadRequest(client, parseErrorMessage)
                .Execute();
            }
            catch (Exception ex) when(ex is EndPointHandlerException || ex is EndpointHandlerRegisterException)
            {
                HttpStatusCodeResult.BadRequest(client, ex.GetFullMessage(verbose: true))
                .Execute();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unerwarteter Fehler: {ex.Message}");

                HttpStatusCodeResult.InternalServerError(client)
                .Execute();
            }
            finally
            {
                client.Close();
            }
        }
Example #2
0
 protected IActionResult BadRequest(string errorMessage = null)
 {
     return(HttpStatusCodeResult.BadRequest(client, errorMessage));
 }