Esempio n. 1
0
        public bool ProcessRequest(HttpContext context)
        {
            SimpleHttpRequest request = new SimpleHttpRequest(
                context.Request.HttpMethod, context.Request.RawUrl, context.Request.Headers, context.Request.InputStream);
            IISResponse response = new IISResponse(context.Response);

            return _handler.ProcessRequest(request, response);
        }
        bool PerformRequest(SimpleHttpRequest request, SimpleHttpResponse response)
        {
            IRequestHandler[] handlers;

            if (!_handlers.TryGetValue(request.HttpMethod, out handlers))
                handlers = new[] { HttpErrorHandler.MethodNotAllowed };

            using (Log.Start("{0} {1}", request.HttpMethod, request.RawUrl))
            using (_content.BeginRequest())
            {
                IRequestHandler handler = 
                    handlers.FirstOrDefault(h => h.IsMatch(request.HttpMethod, request.RawUrl));

                if (handler == null)
                    return false;

                return PerformRequest(request, response, handler);
            }
        }
 public bool ProcessRequest(SimpleHttpRequest request, SimpleHttpResponse response)
 {
     LogConfig.Configure();
     try
     {
         return PerformRequest(request, response);
     }
     catch (CorruptApplicationDomainException ae)
     {
         try
         {
             Log.Error(ae);
         }
         finally { Environment.Exit(1); }
         throw;
     }
     catch (Exception error)
     {
         Log.Error(error);
         throw;
     }
 }
        private bool PerformRequest(SimpleHttpRequest request, SimpleHttpResponse response, IRequestHandler handler)
        {
            using (IContentResponse page = handler.GetResponse(
                request.HttpMethod, request.RawUrl, request.Headers, request.InputStream))
            {
                response.StatusCode = page.Status;

                if (page.Status == HttpStatusCode.Redirect || page.Status == HttpStatusCode.Moved ||
                    page.Status == HttpStatusCode.SeeOther)
                {
                    response.RedirectLocation = page.Redirect;
                }

                if (!String.IsNullOrEmpty(page.ETag) && page.LastModified > new DateTime(1900, 1, 1))
                {
                    response.LastModified = page.LastModified;
                    response.ETag = page.ETag;

                    if ((!String.IsNullOrEmpty(request.Headers["If-None-Match"]) &&
                        request.Headers["If-None-Match"].Trim('"') == page.ETag)
                        ||
                        (!String.IsNullOrEmpty(request.Headers["If-Modified-Since"]) &&
                        request.Headers["If-Modified-Since"] == page.LastModified.ToString("r")))
                    {
                        response.StatusCode = HttpStatusCode.NotModified;
                        return true;
                    }
                }
                if (page.Status == HttpStatusCode.OK && (request.HttpMethod == "GET" || request.HttpMethod == "HEAD"))
                {
                    string pb = String.Format("http://{0}{1}", request.Headers["Host"], PingbackHandler.PingbackUrl);
                    response.AddHeader("X-Pingback", pb);
                }

                if (page.ContentLength <= 0)
                {
                    response.ContentLength = 0;
                }
                else
                {
                    response.ContentType = page.ContentType;
                    bool decompress = page.Compressed;

                    if (decompress &&
                        (request.Headers["Accept-Encoding"] ?? "").IndexOf("gzip", StringComparison.OrdinalIgnoreCase) >=
                        0)
                    {
                        decompress = false;
                        response.ContentEncoding = "gzip";
                    }
                    else if (decompress &&
                             (request.Headers["TE"] ?? "").IndexOf("gzip", StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        decompress = false;
                        response.TransferEncoding = "gzip";
                    }

                    int contentLength = (page.Compressed && !decompress) ? page.CompressedLength : page.ContentLength;
                    response.ContentLength = contentLength;

                    if (request.HttpMethod == "HEAD")
                    {
                        //no-content
                    }
                    else if (page.Compressed && decompress)
                    {
                        response.Write(page.ContentBytes, 0, contentLength);
                    }
                    else if (page.HasFile)
                    {
                        response.TransmitFile(page.ContentFile, 0, contentLength);
                    }
                    else if (page.Compressed)
                    {
                        response.Write(page.CompressedBytes, 0, contentLength);
                    }
                    else
                    {
                        response.Write(page.ContentBytes, 0, contentLength);
                    }
                }
                return true;
            }
        }