Ejemplo n.º 1
0
        private bool CheckFileCache(HttpHandlerContext context, string filename)
        {
#if (CACHECONTROL)
            var connection = context.Connection;
            var request    = connection.ListenerContext.Request;

            var finfo          = new FileInfo(filename);
            var nochacecontrol = request.Headers["Cache-Control"];
            if (!string.IsNullOrEmpty(nochacecontrol) && nochacecontrol.IndexOf("no-cache") != -1)
            {
                return(false);
            }
            if (!string.IsNullOrEmpty(nochacecontrol) && nochacecontrol.IndexOf("only-if-cached") != -1)
            {
                return(true);
            }
            var ifModifiedScince = request.Headers["If-Modified-Since"];

            if (!string.IsNullOrEmpty(ifModifiedScince))
            {
                DateTime cacheModified;
                if (DateTime.TryParseExact(ifModifiedScince, "R", CultureInfo.InvariantCulture, DateTimeStyles.None, out cacheModified) ||
                    DateTime.TryParseExact(ifModifiedScince, "ddd, dd MMM yyyy hh", CultureInfo.InvariantCulture, DateTimeStyles.None, out cacheModified))
                {
                    if (cacheModified.Date >= finfo.LastWriteTimeUtc.Date)
                    {
                        connection.WriteEntireResponseFromString(304, null, "", false);
                        return(true);
                    }
                }
            }

            //Split req etag
            var requestEtag = request.Headers["If-None-Match"];
            var etag        = string.Format("\"{0}.{1}\"", finfo.LastWriteTimeUtc.ToFileTimeUtc(), finfo.Length);
            if (!string.IsNullOrEmpty(requestEtag))
            {
                string[] reqetags = requestEtag.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var reqetag in reqetags)
                {
                    if (etag.Equals(reqetag))
                    {
                        connection.WriteEntireResponseFromString(304, null, "", false);
                        return(true);
                    }
                }
            }
#if (ETAG)
            SendUnknownResponseHeader("Etag", etag);
#endif
            connection.ListenerContext.Response.Headers[HttpResponseHeader.LastModified] = finfo.LastWriteTimeUtc.ToString("R");
            connection.ListenerContext.Response.Headers[HttpResponseHeader.Expires]      = DateTime.UtcNow.AddDays(1).ToString("R");
#endif
            return(false);
        }
Ejemplo n.º 2
0
        private bool CheckFileCache(HttpHandlerContext context, string filename)
        {
#if (CACHECONTROL)
            var connection = context.Connection;
            var request = connection.ListenerContext.Request;

            var finfo = new FileInfo(filename);
            var nochacecontrol = request.Headers["Cache-Control"];
            if (!string.IsNullOrEmpty(nochacecontrol) && nochacecontrol.IndexOf("no-cache") != -1)
            {
                return false;
            }
            if (!string.IsNullOrEmpty(nochacecontrol) && nochacecontrol.IndexOf("only-if-cached") != -1)
            {
                return true;
            }
            var ifModifiedScince = request.Headers["If-Modified-Since"];

            if (!string.IsNullOrEmpty(ifModifiedScince))
            {
                DateTime cacheModified;
                if (DateTime.TryParseExact(ifModifiedScince, "R", CultureInfo.InvariantCulture, DateTimeStyles.None, out cacheModified) ||
                    DateTime.TryParseExact(ifModifiedScince, "ddd, dd MMM yyyy hh", CultureInfo.InvariantCulture, DateTimeStyles.None, out cacheModified))
                {
                    if (cacheModified.Date >= finfo.LastWriteTimeUtc.Date)
                    {
                        connection.WriteEntireResponseFromString(304, null, "", false);
                        return true;
                    }
                }
            }

            //Split req etag
            var requestEtag = request.Headers["If-None-Match"];
            var etag = string.Format("\"{0}.{1}\"", finfo.LastWriteTimeUtc.ToFileTimeUtc(), finfo.Length);
            if (!string.IsNullOrEmpty(requestEtag))
            {
                string[] reqetags = requestEtag.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var reqetag in reqetags)
                {
                    if (etag.Equals(reqetag))
                    {
                        connection.WriteEntireResponseFromString(304, null, "", false);
                        return true;
                    }
                }
            }
#if (ETAG)
            SendUnknownResponseHeader("Etag", etag);
#endif
            connection.ListenerContext.Response.Headers[HttpResponseHeader.LastModified] = finfo.LastWriteTimeUtc.ToString("R");
            connection.ListenerContext.Response.Headers[HttpResponseHeader.Expires] = DateTime.UtcNow.AddDays(1).ToString("R");
#endif
            return false;
        }
Ejemplo n.º 3
0
        private string GetFileName(HttpHandlerContext context)
        {
            var virtRoot = context.Server.VirtualPath;
            var physRoot = context.Server.PhysicalPath;
            var virtPath = context.Connection.ListenerContext.Request.Url.LocalPath;

            if (virtPath.StartsWith(virtRoot, StringComparison.OrdinalIgnoreCase))
            {
                virtPath = virtPath.Substring(virtRoot.Length).Replace('/', '\\');
                return(Path.Combine(physRoot, virtPath));
            }
            return(null);
        }
Ejemplo n.º 4
0
        private static IEnumerable <IHttpHandler> GetHandlers(HttpHandlerContext context)
        {
            var extension = Path.GetExtension(context.Connection.ListenerContext.Request.Url.AbsolutePath);

            if (extension != null && extension.StartsWith("."))
            {
                extension = extension.Substring(1);
                IHttpHandler handler;
                if (handlers.TryGetValue(extension, out handler))
                {
                    yield return(handler);
                }
            }
            yield return(DefaultHttpHandler);

            yield return(AspNetHandler);

            yield return(DirectoryHttpHandler);
        }
Ejemplo n.º 5
0
        public void ProcessRequest(HttpHandlerContext context)
        {
            try
            {
                if (IsInRestrictDir(context.Connection.ListenerContext.Request.Url.LocalPath))
                {
                    context.Connection.WriteErrorWithExtraHeadersAndKeepAlive(403, null);
                    return;
                }
                var filename = GetFileName(context);
                if (string.IsNullOrEmpty(filename) || !File.Exists(filename))
                {
                    context.Connection.WriteErrorWithExtraHeadersAndKeepAlive(404, null);
                    return;
                }

                if (!CheckFileCache(context, filename))
                {
                    using (var f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var keepAlive = string.CompareOrdinal(context.Connection.ListenerContext.Request.Headers["Connection"], "keep-alive") == 0;
                        var compress  = false;
#if (GZIP)
                        if (compressedExt.Contains(Path.GetExtension(filename)))
                        {
                            var acceptEncoding = context.Connection.ListenerContext.Request.Headers["Accept-Encoding"];
                            if (!string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.Contains("gzip"))
                            {
                                compress = true;
                            }
                        }
#endif
                        context.Connection.WriteEntireResponseFromFile(filename, keepAlive, compress);
                    }
                }
            }
            catch (Exception)
            {
                context.Connection.WriteErrorAndClose(500);
            }
        }
Ejemplo n.º 6
0
        public void ProcessRequest(HttpHandlerContext context)
        {
            try
            {
                if (IsInRestrictDir(context.Connection.ListenerContext.Request.Url.LocalPath))
                {
                    context.Connection.WriteErrorWithExtraHeadersAndKeepAlive(403, null);
                    return;
                }
                var filename = GetFileName(context);
                if (string.IsNullOrEmpty(filename) || !File.Exists(filename))
                {
                    context.Connection.WriteErrorWithExtraHeadersAndKeepAlive(404, null);
                    return;
                }

                if (!CheckFileCache(context, filename))
                {
                    using (var f = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var keepAlive = string.CompareOrdinal(context.Connection.ListenerContext.Request.Headers["Connection"], "keep-alive") == 0;
                        var compress = false;
#if (GZIP)
                        if (compressedExt.Contains(Path.GetExtension(filename)))
                        {
                            var acceptEncoding = context.Connection.ListenerContext.Request.Headers["Accept-Encoding"];
                            if (!string.IsNullOrEmpty(acceptEncoding) && acceptEncoding.Contains("gzip"))
                                compress = true;
                        }
#endif
                        context.Connection.WriteEntireResponseFromFile(filename, keepAlive, compress);
                    }
                }
            }
            catch (Exception)
            {
                context.Connection.WriteErrorAndClose(500);
            }
        }
Ejemplo n.º 7
0
 public bool CanHandle(HttpHandlerContext context)
 {
     /*asp.net can handle all requests*/
     return true;
 }
Ejemplo n.º 8
0
 public void ProcessRequest(HttpHandlerContext context)
 {
     context.Host.ProcessRequest(context.Connection, context.Identity);
 }
Ejemplo n.º 9
0
 private static IEnumerable<IHttpHandler> GetHandlers(HttpHandlerContext context)
 {
     var extension = Path.GetExtension(context.Connection.ListenerContext.Request.Url.AbsolutePath);
     if (extension != null && extension.StartsWith("."))
     {
         extension = extension.Substring(1);
         IHttpHandler handler;
         if (handlers.TryGetValue(extension, out handler))
         {
             yield return handler;
         }
     }
     yield return DefaultHttpHandler;
     yield return AspNetHandler;
     yield return DirectoryHttpHandler;
 }
Ejemplo n.º 10
0
 public static IHttpHandler GetHttpHandler(HttpHandlerContext context)
 {
     return GetHandlers(context).FirstOrDefault(x=>x.CanHandle(context)) ?? DirectoryHttpHandler;
 }
Ejemplo n.º 11
0
 private string GetFileName(HttpHandlerContext context)
 {
     var virtRoot = context.Server.VirtualPath;
     var physRoot = context.Server.PhysicalPath;
     var virtPath = context.Connection.ListenerContext.Request.Url.LocalPath;
     if (virtPath.StartsWith(virtRoot, StringComparison.OrdinalIgnoreCase))
     {
         virtPath = virtPath.Substring(virtRoot.Length).Replace('/', '\\');
         return Path.Combine(physRoot, virtPath);
     }
     return null;
 }
Ejemplo n.º 12
0
 public bool CanHandle(HttpHandlerContext context)
 {
     /*asp.net can handle all requests*/
     return(true);
 }
Ejemplo n.º 13
0
 public void ProcessRequest(HttpHandlerContext context)
 {
     context.Host.ProcessRequest(context.Connection, context.Identity);
 }
Ejemplo n.º 14
0
        public bool CanHandle(HttpHandlerContext context)
        {
            var filename = GetFileName(context);

            return(!(string.IsNullOrEmpty(filename) || !File.Exists(filename)));
        }
Ejemplo n.º 15
0
 public static IHttpHandler GetHttpHandler(HttpHandlerContext context)
 {
     return(GetHandlers(context).FirstOrDefault(x => x.CanHandle(context)) ?? DirectoryHttpHandler);
 }
 public bool CanHandle(HttpHandlerContext context)
 {
     var filename = GetFileName(context);
     return !(string.IsNullOrEmpty(filename) || !File.Exists(filename));
 }