Skip to content

Commit ce206d4

Browse files
committed
more strict protocol handling when opening an url
1 parent 4874614 commit ce206d4

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

src/main/java/org/htmlunit/WebClient.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ <P extends Page> P getPage(final WebWindow webWindow, final WebRequest webReques
514514
/**
515515
* Convenient method to build a URL and load it into the current WebWindow as it would be done
516516
* by {@link #getPage(WebWindow, WebRequest)}.
517-
* @param url the URL of the new content
517+
* @param url the URL of the new content; in contrast to real browsers plain file url's are not supported.
518+
* You have to use the 'file', 'data', 'blob', 'http' or 'https' protocol.
518519
* @param <P> the page type
519520
* @return the new page
520521
* @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
@@ -530,7 +531,8 @@ public <P extends Page> P getPage(final String url) throws IOException, FailingH
530531
/**
531532
* Convenient method to load a URL into the current top WebWindow as it would be done
532533
* by {@link #getPage(WebWindow, WebRequest)}.
533-
* @param url the URL of the new content
534+
* @param url the URL of the new content; in contrast to real browsers plain file url's are not supported.
535+
* You have to use the 'file', 'data', 'blob', 'http' or 'https' protocol.
534536
* @param <P> the page type
535537
* @return the new page
536538
* @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
@@ -541,7 +543,6 @@ public <P extends Page> P getPage(final URL url) throws IOException, FailingHttp
541543
final WebRequest request = new WebRequest(url, getBrowserVersion().getHtmlAcceptHeader(),
542544
getBrowserVersion().getAcceptEncodingHeader());
543545
request.setCharset(UTF_8);
544-
545546
return getPage(getCurrentWindow().getTopWindow(), request);
546547
}
547548

@@ -1546,7 +1547,8 @@ private WebResponse makeWebResponseForJavaScriptUrl(final WebWindow webWindow, f
15461547
* @return the WebResponse
15471548
*/
15481549
public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException {
1549-
switch (webRequest.getUrl().getProtocol()) {
1550+
final String protocol = webRequest.getUrl().getProtocol();
1551+
switch (protocol) {
15501552
case UrlUtils.ABOUT:
15511553
return makeWebResponseForAboutUrl(webRequest);
15521554

@@ -1559,8 +1561,12 @@ public WebResponse loadWebResponse(final WebRequest webRequest) throws IOExcepti
15591561
case "blob":
15601562
return makeWebResponseForBlobUrl(webRequest);
15611563

1562-
default:
1564+
case "http":
1565+
case "https":
15631566
return loadWebResponseFromWebConnection(webRequest, ALLOWED_REDIRECTIONS_SAME_URL);
1567+
1568+
default:
1569+
throw new IOException("Unsupported protocol '" + protocol + "'");
15641570
}
15651571
}
15661572

src/test/java/org/htmlunit/WebClient2Test.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
package org.htmlunit;
1616

1717
import static org.htmlunit.httpclient.HtmlUnitBrowserCompatCookieSpec.EMPTY_COOKIE_NAME;
18+
import static org.junit.Assert.fail;
1819

20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.net.URL;
1923
import java.time.ZonedDateTime;
2024
import java.time.format.DateTimeFormatter;
2125
import java.time.temporal.ChronoUnit;
@@ -378,4 +382,61 @@ public void toLocaleLowerCase() throws Exception {
378382
page = loadPage(html);
379383
assertEquals("\u0069", page.getTitleText());
380384
}
385+
386+
/**
387+
* This is supported by reals browsers but not with HtmlUnit.
388+
* @throws Exception if the test fails
389+
*/
390+
@Test
391+
public void localFile() throws Exception {
392+
final URL url = getClass().getClassLoader().getResource("simple.html");
393+
String file = url.getFile();
394+
if (file.startsWith("/")) {
395+
file = file.substring(1);
396+
}
397+
398+
assertTrue(new File(file).exists());
399+
400+
try (WebClient webClient = new WebClient(getBrowserVersion())) {
401+
final HtmlPage page = webClient.getPage(file);
402+
fail("IOException expected");
403+
}
404+
catch (final IOException e) {
405+
assertEquals("Unsupported protocol 'c'", e.getMessage());
406+
}
407+
}
408+
409+
/**
410+
* @throws Exception if the test fails
411+
*/
412+
@Test
413+
@Alerts("titel - simple.html")
414+
public void localFileFile() throws Exception {
415+
final URL url = getClass().getClassLoader().getResource("simple.html");
416+
String file = url.getFile();
417+
if (file.startsWith("/")) {
418+
file = file.substring(1);
419+
}
420+
421+
assertTrue(new File(file).exists());
422+
423+
try (WebClient webClient = new WebClient(getBrowserVersion())) {
424+
final HtmlPage page = webClient.getPage("file://" + file);
425+
assertEquals(getExpectedAlerts()[0], page.getTitleText());
426+
}
427+
}
428+
429+
/**
430+
* @throws Exception if the test fails
431+
*/
432+
@Test
433+
public void unknownProtocol() throws Exception {
434+
try (WebClient webClient = new WebClient(getBrowserVersion())) {
435+
final HtmlPage page = webClient.getPage("unknown://simple.html");
436+
fail("IOException expected");
437+
}
438+
catch (final IOException e) {
439+
assertEquals("Unsupported protocol 'unknown'", e.getMessage());
440+
}
441+
}
381442
}

src/test/resources/simple.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>titel - simple.html</title>
5+
</head>
6+
<body>
7+
<p>Page Content</p>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)