Ejemplo n.º 1
0
 void WriteToFile(string path, HttpResponse response)
 {
     using (Stream stream = fileHandler.OpenWriteStream(path)) {
         using (BinaryWriter writer = new BinaryWriter(stream)) {
             CacheMetadata meta = new CacheMetadata(response);
             writer.Write(CacheMetadata.version);
             writer.Write(meta.domain != null);
             writer.Write(meta.domain);
             writer.Write(meta.path != null);
             writer.Write(meta.path);
             writer.Write(meta.contentType != null);
             writer.Write(meta.contentType);
             writer.Write(meta.eTag != null);
             writer.Write(meta.eTag);
             writer.Write(meta.expireAt.HasValue);
             if (meta.expireAt.HasValue)
             {
                 writer.Write(meta.expireAt.Value.ToBinary());
             }
             writer.Write(meta.lastModified.HasValue);
             if (meta.lastModified.HasValue)
             {
                 writer.Write(meta.lastModified.Value.ToBinary());
             }
             writer.Write(response.MessageBody);
         }
     }
 }
Ejemplo n.º 2
0
        internal HttpResponse Process(HttpRequest request, CacheMetadata cache, Progress progress, CancellationToken cancellationToken)
        {
            HttpResponse response = new HttpResponse(request);

            response.HttpVersion  = "HTTP/1.1";
            response.StatusCode   = 200;
            response.StatusPhrase = "OK (cache)";
            response.Headers.Append(HeaderField.ContentType, cache.contentType);
            response.MessageBody = BuildMessageBodyFromCache(response, progress, cancellationToken);
            return(response);
        }
Ejemplo n.º 3
0
        internal CacheMetadata FindMetadata(HttpRequest request)
        {
            string        filePath = GetFilePath(request.Uri);
            CacheMetadata data     = null;
            Mutex         mutex    = new Mutex(false, filePath);

            mutex.WaitOne();
            try {
                if (fileHandler.Exists(filePath))
                {
                    data = ReadMetaData(filePath);
                }
            }
            finally {
                mutex.ReleaseMutex();
            }
            return(data);
        }
Ejemplo n.º 4
0
        void AddCacheDirectiveToRequest(HttpRequest request)
        {
            CacheMetadata cache = cacheHandler.FindMetadata(request);

            request.Headers.Remove(HeaderField.IfNoneMatch);
            request.Headers.Remove(HeaderField.IfModifiedSince);

            if (cache == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(cache.eTag))
            {
                request.Headers.AddOrReplace(HeaderField.IfNoneMatch, cache.eTag);
            }
            if (cache.lastModified.HasValue)
            {
                request.Headers.AddOrReplace(HeaderField.IfModifiedSince, cache.lastModified.Value.ToString("r"));
            }
        }