コード例 #1
0
        public object GetModule(string name)
        {
            var module = ViewModuleProvider.ResolveViewModule(name);

            if (module == null)
            {
                return(NotFound());
            }

            var eTagHeaderValue = new EntityTagHeaderValue("\"" + module.ETag + "\"");

            if (Request.Headers.IfNoneMatch.Contains(eTagHeaderValue))
            {
                return(new HttpResponseMessage(HttpStatusCode.NotModified));
            }

            var lastModifiedTime = (DateTimeOffset?)module.LastModifiedTime;

            if (Request.Headers.IfModifiedSince >= lastModifiedTime)
            {
                return(new HttpResponseMessage(HttpStatusCode.NotModified));
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Headers.ETag         = eTagHeaderValue;
            response.Headers.CacheControl = new CacheControlHeaderValue()
            {
                Public = true
            };
            response.Content = new PushStreamContent((s, c, t) => { module.Write(s); s.Close(); });
            response.Content.Headers.ContentType  = new MediaTypeHeaderValue(module.ContentType);
            response.Content.Headers.LastModified = lastModifiedTime;
            return(response);
        }
コード例 #2
0
        public void ProcessRequest(HttpContext context)
        {
            // allow browser to cache response
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0));
            context.Response.StatusCode = (int)HttpStatusCode.NotFound;

            // look up module by name
            var file = ViewModuleProvider.ResolveViewModule(context.Request.QueryString["m"]);

            if (file != null)
            {
                var ifNoneMatch = context.Request.Headers["If-None-Match"];
                if (ifNoneMatch != null)
                {
                    var p = ifNoneMatch.IndexOf(",", StringComparison.Ordinal);
                    if (p > -1)
                    {
                        ifNoneMatch = ifNoneMatch.Substring(0, p);
                    }

                    if (file.ETag == ifNoneMatch)
                    {
                        context.Response.ClearContent();
                        context.Response.StatusCode      = (int)HttpStatusCode.NotModified;
                        context.Response.SuppressContent = true;
                        return;
                    }
                }

                var ifModifiedSince = context.Request.Headers["If-Modified-Since"];
                if (ifModifiedSince != null)
                {
                    DateTime d;
                    if (DateTime.TryParse(ifModifiedSince, out d) && file.LastModifiedTime <= d)
                    {
                        context.Response.ClearContent();
                        context.Response.StatusCode      = (int)HttpStatusCode.NotModified;
                        context.Response.SuppressContent = true;
                        return;
                    }
                }

                context.Response.StatusCode  = (int)HttpStatusCode.OK;
                context.Response.ContentType = file.ContentType;
                context.Response.Cache.SetLastModified(file.LastModifiedTime);
                context.Response.Cache.SetETag(file.ETag);
                file.Write(context.Response.OutputStream);
            }
        }