Beispiel #1
0
        /// <summary>
        /// Immutable content
        /// </summary>
        public async Task Execute(HttpContext httpContext, string type, string id)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (!ContentTypes.IsValid(type))
            {
                _Logger.LogError("Invalid generic content type - {Id}.", id);
                httpContext.Response.StatusCode    = 400;
                httpContext.Response.ContentLength = 0;
            }

            if (!_PublishingIdService.Validate(id))
            {
                _Logger.LogError("Invalid content id - {Id}.", id);
                httpContext.Response.StatusCode    = 400;
                httpContext.Response.ContentLength = 0;
            }

            if (!httpContext.Request.Headers.TryGetValue("if-none-match", out var etagValue))
            {
                _Logger.LogError("Required request header missing - if-none-match.");
                httpContext.Response.ContentLength = 0;
                httpContext.Response.StatusCode    = 400;
            }

            var content = await _DbContext.SafeGetContent(type, id, _DateTimeProvider.Snapshot);

            if (content == null)
            {
                _Logger.LogError("Content not found - {Id}.", id);
                httpContext.Response.StatusCode    = 404;
                httpContext.Response.ContentLength = 0;
                return;
            }

            if (etagValue == content.PublishingId)
            {
                _Logger.LogWarning("Matching etag found, responding with 304 - {Id}.", id);
                httpContext.Response.StatusCode    = 304;
                httpContext.Response.ContentLength = 0;
                return;
            }

            httpContext.Response.Headers.Add("etag", content.PublishingId);
            httpContext.Response.Headers.Add("last-modified", content.Release.ToUniversalTime().ToString("r"));
            httpContext.Response.Headers.Add("content-type", content.ContentTypeName);
            httpContext.Response.Headers.Add("cache-control", _HttpResponseHeaderConfig.ImmutableContentCacheControl);
            httpContext.Response.StatusCode    = 200;
            httpContext.Response.ContentLength = content.Content?.Length ?? throw new InvalidOperationException("SignedContent empty.");
            await httpContext.Response.Body.WriteAsync(content.Content);
        }
        /// <summary>
        /// Immutable content
        /// </summary>
        public async Task <ContentEntity> ExecuteAsync(HttpContext httpContext, string type, string id)
        {
            if (httpContext == null)
            {
                throw new ArgumentNullException(nameof(httpContext));
            }

            if (!_publishingIdService.Validate(id))
            {
                _logger.WriteInvalidId(id);
                httpContext.Response.StatusCode    = 400;
                httpContext.Response.ContentLength = 0;
            }

            if (!ContentTypes.IsValid(type))
            {
                _logger.WriteInvalidType(id);
                httpContext.Response.StatusCode    = 400;
                httpContext.Response.ContentLength = 0;
            }

            if (!httpContext.Request.Headers.TryGetValue("if-none-match", out var etagValue))
            {
                _logger.WriteHeaderMissing();
                httpContext.Response.ContentLength = 0;
                httpContext.Response.StatusCode    = 400;
            }

            var content = await _dbContext.SafeGetContentAsync(type, id, _dateTimeProvider.Snapshot);

            if (content == null)
            {
                _logger.WriteNotFound(id);
                httpContext.Response.StatusCode    = 404;
                httpContext.Response.ContentLength = 0;
                return(null);
            }

            if (etagValue == content.PublishingId)
            {
                _logger.WriteEtagFound(id);
                httpContext.Response.StatusCode    = 304;
                httpContext.Response.ContentLength = 0;
                return(null);
            }

            httpContext.Response.Headers.Add("etag", content.PublishingId);
            httpContext.Response.Headers.Add("last-modified", content.Release.ToUniversalTime().ToString("r"));
            httpContext.Response.Headers.Add("content-type", content.ContentTypeName);
            httpContext.Response.StatusCode    = 200;
            httpContext.Response.ContentLength = content.Content?.Length ?? throw new InvalidOperationException("SignedContent empty.");
            return(content);
        }