/** * Gets the host and port from the specified request. Returns the host and port from the request URI if available, * otherwise retrieves the host and port from the Host header. * * @param httpRequest HTTP request * @return host and port of the request */ public static String getHostAndPortFromRequest(HttpRequest httpRequest) { if (startsWithHttpOrHttps(httpRequest.getUri())) { try { return getHostAndPortFromUri(httpRequest.getUri()); } catch (URISyntaxException e) { // the URI could not be parsed, so return the host and port in the Host header } } return parseHostHeader(httpRequest, true); }
private void populateServerIpAddress(HarEntry harEntry) { // populate the server IP address if it was resolved as part of this request. otherwise, populate the IP address from the cache. if (resolvedAddress != null) { harEntry.setServerIPAddress(resolvedAddress.getHostAddress()); } else { String serverHost = HttpUtil.getHostFromRequest(modifiedHttpRequest); if (serverHost != null && !serverHost.isEmpty()) { String resolvedAddress = ResolvedHostnameCacheFilter.getPreviouslyResolvedAddressForHost(serverHost); if (resolvedAddress != null) { harEntry.setServerIPAddress(resolvedAddress); } else { // the resolvedAddress may be null if the ResolvedHostnameCacheFilter has expired the entry (which is unlikely), // or in the far more common case that the proxy is using a chained proxy to connect to connect to the // remote host. since the chained proxy handles IP address resolution, the IP address in the HAR must be blank. log.trace("Unable to find cached IP address for host: {}. IP address in HAR entry will be blank.", serverHost); } } else { log.warn("Unable to identify host from request uri: {}", modifiedHttpRequest.getUri()); } } }
/** * Returns the host and port of the specified request for both HTTP and HTTPS requests. The request may reflect * modifications from this or other filters. This filter instance must be currently handling the specified request; * otherwise the results are undefined. * * @param modifiedRequest a possibly-modified version of the request currently being processed * @return host and port of the specified request */ public String getHostAndPort(HttpRequest modifiedRequest) { // For HTTP requests, the host and port can be read from the request itself using the URI and/or // Host header. for HTTPS requests, the host and port are not available in the request. by using the // getHttpsRequestHostAndPort() helper method, we can retrieve the host and port for HTTPS requests. if (isHttps()) { return getHttpsRequestHostAndPort(); } else { return HttpUtil.getHostAndPortFromRequest(modifiedRequest); } }
/** * Identify the host of an HTTP request. This method uses the URI of the request if possible, otherwise it attempts to find the host * in the request headers. * * @param httpRequest HTTP request to parse the host from * @return the host the request is connecting to, or null if no host can be found */ public static String getHostFromRequest(HttpRequest httpRequest) { // try to use the URI from the request first, if the URI starts with http:// or https://. checking for http/https avoids confusing // java's URI class when the request is for a malformed URL like '//some-resource'. String host = null; if (startsWithHttpOrHttps(httpRequest.getUri())) { try { URI uri = new URI(httpRequest.getUri()); host = uri.getHost(); } catch (URISyntaxException e) { } } // if there was no host in the URI, attempt to grab the host from the Host header if (host == null || host.isEmpty()) { host = parseHostHeader(httpRequest, false); } return host; }
if (HttpUtil.startsWithHttpOrHttps(uriFromRequest)) { httpRequest.setUri(rewrittenUrl); } else { originalHostAndPort = HttpUtil.getHostAndPortFromUri(originalUrl); } catch (URISyntaxException e) { modifiedHostAndPort = HttpUtil.getHostAndPortFromUri(rewrittenUrl); } catch (URISyntaxException e) { log.warn("Unable to determine host and port from rewritten URL. Host header will not be updated.\n\tOriginal URL: {}\n\tRewritten URL: {}",
/** * Retrieves the raw (unescaped) path + query string from the specified request. The returned path will not include * the scheme, host, or port. * * @param httpRequest HTTP request * @return the unescaped path + query string from the HTTP request * @throws URISyntaxException if the path could not be parsed (due to invalid characters in the URI, etc.) */ public static String getRawPathAndParamsFromRequest(HttpRequest httpRequest) throws URISyntaxException { // if this request's URI contains a full URI (including scheme, host, etc.), strip away the non-path components if (HttpUtil.startsWithHttpOrHttps(httpRequest.getUri())) { return getRawPathAndParamsFromUri(httpRequest.getUri()); } else { // to provide consistent validation behavior for URIs that contain a scheme and those that don't, attempt to parse // the URI, even though we discard the parsed URI object new URI(httpRequest.getUri()); return httpRequest.getUri(); } }
if (HttpUtil.startsWithHttpOrHttps(uriFromRequest)) { httpRequest.setUri(rewrittenUrl); } else { originalHostAndPort = HttpUtil.getHostAndPortFromUri(originalUrl); } catch (URISyntaxException e) { modifiedHostAndPort = HttpUtil.getHostAndPortFromUri(rewrittenUrl); } catch (URISyntaxException e) { log.warn("Unable to determine host and port from rewritten URL. Host header will not be updated.\n\tOriginal URL: {}\n\tRewritten URL: {}",
/** * Identify the host of an HTTP request. This method uses the URI of the request if possible, otherwise it attempts to find the host * in the request headers. * * @param httpRequest HTTP request to parse the host from * @return the host the request is connecting to, or null if no host can be found */ public static String getHostFromRequest(HttpRequest httpRequest) { // try to use the URI from the request first, if the URI starts with http:// or https://. checking for http/https avoids confusing // java's URI class when the request is for a malformed URL like '//some-resource'. String host = null; if (startsWithHttpOrHttps(httpRequest.getUri())) { try { URI uri = new URI(httpRequest.getUri()); host = uri.getHost(); } catch (URISyntaxException e) { } } // if there was no host in the URI, attempt to grab the host from the Host header if (host == null || host.isEmpty()) { host = parseHostHeader(httpRequest, false); } return host; }
if (HttpUtil.startsWithHttpOrHttps(modifiedRequest.getUri())) { return modifiedRequest.getUri();
/** * Gets the host and port from the specified request. Returns the host and port from the request URI if available, * otherwise retrieves the host and port from the Host header. * * @param httpRequest HTTP request * @return host and port of the request */ public static String getHostAndPortFromRequest(HttpRequest httpRequest) { if (startsWithHttpOrHttps(httpRequest.getUri())) { try { return getHostAndPortFromUri(httpRequest.getUri()); } catch (URISyntaxException e) { // the URI could not be parsed, so return the host and port in the Host header } } return parseHostHeader(httpRequest, true); }
if (HttpUtil.startsWithHttpOrHttps(uriFromRequest)) { httpRequest.setUri(rewrittenUrl); } else { originalHostAndPort = HttpUtil.getHostAndPortFromUri(originalUrl); } catch (URISyntaxException e) { modifiedHostAndPort = HttpUtil.getHostAndPortFromUri(rewrittenUrl); } catch (URISyntaxException e) { log.warn("Unable to determine host and port from rewritten URL. Host header will not be updated.\n\tOriginal URL: {}\n\tRewritten URL: {}",
@Override public SSLEngine clientSslEngineFor(HttpRequest httpRequest, SSLSession sslSession) { String requestedHostname = HttpUtil.getHostFromRequest(httpRequest); try { SslContext ctx = getHostnameImpersonatingSslContext(requestedHostname, sslSession); return ctx.newEngine(ByteBufAllocator.DEFAULT); } catch (RuntimeException e) { throw new MitmException("Error creating SSLEngine for connection to client to impersonate upstream host: " + requestedHostname, e); } }
/** * Identify the host of an HTTP request. This method uses the URI of the request if possible, otherwise it attempts to find the host * in the request headers. * * @param httpRequest HTTP request to parse the host from * @return the host the request is connecting to, or null if no host can be found */ public static String getHostFromRequest(HttpRequest httpRequest) { // try to use the URI from the request first, if the URI starts with http:// or https://. checking for http/https avoids confusing // java's URI class when the request is for a malformed URL like '//some-resource'. String host = null; if (startsWithHttpOrHttps(httpRequest.getUri())) { try { URI uri = new URI(httpRequest.getUri()); host = uri.getHost(); } catch (URISyntaxException e) { } } // if there was no host in the URI, attempt to grab the host from the Host header if (host == null || host.isEmpty()) { host = parseHostHeader(httpRequest, false); } return host; }
/** * Retrieves the raw (unescaped) path + query string from the specified request. The returned path will not include * the scheme, host, or port. * * @param httpRequest HTTP request * @return the unescaped path + query string from the HTTP request * @throws URISyntaxException if the path could not be parsed (due to invalid characters in the URI, etc.) */ public static String getRawPathAndParamsFromRequest(HttpRequest httpRequest) throws URISyntaxException { // if this request's URI contains a full URI (including scheme, host, etc.), strip away the non-path components if (HttpUtil.startsWithHttpOrHttps(httpRequest.getUri())) { return getRawPathAndParamsFromUri(httpRequest.getUri()); } else { // to provide consistent validation behavior for URIs that contain a scheme and those that don't, attempt to parse // the URI, even though we discard the parsed URI object new URI(httpRequest.getUri()); return httpRequest.getUri(); } }
/** * Returns the host and port of the specified request for both HTTP and HTTPS requests. The request may reflect * modifications from this or other filters. This filter instance must be currently handling the specified request; * otherwise the results are undefined. * * @param modifiedRequest a possibly-modified version of the request currently being processed * @return host and port of the specified request */ public String getHostAndPort(HttpRequest modifiedRequest) { // For HTTP requests, the host and port can be read from the request itself using the URI and/or // Host header. for HTTPS requests, the host and port are not available in the request. by using the // getHttpsRequestHostAndPort() helper method, we can retrieve the host and port for HTTPS requests. if (isHttps()) { return getHttpsRequestHostAndPort(); } else { return HttpUtil.getHostAndPortFromRequest(modifiedRequest); } }
/** * Gets the host and port from the specified request. Returns the host and port from the request URI if available, * otherwise retrieves the host and port from the Host header. * * @param httpRequest HTTP request * @return host and port of the request */ public static String getHostAndPortFromRequest(HttpRequest httpRequest) { if (startsWithHttpOrHttps(httpRequest.getUri())) { try { return getHostAndPortFromUri(httpRequest.getUri()); } catch (URISyntaxException e) { // the URI could not be parsed, so return the host and port in the Host header } } return parseHostHeader(httpRequest, true); }
if (HttpUtil.startsWithHttpOrHttps(uriFromRequest)) { httpRequest.setUri(rewrittenUrl); } else { originalHostAndPort = HttpUtil.getHostAndPortFromUri(originalUrl); } catch (URISyntaxException e) { modifiedHostAndPort = HttpUtil.getHostAndPortFromUri(rewrittenUrl); } catch (URISyntaxException e) { log.warn("Unable to determine host and port from rewritten URL. Host header will not be updated.\n\tOriginal URL: {}\n\tRewritten URL: {}",
/** * Returns the hostname (but not the port) the specified request for both HTTP and HTTPS requests. The request may reflect * modifications from this or other filters. This filter instance must be currently handling the specified request; * otherwise the results are undefined. * * @param modifiedRequest a possibly-modified version of the request currently being processed * @return hostname of the specified request, without the port */ public String getHost(HttpRequest modifiedRequest) { String serverHost; if (isHttps()) { HostAndPort hostAndPort = HostAndPort.fromString(getHttpsRequestHostAndPort()); serverHost = hostAndPort.getHost(); } else { serverHost = HttpUtil.getHostFromRequest(modifiedRequest); } return serverHost; }
/** * Retrieves the raw (unescaped) path + query string from the specified request. The returned path will not include * the scheme, host, or port. * * @param httpRequest HTTP request * @return the unescaped path + query string from the HTTP request * @throws URISyntaxException if the path could not be parsed (due to invalid characters in the URI, etc.) */ public static String getRawPathAndParamsFromRequest(HttpRequest httpRequest) throws URISyntaxException { // if this request's URI contains a full URI (including scheme, host, etc.), strip away the non-path components if (HttpUtil.startsWithHttpOrHttps(httpRequest.getUri())) { return getRawPathAndParamsFromUri(httpRequest.getUri()); } else { // to provide consistent validation behavior for URIs that contain a scheme and those that don't, attempt to parse // the URI, even though we discard the parsed URI object new URI(httpRequest.getUri()); return httpRequest.getUri(); } }
/** * Returns the host and port of the specified request for both HTTP and HTTPS requests. The request may reflect * modifications from this or other filters. This filter instance must be currently handling the specified request; * otherwise the results are undefined. * * @param modifiedRequest a possibly-modified version of the request currently being processed * @return host and port of the specified request */ public String getHostAndPort(HttpRequest modifiedRequest) { // For HTTP requests, the host and port can be read from the request itself using the URI and/or // Host header. for HTTPS requests, the host and port are not available in the request. by using the // getHttpsRequestHostAndPort() helper method, we can retrieve the host and port for HTTPS requests. if (isHttps()) { return getHttpsRequestHostAndPort(); } else { return HttpUtil.getHostAndPortFromRequest(modifiedRequest); } }