Beispiel #1
0
        public PodeContext(HttpContext context, PodeListener listener)
        {
            ID        = PodeHelpers.NewGuid();
            Context   = context;
            Listener  = listener;
            Timestamp = DateTime.UtcNow;

            Request  = new PodeRequest(context.Request, this);
            Response = new PodeResponse(context.Response, this);

            PodeSocket = Listener.FindSocket(Request.LocalEndpoint);
            if (PodeSocket != default(PodeSocket) && !PodeSocket.CheckHostname(Request.Host))
            {
                Request.Error = new HttpRequestException($"Invalid request Host: {Request.Host}");
            }

            ContextCancellationToken = new CancellationTokenSource();
            ContextTask = new Task(() => {
                try
                {
                    var _task = Task.Delay(30000, ContextCancellationToken.Token);
                    _task.Wait();
                    Response.Close();
                    ContextCancellationToken.Cancel();
                }
                catch {}
            });
        }
Beispiel #2
0
        public PodeContext(HttpContext context, PodeListener listener)
        {
            // set id/timestamp
            ID        = PodeHelpers.NewGuid();
            Context   = context;
            Listener  = listener;
            Timestamp = DateTime.UtcNow;

            // build req/resp mappers
            Request  = new PodeRequest(context.Request, this);
            Response = new PodeResponse(context.Response, this);

            // is the body too big?
            if (Request.ContentLength > Listener.RequestBodySize)
            {
                Response.StatusCode = 413;
                Request.Error       = new HttpRequestException("Payload too large");
                Request.Error.Data.Add("PodeStatusCode", 413);
            }

            // check hostname
            PodeSocket = Listener.FindSocket(Request.LocalEndpoint);
            if (PodeSocket != default(PodeSocket) && !PodeSocket.CheckHostname(Request.Host))
            {
                Request.Error = new HttpRequestException($"Invalid request Host: {Request.Host}");
            }

            // configure req timeout
            if (ContextCancellationToken != default(CancellationTokenSource))
            {
                ContextCancellationToken.Dispose();
            }

            ContextCancellationToken = new CancellationTokenSource();

            if (ContextTask != default(Task))
            {
                ContextTask.Dispose();
            }

            ContextTask = new Task(() => {
                try
                {
                    var _task = Task.Delay(Listener.RequestTimeout * 1000, ContextCancellationToken.Token);
                    _task.Wait();

                    Response.StatusCode = 408;
                    Request.Error       = new HttpRequestException("Request timeout");
                    Request.Error.Data.Add("PodeStatusCode", 408);

                    this.Dispose();
                }
                catch {}
            });
        }
Beispiel #3
0
        public static void WriteException(Exception ex, PodeListener listener = default(PodeListener), PodeLoggingLevel level = PodeLoggingLevel.Error)
        {
            if (ex == default(Exception))
            {
                return;
            }

            if (listener != default(PodeListener) && (!listener.ErrorLoggingEnabled || !listener.ErrorLoggingLevels.Contains(level.ToString(), StringComparer.InvariantCultureIgnoreCase)))
            {
                return;
            }

            Console.WriteLine($"[{level}] {ex.GetType().Name}: {ex.Message}");
            Console.WriteLine(ex.StackTrace);
        }
Beispiel #4
0
        public static void WriteException(Exception ex, PodeListener listener = default(PodeListener))
        {
            if (ex == default(Exception))
            {
                return;
            }

            if (listener != default(PodeListener) && !listener.ErrorLoggingEnabled)
            {
                return;
            }

            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
        }