/* SOURCE: https://raw.github.com/lightbody/browsermob-proxy/7f0a6ec2663bace3f64c878e7f006090c38fbfdc/src/main/java/net/lightbody/bmp/proxy/BrowserMobProxyHandler.java ORIGINAL LICENSE: Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. GROUPON LICENSE: Copyright 2014 Groupon, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.groupon.odo.bmp; import com.groupon.odo.proxylib.Constants; import com.groupon.odo.proxylib.ServerRedirectService; import java.io.File; import java.io.IOException; import java.net.BindException; import java.net.ConnectException; import java.net.InetAddress; import java.net.Socket; import java.net.URL; import java.net.UnknownHostException; import java.security.cert.X509Certificate; import java.util.Date; import java.util.Enumeration; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.MediaType; import net.lightbody.bmp.proxy.FirefoxErrorConstants; import net.lightbody.bmp.proxy.FirefoxErrorContent; import net.lightbody.bmp.proxy.http.BadURIException; import net.lightbody.bmp.proxy.jetty.http.EOFException; import net.lightbody.bmp.proxy.jetty.http.HttpConnection; import net.lightbody.bmp.proxy.jetty.http.HttpException; import net.lightbody.bmp.proxy.jetty.http.HttpFields; import net.lightbody.bmp.proxy.jetty.http.HttpListener; import net.lightbody.bmp.proxy.jetty.http.HttpMessage; import net.lightbody.bmp.proxy.jetty.http.HttpRequest; import net.lightbody.bmp.proxy.jetty.http.HttpResponse; import net.lightbody.bmp.proxy.jetty.http.HttpServer; import net.lightbody.bmp.proxy.jetty.http.HttpTunnel; import net.lightbody.bmp.proxy.jetty.http.SocketListener; import net.lightbody.bmp.proxy.jetty.http.SslListener; import net.lightbody.bmp.proxy.jetty.jetty.Server; import net.lightbody.bmp.proxy.jetty.util.InetAddrPort; import net.lightbody.bmp.proxy.jetty.util.URI; import net.lightbody.bmp.proxy.selenium.KeyStoreManager; import net.lightbody.bmp.proxy.selenium.LauncherUtils; import net.lightbody.bmp.proxy.selenium.SeleniumProxyHandler; import net.lightbody.bmp.proxy.util.Log; import okio.BufferedSink; import org.apache.commons.io.IOUtils; import org.apache.http.NoHttpResponseException; import org.apache.http.conn.ConnectTimeoutException; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLProtocolException; import java.security.cert.CertificateException; public class BrowserMobProxyHandler extends SeleniumProxyHandler { private static final Log LOG = new Log(); private static final int HEADER_BUFFER_DEFAULT = 2; private static final long serialVersionUID = 1L; private final String localIP = "127.0.0.1"; private Server jettyServer; private int headerBufferMultiplier = HEADER_BUFFER_DEFAULT; protected final Set<SslRelay> sslRelays = new HashSet<SslRelay>(); // BEGIN ODO CHANGES // Map to hold onto listeners per hostname protected final Map<String, SslRelayOdo> _sslMap = new LinkedHashMap<String, SslRelayOdo>(); protected final Map<String, Date> _certExpirationMap = new LinkedHashMap<String, Date>(); // END ODO CHANGES public BrowserMobProxyHandler() { super(true, "", "", false, false); setShutdownLock(new Object()); // set the tunnel timeout to something larger than the default 30 seconds // we're doing this because SSL connections taking longer than this timeout // will result in a socket connection close that does NOT get handled by the // normal socket connection closing reportError(). Further, it has been seen // that Firefox will actually retry the connection, causing very strange // behavior observed in case http://browsermob.assistly.com/agent/case/27843 // // You can also reproduce it by simply finding some slow loading SSL site // that takes greater than 30 seconds to response. // // Finally, it should be noted that we're setting this timeout to some value // that we anticipate will be larger than any reasonable response time of a // real world request. We don't set it to -1 because the underlying ProxyHandler // will not use it if it's <= 0. We also don't set it to Long.MAX_VALUE because // we don't yet know if this will cause a serious resource drain, so we're // going to try something like 5 minutes for now. setTunnelTimeoutMs(300000); } // BEGIN ODO CHANGES private static final ThreadLocal<String> requestOriginalHostName = new ThreadLocal<String>(); private static final ThreadLocal<URI> requestOriginalURI = new ThreadLocal<URI>(); @Override /** * This is the original handleConnect from BrowserMobProxyHandler with the following changes: * * 1. Store the original URI in a ThreadLocal so that we can determine the host addr later * 2. Store the original hostname in a ThreadLocal so we don't need to do the same string processing again later * 2. Set the URI to 127.0.0.1 to pass into Odo * 3. Call the original handleConnect from SeleniumProxyHandler(copied to handle an Odo SslRelay) */ public void handleConnect(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws HttpException, IOException { URI uri = request.getURI(); String original = uri.toString(); LOG.info("Hostname: " + original); String host = original; int colon = original.indexOf(':'); if (colon != -1) { host = original.substring(0, colon); } // store the original host name requestOriginalHostName.set(host); // make a copy of the URI(have to create a new URI otherwise things are copied by reference and get changed) URI realURI = new URI(request.getURI()); requestOriginalURI.set(realURI); // send requests to Odo HTTPS port int httpsPort = com.groupon.odo.proxylib.Utils.getSystemPort(Constants.SYS_HTTPS_PORT); uri.setURI("127.0.0.1:" + httpsPort); uri.setPort(httpsPort); handleConnectOriginal(pathInContext, pathParams, request, response); } // END ODO CHANGES // BEGIN ODO CHANGES /** * Copied from original SeleniumProxyHandler * Changed SslRelay to SslListener and getSslRelayOrCreateNew to getSslRelayOrCreateNewOdo * No other changes to the function * * @param pathInContext * @param pathParams * @param request * @param response * @throws HttpException * @throws IOException */ public void handleConnectOriginal(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws HttpException, IOException { URI uri = request.getURI(); try { LOG.fine("CONNECT: " + uri); InetAddrPort addrPort; // When logging, we'll attempt to send messages to hosts that don't exist if (uri.toString().endsWith(".selenium.doesnotexist:443")) { // so we have to do set the host to be localhost (you can't new up an IAP with a non-existent hostname) addrPort = new InetAddrPort(443); } else { addrPort = new InetAddrPort(uri.toString()); } if (isForbidden(HttpMessage.__SSL_SCHEME, addrPort.getHost(), addrPort.getPort(), false)) { sendForbid(request, response, uri); } else { HttpConnection http_connection = request.getHttpConnection(); http_connection.forceClose(); HttpServer server = http_connection.getHttpServer(); SslListener listener = getSslRelayOrCreateNewOdo(uri, addrPort, server); int port = listener.getPort(); // Get the timeout int timeoutMs = 30000; Object maybesocket = http_connection.getConnection(); if (maybesocket instanceof Socket) { Socket s = (Socket) maybesocket; timeoutMs = s.getSoTimeout(); } // Create the tunnel HttpTunnel tunnel = newHttpTunnel(request, response, InetAddress.getByName(null), port, timeoutMs); if (tunnel != null) { // TODO - need to setup semi-busy loop for IE. if (_tunnelTimeoutMs > 0) { tunnel.getSocket().setSoTimeout(_tunnelTimeoutMs); if (maybesocket instanceof Socket) { Socket s = (Socket) maybesocket; s.setSoTimeout(_tunnelTimeoutMs); } } tunnel.setTimeoutMs(timeoutMs); customizeConnection(pathInContext, pathParams, request, tunnel.getSocket()); request.getHttpConnection().setHttpTunnel(tunnel); response.setStatus(HttpResponse.__200_OK); response.setContentLength(0); } request.setHandled(true); } } catch (Exception e) { LOG.fine("error during handleConnect", e); response.sendError(HttpResponse.__500_Internal_Server_Error, e.toString()); } } // END ODO CHANGES // BEGIN ODO CHANGES /** * This function wires up a SSL Listener with the cyber villians root CA and cert with the correct CNAME for the request * * @param host * @param listener */ protected X509Certificate wireUpSslWithCyberVilliansCAOdo(String host, SslListener listener) { host = requestOriginalHostName.get(); // Add cybervillians CA(from browsermob) try { // see https://github.com/webmetrics/browsermob-proxy/issues/105 String escapedHost = host.replace('*', '_'); KeyStoreManager keyStoreManager = Utils.getKeyStoreManager(escapedHost); keyStoreManager.getKeyStore().deleteEntry(KeyStoreManager._caPrivKeyAlias); keyStoreManager.persist(); listener.setKeystore(new File("seleniumSslSupport" + File.separator + escapedHost + File.separator + "cybervillainsCA.jks").getAbsolutePath()); return keyStoreManager.getCertificateByAlias(escapedHost); } catch (Exception e) { throw new RuntimeException(e); } } private SslRelayOdo getSslRelayOrCreateNewOdo(URI uri, InetAddrPort addrPort, HttpServer server) throws Exception { URI realURI = requestOriginalURI.get(); InetAddrPort realPort = new InetAddrPort(realURI.toString()); LOG.info("GETSSLRELAY: {}, {}", realURI, realPort); String host = new URL("https://" + realURI.toString()).getHost(); // create a host and port string so the listener sslMap can be keyed off the combination String hostAndPort = host.concat(String.valueOf(realPort.getPort())); LOG.info("getSSLRelay host: {}", hostAndPort); SslRelayOdo listener = null; synchronized (_sslMap) { listener = _sslMap.get(hostAndPort); // check the certificate expiration to see if we need to reload it if (listener != null) { Date exprDate = _certExpirationMap.get(hostAndPort); if (exprDate.before(new Date())) { // destroy the listener if (listener.getHttpServer() != null && listener.isStarted()) { listener.getHttpServer().removeListener(listener); } listener = null; } } // create the listener if it didn't exist if (listener == null) { listener = new SslRelayOdo(addrPort); listener.setNukeDirOrFile(null); _certExpirationMap.put(hostAndPort, wireUpSslWithCyberVilliansCAOdo(host, listener).getNotAfter()); listener.setPassword("password"); listener.setKeyPassword("password"); if (!listener.isStarted()) { server.addListener(listener); startRelayWithPortTollerance(server, listener, 1); } _sslMap.put(hostAndPort, listener); } } return listener; } // END ODO CHANGES // BEGIN ODO CHANGES // Changed method signature from SslRelay to SslListener // END ODO CHANGES private void startRelayWithPortTollerance(HttpServer server, SslListener relay, int tries) throws Exception { if (tries >= 5) { throw new BindException("Unable to bind to several ports, most recently " + relay.getPort() + ". Giving up"); } try { if (server.isStarted()) { relay.start(); } else { throw new RuntimeException("Can't start SslRelay: server is not started (perhaps it was just shut down?)"); } } catch (BindException e) { // doh - the port is being used up, let's pick a new port LOG.info("Unable to bind to port %d, going to try port %d now", relay.getPort(), relay.getPort() + 1); relay.setPort(relay.getPort() + 1); startRelayWithPortTollerance(server, relay, tries + 1); } } @Override protected HttpTunnel newHttpTunnel(HttpRequest httpRequest, HttpResponse httpResponse, InetAddress inetAddress, int i, int i1) throws IOException { // we're opening up a new tunnel, so let's make sure that the associated SslRelay (which may or may not be new) has the proper buffer settings adjustListenerBuffers(); return super.newHttpTunnel(httpRequest, httpResponse, inetAddress, i, i1); } /** * Returns a OkHttpClient that ignores SSL cert errors * @return */ private static OkHttpClient getUnsafeOkHttpClient() { try { // Create a trust manager that does not validate certificate chains final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { } @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } } }; // Install the all-trusting trust manager final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); // Create an ssl socket factory with our all-trusting manager final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setSslSocketFactory(sslSocketFactory); okHttpClient.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); return okHttpClient; } catch (Exception e) { throw new RuntimeException(e); } } // ODO VERSION protected long proxyPlainTextRequest(final URL url, String pathInContext, String pathParams, HttpRequest request, final HttpResponse response) throws IOException { try { String urlStr = url.toString(); if (urlStr.toLowerCase().startsWith(Constants.ODO_INTERNAL_WEBAPP_URL)) { urlStr = "http://localhost:" + com.groupon.odo.proxylib.Utils.getSystemPort(Constants.SYS_HTTP_PORT) + "/odo"; } // setup okhttp to ignore ssl issues OkHttpClient okHttpClient = getUnsafeOkHttpClient(); okHttpClient.setFollowRedirects(false); okHttpClient.setFollowSslRedirects(false); Request.Builder okRequestBuilder = new Request.Builder(); /* * urlStr.indexOf(":") == urlStr.lastIndexOf(":") verifies that the url does not have a port * by checking it only has a : as part of http:// */ if (urlStr.startsWith("http://") && urlStr.indexOf(":") == urlStr.lastIndexOf(":")) { int httpPort = com.groupon.odo.proxylib.Utils.getSystemPort(Constants.SYS_HTTP_PORT); urlStr = urlStr.replace(getHostNameFromURL(urlStr), localIP + ":" + httpPort); } okRequestBuilder = okRequestBuilder.url(urlStr); // copy request headers Enumeration<?> enm = request.getFieldNames(); boolean isGet = "GET".equals(request.getMethod()); boolean hasContent = false; boolean usedContentLength = false; long contentLength = 0; while (enm.hasMoreElements()) { String hdr = (String) enm.nextElement(); if (!isGet && HttpFields.__ContentType.equals(hdr)) { hasContent = true; } if (!isGet && HttpFields.__ContentLength.equals(hdr)) { contentLength = Long.parseLong(request.getField(hdr)); usedContentLength = true; } Enumeration<?> vals = request.getFieldValues(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { if (!isGet && HttpFields.__ContentLength.equals(hdr) && Integer.parseInt(val) > 0) { hasContent = true; } if (!_DontProxyHeaders.containsKey(hdr)) { okRequestBuilder = okRequestBuilder.addHeader(hdr, val); //httpReq.addRequestHeader(hdr, val); } } } } if ("GET".equals(request.getMethod())) { // don't need to do anything else } else if ("POST".equals(request.getMethod()) || "PUT".equals(request.getMethod()) || "DELETE".equals(request.getMethod())) { RequestBody okRequestBody = null; if (hasContent) { final String contentType = request.getContentType(); final byte[] bytes = IOUtils.toByteArray(request.getInputStream()); okRequestBody = new RequestBody() { @Override public MediaType contentType() { MediaType.parse(contentType); return null; } @Override public void writeTo(BufferedSink bufferedSink) throws IOException { bufferedSink.write(bytes); } }; // we need to add some ODO specific headers to give ODO a hint for content-length vs transfer-encoding // since okHTTP will automatically chunk even if the request was not chunked // this allows Odo to set the appropriate headers when the server request is made if (usedContentLength) { okRequestBuilder = okRequestBuilder.addHeader("ODO-POST-TYPE", "content-length:" + contentLength); } } else { okRequestBody = RequestBody.create(null, new byte[0]); } if ("POST".equals(request.getMethod())) { okRequestBuilder = okRequestBuilder.post(okRequestBody); } else if ("PUT".equals(request.getMethod())) { okRequestBuilder = okRequestBuilder.put(okRequestBody); } else if ("DELETE".equals(request.getMethod())) { okRequestBuilder = okRequestBuilder.delete(okRequestBody); } } else if ("OPTIONS".equals(request.getMethod())) { // NOT SUPPORTED } else if ("HEAD".equals(request.getMethod())) { okRequestBuilder = okRequestBuilder.head(); } else { LOG.warn("Unexpected request method %s, giving up", request.getMethod()); request.setHandled(true); return -1; } Request okRequest = okRequestBuilder.build(); Response okResponse = okHttpClient.newCall(okRequest).execute(); // Set status and response message response.setStatus(okResponse.code()); response.setReason(okResponse.message()); // copy response headers for (int headerNum = 0; headerNum < okResponse.headers().size(); headerNum++) { String headerName = okResponse.headers().name(headerNum); if (!_DontProxyHeaders.containsKey(headerName) && !_ProxyAuthHeaders.containsKey(headerName)) { response.addField(headerName, okResponse.headers().value(headerNum)); } } // write output to response output stream try { IOUtils.copy(okResponse.body().byteStream(), response.getOutputStream()); } catch (Exception e) { // ignoring this until we refactor the proxy // The copy occasionally fails due to an issue where okResponse has more data in the body than it's supposed to // The client still gets all of the data it was expecting } request.setHandled(true); return okResponse.body().contentLength(); } catch (Exception e) { LOG.warn("Caught exception proxying: ", e); reportError(e, url, response); request.setHandled(true); return -1; } } private static void reportError(Exception e, URL url, HttpResponse response) { FirefoxErrorContent error = FirefoxErrorContent.GENERIC; if (e instanceof UnknownHostException) { error = FirefoxErrorContent.DNS_NOT_FOUND; } else if (e instanceof ConnectException) { error = FirefoxErrorContent.CONN_FAILURE; } else if (e instanceof ConnectTimeoutException) { error = FirefoxErrorContent.NET_TIMEOUT; } else if (e instanceof NoHttpResponseException) { error = FirefoxErrorContent.NET_RESET; } else if (e instanceof EOFException) { error = FirefoxErrorContent.NET_INTERRUPT; } else if (e instanceof IllegalArgumentException && e.getMessage().startsWith("Host name may not be null")) { error = FirefoxErrorContent.DNS_NOT_FOUND; } else if (e instanceof BadURIException) { error = FirefoxErrorContent.MALFORMED_URI; } else if (e instanceof SSLProtocolException) { return; } String shortDesc = String.format(error.getShortDesc(), url.getHost()); String text = String.format(FirefoxErrorConstants.ERROR_PAGE, error.getTitle(), shortDesc, error.getLongDesc()); try { response.setStatus(HttpResponse.__502_Bad_Gateway); response.setContentLength(text.length()); response.getOutputStream().write(text.getBytes()); } catch (IOException e1) { LOG.warn("IOException while trying to report an HTTP error"); } } public void setJettyServer(Server jettyServer) { this.jettyServer = jettyServer; } public void adjustListenerBuffers() { // configure the listeners to have larger buffers. We do this because we've seen cases where the header is // too large. Normally this would happen on "meaningless" JS includes for ad networks, but we eventually saw // it in a way that caused a Selenium script not to work due to too many headers (see tom.schwenk@musictoday.com) HttpListener[] listeners = jettyServer.getListeners(); for (HttpListener listener : listeners) { if (listener instanceof SocketListener) { SocketListener sl = (SocketListener) listener; if (sl.getBufferReserve() != 512 * headerBufferMultiplier) { sl.setBufferReserve(512 * headerBufferMultiplier); } if (sl.getBufferSize() != 8192 * headerBufferMultiplier) { sl.setBufferSize(8192 * headerBufferMultiplier); } } } } // BEGIN ODO CHANGES /** * Cleanup function to remove all allocated listeners */ public void cleanup() { synchronized (_sslMap) { for (SslRelayOdo relay : _sslMap.values()) { if (relay.getHttpServer() != null && relay.isStarted()) { relay.getHttpServer().removeListener(relay); } } sslRelays.clear(); } } // END ODO CHANGES // BEGIN ODO CHANGES // Copied from SeleniumProxyHandler(renamed to SslRelayOdo from SslRelay; no other changes) public static class SslRelayOdo extends SslListener { InetAddrPort _addr; File nukeDirOrFile; private static final long serialVersionUID = 1L; SslRelayOdo(InetAddrPort addr) { _addr = addr; } public void setNukeDirOrFile(File nukeDirOrFile) { this.nukeDirOrFile = nukeDirOrFile; } protected void customizeRequest(Socket socket, HttpRequest request) { super.customizeRequest(socket, request); URI uri = request.getURI(); // Convert the URI to a proxy URL // // NOTE: Don't just add a host + port to the request URI, since this causes the URI to // get "dirty" and be rewritten, potentially breaking the proxy slightly. Instead, // create a brand new URI that includes the protocol, the host, and the port, but leaves // intact the path + query string "as is" so that it does not get rewritten. request.setURI(new URI("https://" + _addr.getHost() + ":" + _addr.getPort() + uri.toString())); } public void stop() throws InterruptedException { super.stop(); if (nukeDirOrFile != null) { if (nukeDirOrFile.isDirectory()) { LauncherUtils.recursivelyDeleteDir(nukeDirOrFile); } else { nukeDirOrFile.delete(); } } } } /** * @param url full url containing hostname * @return hostname */ public static String getHostNameFromURL(String url) { int urlLeftPos = url.indexOf("//"); String hostName = url.substring(urlLeftPos + 2); int urlRightPos = hostName.indexOf("/"); if (urlRightPos != -1) { hostName = hostName.substring(0, urlRightPos); } // now look for a port int portPos = hostName.indexOf(":"); if (portPos != -1) { hostName = hostName.substring(0, portPos); } return hostName; } // END ODO CHANGES }