protected HttpResponseMessage GetInternal(string path) { var pathInfo = PathInfo.Create(path); var filePath = _fileSystem.Path.Combine(this.FilePath, pathInfo); if (!_fileSystem.FileExists(filePath)) return new HttpResponseMessage(HttpStatusCode.NotFound); var eTag = new EntityTagHeaderValue(string.Concat("\"", _fileSystem.GetFileInfo(filePath).Etag, "\"")); if (Request.Headers.IfNoneMatch != null) { foreach (var noneMatch in Request.Headers.IfNoneMatch) { if (eTag.Equals(noneMatch)) return new HttpResponseMessage(HttpStatusCode.NotModified); } } var stream = _fileSystem.OpenRead(filePath); var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; message.Content.Headers.ContentType = new MediaTypeHeaderValue(GetMimeType(_fileSystem.Path.GetExtension(pathInfo).ToLower())); message.Headers.ETag = eTag; // remove Pragma message.Headers.Remove("Pragma"); message.Headers.CacheControl = new CacheControlHeaderValue {Private = true, MaxAge = TimeSpan.Zero}; message.Content.Headers.Expires = DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(1)); //message.Content.Headers.LastModified return message; }
/// <summary> /// Checks if the request is conditional having a <c>If-None-Match</c> HTTP header field with a value that matches the /// <paramref name="current"/> value. In the case of <c>true</c> this can be used to indicate that a /// 304 (Not Modified) or a 412 (Precondition Failed) status code should be used. /// </summary> /// <param name="request">The request to match.</param> /// <param name="current">The current etag for the resource. If there is no current etag (i.e. the resource does not yet /// exist) then use <c>null</c>.</param> /// <returns>Returns <c>true</c> if one of the <c>If-None-Match</c> values match the current etag; or the /// <c>If-None-Match</c> value is "*" and <paramref name="current"/> is not null; otherwise false.</returns> public static bool IsIfNoneMatch(this HttpRequestMessage request, EntityTagHeaderValue current) { if (request == null) { throw new ArgumentNullException("request"); } if (request.Headers.IfNoneMatch != null) { foreach (EntityTagHeaderValue etag in request.Headers.IfNoneMatch) { if (current != null && (etag == EntityTagHeaderValue.Any || current.Equals(etag))) { return true; } } } return false; }
protected HttpResponseMessage GetInternal(string path) { var pathInfo = PathInfo.Create(path); var filePath = _fileSystem.Path.Combine(this.FilePath, pathInfo); if (!_fileSystem.FileExists(filePath)) return new HttpResponseMessage(HttpStatusCode.NotFound); var eTag = new EntityTagHeaderValue(string.Concat("\"", _fileSystem.GetFileInfo(filePath).Etag, "\"")); if (Request.Headers.IfNoneMatch != null) { foreach (var noneMatch in Request.Headers.IfNoneMatch) { if (eTag.Equals(noneMatch)) return new HttpResponseMessage(HttpStatusCode.NotModified); } } var stream = _fileSystem.OpenRead(filePath); var message = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(stream) }; message.Content.Headers.ContentType = new MediaTypeHeaderValue(GetMimeType(_fileSystem.Path.GetExtension(pathInfo).ToLower())); message.Headers.ETag = eTag; return message; }
public void Equals_UseSameAndDifferentETags_EqualOrNotEqualNoExceptions() { EntityTagHeaderValue etag1 = new EntityTagHeaderValue("\"tag\""); EntityTagHeaderValue etag2 = new EntityTagHeaderValue("\"TAG\""); EntityTagHeaderValue etag3 = new EntityTagHeaderValue("\"tag\"", true); EntityTagHeaderValue etag4 = new EntityTagHeaderValue("\"tag1\""); EntityTagHeaderValue etag5 = new EntityTagHeaderValue("\"tag\""); EntityTagHeaderValue etag6 = EntityTagHeaderValue.Any; Assert.False(etag1.Equals(etag2)); Assert.False(etag2.Equals(etag1)); Assert.False(etag1.Equals(null)); Assert.False(etag1.Equals(etag3)); Assert.False(etag3.Equals(etag1)); Assert.False(etag1.Equals(etag4)); Assert.False(etag1.Equals(etag6)); Assert.True(etag1.Equals(etag5)); }
/// <summary> /// Indicates whether this is a If-None-Match request with a matching etag. /// </summary> protected bool IsIfNoneMatchRequest(EntityTagHeaderValue currentEtag) { return currentEtag != null && Request.Headers.IfNoneMatch != null && Request.Headers.IfNoneMatch.Any(entityTag => currentEtag.Equals(entityTag)); }
private static bool EtagEquals(HttpRequestMessage request, EntityTagHeaderValue currentEtag) { return request.Headers.IfNoneMatch != null && request.Headers.IfNoneMatch.Any(etag => currentEtag.Equals(etag)); }
/// <summary> /// Indicates whether this is a conditional range request containing an /// If-Range header with a matching etag and a Range header indicating the /// desired ranges /// </summary> protected bool IsRangeRequest(EntityTagHeaderValue currentEtag) { if (currentEtag != null && Request.Headers.IfRange != null && Request.Headers.Range != null) { // First check that the etag matches so that we can consider the range request if (currentEtag.Equals(Request.Headers.IfRange.EntityTag)) { return true; } } return false; }