Example #1
0
        /// <summary>
        /// Honours requests for files from the file system. Returns false if no file matched the request.
        /// </summary>
        /// <param name="environment"></param>
        /// <returns></returns>
        private bool ServeFromFileSystem(IDictionary <string, object> environment)
        {
            var result = false;

            var context  = PipelineContext.GetOrCreate(environment);
            var request  = context.Request;
            var response = context.Response;

            var relativePath = ConvertRequestPathToRelativeFilePath(request.FlattenedPath);

            relativePath = RejectInvalidCharacters(relativePath);
            if (!String.IsNullOrEmpty(relativePath))
            {
                foreach (var siteRoot in _Configuration.GetSiteRootFolders())
                {
                    var fullPath = Path.Combine(siteRoot, relativePath);

                    if (FileSystemProvider.FileExists(fullPath))
                    {
                        var extension = Path.GetExtension(fullPath);
                        var mimeType  = MimeType.GetForExtension(extension) ?? "application/octet-stream";
                        var content   = FileSystemProvider.FileReadAllBytes(fullPath);

                        if (_Configuration.IsFileUnmodified(siteRoot, request.FlattenedPath, content))
                        {
                            content = RaiseTextLoadedFromFile(request, content, mimeType);
                            SendContent(response, content, mimeType);
                        }
                        else
                        {
                            if (mimeType != MimeType.Html)
                            {
                                response.StatusCode = (int)HttpStatusCode.BadRequest;
                            }
                            else
                            {
                                content = Encoding.UTF8.GetBytes("<HTML><HEAD><TITLE>No</TITLE></HEAD><BODY>VRS will not serve content that has been tampered with. Install the custom content plugin if you want to alter the site's files.</BODY></HTML>");
                                SendContent(response, content, mimeType);
                            }
                        }

                        result = true;
                        break;
                    }
                }
            }

            return(result);
        }
Example #2
0
 public void FileSystemConfiguration_IsFileUnmodified_Throws_If_SiteRootFolder_Is_Null()
 {
     _Configuration.IsFileUnmodified(null, "/", new byte[0]);
 }