Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF78;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.URIBuilder;

import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
Expand All @@ -48,7 +51,7 @@
@JsxClass
public class URL extends SimpleScriptable {

private java.net.URL url_;
private URIBuilder url_;

/**
* Creates an instance.
Expand All @@ -73,15 +76,16 @@ public URL(final String url, final Object base) {

try {
if (StringUtils.isBlank(baseStr)) {
url_ = UrlUtils.toUrlUnsafe(url);
url_ = new URIBuilder(url);
}
else {
final java.net.URL baseUrl = UrlUtils.toUrlUnsafe(baseStr);
url_ = new java.net.URL(baseUrl, url);
url_ = new URIBuilder(new java.net.URL(baseUrl, url).toURI());
}
checkRemoveRedundantPort();
url_.build().toURL(); //validate
}
catch (final MalformedURLException e) {
catch (final URISyntaxException | MalformedURLException e) {
throw ScriptRuntime.typeError(e.toString());
}
}
Expand Down Expand Up @@ -121,7 +125,7 @@ public String getHash() {
if (url_ == null) {
return null;
}
final String ref = url_.getRef();
final String ref = url_.getFragment();
return ref == null ? "" : "#" + ref;
}

Expand All @@ -130,7 +134,7 @@ public void setHash(final String fragment) throws MalformedURLException {
if (url_ == null) {
return;
}
url_ = UrlUtils.getUrlWithNewRef(url_, StringUtils.isEmpty(fragment) ? null : fragment);
url_.setFragment(StringUtils.isEmpty(fragment) ? null : fragment);
}

/**
Expand Down Expand Up @@ -179,29 +183,26 @@ public void setHost(final String host) throws MalformedURLException {
// back to string
}

url_ = UrlUtils.getUrlWithNewHost(url_, newHost);
url_.setHost(newHost);

final String newPort = StringUtils.substringAfter(host, ':');
if (StringUtils.isNotBlank(newHost)) {
try {
url_ = UrlUtils.getUrlWithNewHostAndPort(url_, newHost, Integer.parseInt(newPort));
url_.setPort(Integer.parseInt(newPort));
}
catch (final Exception e) {
// back to string
}
}
else {
url_ = UrlUtils.getUrlWithNewHost(url_, newHost);
}

checkRemoveRedundantPort();
}

/** Removes port if it can be deduced from protocol */
private void checkRemoveRedundantPort() throws MalformedURLException {
if (("https".equals(url_.getProtocol()) && url_.getPort() == 443)
|| ("http".equals(url_.getProtocol()) && url_.getPort() == 80)) {
url_ = UrlUtils.getUrlWithNewPort(url_, -1);
private void checkRemoveRedundantPort() {
if (("https".equals(url_.getScheme()) && url_.getPort() == 443)
|| ("http".equals(url_.getScheme()) && url_.getPort() == 80)) {
url_.setPort(-1);
}
}

Expand All @@ -222,11 +223,11 @@ public String getHostname() {
public void setHostname(final String hostname) throws MalformedURLException {
if (getBrowserVersion().hasFeature(JS_ANCHOR_HOSTNAME_IGNORE_BLANK)) {
if (!StringUtils.isBlank(hostname)) {
url_ = UrlUtils.getUrlWithNewHost(url_, hostname);
url_.setHost(hostname);
}
}
else if (!StringUtils.isEmpty(hostname)) {
url_ = UrlUtils.getUrlWithNewHost(url_, hostname);
url_.setHost(hostname);
}
}

Expand All @@ -243,12 +244,12 @@ public String getHref() {
}

@JsxSetter
public void setHref(final String href) throws MalformedURLException {
public void setHref(final String href) throws URISyntaxException {
if (url_ == null) {
return;
}

url_ = UrlUtils.toUrlUnsafe(href);
url_ = new URIBuilder(href);
checkRemoveRedundantPort();
}

Expand All @@ -261,7 +262,7 @@ public Object getOrigin() {
return null;
}

return url_.getProtocol() + "://" + url_.getHost();
return url_.getScheme() + "://" + url_.getHost();
}

/**
Expand All @@ -273,7 +274,7 @@ public URLSearchParams getSearchParams() {
return null;
}

final URLSearchParams searchParams = new URLSearchParams(url_.getQuery());
final URLSearchParams searchParams = new URLSearchParams(url_);
searchParams.setParentScope(getParentScope());
searchParams.setPrototype(getPrototype(searchParams.getClass()));
return searchParams;
Expand All @@ -298,8 +299,9 @@ public void setPassword(final String password) throws MalformedURLException {
if (url_ == null) {
return;
}

url_ = UrlUtils.getUrlWithNewUserPassword(url_, password.isEmpty() ? null : password);
url_.setUserInfo(UrlUtils.getUserInfoWithNewUserPassword(
url_.getUserInfo(),
password.isEmpty() ? null : password));
}

/**
Expand All @@ -312,7 +314,7 @@ public String getPathname() {
}

final String path = url_.getPath();
return path.isEmpty() ? "/" : path;
return StringUtils.isBlank(path) ? "/" : path;
}

@JsxSetter
Expand All @@ -321,7 +323,7 @@ public void setPathname(final String path) throws MalformedURLException {
return;
}

url_ = UrlUtils.getUrlWithNewPath(url_, path.startsWith("/") ? path : "/" + path);
url_.setPath(path.startsWith("/") ? path : "/" + path);
}

/**
Expand All @@ -344,7 +346,7 @@ public void setPort(final String port) throws MalformedURLException {
return;
}
final int portInt = port.isEmpty() ? -1 : Integer.parseInt(port);
url_ = UrlUtils.getUrlWithNewPort(url_, portInt);
url_.setPort(portInt);
checkRemoveRedundantPort();
}

Expand All @@ -356,7 +358,7 @@ public String getProtocol() {
if (url_ == null) {
return null;
}
final String protocol = url_.getProtocol();
final String protocol = url_.getScheme();
return protocol.isEmpty() ? "" : (protocol + ":");
}

Expand All @@ -366,13 +368,8 @@ public void setProtocol(final String protocol) throws MalformedURLException {
return;
}

try {
url_ = UrlUtils.getUrlWithNewProtocol(url_, protocol);
checkRemoveRedundantPort();
}
catch (final MalformedURLException e) {
// ignore
}
url_.setScheme(protocol.toLowerCase());
checkRemoveRedundantPort();
}

/**
Expand All @@ -383,8 +380,12 @@ public String getSearch() {
if (url_ == null) {
return null;
}
final String search = url_.getQuery();
return search == null ? "" : "?" + search;
final String search = url_.getQueryParams().stream()
.map(pair -> pair.getValue() != null
? String.format("%s=%s", pair.getName(), pair.getValue())
: pair.getName())
.collect(Collectors.joining("&"));
return StringUtils.isBlank(search) ? "" : "?" + search;
}

@JsxSetter
Expand All @@ -404,7 +405,7 @@ else if (search.charAt(0) == '?') {
query = search;
}

url_ = UrlUtils.getUrlWithNewQuery(url_, query);
url_.setQuery(query);
}

/**
Expand All @@ -429,7 +430,7 @@ public void setUsername(final String username) throws MalformedURLException {
if (url_ == null) {
return;
}
url_ = UrlUtils.getUrlWithNewUserName(url_, username.isEmpty() ? null : username);
url_.setUserInfo(UrlUtils.getUserInfoWithNewUserName(url_.getUserInfo(), username));
}

/**
Expand All @@ -445,9 +446,9 @@ public Object getDefaultValue(final Class<?> hint) {
}

if (StringUtils.isEmpty(url_.getPath())) {
return url_.toExternalForm() + "/";
return url_.toString() + "/";
}
return url_.toExternalForm();
return url_.toString();
}

/**
Expand All @@ -467,12 +468,11 @@ public String toJSON() {
public String jsToString() {
if (StringUtils.isEmpty(url_.getPath())) {
try {
return UrlUtils.getUrlWithNewPath(url_, "/").toExternalForm();
}
catch (final MalformedURLException e) {
return url_.toExternalForm();
return UrlUtils.getUrlWithNewPath(url_.build().toURL(), "/").toExternalForm();
} catch (final URISyntaxException | MalformedURLException e) {
return url_.toString();
}
}
return url_.toExternalForm();
return url_.toString();
}
}
Loading