private void replaceConnection() {
   EndPoint endPoint = getEndPoint();
   Connection connection =
       client.getConnectionFactory().newConnection(channel, endPoint, attachment);
   endPoint.getConnection().onClose();
   endPoint.setConnection(connection);
   connection.onOpen();
 }
예제 #2
0
 private void tunnelSucceeded() {
   try {
     // Replace the promise back with the original
     context.put(HttpClientTransport.HTTP_CONNECTION_PROMISE_CONTEXT_KEY, promise);
     HttpDestination destination =
         (HttpDestination) context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY);
     HttpClient client = destination.getHttpClient();
     ClientConnectionFactory sslConnectionFactory =
         new SslClientConnectionFactory(
             client.getSslContextFactory(),
             client.getByteBufferPool(),
             client.getExecutor(),
             connectionFactory);
     org.eclipse.jetty.io.Connection oldConnection = endPoint.getConnection();
     org.eclipse.jetty.io.Connection newConnection =
         sslConnectionFactory.newConnection(endPoint, context);
     Helper.replaceConnection(oldConnection, newConnection);
     LOG.debug("HTTP tunnel established: {} over {}", oldConnection, newConnection);
   } catch (Throwable x) {
     tunnelFailed(x);
   }
 }
  @Test
  public void testMaxIdleWithRequest10ClientIgnoresClose() throws Exception {
    final Exchanger<EndPoint> exchanger = new Exchanger<>();
    configureServer(
        new HelloWorldHandler() {
          @Override
          public void handle(
              String target,
              Request baseRequest,
              HttpServletRequest request,
              HttpServletResponse response)
              throws IOException, ServletException {
            try {
              exchanger.exchange(baseRequest.getHttpChannel().getEndPoint());
            } catch (Exception e) {
              e.printStackTrace();
            }
            super.handle(target, baseRequest, request, response);
          }
        });
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    client.setSoTimeout(10000);

    assertFalse(client.isClosed());

    OutputStream os = client.getOutputStream();
    InputStream is = client.getInputStream();

    os.write(
        ("GET / HTTP/1.0\r\n"
                + "host: "
                + _serverURI.getHost()
                + ":"
                + _serverURI.getPort()
                + "\r\n"
                + "connection: close\r\n"
                + "\r\n")
            .getBytes("utf-8"));
    os.flush();

    // Get the server side endpoint
    EndPoint endPoint = exchanger.exchange(null, 10, TimeUnit.SECONDS);
    if (endPoint instanceof SslConnection.DecryptedEndPoint)
      endPoint = endPoint.getConnection().getEndPoint();

    // read the response
    String result = IO.toString(is);
    Assert.assertThat("OK", result, containsString("200 OK"));

    // check client reads EOF
    assertEquals(-1, is.read());
    assertTrue(endPoint.isOutputShutdown());

    Thread.sleep(2 * MAX_IDLE_TIME);

    // further writes will get broken pipe or similar
    try {
      long end = System.currentTimeMillis() + MAX_IDLE_TIME + 3000;
      while (System.currentTimeMillis() < end) {
        os.write("THIS DATA SHOULD NOT BE PARSED!\n\n".getBytes("utf-8"));
        os.flush();
        Thread.sleep(100);
      }
      Assert.fail("half close should have timed out");
    } catch (SocketException e) {
      // expected

      // Give the SSL onClose time to act
      Thread.sleep(100);
    }

    // check the server side is closed
    Assert.assertFalse(endPoint.isOpen());
  }