Skip to content

Commit a853946

Browse files
committed
fix logic error
1 parent 1589275 commit a853946

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

Microsoft.NET.Build.Containers/Registry.cs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,16 +198,7 @@ private readonly async Task<UriBuilder> UploadBlobChunked(string name, string di
198198
throw new ApplicationException(errorMessage);
199199
}
200200

201-
if (patchResponse.Headers.Location is { IsAbsoluteUri: true })
202-
{
203-
localUploadUri = new UriBuilder(patchResponse.Headers.Location);
204-
}
205-
else
206-
{
207-
// if we don't trim the BaseUri and relative Uri of slashes, you can get invalid urls.
208-
// Uri constructor does this on our behalf.
209-
localUploadUri = new UriBuilder(new Uri(BaseUri, patchResponse.Headers.Location?.OriginalString ?? ""));
210-
}
201+
localUploadUri = GetNextLocation(patchResponse);
211202

212203
patchUri = localUploadUri.Uri;
213204

@@ -217,13 +208,26 @@ private readonly async Task<UriBuilder> UploadBlobChunked(string name, string di
217208
return new UriBuilder(patchUri);
218209
}
219210

211+
private readonly UriBuilder GetNextLocation(HttpResponseMessage response) {
212+
if (response.Headers.Location is {IsAbsoluteUri: true })
213+
{
214+
return new UriBuilder(response.Headers.Location);
215+
}
216+
else
217+
{
218+
// if we don't trim the BaseUri and relative Uri of slashes, you can get invalid urls.
219+
// Uri constructor does this on our behalf.
220+
return new UriBuilder(new Uri(BaseUri, response.Headers.Location?.OriginalString ?? ""));
221+
}
222+
}
223+
220224
private readonly async Task<UriBuilder> UploadBlobWhole(string name, string digest, Stream contents, HttpClient client, UriBuilder uploadUri) {
221225
StreamContent content = new StreamContent(contents);
222226
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
223227
content.Headers.ContentLength = contents.Length;
224228
HttpResponseMessage putResponse = await client.PutAsync(uploadUri.Uri, content);
225229
putResponse.EnsureSuccessStatusCode();
226-
return new UriBuilder(putResponse.Headers.Location ?? uploadUri.Uri);
230+
return GetNextLocation(putResponse);
227231
}
228232

229233
private readonly async Task<UriBuilder> StartUploadSession(string name, string digest, HttpClient client) {
@@ -237,21 +241,12 @@ private readonly async Task<UriBuilder> StartUploadSession(string name, string d
237241
throw new ApplicationException(errorMessage);
238242
}
239243

240-
if (pushResponse.Headers.Location is {IsAbsoluteUri: true })
241-
{
242-
return new UriBuilder(pushResponse.Headers.Location);
243-
}
244-
else
245-
{
246-
// if we don't trim the BaseUri and relative Uri of slashes, you can get invalid urls.
247-
// Uri constructor does this on our behalf.
248-
return new UriBuilder(new Uri(BaseUri, pushResponse.Headers.Location?.OriginalString ?? ""));
249-
}
244+
return GetNextLocation(pushResponse);
250245
}
251246

252247
private readonly async Task<UriBuilder> UploadBlobContents(string name, string digest, Stream contents, HttpClient client, UriBuilder uploadUri) {
253-
if (SupportsChunkedUpload) return await UploadBlobWhole(name, digest, contents, client, uploadUri);
254-
else return await UploadBlobChunked(name, digest, contents, client, uploadUri);
248+
if (SupportsChunkedUpload) return await UploadBlobChunked(name, digest, contents, client, uploadUri);
249+
else return await UploadBlobWhole(name, digest, contents, client, uploadUri);
255250
}
256251

257252
private readonly async Task FinishUploadSession(string digest, HttpClient client, UriBuilder uploadUri) {

0 commit comments

Comments
 (0)