Skip to content

Commit 5d0fb2d

Browse files
mgmt-core, ignore invalid url in location, as this header usually not used in ARM, but could be used by other purposes (Azure#18442)
1 parent 0a884cc commit 5d0fb2d

File tree

2 files changed

+51
-5
lines changed
  • sdk/core/azure-core-management/src
    • main/java/com/azure/core/management/implementation/polling
    • test/java/com/azure/core/management/implementation/polling

2 files changed

+51
-5
lines changed

sdk/core/azure-core-management/src/main/java/com/azure/core/management/implementation/polling/Util.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Util {
2121
* @return the Azure-AsyncOperation header value if exists, null otherwise
2222
*/
2323
static URL getAzureAsyncOperationUrl(HttpHeaders headers, ClientLogger logger) {
24-
return getUrl("Azure-AsyncOperation", headers, logger);
24+
return getUrl("Azure-AsyncOperation", headers, logger, false);
2525
}
2626

2727
/**
@@ -32,7 +32,7 @@ static URL getAzureAsyncOperationUrl(HttpHeaders headers, ClientLogger logger) {
3232
* @return the Location header value if exists, null otherwise
3333
*/
3434
static URL getLocationUrl(HttpHeaders headers, ClientLogger logger) {
35-
return getUrl("Location", headers, logger);
35+
return getUrl("Location", headers, logger, true);
3636
}
3737

3838
/**
@@ -43,14 +43,18 @@ static URL getLocationUrl(HttpHeaders headers, ClientLogger logger) {
4343
* @param logger the logger
4444
* @return the URL value of the given header, null if header does not exists.
4545
*/
46-
static URL getUrl(String urlHeaderName, HttpHeaders headers, ClientLogger logger) {
46+
static URL getUrl(String urlHeaderName, HttpHeaders headers, ClientLogger logger, boolean ignoreException) {
4747
String value = headers.getValue(urlHeaderName);
4848
if (value != null) {
4949
try {
5050
return new URL(value);
5151
} catch (MalformedURLException me) {
52-
throw logger.logExceptionAsError(new RuntimeException("Malformed value '" + value
53-
+ "' for URL header: '" + urlHeaderName + "'.", me));
52+
String message = "Malformed value '" + value + "' for URL header: '" + urlHeaderName + "'.";
53+
if (ignoreException) {
54+
logger.logExceptionAsError(new RuntimeException(message, me));
55+
} else {
56+
throw logger.logExceptionAsError(new RuntimeException(message, me));
57+
}
5458
}
5559
}
5660
return null;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.core.management.implementation.polling;
5+
6+
import com.azure.core.http.HttpHeaders;
7+
import com.azure.core.util.logging.ClientLogger;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.net.MalformedURLException;
12+
import java.net.URL;
13+
14+
public class UtilTests {
15+
16+
private final ClientLogger logger = new ClientLogger(UtilTests.class);
17+
18+
@Test
19+
public void testGetURL() throws MalformedURLException {
20+
String asyncOpUrl = "https://management.azure.com/subscriptions/###/providers/Microsoft.Network/locations/eastus/operations/###";
21+
String locationUrl = "https://management.azure.com/subscriptions/###/resourceGroups/rg86829b7a87d74/providers/Microsoft.Search/searchServices/ss3edfb54d";
22+
23+
HttpHeaders headers = new HttpHeaders();
24+
headers.put("Azure-AsyncOperation", asyncOpUrl);
25+
headers.put("Location", locationUrl);
26+
27+
Assertions.assertEquals(new URL(asyncOpUrl), Util.getAzureAsyncOperationUrl(headers, logger));
28+
Assertions.assertEquals(new URL(locationUrl), Util.getLocationUrl(headers, logger));
29+
}
30+
31+
@Test
32+
public void testGetMalformedURL() {
33+
HttpHeaders asyncOpHeaders = new HttpHeaders();
34+
asyncOpHeaders.put("Azure-AsyncOperation", "invalidUrl");
35+
Assertions.assertThrows(RuntimeException.class, () -> Util.getAzureAsyncOperationUrl(asyncOpHeaders, logger));
36+
37+
// malformed URL in location will be ignored
38+
HttpHeaders locationHeaders = new HttpHeaders();
39+
locationHeaders.put("Location", "invalidUrl");
40+
Assertions.assertNull(Util.getLocationUrl(locationHeaders, logger));
41+
}
42+
}

0 commit comments

Comments
 (0)