8000 http: MOE Sync: GenericUrl should handle hostname with underscore · DingaGa/google-http-java-client@c2904dd · GitHub
[go: up one dir, main page]

Skip to content

Commit c2904dd

Browse files
author
Yaniv Inbar
committed
http: MOE Sync: GenericUrl should handle hostname with underscore
https://codereview.appspot.com/14037043/
1 parent 3f75978 commit c2904dd

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

google-http-client/src/main/java/com/google/api/client/http/GenericUrl.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ public GenericUrl() {
9999
* </p>
100100
*
101101
* <p>
102-
* Any {@link URISyntaxException} is wrapped in an {@link IllegalArgumentException}.
102+
* Any {@link MalformedURLException} is wrapped in an {@link IllegalArgumentException}.
103103
* </p>
104104
*
105+
* <p>Upgrade warning: starting in version 1.18 this parses the encodedUrl using
106+
* new URL(encodedUrl). In previous versions it used new URI(encodedUrl).
107+
* In particular, this means that only a limited set of schemes are allowed such as "http" and
108+
* "https", but that parsing is compliant with, at least, RFC 3986.</p>
109+
*
105110
* @param encodedUrl encoded URL, including any existing query parameters that should be parsed
106111
* @throws IllegalArgumentException if URL has a syntax error
107112
*/
108113
public GenericUrl(String encodedUrl) {
109-
this(toURI(encodedUrl));
114+
this(parseURL(encodedUrl));
110115
}
111116

112117
/**
@@ -408,11 +413,7 @@ public final URI toURI() {
408413
* @since 1.14
409414
*/
410415
public final URL toURL() {
411-
try {
412-
return new URL(build());
413-
} catch (MalformedURLException e) {
414-
throw new IllegalArgumentException(e);
415-
}
416+
return parseURL(build());
416417
}
417418

418419
/**
@@ -619,4 +620,22 @@ private static URI toURI(String encodedUrl) {
619620
throw new IllegalArgumentException(e);
620621
}
621622
}
623+
624+
/**
625+
* Returns the URI for the given encoded URL.
626+
*
627+
* <p>
628+
* Any {@link MalformedURLException} is wrapped in an {@link IllegalArgumentException}.
629+
* </p>
630+
*
631+
* @param encodedUrl encoded URL
632+
* @return URL
633+
*/
634+
private static URL parseURL(String encodedUrl) {
635+
try {
636+
return new URL(encodedUrl);
637+
} catch (MalformedURLException e) {
638+
throw new IllegalArgumentException(e);
639+
}
640+
}
622641
}

google-http-client/src/test/java/com/google/api/client/http/GenericUrlTest.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,25 @@ public GenericUrlTest(String name) {
4343
super(name);
4444
}
4545

46-
private static String MINIMAL = "foo://bar";
46+
private static final String MINIMAL = "http://bar";
4747

4848
public void testBuild_minimal() {
4949
GenericUrl url = new GenericUrl();
50-
url.setScheme("foo");
50+
url.setScheme("http");
5151
url.setHost("bar");
5252
assertEquals(MINIMAL, url.build());
5353
}
5454

5555
public void testParse_minimal() {
5656
GenericUrl url = new GenericUrl(MINIMAL);
57-
assertEquals("foo", url.getScheme());
57+
assertEquals("http", url.getScheme());
5858
}
5959

60-
private static String NO_PATH = "foo://bar?a=b";
60+
private static final String NO_PATH = "http://bar?a=b";
6161

6262
public void testBuild_noPath() {
6363
GenericUrl url = new GenericUrl();
64-
url.setScheme("foo");
64+
url.setScheme("http");
6565
url.setHost("bar");
6666
url.set("a", "b");
6767
assertEquals(NO_PATH, url.build());
@@ -85,7 +85,7 @@ public void testBuild_noScheme() {
8585
public void testBuild_noHost() {
8686
GenericUrl url = new GenericUrl();
8787
try {
88-
url.setScheme("foo");
88+
url.setScheme("http");
8989
url.build();
9090
fail("expected " + NullPointerException.class);
9191
} catch (NullPointerException e) {
@@ -95,19 +95,19 @@ public void testBuild_noHost() {
9595

9696
public void testParse_noPath() {
9797
GenericUrl url = new GenericUrl(NO_PATH);
98-
assertEquals("foo", url.getScheme());
98+
assertEquals("http", url.getScheme());
9999
assertEquals("bar", url.getHost());
100100
assertEquals("b", url.getFirst("a"));
101101
assertNull(url.getPathParts());
102102
}
103103

104-
private static String SHORT_PATH = "foo://bar/path?a=b";
104+
private static final String SHORT_PATH = "http://bar/path?a=b";
105105

106-
private static List<String> SHORT_PATH_PARTS = Arrays.asList("", "path");
106+
private static final List<String> SHORT_PATH_PARTS = Arrays.asList("", "path");
107107

108108
public void testBuild_shortPath() {
109109
GenericUrl url = new GenericUrl();
110-
url.setScheme("foo");
110+
url.setScheme("http");
111111
url.setHost("bar");
112112
url.setPathParts(SHORT_PATH_PARTS);
113113
url.set("a", "b");
@@ -116,19 +116,19 @@ public void testBuild_shortPath() {
116116

117117
public void testParse_shortPath() {
118118
GenericUrl url = new GenericUrl(SHORT_PATH);
119-
assertEquals("foo", url.getScheme());
119+
assertEquals("http", url.getScheme());
120120
assertEquals("bar", url.getHost());
121121
assertEquals(SHORT_PATH_PARTS, url.getPathParts());
122122
assertEquals("b", url.getFirst("a"));
123123
}
124124

125-
private static String LONG_PATH = "foo://bar/path/to/resource?a=b";
125+
private static final String LONG_PATH = "http://bar/path/to/resource?a=b";
126126

127-
private static List<String> LONG_PATH_PARTS = Arrays.asList("", "path", "to", "resource");
127+
private static final List<String> LONG_PATH_PARTS = Arrays.asList("", "path", "to", "resource");
128128

129129
public void testBuild_longPath() {
130130
GenericUrl url = new GenericUrl();
131-
url.setScheme("foo");
131+
url.setScheme("http");
132132
url.setHost("bar");
133133
url.setPathParts(LONG_PATH_PARTS);
134134
url.set("a", "b");
@@ -137,7 +137,7 @@ public void testBuild_longPath() {
137137

138138
public void testParse_longPath() {
139139
GenericUrl url = new GenericUrl(LONG_PATH);
140-
assertEquals("foo", url.getScheme());
140+
assertEquals("http", url.getScheme());
141141
assertEquals("bar", url.getHost());
142142
assertEquals(LONG_PATH_PARTS, url.getPathParts());
143143
assertEquals("b", url.getFirst("a"));
@@ -157,17 +157,17 @@ public TestUrl(String encodedUrl) {
157157
}
158158
}
159159

160-
private static String FULL =
160+
private static final String FULL =
161161
"https://user:%3Cpa&$w%40rd%3E@www.google.com:223/m8/feeds/contacts/"
162162
+ "someone=%23%25&%20%3F%3Co%3E%7B%7D@gmail.com/"
163163
+ "full?" + "foo=bar&" + "alt=json&" + "max-results=3&" + "prettyprint=true&"
164164
+ "q=Go%3D%23/%25%26%20?%3Co%3Egle#%3CD@WNL:ADING%3E";
165165

166-
private static List<String> FULL_PARTS =
166+
private static final List<String> FULL_PARTS =
167167
Arrays.asList("", "m8", "feeds", "contacts", "someone=#%& ?<o>{}@gmail.com", "full");
168168

169-
private static String USER_INFO = "user:<pa&$w@rd>";
170-
private static String FRAGMENT = "<D@WNL:ADING>";
169+
private static final String USER_INFO = "user:<pa&$w@rd>";
170+
private static final String FRAGMENT = "<D@WNL:ADING>";
171171

172172
public void testBuild_full() {
173173
TestUrl url = new TestUrl();
@@ -268,11 +268,12 @@ public FieldTypesUrl set(String fieldName, Object value) {
268268
}
269269
}
270270

271-
private static String FIELD_TYPES = "foo://bar?B=true&D=-3.14&I=-3&b=true&d=-3.14&i=-3&s=a&a=b";
271+
private static final String FIELD_TYPES =
272+
"http://bar?B=true&D=-3.14&I=-3&b=true&d=-3.14&i=-3&s=a&a=b";
272273

273274
public void testBuild_fieldTypes() {
274275
FieldTypesUrl url = new FieldTypesUrl();
275-
url.setScheme("foo");
276+
url.setScheme("http");
276277
url.setHost("bar");
277278
url.set("a", "b");
278279
url.B = true;
@@ -288,7 +289,7 @@ public void testBuild_fieldTypes() {
288289

289290
public void testParse_fieldTypes() {
290291
FieldTypesUrl url = new FieldTypesUrl(FIELD_TYPES);
291-
assertEquals("foo", url.getScheme());
292+
assertEquals("http", url.getScheme());
292293
assertEquals("bar", url.getHost());
293294
assertEquals("b", url.getFirst("a"));
294295
assertNull(url.hidden);
@@ -301,11 +302,12 @@ public void testParse_fieldTypes() {
301302
assertEquals("a", url.s);
302303
}
303304

304-
private static String FRAGMENT1 = "foo://bar/path/to/resource#fragme=%23/%25&%20?%3Co%3Ent";
305+
private static final String FRAGMENT1 =
306+
"http://bar/path/to/resource#fragme=%23/%25&%20?%3Co%3Ent";
305307

306308
public void testBuild_fragment1() {
307309
GenericUrl url = new GenericUrl();
308-
url.setScheme("foo");
310+
url.setScheme("http");
309311
url.setHost("bar");
310312
url.setPathParts(LONG_PATH_PARTS);
311313
url.setFragment("fragme=#/%& ?<o>nt");
@@ -314,17 +316,17 @@ public void testBuild_fragment1() {
314316

315317
public void testParse_fragment1() {
316318
GenericUrl url = new GenericUrl(FRAGMENT1);
317-
assertEquals("foo", url.getScheme());
319+
assertEquals("http", url.getScheme());
318320
assertEquals("bar", url.getHost());
319321
assertEquals(LONG_PATH_PARTS, url.getPathParts());
320322
assertEquals("fragme=#/%& ?<o>nt", url.getFragment());
321323
}
322324

323-
private static String FRAGMENT2 = "foo://bar/path/to/resource?a=b#fragment";
325+
private static final String FRAGMENT2 = "http://bar/path/to/resource?a=b#fragment";
324326

325327
public void testBuild_fragment2() {
326328
GenericUrl url = new GenericUrl();
327-
url.setScheme("foo");
329+
url.setScheme("http");
328330
url.setHost("bar");
329331
url.setPathParts(LONG_PATH_PARTS);
330332
url.set("a", "b");
@@ -334,7 +336,7 @@ public void testBuild_fragment2() {
334336

335337
public void testParse_fragment2() {
336338
GenericUrl url = new GenericUrl(FRAGMENT2);
337-
assertEquals("foo", url.getScheme());
339+
assertEquals("http", url.getScheme());
338340
assertEquals("bar", url.getHost());
339341
assertEquals(LONG_PATH_PARTS, url.getPathParts());
340342
assertEquals("b", url.getFirst("a"));
@@ -451,6 +453,12 @@ public void testBuild_pathWithSlash() {
451453
assertEquals(PATH_WITH_SLASH, url.build());
452454
}
453455

456+
public void testConstructorUnderscore() {
457+
String url = "http://url_with_underscore.google.com";
458+< 1241 /span>
GenericUrl parsed = new GenericUrl(url);
459+
assertEquals("url_with_underscore.google.com", parsed.getHost());
460+
}
461+
454462
public void testParse_pathWithSlash() {
455463
GenericUrl url = new GenericUrl(PATH_WITH_SLASH);
456464
assertEquals("http", url.getScheme());

0 commit comments

Comments
 (0)
0