Skip to content

Commit 19713f1

Browse files
authored
Fix URI parse error (#2067)
1 parent 67c06bd commit 19713f1

File tree

4 files changed

+215
-18
lines changed

4 files changed

+215
-18
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
import io.opentelemetry.sdk.trace.data.SpanData;
5555
import io.opentelemetry.sdk.trace.export.SpanExporter;
5656
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
57-
import java.net.URI;
58-
import java.net.URISyntaxException;
5957
import java.util.Arrays;
6058
import java.util.Collection;
6159
import java.util.Collections;
@@ -125,9 +123,6 @@ public class Exporter implements SpanExporter {
125123
private static final OperationLogger exportingSpanLogger =
126124
new OperationLogger(Exporter.class, "Exporting span");
127125

128-
private static final OperationLogger parsingHttpUrlLogger =
129-
new OperationLogger(Exporter.class, "Parsing http.url");
130-
131126
static {
132127
Set<String> dbSystems = new HashSet<>();
133128
dbSystems.add(SemanticAttributes.DbSystemValues.DB2);
@@ -547,18 +542,8 @@ private static String getTargetForHttpClientSpan(Attributes attributes) {
547542
}
548543
String url = attributes.get(SemanticAttributes.HTTP_URL);
549544
if (url != null) {
550-
URI uri;
551-
try {
552-
uri = new URI(url);
553-
} catch (URISyntaxException e) {
554-
parsingHttpUrlLogger.recordFailure(e.getMessage(), e);
555-
uri = null;
556-
}
557-
if (uri != null) {
558-
target = uri.getHost();
559-
if (uri.getPort() != 80 && uri.getPort() != 443 && uri.getPort() != -1) {
560-
target += ":" + uri.getPort();
561-
}
545+
target = UrlParser.getTargetFromUrl(url);
546+
if (target != null) {
562547
return target;
563548
}
564549
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/UrlParser.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,43 @@
2525

2626
class UrlParser {
2727

28+
/**
29+
* Returns the "target" (host:port) portion of the url.
30+
*
31+
* <p>Returns {@code null} if the target cannot be extracted from url for any reason.
32+
*/
33+
@Nullable
34+
static String getTargetFromUrl(String url) {
35+
36+
int schemeEndIndex = url.indexOf(':');
37+
if (schemeEndIndex == -1) {
38+
// not a valid url
39+
return null;
40+
}
41+
42+
int len = url.length();
43+
if (schemeEndIndex + 2 < len
44+
&& url.charAt(schemeEndIndex + 1) == '/'
45+
&& url.charAt(schemeEndIndex + 2) == '/') {
46+
// has authority component
47+
// look for
48+
// '/' - start of path
49+
// '?' or end of string - empty path
50+
int index;
51+
for (index = schemeEndIndex + 3; index < len; index++) {
52+
char c = url.charAt(index);
53+
if (c == '/' || c == '?' || c == '#') {
54+
break;
55+
}
56+
}
57+
String target = url.substring(schemeEndIndex + 3, index);
58+
return target.isEmpty() ? null : target;
59+
} else {
60+
// has no authority
61+
return null;
62+
}
63+
}
64+
2865
/**
2966
* Returns the path portion of the url.
3067
*
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import org.junit.jupiter.api.Test;
2727

28-
public class UrlParserTest {
28+
public class UrlParserPathTest {
2929

3030
@Test
3131
public void testGetPathFromUrl() {
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* ApplicationInsights-Java
3+
* Copyright (c) Microsoft Corporation
4+
* All rights reserved.
5+
*
6+
* MIT License
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
8+
* software and associated documentation files (the ""Software""), to deal in the Software
9+
* without restriction, including without limitation the rights to use, copy, modify, merge,
10+
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
11+
* persons to whom the Software is furnished to do so, subject to the following conditions:
12+
* The above copyright notice and this permission notice shall be included in all copies or
13+
* substantial portions of the Software.
14+
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16+
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
17+
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19+
* DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
package com.microsoft.applicationinsights.agent.internal.exporter;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
public class UrlParserTargetTest {
29+
30+
// there are more test cases here than needed, but they are just copied from UrlParserPathTest
31+
32+
@Test
33+
public void testGetTargetFromUrl() {
34+
assertThat(UrlParser.getTargetFromUrl("https://localhost")).isEqualTo("localhost");
35+
assertThat(UrlParser.getTargetFromUrl("https://localhost/")).isEqualTo("localhost");
36+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path")).isEqualTo("localhost");
37+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path/")).isEqualTo("localhost");
38+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path")).isEqualTo("localhost");
39+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/")).isEqualTo("localhost");
40+
41+
assertThat(UrlParser.getTargetFromUrl("https://localhost?")).isEqualTo("localhost");
42+
assertThat(UrlParser.getTargetFromUrl("https://localhost/?")).isEqualTo("localhost");
43+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path?")).isEqualTo("localhost");
44+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path/?")).isEqualTo("localhost");
45+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path?")).isEqualTo("localhost");
46+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/?")).isEqualTo("localhost");
47+
48+
assertThat(UrlParser.getTargetFromUrl("https://localhost?query")).isEqualTo("localhost");
49+
assertThat(UrlParser.getTargetFromUrl("https://localhost/?query")).isEqualTo("localhost");
50+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path?query")).isEqualTo("localhost");
51+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path/?query")).isEqualTo("localhost");
52+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path?query"))
53+
.isEqualTo("localhost");
54+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/?query"))
55+
.isEqualTo("localhost");
56+
57+
assertThat(UrlParser.getTargetFromUrl("https://localhost#")).isEqualTo("localhost");
58+
assertThat(UrlParser.getTargetFromUrl("https://localhost/#")).isEqualTo("localhost");
59+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path#")).isEqualTo("localhost");
60+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path/#")).isEqualTo("localhost");
61+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path#")).isEqualTo("localhost");
62+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/#")).isEqualTo("localhost");
63+
64+
assertThat(UrlParser.getTargetFromUrl("https://localhost#fragment")).isEqualTo("localhost");
65+
assertThat(UrlParser.getTargetFromUrl("https://localhost/#fragment")).isEqualTo("localhost");
66+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path#fragment"))
67+
.isEqualTo("localhost");
68+
assertThat(UrlParser.getTargetFromUrl("https://localhost/path/#fragment"))
69+
.isEqualTo("localhost");
70+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path#fragment"))
71+
.isEqualTo("localhost");
72+
assertThat(UrlParser.getTargetFromUrl("https://localhost/more/path/#fragment"))
73+
.isEqualTo("localhost");
74+
}
75+
76+
@Test
77+
public void testGetTargetFromUrlWithPort() {
78+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080")).isEqualTo("localhost:8080");
79+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/")).isEqualTo("localhost:8080");
80+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path"))
81+
.isEqualTo("localhost:8080");
82+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/"))
83+
.isEqualTo("localhost:8080");
84+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path"))
85+
.isEqualTo("localhost:8080");
86+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/"))
87+
.isEqualTo("localhost:8080");
88+
89+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080?")).isEqualTo("localhost:8080");
90+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/?")).isEqualTo("localhost:8080");
91+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path?"))
92+
.isEqualTo("localhost:8080");
93+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/?"))
94+
.isEqualTo("localhost:8080");
95+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path?"))
96+
.isEqualTo("localhost:8080");
97+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/?"))
98+
.isEqualTo("localhost:8080");
99+
100+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080?query"))
101+
.isEqualTo("localhost:8080");
102+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/?query"))
103+
.isEqualTo("localhost:8080");
104+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path?query"))
105+
.isEqualTo("localhost:8080");
106+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/?query"))
107+
.isEqualTo("localhost:8080");
108+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path?query"))
109+
.isEqualTo("localhost:8080");
110+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/?query"))
111+
.isEqualTo("localhost:8080");
112+
113+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080#")).isEqualTo("localhost:8080");
114+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/#")).isEqualTo("localhost:8080");
115+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path#"))
116+
.isEqualTo("localhost:8080");
117+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/#"))
118+
.isEqualTo("localhost:8080");
119+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path#"))
120+
.isEqualTo("localhost:8080");
121+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/#"))
122+
.isEqualTo("localhost:8080");
123+
124+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080#fragment"))
125+
.isEqualTo("localhost:8080");
126+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/#fragment"))
127+
.isEqualTo("localhost:8080");
128+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path#fragment"))
129+
.isEqualTo("localhost:8080");
130+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/path/#fragment"))
131+
.isEqualTo("localhost:8080");
132+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path#fragment"))
133+
.isEqualTo("localhost:8080");
134+
assertThat(UrlParser.getTargetFromUrl("https://localhost:8080/more/path/#fragment"))
135+
.isEqualTo("localhost:8080");
136+
}
137+
138+
@Test
139+
public void testGetTargetFromUrlWithNoAuthority() {
140+
assertThat(UrlParser.getTargetFromUrl("https:")).isNull();
141+
assertThat(UrlParser.getTargetFromUrl("https:/")).isNull();
142+
assertThat(UrlParser.getTargetFromUrl("https:/path")).isNull();
143+
assertThat(UrlParser.getTargetFromUrl("https:/path/")).isNull();
144+
assertThat(UrlParser.getTargetFromUrl("https:/more/path")).isNull();
145+
assertThat(UrlParser.getTargetFromUrl("https:/more/path/")).isNull();
146+
147+
assertThat(UrlParser.getTargetFromUrl("https:?")).isNull();
148+
assertThat(UrlParser.getTargetFromUrl("https:/?")).isNull();
149+
assertThat(UrlParser.getTargetFromUrl("https:/path?")).isNull();
150+
assertThat(UrlParser.getTargetFromUrl("https:/path/?")).isNull();
151+
assertThat(UrlParser.getTargetFromUrl("https:/more/path?")).isNull();
152+
assertThat(UrlParser.getTargetFromUrl("https:/more/path/?")).isNull();
153+
154+
assertThat(UrlParser.getTargetFromUrl("https:?query")).isNull();
155+
assertThat(UrlParser.getTargetFromUrl("https:/?query")).isNull();
156+
assertThat(UrlParser.getTargetFromUrl("https:/path?query")).isNull();
157+
assertThat(UrlParser.getTargetFromUrl("https:/path/?query")).isNull();
158+
assertThat(UrlParser.getTargetFromUrl("https:/more/path?query")).isNull();
159+
assertThat(UrlParser.getTargetFromUrl("https:/more/path/?query")).isNull();
160+
161+
assertThat(UrlParser.getTargetFromUrl("https:#")).isNull();
162+
assertThat(UrlParser.getTargetFromUrl("https:/#")).isNull();
163+
assertThat(UrlParser.getTargetFromUrl("https:/path#")).isNull();
164+
assertThat(UrlParser.getTargetFromUrl("https:/path/#")).isNull();
165+
assertThat(UrlParser.getTargetFromUrl("https:/more/path#")).isNull();
166+
assertThat(UrlParser.getTargetFromUrl("https:/more/path/#")).isNull();
167+
168+
assertThat(UrlParser.getTargetFromUrl("https:#fragment")).isNull();
169+
assertThat(UrlParser.getTargetFromUrl("https:/#fragment")).isNull();
170+
assertThat(UrlParser.getTargetFromUrl("https:/path#fragment")).isNull();
171+
assertThat(UrlParser.getTargetFromUrl("https:/path/#fragment")).isNull();
172+
assertThat(UrlParser.getTargetFromUrl("https:/more/path#fragment")).isNull();
173+
assertThat(UrlParser.getTargetFromUrl("https:/more/path/#fragment")).isNull();
174+
}
175+
}

0 commit comments

Comments
 (0)