Ejemplo n.º 1
0
        public void FileShouldBeCompressedIfRequested(string acceptEncodingValue)
        {
            context.RequestHeaders.Add(HttpConsts.HeaderAcceptEncoding, acceptEncodingValue);

            fileSystem.Stub(x => x.DoesFileExist(FileName)).Return(true);
            byte[] fileBytes = new byte[100];
            fileSystem.Stub(x => x.ReadFileAsBytes(FileName)).Return(fileBytes);

            ICachingPolicy cachingPolicy = new NoCachingPolicy();
            FileResult     result        = new FileResult(FileName, cachingPolicy);

            result.AllowGzipCompression = true;
            result.Apply(context);

            Assert.AreEqual((int)HttpStatusCode.OK, context.StatusCode);
            Assert.AreEqual("gzip", context.ResponseHeaders[HttpConsts.HeaderContentEncoding]);
            Assert.AreEqual(HttpConsts.ContentTypeImagePng, context.ResponseContentType);
        }
Ejemplo n.º 2
0
		/// <summary>
		/// Initializes static members of the CachingPolicy class.
		/// </summary>
		static CachingPolicy()
		{
			NoCaching = new NoCachingPolicy();
			CacheAll = new CacheAllPolicy();
		}
Ejemplo n.º 3
0
        public IWebCommandResult Execute(IWebContext context, WebRequestRouteMatch routeMatch)
        {
            // do not allow rooted (absolute) paths
            string path = routeMatch["path"];

            if (path == null)
            {
                return(new HttpStatusResult(HttpStatusCode.BadRequest));
            }

            if (Path.IsPathRooted(path))
            {
                HttpStatusResult httpStatusResult = new HttpStatusResult(HttpStatusCode.BadRequest);
                httpStatusResult.LoggingSeverity = LoggingSeverity.Error;
                return(httpStatusResult);
            }

            string fileFullPath = Path.Combine(contentRootDirectory, path);

            // do not allow access outside of the contents folder (and its children)
            fileFullPath = Path.GetFullPath(fileFullPath);

            if (!fileFullPath.StartsWith(contentRootDirectory, StringComparison.Ordinal))
            {
                return(new HttpStatusResult(HttpStatusCode.Forbidden));
            }

            if (fileSystem.DoesFileExist(fileFullPath))
            {
                ICachingPolicy cachingPolicy;

                if (context.Configuration.WebServerDevelopmentMode)
                {
                    cachingPolicy = new NoCachingPolicy();
                }
                else
                {
                    // choose the policy based on wildcard match
                    cachingPolicy = FindCachingPolicyForFile(path);
                }

                if (log.IsDebugEnabled)
                {
                    log.DebugFormat("Returning contents of the file '{0}'", fileFullPath);
                }

                FileResult result = new FileResult(fileFullPath, cachingPolicy);
                result.AllowGzipCompression = allowGzipCompression;
                result.FileCache            = fileCache;
                result.LoggingSeverity      = LoggingSeverity.Verbose;
                return(result);
            }

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Requested file '{0}' does not exist", fileFullPath);
            }

            HttpStatusResult notFoundResult = new HttpStatusResult(HttpStatusCode.NotFound);

            notFoundResult.LoggingSeverity = LoggingSeverity.Error;
            return(notFoundResult);
        }
Ejemplo n.º 4
0
        private void RenderView(IWebContext context)
        {
            ICachingPolicy cachingPolicy = new NoCachingPolicy();

            cachingPolicy.ProcessRequest(null, context, ReturnView);
        }