public async Task Execute(HttpContext httpContext, string id) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } //This looked like a bug? 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; //TODO! } if (!_PublishingId.Validate(id)) { _Logger.LogError($"Invalid content id - {id}."); httpContext.Response.StatusCode = 400; httpContext.Response.ContentLength = 0; } var content = await _DbContext.SafeGetContent(id); if (content == null) { //TODO tell CDN to ignore hunting? _Logger.LogError($"Content not found - {id}."); httpContext.Response.StatusCode = 404; httpContext.Response.ContentLength = 0; return; } if (etagValue == content.PublishingId) { _Logger.LogWarning($"Matching etag found, responding with 304 - {id}."); httpContext.Response.StatusCode = 304; httpContext.Response.ContentLength = 0; return; } //var accepts = httpContext.Request.Headers["accept"].ToHashSet(); //var signedResponse = content.SignedContentTypeName != null && accepts.Contains(content.SignedContentTypeName); //if (!signedResponse && !accepts.Contains(content.ContentTypeName)) //{ // _Logger.LogWarning($"Cannot give acceptable response, responding with 406 - {id}."); // httpContext.Response.StatusCode = 406; // 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.StatusCode = 200; httpContext.Response.ContentLength = content.Content?.Length ?? throw new InvalidOperationException("SignedContent empty."); await httpContext.Response.Body.WriteAsync(content.Content); }
public async Task <IActionResult> Execute(string id, HttpContext httpContext) { if (!_PublishingId.Validate(id)) { return(new BadRequestResult()); } var e = await _SafeReader.Execute(id); if (e == null) { return(new NotFoundResult()); } var r = new BinaryContentResponse { LastModified = e.Release, PublishingId = e.PublishingId, ContentTypeName = e.ContentTypeName, Content = e.Content, SignedContentTypeName = e.SignedContentTypeName, SignedContent = e.SignedContent }; return(new OkObjectResult(r)); }
public async Task Execute(HttpContext httpContext, string id) { if (httpContext.Request.Headers.TryGetValue("if-none-match", out var etagValue)) { httpContext.Response.ContentLength = 0; httpContext.Response.StatusCode = 400; //TODO! } var parsed = _PublishingId.ParseUri(id); if (typeof(T) != typeof(ManifestEntity) && !_PublishingId.Validate(id)) { httpContext.Response.StatusCode = 400; httpContext.Response.ContentLength = 0; } var content = await _SafeReader.Execute(parsed); if (content == null) { //TODO tell CDN to ignore hunting? httpContext.Response.StatusCode = 404; httpContext.Response.ContentLength = 0; return; } if (etagValue == content.PublishingId) { httpContext.Response.StatusCode = 304; httpContext.Response.ContentLength = 0; return; } var accepts = httpContext.Request.Headers["accept"].ToHashSet(); var signedResponse = content.SignedContentTypeName != null && accepts.Contains(content.SignedContentTypeName); if (!signedResponse && !accepts.Contains(content.ContentTypeName)) { httpContext.Response.StatusCode = 406; 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", signedResponse ? content.SignedContentTypeName : content.ContentTypeName); httpContext.Response.Headers.Add("x-vws-signed", signedResponse.ToString()); httpContext.Response.StatusCode = 200; httpContext.Response.ContentLength = (signedResponse ? content.SignedContent : content.Content).Length; await httpContext.Response.Body.WriteAsync(signedResponse?content.SignedContent : content.Content); }