下面列出了java.net.URL#getHost ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Use xml:base attributes at feed and entry level to resolve relative links
*/
private static String resolveURI(final URL baseURI, final Parent parent, String url) {
url = url.equals(".") || url.equals("./") ? "" : url;
if (isRelativeURI(url) && parent != null && parent instanceof Element) {
final Attribute baseAtt = ((Element) parent).getAttribute("base", Namespace.XML_NAMESPACE);
String xmlBase = baseAtt == null ? "" : baseAtt.getValue();
if (!isRelativeURI(xmlBase) && !xmlBase.endsWith("/")) {
xmlBase = xmlBase.substring(0, xmlBase.lastIndexOf("/") + 1);
}
return resolveURI(baseURI, parent.getParent(), xmlBase + url);
} else if (isRelativeURI(url) && parent == null) {
return baseURI + url;
} else if (baseURI != null && url.startsWith("/")) {
String hostURI = baseURI.getProtocol() + "://" + baseURI.getHost();
if (baseURI.getPort() != baseURI.getDefaultPort()) {
hostURI = hostURI + ":" + baseURI.getPort();
}
return hostURI + url;
}
return url;
}
/**
* Same as previous constructor except using a Proxy
*/
HttpsClient(SSLSocketFactory sf, URL url, Proxy proxy,
int connectTimeout)
throws IOException {
this.proxy = proxy;
setSSLSocketFactory(sf);
this.proxyDisabled = true;
this.host = url.getHost();
this.url = url;
port = url.getPort();
if (port == -1) {
port = getDefaultPort();
}
setConnectTimeout(connectTimeout);
openServer();
}
private String getHostAndPort(URL url) {
String host = url.getHost();
final String hostarg = host;
try {
// lookup hostname and use IP address if available
host = AccessController.doPrivileged(
new PrivilegedExceptionAction<String>() {
public String run() throws IOException {
InetAddress addr = InetAddress.getByName(hostarg);
return addr.getHostAddress();
}
}
);
} catch (PrivilegedActionException e) {}
int port = url.getPort();
if (port == -1) {
String scheme = url.getProtocol();
if ("http".equals(scheme)) {
return host + ":80";
} else { // scheme must be https
return host + ":443";
}
}
return host + ":" + Integer.toString(port);
}
static String getLocation(HttpURLConnection connection, String path) throws MalformedURLException {
if (connection == null || TextUtils.isEmpty(path)) {
return null;
}
String location = connection.getHeaderField("Location");
if (TextUtils.isEmpty(location)) {
location = connection.getHeaderField("location");
}
if (TextUtils.isEmpty(location)) {
return null;
}
if (!(location.startsWith("http://") || location
.startsWith("https://"))) {
//某些时候会省略host,只返回后面的path,所以需要补全url
URL originUrl = new URL(path);
location = originUrl.getProtocol() + "://"
+ originUrl.getHost() + location;
}
return location;
}
public void wrappCookie(boolean inResponse) {
if (inResponse) {
HttpServletResponse resp = responseHolder.get();
HttpServletRequest req = requestHolder.get();
if (resp != null) {
AVUser u = AVUser.getCurrentUser();
String host = null;
try {
URL requestURL = new URL(req.getRequestURL().toString());
host = requestURL.getHost();
} catch (Exception e) {
LOGGER.w(e);
}
addCookie(req, resp, sign.encodeUser(u));
addCookie(req, resp, sign.getCookieSign(u));
}
} else {
responseHolder.set(null);
}
}
private String getHostAndPort(URL url) {
String host = url.getHost();
final String hostarg = host;
try {
// lookup hostname and use IP address if available
host = AccessController.doPrivileged(
new PrivilegedExceptionAction<String>() {
public String run() throws IOException {
InetAddress addr = InetAddress.getByName(hostarg);
return addr.getHostAddress();
}
}
);
} catch (PrivilegedActionException e) {}
int port = url.getPort();
if (port == -1) {
String scheme = url.getProtocol();
if ("http".equals(scheme)) {
return host + ":80";
} else { // scheme must be https
return host + ":443";
}
}
return host + ":" + Integer.toString(port);
}
public static OutputStream createOutputStream(String uri) throws IOException {
// URI was specified. Handle relative URIs.
final String expanded = XMLEntityManager.expandSystemId(uri, null, true);
final URL url = new URL(expanded != null ? expanded : uri);
OutputStream out = null;
String protocol = url.getProtocol();
String host = url.getHost();
// Use FileOutputStream if this URI is for a local file.
if (protocol.equals("file")
&& (host == null || host.length() == 0 || host.equals("localhost"))) {
File file = new File(getPathWithoutEscapes(url.getPath()));
if (!file.exists()) {
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
}
out = new FileOutputStream(file);
}
// Try to write to some other kind of URI. Some protocols
// won't support this, though HTTP should work.
else {
URLConnection urlCon = url.openConnection();
urlCon.setDoInput(false);
urlCon.setDoOutput(true);
urlCon.setUseCaches(false); // Enable tunneling.
if (urlCon instanceof HttpURLConnection) {
// The DOM L3 REC says if we are writing to an HTTP URI
// it is to be done with an HTTP PUT.
HttpURLConnection httpCon = (HttpURLConnection) urlCon;
httpCon.setRequestMethod("PUT");
}
out = urlCon.getOutputStream();
}
return out;
}
public JsonNode call(JsonNode input, JsonNode[] arguments) {
if (arguments[0].isNull())
return NullNode.instance;
String urlString = arguments[0].asText();
try {
URL aURL = new URL(arguments[0].asText());
final ObjectNode objectNode = NodeUtils.mapper.createObjectNode();
if (aURL.getHost() != null && !aURL.getHost().isEmpty())
objectNode.put("host", aURL.getHost());
if (aURL.getPort() != -1)
objectNode.put("port", aURL.getPort());
if (!aURL.getPath().isEmpty())
objectNode.put("path", aURL.getPath());
if (aURL.getProtocol() != null && !aURL.getProtocol().isEmpty())
objectNode.put("scheme", aURL.getProtocol());
if (aURL.getQuery() != null && !aURL.getQuery().isEmpty()) {
objectNode.put("query", aURL.getQuery());
final ObjectNode queryParamsNode = NodeUtils.mapper.createObjectNode();
objectNode.set("parameters", queryParamsNode);
final String[] pairs = aURL.getQuery().split("&");
for (String pair : pairs) {
final int idx = pair.indexOf("=");
final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
if (!queryParamsNode.has(key)) queryParamsNode.set(key, NodeUtils.mapper.createArrayNode());
final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
final ArrayNode valuesNode = (ArrayNode) queryParamsNode.get(key);
valuesNode.add(value);
}
}
if(aURL.getRef() != null)
objectNode.put("fragment", aURL.getRef());
if(aURL.getUserInfo() != null && !aURL.getUserInfo().isEmpty())
objectNode.put("userinfo", aURL.getUserInfo());
return objectNode;
} catch (MalformedURLException | UnsupportedEncodingException e) {
throw new JsltException("Can't parse " + urlString, e);
}
}
/**
* 上传文件
* @param file 文件
*/
public String uploadFile(MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
ossClient.putObject(BUCKET_NAME, fileName,file.getInputStream());
URL url = ossClient.generatePresignedUrl(
BUCKET_NAME,
fileName,
DateUtil.convertLdtToDate(DateUtil.getPlusMonths(99999)),
HttpMethod.GET);
return "https://"+url.getHost()+url.getPath();
}
private static String getFullContext(final String address, final String context) {
if (context == null) {
return address;
}
if (context.isEmpty() && address.contains("/")) {
return address.substring(0, address.lastIndexOf("/"));
}
// context can get the app path too
// so keep only web context without /
String webCtx = context;
while (webCtx.startsWith("/")) {
webCtx = webCtx.substring(1);
}
// get root path ending with /
try {
final URL url = new URL(address);
final int port = url.getPort();
if (port > 0) {
return url.getProtocol() + "://" + url.getHost() + ":" + port + "/" + webCtx;
} else {
return url.getProtocol() + "://" + url.getHost() + "/" + webCtx;
}
} catch (final MalformedURLException e) {
throw new OpenEJBRestRuntimeException("bad url: " + address, e);
}
}
public static String getDomainName(String url) throws MalformedURLException{
if(!url.startsWith("http") && !url.startsWith("https")){
url = "http://" + url;
}
URL netUrl = new URL(url);
String host = netUrl.getHost();
if(host.startsWith("www")){
host = host.substring("www".length()+1);
}
return host;
}
/**
* Get the protocol and the host, example value <code>https://www.abcdef.xyz</code>
*
* @param url server url
* @return protocolAndHost
*/
public static String getProtocolAndHost(URL url) {
if (url == null) {
return LOCAL_HOST;
} else {
String protocol = (url.getProtocol() == null) ? "http" : url.getProtocol();
return protocol + "://" + url.getHost();
}
}
/**
* Helper that builds a CookieOrigin.
* @param url the url to be used
* @return the new CookieOrigin
*/
public CookieOrigin buildCookieOrigin(final URL url) {
final URL normalizedUrl = replaceForCookieIfNecessary(url);
return new CookieOrigin(
normalizedUrl.getHost(),
getPort(normalizedUrl),
normalizedUrl.getPath(),
"https".equals(normalizedUrl.getProtocol()));
}
private String sanitizeUrl(String urlStr) {
if (urlStr == null) return null;
try {
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
return uri.toURL().toString();
} catch (Exception e) {
Logger.e(TAG, "sanitize url", e);
return urlStr;
}
}
private RestHighLevelClient createEsClient() {
HttpHost[] hosts = new HttpHost[config.esHosts.length];
int i = 0;
for (String host : config.esHosts) {
try {
URL url = new URL(host);
hosts[i] = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
i++;
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
SniffOnFailureListener sniffOnFailureListener =
new SniffOnFailureListener();
RestClientBuilder builder =
RestClient.builder(hosts)
.setRequestConfigCallback(
requestConfigBuilder ->
requestConfigBuilder
.setConnectTimeout(config.esConnectTimeout)
.setSocketTimeout(config.esSocketTimeout))
.setMaxRetryTimeoutMillis(config.esSocketTimeout)
.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS)
.setFailureListener(sniffOnFailureListener);
RestHighLevelClient highLevelClient = new RestHighLevelClient(builder);
esSniffer =
Sniffer.builder(highLevelClient.getLowLevelClient())
.setSniffIntervalMillis(config.esSniffInterval)
.setSniffAfterFailureDelayMillis(config.esSniffAfterFailureDelay)
.build();
sniffOnFailureListener.setSniffer(esSniffer);
return highLevelClient;
}
private Proxy getHttpsProxy(String httpsProxy) {
Proxy secureProxy = null;
if (httpsProxy != null) {
URL url = url(httpsProxy);
Authentication authentication = extractAuth(url);
secureProxy = new Proxy("https", url.getHost(), url.getPort(), authentication);
}
return secureProxy;
}
@RunOnlyOnce
public List<IScanIssue> scan(IBurpExtenderCallbacks callbacks, IHttpRequestResponse baseRequestResponse, IScannerInsertionPoint insertionPoint) {
IExtensionHelpers helpers = callbacks.getHelpers();
stderr = new PrintWriter(callbacks.getStderr(), true);
List<IScanIssue> issues = new ArrayList<>();
IRequestInfo reqInfo = helpers.analyzeRequest(baseRequestResponse);
URL url = reqInfo.getUrl();
String host = url.getHost();
int port = url.getPort();
String protocol = url.getProtocol();
Boolean isSSL = (protocol.equals("https"));
String system = host.concat(Integer.toString(port));
// Collaborator context
IBurpCollaboratorClientContext collaboratorContext = callbacks.createBurpCollaboratorClientContext();
String currentCollaboratorPayload = collaboratorContext.generatePayload(true);
String Oracle_SSRF_Help = String.format("/OA_HTML/help?locale=en_AE&group=per:br_prod_HR:US&topic=http://%s:80/", currentCollaboratorPayload);
// System not yet tested for this vulnerability
if (!hs.contains(system)) {
hs.add(system);
try {
URL urlToTest = new URL(protocol, url.getHost(), url.getPort(), Oracle_SSRF_Help);
byte[] helpSSRFtest = helpers.buildHttpRequest(urlToTest);
byte[] responseBytes = callbacks.makeHttpRequest(url.getHost(),
url.getPort(), isSSL, helpSSRFtest);
// Poll Burp Collaborator for remote interaction
List<IBurpCollaboratorInteraction> collaboratorInteractions = collaboratorContext.fetchCollaboratorInteractionsFor(currentCollaboratorPayload);
if (!collaboratorInteractions.isEmpty()) {
issues.add(new CustomScanIssue(
baseRequestResponse.getHttpService(),
urlToTest,
new CustomHttpRequestResponse(helpSSRFtest, responseBytes, baseRequestResponse.getHttpService()),
TITLE,
DESCRIPTION,
REMEDY,
Risk.High,
Confidence.Certain
));
}
} catch (MalformedURLException ex) {
stderr.println("Malformed URL Exception " + ex);
}
}
return issues;
}
private void getAccessToken() throws Exception {
SocialAuthConfig config = SocialAuthConfig.getDefault();
config.load();
SocialAuthManager manager = new SocialAuthManager();
manager.setSocialAuthConfig(config);
URL aURL = new URL(successURL);
host = aURL.getHost();
port = aURL.getPort();
port = port == -1 ? 80 : port;
callbackPath = aURL.getPath();
if (tokenFilePath == null) {
tokenFilePath = System.getProperty("user.home");
}
String url = manager.getAuthenticationUrl(providerId, successURL);
startServer();
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Action.BROWSE)) {
try {
desktop.browse(URI.create(url));
// return;
} catch (IOException e) {
// handled below
}
}
}
lock.lock();
try {
while (paramsMap == null && error == null) {
gotAuthorizationResponse.awaitUninterruptibly();
}
if (error != null) {
throw new IOException("User authorization failed (" + error
+ ")");
}
} finally {
lock.unlock();
}
stop();
AccessGrant accessGrant = manager.createAccessGrant(providerId,
paramsMap, successURL);
Exporter.exportAccessGrant(accessGrant, tokenFilePath);
LOG.info("Access Grant Object saved in a file :: " + tokenFilePath
+ File.separatorChar + accessGrant.getProviderId()
+ "_accessGrant_file.txt");
}
public static String constructAbsoluteTempFilePath(final IScope scope, final URL url) {
return CACHE.getAbsolutePath() + SEPARATOR + url.getHost() + URL_SEPARATOR_REPLACEMENT
+ url.getPath().replace(SEPARATOR, URL_SEPARATOR_REPLACEMENT);
}
/**
* Compares the host components of two URLs.
* @param u1 the URL of the first host to compare
* @param u2 the URL of the second host to compare
* @return <tt>true</tt> if and only if they
* are equal, <tt>false</tt> otherwise.
*/
@Override
protected boolean hostsEqual(URL u1, URL u2) {
final String s1 = u1.getHost();
final String s2 = u2.getHost();
return (s1 == s2) || (s1 != null && s1.equals(s2));
}