-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add package version to query string for v2 CDN endpoint (#10082) #10083
Changes from 1 commit
0a1251c
15e626c
030504e
e5c703f
b3ccb52
7a6e130
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using NuGetGallery.Configuration; | ||
using NuGetGallery.Diagnostics; | ||
|
||
using System; | ||
using System.Collections.Specialized; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using NuGetGallery.Configuration; | ||
using NuGetGallery.Diagnostics; | ||
|
||
namespace NuGetGallery | ||
{ | ||
|
@@ -21,35 +23,36 @@ public CloudBlobFileStorageService( | |
IAppConfiguration configuration, | ||
ISourceDestinationRedirectPolicy redirectPolicy, | ||
IDiagnosticsService diagnosticsService, | ||
ICloudBlobContainerInformationProvider cloudBlobFolderInformationProvider) | ||
ICloudBlobContainerInformationProvider cloudBlobFolderInformationProvider) | ||
: base(client, diagnosticsService, cloudBlobFolderInformationProvider) | ||
{ | ||
_configuration = configuration; | ||
_redirectPolicy = redirectPolicy; | ||
} | ||
|
||
public async Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName) | ||
public async Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName, string versionParameter) | ||
{ | ||
ICloudBlobContainer container = await GetContainerAsync(folderName); | ||
var blob = container.GetBlobReference(fileName); | ||
|
||
var redirectUri = GetRedirectUri(requestUrl, blob.Uri); | ||
var redirectUri = GetRedirectUri(requestUrl, blob.Uri, versionParameter); | ||
return new RedirectResult(redirectUri.AbsoluteUri, false); | ||
} | ||
|
||
internal async Task<ActionResult> CreateDownloadFileActionResult( | ||
HttpContextBase httpContext, | ||
string folderName, | ||
string fileName) | ||
string fileName, | ||
string versionParameter) | ||
{ | ||
var container = await GetContainerAsync(folderName); | ||
var blob = container.GetBlobReference(fileName); | ||
|
||
var redirectUri = GetRedirectUri(httpContext.Request.Url, blob.Uri); | ||
var redirectUri = GetRedirectUri(httpContext.Request.Url, blob.Uri, versionParameter); | ||
return new RedirectResult(redirectUri.AbsoluteUri, false); | ||
} | ||
|
||
internal Uri GetRedirectUri(Uri requestUrl, Uri blobUri) | ||
internal Uri GetRedirectUri(Uri requestUrl, Uri blobUri, string versionParameter) | ||
{ | ||
if (!_redirectPolicy.IsAllowed(requestUrl, blobUri)) | ||
{ | ||
|
@@ -79,19 +82,17 @@ internal Uri GetRedirectUri(Uri requestUrl, Uri blobUri) | |
// When no blob query string is passed, we forward the request | ||
// URI's query string to the CDN. See https://github.com/NuGet/NuGetGallery/issues/3168 | ||
// and related PR's. | ||
var queryString = !string.IsNullOrEmpty(blobUri.Query) | ||
? blobUri.Query | ||
: requestUrl.Query; | ||
var queryStringUri = !string.IsNullOrEmpty(blobUri.Query) | ||
? blobUri | ||
: requestUrl; | ||
|
||
if (!string.IsNullOrEmpty(queryString)) | ||
{ | ||
queryString = queryString.TrimStart('?'); | ||
} | ||
var queryValues = ParseQueryString(queryStringUri); | ||
jimmylewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
queryValues.Add(CoreConstants.PackageVersionParameterName, versionParameter); | ||
|
||
var urlBuilder = new UriBuilder(scheme, host, port) | ||
{ | ||
Path = blobUri.LocalPath, | ||
Query = queryString | ||
Query = queryValues.ToString() | ||
}; | ||
|
||
return urlBuilder.Uri; | ||
|
@@ -102,5 +103,16 @@ public async Task<bool> IsAvailableAsync() | |
var container = await GetContainerAsync(CoreConstants.Folders.PackagesFolderName); | ||
return await container.ExistsAsync(cloudBlobLocationMode: null); | ||
} | ||
|
||
private static NameValueCollection ParseQueryString(Uri uri) | ||
{ | ||
int queryIndex = uri.Query?.LastIndexOf("?") ?? -1; | ||
clairernovotny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (queryIndex == -1) return new NameValueCollection(); | ||
|
||
string query = uri.Query.Substring(queryIndex); | ||
|
||
var nvcol = HttpUtility.ParseQueryString(query); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Explicit type please |
||
return nvcol; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using NuGetGallery.Configuration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this file and a few others modified in this PR don't adhere to the new style rules put in place by this PR (.editorconfig). |
||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Web.Hosting; | ||
using System.Web.Mvc; | ||
using NuGetGallery.Configuration; | ||
|
||
namespace NuGetGallery | ||
{ | ||
|
@@ -23,7 +24,7 @@ public FileSystemFileStorageService(IAppConfiguration configuration, IFileSystem | |
_fileSystemService = fileSystemService; | ||
} | ||
|
||
public Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName) | ||
public Task<ActionResult> CreateDownloadFileActionResultAsync(Uri requestUrl, string folderName, string fileName, string versionParameter) | ||
{ | ||
if (string.IsNullOrWhiteSpace(folderName)) | ||
{ | ||
|
@@ -270,7 +271,7 @@ public Task SetMetadataAsync( | |
|
||
public Task SetPropertiesAsync( | ||
string folderName, | ||
string fileName, | ||
string fileName, | ||
Func<Lazy<Task<Stream>>, ICloudBlobProperties, Task<bool>> updatePropertiesAsync) | ||
{ | ||
return Task.CompletedTask; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using NuGet.Services.Entities; | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Web.Mvc; | ||
using NuGet.Services.Entities; | ||
|
||
namespace NuGetGallery | ||
{ | ||
|
@@ -28,13 +29,18 @@ public PackageFileService(IFileStorageService fileStorageService) | |
public Task<ActionResult> CreateDownloadPackageActionResultAsync(Uri requestUrl, Package package) | ||
joelverhagen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var fileName = FileNameHelper.BuildFileName(package, CoreConstants.PackageFileSavePathTemplate, CoreConstants.NuGetPackageFileExtension); | ||
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName); | ||
|
||
var packageVersion = NuGetVersionFormatter.GetNormalizedPackageVersion(package).ToLowerInvariant(); // will not return null bc BuildFileName will throw if values are null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why lower invariant? Generally we use the original casing when we have it and allow the user of the value to do the comparison in a case insensitive manner if necessary. This means we can retain that original author intent as much as possible.
clairernovotny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName, packageVersion); | ||
} | ||
|
||
public Task<ActionResult> CreateDownloadPackageActionResultAsync(Uri requestUrl, string id, string version) | ||
{ | ||
var fileName = FileNameHelper.BuildFileName(id, version, CoreConstants.PackageFileSavePathTemplate, CoreConstants.NuGetPackageFileExtension); | ||
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName); | ||
|
||
// version cannot be null here as BuildFileName will throw if it is | ||
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.Folders.PackagesFolderName, fileName, NuGetVersionFormatter.Normalize(version).ToLowerInvariant()); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -47,7 +53,7 @@ public Task DeleteReadMeMdFileAsync(Package package) | |
{ | ||
throw new ArgumentNullException(nameof(package)); | ||
} | ||
|
||
var fileName = FileNameHelper.BuildFileName(package, ReadMeFilePathTemplateActive, ServicesConstants.MarkdownFileExtension); | ||
|
||
return _fileStorageService.DeleteFileAsync(CoreConstants.Folders.PackageReadMesFolderName, fileName); | ||
|
@@ -83,7 +89,7 @@ public async Task<string> DownloadReadMeMdFileAsync(Package package) | |
{ | ||
throw new ArgumentNullException(nameof(package)); | ||
} | ||
|
||
var fileName = FileNameHelper.BuildFileName(package, ReadMeFilePathTemplateActive, ServicesConstants.MarkdownFileExtension); | ||
|
||
using (var readMeMdStream = await _fileStorageService.GetFileAsync(CoreConstants.Folders.PackageReadMesFolderName, fileName)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a low level method not specific to a package or version. What do you think about a simple
queryString
parameter instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would allow flexibility at the call site.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked a bit closer and the easiest approach hardcodes the parameter name lower down--this just passes the version. Making it more generic would also put the burden on a properly urlencoded query string on the caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems okay to me. A
Dictionary<string, string>
would be more flexible. but it's not a big deal to me.