当前位置: 首页>>代码示例>>C#>>正文


C# DreamMessage.CheckCacheRevalidation方法代码示例

本文整理汇总了C#中DreamMessage.CheckCacheRevalidation方法的典型用法代码示例。如果您正苦于以下问题:C# DreamMessage.CheckCacheRevalidation方法的具体用法?C# DreamMessage.CheckCacheRevalidation怎么用?C# DreamMessage.CheckCacheRevalidation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DreamMessage的用法示例。


在下文中一共展示了DreamMessage.CheckCacheRevalidation方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetFileOrFolderListing

        public Yield GetFileOrFolderListing(DreamContext context, DreamMessage request, Result<DreamMessage> response)
        {
            bool head = StringUtil.EqualsInvariant(context.Verb, "HEAD");
            string path = GetPath(context);

            DreamMessage result;
            if(File.Exists(path)) {

                // dealing with a file request
                TouchMeta(path);

                // check if request contains a 'if-modified-since' header
                var lastmodified = File.GetLastWriteTime(path);
                if(request.CheckCacheRevalidation(lastmodified) && (lastmodified.Year >= 1900)) {
                    response.Return(DreamMessage.NotModified());
                    yield break;
                }

                // retrieve file
                try {
                    result = DreamMessage.FromFile(path, head);
                } catch(FileNotFoundException) {
                    result = DreamMessage.NotFound("file not found");
                } catch(Exception) {
                    result = DreamMessage.BadRequest("invalid path");
                }

                // add caching headers if file was found
                if(!head && result.IsSuccessful) {

                    // add caching information; this will avoid unnecessary data transfers by user-agents with caches
                    result.SetCacheMustRevalidate(lastmodified);
                }
            } else if(Directory.Exists(path)) {

                // dealing with a directory request
                if(head) {

                    // HEAD for a directory doesn't really mean anything, so we just return ok, to indicate that it exists
                    result = DreamMessage.Ok();
                } else {
                    var doc = new XDoc("files");

                    // list directory contents
                    var directories = Directory.GetDirectories(path);
                    foreach(var dir in directories) {
                        if(dir.EndsWithInvariantIgnoreCase(META)) {
                            continue;
                        }
                        doc.Start("folder")
                            .Elem("name", Path.GetFileName(dir))
                            .End();
                    }
                    foreach(var filepath in Directory.GetFiles(path)) {
                        var file = new FileInfo(filepath);
                        doc.Start("file")
                            .Elem("name", file.Name)
                            .Elem("size", file.Length)
                            .Elem("date.created", file.CreationTimeUtc)
                            .Elem("date.modified", file.LastWriteTimeUtc);
                        var entry = SyncMeta(filepath);
                        if(entry != null) {
                            doc.Elem("date.expire", entry.When);
                            doc.Elem("date.ttl", entry.TTL);
                        }
                        doc.End();
                    }
                    result = DreamMessage.Ok(doc);
                }
            } else {

                // nothin here
                result = DreamMessage.NotFound("no such file or folder");
            }

            response.Return(result);
            yield break;
        }
开发者ID:heran,项目名称:DReAM,代码行数:78,代码来源:StorageService.cs

示例2: GetFileOrFolderListing

        public Yield GetFileOrFolderListing(DreamContext context, DreamMessage request, Result<DreamMessage> response)
        {
            var head = "HEAD".EqualsInvariant(context.Verb);
            var path = GetPath(context);
            var data = _s3Client.GetDataInfo(path, head);
            DreamMessage result;
            if(data == null) {
                response.Return(DreamMessage.NotFound("no such file or folder"));
                yield break;
            }
            if(data.IsDirectory) {

                // Note (arnec): HEAD for a directory doesn't really mean anything, so we just return ok, to indicate that it exists
                result = head ? DreamMessage.Ok() : DreamMessage.Ok(data.AsDirectoryDocument());
            } else {

                // dealing with a file request
                var filehandle = data.AsFileHandle();

                // check if request contains a 'if-modified-since' header
                if(request.CheckCacheRevalidation(filehandle.Modified) && (filehandle.Modified.Year >= 1900)) {
                    response.Return(DreamMessage.NotModified());
                    yield break;
                }
                result = head
                             ? new DreamMessage(DreamStatus.Ok, null, filehandle.MimeType, filehandle.Size, Stream.Null)
                             : DreamMessage.Ok(filehandle.MimeType, filehandle.Size, filehandle.Stream);

                // add caching headers if file was found
                if(!head && result.IsSuccessful) {

                    // add caching information; this will avoid unnecessary data transfers by user-agents with caches
                    result.SetCacheMustRevalidate(filehandle.Modified);
                }
            }
            response.Return(result);
            yield break;
        }
开发者ID:danice,项目名称:DReAM,代码行数:38,代码来源:S3StorageService.cs


注:本文中的DreamMessage.CheckCacheRevalidation方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。