Esempio n. 1
0
        public HttpRequestImpl(HttpContextImpl context, IHttpChannel channel, HttpServerSettings settings)
        {
            _context = context;

            Method = channel.Method;

            var path = channel.Path;
            if (!path.StartsWith("/")) path = "/" + path;
            Uri = new Uri(string.Format("http://localhost:{0}{1}", settings.Port, path));

            _headers.AddRange(
                from key in channel.Headers.AllKeys
                select new KeyValuePair<string, string>(key, channel.Headers[key])
                );

            var len = _headers.GetContentLength();
            Body = len > 0 ? channel.Body : new MemoryStream(new byte[0], false);
        }
        public static void ProcessRequest(this IHttpChannel channel, IHttpHandler handler, HttpServerSettings settings)
        {
            var context = new HttpContextImpl(channel, settings);
            var res = context.Response;

            using (res.OutputStream)
            {
                try
                {
                    var app = handler as ExpressApplication;
                    if (app != null)
                    {
                        if (!app.Process(context))
                        {
                            res.StatusCode = (int)HttpStatusCode.NotFound;
                            res.StatusDescription = "Not found";
                            res.ContentType = "text/plain";
                            res.Write("Resource not found!");
                        }

                        res.Flush();
                        res.End();
                    }
                    else
                    {
                        var workerRequest = new HttpWorkerRequestImpl(context, settings);
                        handler.ProcessRequest(new HttpContext(workerRequest));
                        workerRequest.EndOfRequest();
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);

                    res.StatusCode = (int)HttpStatusCode.InternalServerError;
                    res.ContentType = "text/plain";
                    res.Write(e.ToString());
                }
            }
        }
Esempio n. 3
0
        public static void ProcessRequest(this IHttpChannel channel, IHttpHandler handler, HttpServerSettings settings)
        {
            var context = new HttpContextImpl(channel, settings);
            var res     = context.Response;

            using (res.OutputStream)
            {
                try
                {
                    var app = handler as ExpressApplication;
                    if (app != null)
                    {
                        if (!app.Process(context))
                        {
                            res.StatusCode        = (int)HttpStatusCode.NotFound;
                            res.StatusDescription = "Not found";
                            res.ContentType       = "text/plain";
                            res.Write("Resource not found!");
                        }

                        res.Flush();
                        res.End();
                    }
                    else
                    {
                        var workerRequest = new HttpWorkerRequestImpl(context, settings);
                        handler.ProcessRequest(new HttpContext(workerRequest));
                        workerRequest.EndOfRequest();
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e);

                    res.StatusCode  = (int)HttpStatusCode.InternalServerError;
                    res.ContentType = "text/plain";
                    res.Write(e.ToString());
                }
            }
        }
Esempio n. 4
0
        public HttpRequestImpl(HttpContextImpl context, IHttpChannel channel, HttpServerSettings settings)
        {
            _context = context;

            Method = channel.Method;

            var path = channel.Path;

            if (!path.StartsWith("/"))
            {
                path = "/" + path;
            }
            Uri = new Uri(string.Format("http://localhost:{0}{1}", settings.Port, path));

            _headers.AddRange(
                from key in channel.Headers.AllKeys
                select new KeyValuePair <string, string>(key, channel.Headers[key])
                );

            var len = _headers.GetContentLength();

            Body = len > 0 ? channel.Body : new MemoryStream(new byte[0], false);
        }