Skip to content

Commit 3e7596c

Browse files
committed
Introduce Uri#toFullUrl that have the fragment
1 parent 30611c6 commit 3e7596c

File tree

5 files changed

+64
-21
lines changed

5 files changed

+64
-21
lines changed

client/src/main/java/org/asynchttpclient/uri/Uri.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class Uri {
3434
private final int port;
3535
private final String query;
3636
private final String path;
37+
private final String fragment;
3738
private String url;
3839
private boolean secured;
3940
private boolean webSocket;
@@ -43,14 +44,16 @@ public Uri(String scheme,
4344
String host,
4445
int port,
4546
String path,
46-
String query) {
47+
String query,
48+
String fragment) {
4749

4850
this.scheme = assertNotEmpty(scheme, "scheme");
4951
this.userInfo = userInfo;
5052
this.host = assertNotEmpty(host, "host");
5153
this.port = port;
5254
this.path = path;
5355
this.query = query;
56+
this.fragment = fragment;
5457
this.secured = HTTPS.equals(scheme) || WSS.equals(scheme);
5558
this.webSocket = WS.equals(scheme) || WSS.equalsIgnoreCase(scheme);
5659
}
@@ -75,7 +78,8 @@ public static Uri create(Uri context, final String originalUrl) {
7578
parser.host,
7679
parser.port,
7780
parser.path,
78-
parser.query);
81+
parser.query,
82+
parser.fragment);
7983
}
8084

8185
public String getQuery() {
@@ -102,6 +106,10 @@ public String getHost() {
102106
return host;
103107
}
104108

109+
public String getFragment() {
110+
return fragment;
111+
}
112+
105113
public boolean isSecured() {
106114
return secured;
107115
}
@@ -168,6 +176,10 @@ public String toRelativeUrl() {
168176
return sb.toString();
169177
}
170178

179+
public String toFullUrl() {
180+
return fragment == null ? toUrl() : toUrl() + "#" + fragment;
181+
}
182+
171183
public String getBaseUrl() {
172184
return scheme + "://" + host + ":" + getExplicitPort();
173185
}
@@ -198,7 +210,8 @@ public Uri withNewScheme(String newScheme) {
198210
host,
199211
port,
200212
path,
201-
query);
213+
query,
214+
fragment);
202215
}
203216

204217
public Uri withNewQuery(String newQuery) {
@@ -207,7 +220,8 @@ public Uri withNewQuery(String newQuery) {
207220
host,
208221
port,
209222
path,
210-
newQuery);
223+
newQuery,
224+
fragment);
211225
}
212226

213227
@Override
@@ -220,6 +234,7 @@ public int hashCode() {
220234
result = prime * result + ((query == null) ? 0 : query.hashCode());
221235
result = prime * result + ((scheme == null) ? 0 : scheme.hashCode());
222236
result = prime * result + ((userInfo == null) ? 0 : userInfo.hashCode());
237+
result = prime * result + ((fragment == null) ? 0 : fragment.hashCode());
223238
return result;
224239
}
225240

@@ -259,6 +274,11 @@ public boolean equals(Object obj) {
259274
return false;
260275
} else if (!userInfo.equals(other.userInfo))
261276
return false;
277+
if (fragment == null) {
278+
if (other.fragment != null)
279+
return false;
280+
} else if (!fragment.equals(other.fragment))
281+
return false;
262282
return true;
263283
}
264284

client/src/main/java/org/asynchttpclient/uri/UriParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class UriParser {
2121
public String host;
2222
public int port = -1;
2323
public String query;
24+
public String fragment;
2425
private String authority;
2526
public String path;
2627
public String userInfo;
@@ -116,13 +117,17 @@ private void trimFragment() {
116117
int charpPosition = findWithinCurrentRange('#');
117118
if (charpPosition >= 0) {
118119
end = charpPosition;
120+
if (charpPosition + 1 < originalUrl.length()) {
121+
fragment = originalUrl.substring(charpPosition + 1);
122+
}
119123
}
120124
}
121125

122126
private void inheritContextQuery(Uri context, boolean isRelative) {
123127
// see RFC2396 5.2.2: query and fragment inheritance
124128
if (isRelative && currentIndex == end) {
125129
query = context.getQuery();
130+
fragment = context.getFragment();
126131
}
127132
}
128133

client/src/main/java/org/asynchttpclient/util/UriEncoder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public Uri encode(Uri uri, List<Param> queryParams) {
133133
uri.getHost(),
134134
uri.getPort(),
135135
newPath,
136-
newQuery);
136+
newQuery,
137+
uri.getFragment());
137138
}
138139

139140
protected abstract String encodePath(String path);

client/src/test/java/org/asynchttpclient/uri/UriParserTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,61 +62,61 @@ public void testUrlHasLeadingAndTrailingWhiteSpace() {
6262

6363
@Test
6464
public void testResolveAbsoluteUriAgainstContext() {
65-
Uri context = new Uri("https", null, "example.com", 80, "/path", "");
65+
Uri context = new Uri("https", null, "example.com", 80, "/path", "", null);
6666
validateAgainstRelativeURI(context, "https://example.com:80/path", "http://example.com/path");
6767
}
6868

6969
@Test
7070
public void testRootRelativePath() {
71-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
71+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
7272
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "/relativeUrl");
7373
}
7474

7575
@Test
7676
public void testCurrentDirRelativePath() {
77-
Uri context = new Uri("https", null, "example.com", 80, "/foo/bar", "q=2");
77+
Uri context = new Uri("https", null, "example.com", 80, "/foo/bar", "q=2", null);
7878
validateAgainstRelativeURI(context, "https://example.com:80/foo/bar?q=2", "relativeUrl");
7979
}
8080

8181
@Test
8282
public void testFragmentOnly() {
83-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
83+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
8484
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "#test");
8585
}
8686

8787
@Test
8888
public void testRelativeUrlWithQuery() {
89-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
89+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
9090
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "/relativePath?q=3");
9191
}
9292

9393
@Test
9494
public void testRelativeUrlWithQueryOnly() {
95-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
95+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
9696
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "?q=3");
9797
}
9898

9999
@Test
100100
public void testRelativeURLWithDots() {
101-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
101+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
102102
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "./relative/./url");
103103
}
104104

105105
@Test
106106
public void testRelativeURLWithTwoEmbeddedDots() {
107-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
107+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
108108
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "./relative/../url");
109109
}
110110

111111
@Test
112112
public void testRelativeURLWithTwoTrailingDots() {
113-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
113+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
114114
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "./relative/url/..");
115115
}
116116

117117
@Test
118118
public void testRelativeURLWithOneTrailingDot() {
119-
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2");
119+
Uri context = new Uri("https", null, "example.com", 80, "/path", "q=2", null);
120120
validateAgainstRelativeURI(context, "https://example.com:80/path?q=2", "./relative/url/.");
121121
}
122122
}

client/src/test/java/org/asynchttpclient/uri/UriTest.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void testCreateAndToUrl() {
133133

134134
@Test
135135
public void testToUrlWithUserInfoPortPathAndQuery() {
136-
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4");
136+
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4", null);
137137
assertEquals(uri.toUrl(), "http://[email protected]:44/path/path2?query=4", "toUrl returned incorrect url");
138138
}
139139

@@ -167,30 +167,30 @@ public void testQueryWithRootPathAndTrailingSlash() {
167167

168168
@Test
169169
public void testWithNewScheme() {
170-
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4");
170+
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4", null);
171171
Uri newUri = uri.withNewScheme("https");
172172
assertEquals(newUri.getScheme(), "https");
173173
assertEquals(newUri.toUrl(), "https://[email protected]:44/path/path2?query=4", "toUrl returned incorrect url");
174174
}
175175

176176
@Test
177177
public void testWithNewQuery() {
178-
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4");
178+
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4", null);
179179
Uri newUri = uri.withNewQuery("query2=10&query3=20");
180180
assertEquals(newUri.getQuery(), "query2=10&query3=20");
181181
assertEquals(newUri.toUrl(), "http://[email protected]:44/path/path2?query2=10&query3=20", "toUrl returned incorrect url");
182182
}
183183

184184
@Test
185185
public void testToRelativeUrl() {
186-
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4");
186+
Uri uri = new Uri("http", "user", "example.com", 44, "/path/path2", "query=4", null);
187187
String relativeUrl = uri.toRelativeUrl();
188188
assertEquals(relativeUrl, "/path/path2?query=4", "toRelativeUrl returned incorrect url");
189189
}
190190

191191
@Test
192192
public void testToRelativeUrlWithEmptyPath() {
193-
Uri uri = new Uri("http", "user", "example.com", 44, null, "query=4");
193+
Uri uri = new Uri("http", "user", "example.com", 44, null, "query=4", null);
194194
String relativeUrl = uri.toRelativeUrl();
195195
assertEquals(relativeUrl, "/?query=4", "toRelativeUrl returned incorrect url");
196196
}
@@ -232,10 +232,27 @@ public void testGetExplicitPort() {
232232
public void testEquals() {
233233
String url = "http://[email protected]:8080/level1/level2/level3?q=1";
234234
Uri createdUri = Uri.create(url);
235-
Uri constructedUri = new Uri("http", "user", "hello.com", 8080, "/level1/level2/level3", "q=1");
235+
Uri constructedUri = new Uri("http", "user", "hello.com", 8080, "/level1/level2/level3", "q=1", null);
236236
assertTrue(createdUri.equals(constructedUri), "The equals method returned false for two equal urls");
237237
}
238238

239+
@Test
240+
void testFragment() {
241+
String url = "http://[email protected]:8080/level1/level2/level3?q=1";
242+
String fragment = "foo";
243+
String urlWithFragment = url + "#" + fragment;
244+
Uri uri = Uri.create(urlWithFragment);
245+
assertEquals(fragment, uri.getFragment(), "Fragment should be extracted");
246+
assertEquals(uri.toUrl(), url, "toUrl should return without fragment");
247+
assertEquals(uri.toFullUrl(), urlWithFragment, "toFullUrl should return with fragment");
248+
}
249+
250+
@Test
251+
void testRelativeFragment() {
252+
Uri uri = Uri.create(Uri.create("http://[email protected]:8080"), "/level1/level2/level3?q=1#foo");
253+
assertEquals("foo", uri.getFragment(), "fragment should be kept when computing a relative url");
254+
}
255+
239256
@Test
240257
public void testIsWebsocket() {
241258
String url = "http://[email protected]:8080/level1/level2/level3?q=1";

0 commit comments

Comments
 (0)