/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private void makeTunnel(TunnelRequest tunnelRequest) throws IOException { HttpConnection tunnelConnection = new HttpConnection(pool, this, socket); Request request = tunnelRequest.getRequest(); String requestLine = tunnelRequest.requestLine(); while (true) { tunnelConnection.writeRequest(request.headers(), requestLine); tunnelConnection.flush(); Response response = tunnelConnection.readResponse().request(request).build(); tunnelConnection.emptyResponseBody(); switch (response.code()) { case HTTP_OK: // Assume the server won't send a TLS ServerHello until we send a TLS ClientHello. If that // happens, then we will have buffered bytes that are needed by the SSLSocket! if (tunnelConnection.bufferSize() > 0) { throw new IOException("TLS tunnel buffered too many bytes!"); } return; case HTTP_PROXY_AUTH: request = HttpAuthenticator.processAuthHeader( route.address.authenticator, response, route.proxy); if (request != null) continue; throw new IOException("Failed to authenticate with proxy"); default: throw new IOException("Unexpected response code for CONNECT: " + response.code()); } } }
@Test public void testHeaders() { final String headerKey = "host"; final String host = "www.google.com"; when(servletRequest.getHeader(headerKey)).thenReturn(host); assertEquals( "The value of the header specified should be returned", host, request.headers(headerKey)); }
ExchangeImpl(String m, URI u, Request req, int len, HttpConnection connection) throws IOException { this.req = req; this.reqHdrs = req.headers(); this.rspHdrs = new Headers(); this.method = m; this.uri = u; this.connection = connection; this.reqContentLen = len; /* ros only used for headers, body written directly to stream */ this.ros = req.outputStream(); this.ris = req.inputStream(); server = getServerImpl(); server.startExchange(); }
@Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); long t1 = System.nanoTime(); System.out.println( String.format( "Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())); Response response = chain.proceed(request); long t2 = System.nanoTime(); System.out.println( String.format( "Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())); return response; }
/** * To make an HTTPS connection over an HTTP proxy, send an unencrypted CONNECT request to create * the proxy connection. This may need to be retried if the proxy requires authorization. */ private void makeTunnel(TunnelRequest tunnelRequest) throws IOException { Request request = tunnelRequest.getRequest(); String requestLine = tunnelRequest.requestLine(); while (true) { HttpTransport.writeRequest(out, request.headers(), requestLine); Response response = HttpTransport.readResponse(in).request(request).build(); switch (response.code()) { case HTTP_OK: return; case HTTP_PROXY_AUTH: request = HttpAuthenticator.processAuthHeader( route.address.authenticator, response, route.proxy); if (request != null) continue; throw new IOException("Failed to authenticate with proxy"); default: throw new IOException("Unexpected response code for CONNECT: " + response.code()); } } }
@Override public String headerValue(int index) { return mRequest.headers().value(index); }
@Override public String headerName(int index) { return mRequest.headers().name(index); }
@Override public int headerCount() { return mRequest.headers().size(); }
/* */ public void run() /* */ { /* 494 */ this.context = this.connection.getHttpContext(); /* */ /* 496 */ SSLEngine localSSLEngine = null; /* 497 */ String str1 = null; /* 498 */ SSLStreams localSSLStreams = null; /* */ try /* */ { /* */ int i; /* 500 */ if (this.context != null) { /* 501 */ this.rawin = this.connection.getInputStream(); /* 502 */ this.rawout = this.connection.getRawOutputStream(); /* 503 */ i = 0; /* */ } /* */ else { /* 506 */ i = 1; /* 507 */ if (ServerImpl.this.https) { /* 508 */ if (ServerImpl.this.sslContext == null) { /* 509 */ ServerImpl.this.logger.warning("SSL connection received. No https contxt created"); /* 510 */ throw new HttpError("No SSL context established"); /* */ } /* 512 */ localSSLStreams = new SSLStreams(ServerImpl.this, ServerImpl.this.sslContext, this.chan); /* 513 */ this.rawin = localSSLStreams.getInputStream(); /* 514 */ this.rawout = localSSLStreams.getOutputStream(); /* 515 */ localSSLEngine = localSSLStreams.getSSLEngine(); /* 516 */ this.connection.sslStreams = localSSLStreams; /* */ } else { /* 518 */ this.rawin = new BufferedInputStream(new Request.ReadStream(ServerImpl.this, this.chan)); /* */ /* 522 */ this.rawout = new Request.WriteStream(ServerImpl.this, this.chan); /* */ } /* */ /* 526 */ this.connection.raw = this.rawin; /* 527 */ this.connection.rawout = this.rawout; /* */ } /* 529 */ Request localRequest = new Request(this.rawin, this.rawout); /* 530 */ str1 = localRequest.requestLine(); /* 531 */ if (str1 == null) /* */ { /* 533 */ ServerImpl.this.closeConnection(this.connection); /* 534 */ return; /* */ } /* 536 */ int j = str1.indexOf(' '); /* 537 */ if (j == -1) { /* 538 */ reject(400, str1, "Bad request line"); /* */ /* 540 */ return; /* */ } /* 542 */ String str2 = str1.substring(0, j); /* 543 */ int k = j + 1; /* 544 */ j = str1.indexOf(' ', k); /* 545 */ if (j == -1) { /* 546 */ reject(400, str1, "Bad request line"); /* */ /* 548 */ return; /* */ } /* 550 */ String str3 = str1.substring(k, j); /* 551 */ URI localURI = new URI(str3); /* 552 */ k = j + 1; /* 553 */ String str4 = str1.substring(k); /* 554 */ Headers localHeaders1 = localRequest.headers(); /* 555 */ String str5 = localHeaders1.getFirst("Transfer-encoding"); /* 556 */ long l = 0L; /* 557 */ if ((str5 != null) && (str5.equalsIgnoreCase("chunked"))) { /* 558 */ l = -1L; /* */ } else { /* 560 */ str5 = localHeaders1.getFirst("Content-Length"); /* 561 */ if (str5 != null) { /* 562 */ l = Long.parseLong(str5); /* */ } /* 564 */ if (l == 0L) { /* 565 */ ServerImpl.this.requestCompleted(this.connection); /* */ } /* */ } /* 568 */ this.ctx = ServerImpl.this.contexts.findContext(this.protocol, localURI.getPath()); /* 569 */ if (this.ctx == null) { /* 570 */ reject(404, str1, "No context found for request"); /* */ /* 572 */ return; /* */ } /* 574 */ this.connection.setContext(this.ctx); /* 575 */ if (this.ctx.getHandler() == null) { /* 576 */ reject(500, str1, "No handler for context"); /* */ /* 578 */ return; /* */ } /* 580 */ this.tx = new ExchangeImpl(str2, localURI, localRequest, l, this.connection); /* */ /* 583 */ String str6 = localHeaders1.getFirst("Connection"); /* 584 */ Headers localHeaders2 = this.tx.getResponseHeaders(); /* */ /* 586 */ if ((str6 != null) && (str6.equalsIgnoreCase("close"))) { /* 587 */ this.tx.close = true; /* */ } /* 589 */ if (str4.equalsIgnoreCase("http/1.0")) { /* 590 */ this.tx.http10 = true; /* 591 */ if (str6 == null) { /* 592 */ this.tx.close = true; /* 593 */ localHeaders2.set("Connection", "close"); /* 594 */ } else if (str6.equalsIgnoreCase("keep-alive")) { /* 595 */ localHeaders2.set("Connection", "keep-alive"); /* 596 */ int m = (int)ServerConfig.getIdleInterval() / 1000; /* 597 */ int n = ServerConfig.getMaxIdleConnections(); /* 598 */ localObject = "timeout=" + m + ", max=" + n; /* 599 */ localHeaders2.set("Keep-Alive", (String)localObject); /* */ } /* */ } /* */ /* 603 */ if (i != 0) { /* 604 */ this.connection.setParameters(this.rawin, this.rawout, this.chan, localSSLEngine, localSSLStreams, ServerImpl.this.sslContext, this.protocol, this.ctx, this.rawin); /* */ } /* */ /* 614 */ String str7 = localHeaders1.getFirst("Expect"); /* 615 */ if ((str7 != null) && (str7.equalsIgnoreCase("100-continue"))) { /* 616 */ ServerImpl.this.logReply(100, str1, null); /* 617 */ sendReply(100, false, null); /* */ } /* */ /* 628 */ List localList = this.ctx.getSystemFilters(); /* 629 */ Object localObject = this.ctx.getFilters(); /* */ /* 631 */ Filter.Chain localChain1 = new Filter.Chain(localList, this.ctx.getHandler()); /* 632 */ Filter.Chain localChain2 = new Filter.Chain((List)localObject, new LinkHandler(localChain1)); /* */ /* 635 */ this.tx.getRequestBody(); /* 636 */ this.tx.getResponseBody(); /* 637 */ if (ServerImpl.this.https) /* 638 */ localChain2.doFilter(new HttpsExchangeImpl(this.tx)); /* */ else /* 640 */ localChain2.doFilter(new HttpExchangeImpl(this.tx)); /* */ } /* */ catch (IOException localIOException) /* */ { /* 644 */ ServerImpl.this.logger.log(Level.FINER, "ServerImpl.Exchange (1)", localIOException); /* 645 */ ServerImpl.this.closeConnection(this.connection); /* */ } catch (NumberFormatException localNumberFormatException) { /* 647 */ reject(400, str1, "NumberFormatException thrown"); /* */ } /* */ catch (URISyntaxException localURISyntaxException) { /* 650 */ reject(400, str1, "URISyntaxException thrown"); /* */ } /* */ catch (Exception localException) { /* 653 */ ServerImpl.this.logger.log(Level.FINER, "ServerImpl.Exchange (2)", localException); /* 654 */ ServerImpl.this.closeConnection(this.connection); /* */ } /* */ }