public void WriteResponse() { using (var layout = new TestDirectoryLayout()) { layout.WriteAppDataFile("static/__test.txt", "test contents"); var fileStorage = Application.Ioc.Resolve <IFileStorage>(); var fileEntry = fileStorage.GetResourceFile("static/__test.txt"); var lastModified = fileEntry.LastWriteTimeUtc.Truncate(); Assert.Equals(fileEntry.ReadAllText(), "test contents"); var ifModifiedSinces = new DateTime?[] { null, DateTime.UtcNow.AddDays(1), lastModified }; foreach (var ifModifiedSince in ifModifiedSinces) { var result = new FileEntryResult(fileEntry, ifModifiedSince); var contextMock = new HttpContextMock(); result.WriteResponse(contextMock.response); if (ifModifiedSince == lastModified) { // 304 Assert.Equals(contextMock.response.StatusCode, 304); Assert.Equals(contextMock.response.GetContentsFromBody(), ""); } else { // 200 Assert.Equals(contextMock.response.StatusCode, 200); Assert.Equals(contextMock.response.ContentType, "text/plain"); Assert.Equals(contextMock.response.GetContentsFromBody(), "test contents"); } contextMock.request.headers["If-Modified-Since"] = ( contextMock.response.headers["Last-Modified"][0]); Assert.Equals(contextMock.request.GetIfModifiedSince(), lastModified); } } }
/// <summary> /// 处理请求 /// </summary> public void OnRequest() { var context = HttpManager.CurrentContext; var path = context.Request.Path; if (!path.StartsWith(Prefix)) { return; } var fileStorage = Application.Ioc.Resolve <IFileStorage>(); var subPath = HttpUtils.UrlDecode(path.Substring(Prefix.Length)); if (string.IsNullOrEmpty(subPath)) { return; } var fileEntry = fileStorage.GetResourceFile("static", subPath); if (!fileEntry.Exists) { return; } var ifModifiedSince = context.Request.GetIfModifiedSince(); var bytesRange = context.Request.GetBytesRange(); var result = new FileEntryResult(fileEntry, ifModifiedSince, bytesRange); result.WriteResponse(context.Response); context.Response.End(); }
/// <summary> /// 处理请求 /// </summary> public void OnRequest() { var context = HttpManager.CurrentContext; // 不处理api请求 if (context.Request.Path.StartsWith(_apiPrefix)) { return; } // 查找对应的文件 var path = context.Request.Path.Substring(1); var contentType = ""; IFileEntry fileEntry = null; if (string.IsNullOrEmpty(path)) { // 请求的是首页 fileEntry = _fileStorage.GetResourceFile("static", "dist", "index.html"); } else { // 请求的是子页或资源文件 var extension = Path.GetExtension(path); if (string.IsNullOrEmpty(extension)) { // 请求的是子页 fileEntry = _fileStorage.GetResourceFile("static", "dist", "index.html"); } else { // 请求的是资源文件,首先判断是否有对应的预压缩文件 fileEntry = _fileStorage.GetResourceFile("static", "dist", path + ".gz"); if (fileEntry.Exists && context.Request.GetHeader("Accept-Encoding").Contains("gzip")) { // 返回预压缩文件,需要使用原来的Content-Type context.Response.AddHeader("Content-Encoding", "gzip"); contentType = MimeUtils.GetMimeType(path); } else { // 原路径存在则使用原路径内容,否则不处理 fileEntry = _fileStorage.GetResourceFile("static", "dist", path); } } } // 返回文件内容 if (fileEntry.Exists) { var result = new FileEntryResult(fileEntry, context.Request.GetIfModifiedSince()); if (!string.IsNullOrEmpty(contentType)) { result.ContentType = contentType; } result.WriteResponse(context.Response); context.Response.End(); } }