Esempio n. 1
0
        internal Func <HttpRequestMessage, Task <HttpResponseMessage> > GetIfModifiedUnmodifiedSince()
        {
            return((request) =>
            {
                try
                {
                    if (request.Method != HttpMethod.Get)
                    {
                        return null;
                    }

                    DateTimeOffset?ifModifiedSince = request.Headers.IfModifiedSince;
                    DateTimeOffset?ifUnmodifiedSince = request.Headers.IfUnmodifiedSince;

                    if (ifModifiedSince == null && ifUnmodifiedSince == null)
                    {
                        return null;                                          // no etag
                    }
                    if (ifModifiedSince != null && ifUnmodifiedSince != null) // both exist
                    {
                        return request.CreateResponse(HttpStatusCode.BadRequest)
                        .ToTask();
                    }
                    bool ifModified = (ifUnmodifiedSince == null);
                    DateTimeOffset modifiedInQuestion = ifModified ? ifModifiedSince.Value : ifUnmodifiedSince.Value;


                    var entityTagKey = GenerateCacheKey(request);

                    TimedEntityTagHeaderValue actualEtag = null;

                    bool isModified = true;
                    if (_entityTagStore.TryGetValue(entityTagKey, out actualEtag))
                    {
                        isModified = actualEtag.LastModified > modifiedInQuestion;
                    }

                    return isModified ^ ifModified
                            ? new NotModifiedResponse(request, actualEtag.ToEntityTagHeaderValue()).ToTask()
                            : null;
                }
                catch (Exception e)
                {
                    throw new CacheCowServerException("Error in GetIfModifiedUnmodifiedSince", e);
                }
            });
        }
Esempio n. 2
0
        internal Func <HttpRequestMessage, Task <HttpResponseMessage> > GetIfMatchNoneMatch()
        {
            return((request) =>
            {
                if (request.Method != HttpMethod.Get)
                {
                    return null;
                }

                ICollection <EntityTagHeaderValue> noneMatchTags = request.Headers.IfNoneMatch;
                ICollection <EntityTagHeaderValue> matchTags = request.Headers.IfMatch;

                if (matchTags.Count == 0 && noneMatchTags.Count == 0)
                {
                    return null;                                    // no etag
                }
                if (matchTags.Count > 0 && noneMatchTags.Count > 0) // both if-match and if-none-match exist
                {
                    return request.CreateResponse(HttpStatusCode.BadRequest)
                    .ToTask();
                }

                var isNoneMatch = noneMatchTags.Count > 0;
                var etags = isNoneMatch ? noneMatchTags : matchTags;

                var entityTagKey = GenerateCacheKey(request);
                // compare the Etag with the one in the cache
                // do conditional get.
                TimedEntityTagHeaderValue actualEtag = null;

                bool matchFound = false;
                if (_entityTagStore.TryGetValue(entityTagKey, out actualEtag))
                {
                    if (etags.Any(etag => etag.Tag == actualEtag.Tag))
                    {
                        matchFound = true;
                    }
                }
                return matchFound ^ isNoneMatch ? null : new NotModifiedResponse(request,
                                                                                 actualEtag.ToEntityTagHeaderValue()).ToTask();
            });
        }