From 39f80cf34cc7de565121505ab836bcd190637da9 Mon Sep 17 00:00:00 2001 From: xyb Date: Wed, 11 Oct 2023 15:47:30 -0700 Subject: [PATCH] Fix playback service doesn't work with azure CDN with media optimization feature enabled. For azure CDN with media optimization enabled, if the media file is missing, the CDN will request a very big range. In the original implementation, playback service doesn't honor the real content length instead, it will return the request range. This incorrect behavior will make the cdn return network protocol error. --- .../src/Controllers/PlaybackController.cs | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tools/PlaybackService/src/Controllers/PlaybackController.cs b/tools/PlaybackService/src/Controllers/PlaybackController.cs index 5d30231..95171e4 100644 --- a/tools/PlaybackService/src/Controllers/PlaybackController.cs +++ b/tools/PlaybackService/src/Controllers/PlaybackController.cs @@ -425,21 +425,20 @@ private async Task GetOtherFilesAsync(BlobClient blob, long contentLength) throw new Exception($"Can't download the blob {blob.Uri}"); } - // Write the response. - if (range != null) - { - var readedLength = range.Value.Length ?? contentLength - range.Value.Offset; - Response.ContentLength = readedLength; + // Copy the http status code and response headers. + var blobServerResponse = blobStreamResponse.GetRawResponse(); + Response.StatusCode = blobServerResponse.Status; - var contentRange = $"{range.Value.Offset}-{readedLength + range.Value.Offset - 1}/{contentLength}"; - logger.LogDebug($"Add header Content-Range: {contentRange}"); - Response.Headers.Add("Content-Range", contentRange); - } - else + // For security, we only expose the following headers: + foreach (var allowHeader in new[] { "Content-Length", "Content-Type", "Date", "Accept-Ranges", "Content-MD5", "ETag", "Last-Modified" }) { - Response.ContentLength = contentLength; + if (blobServerResponse.Headers.TryGetValue(allowHeader, out var header)) + { + Response.Headers.TryAdd(allowHeader, header); + } } + // Copy the body. await blobStreamResponse.Value.Content.CopyToAsync(Response.Body).ConfigureAwait(false); // All done.