Ejemplo n.º 1
0
  private synchronized void connect() {
    if (!connecting) {
      // We defer actual connection until the first part of body is written or end is called
      // This gives the user an opportunity to set an exception handler before connecting so
      // they can capture any exceptions on connection
      client.getConnection(
          port,
          host,
          conn -> {
            synchronized (this) {
              if (exceptionOccurred) {
                // The request already timed out before it has left the pool waiter queue
                // So return it
                conn.close();
              } else if (!conn.isClosed()) {
                connected(conn);
              } else {
                // The connection has been closed - closed connections can be in the pool
                // Get another connection - Note that we DO NOT call connectionClosed() on the pool
                // at this point
                // that is done asynchronously in the connection closeHandler()
                connect();
              }
            }
          },
          exceptionHandler,
          vertx.getOrCreateContext());

      connecting = true;
    }
  }
Ejemplo n.º 2
0
 @Override
 public HttpClientRequest setTimeout(long timeoutMs) {
   synchronized (getLock()) {
     cancelOutstandingTimeoutTimer();
     currentTimeoutTimerId = client.getVertx().setTimer(timeoutMs, id -> handleTimeout(timeoutMs));
     return this;
   }
 }
Ejemplo n.º 3
0
 private void prepareHeaders() {
   HttpHeaders headers = request.headers();
   headers.remove(TRANSFER_ENCODING);
   if (!headers.contains(HOST)) {
     request.headers().set(HOST, conn.hostHeader());
   }
   if (chunked) {
     HttpHeaders.setTransferEncodingChunked(request);
   }
   if (client.getOptions().isTryUseCompression()
       && request.headers().get(ACCEPT_ENCODING) == null) {
     // if compression should be used but nothing is specified by the user support deflate and
     // gzip.
     request.headers().set(ACCEPT_ENCODING, DEFLATE_GZIP);
   }
   if (!client.getOptions().isKeepAlive()
       && client.getOptions().getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_1) {
     request.headers().set(CONNECTION, CLOSE);
   } else if (client.getOptions().isKeepAlive()
       && client.getOptions().getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_0) {
     request.headers().set(CONNECTION, KEEP_ALIVE);
   }
 }
Ejemplo n.º 4
0
 @Override
 public HttpClientRequestImpl setChunked(boolean chunked) {
   synchronized (getLock()) {
     checkComplete();
     if (written > 0) {
       throw new IllegalStateException(
           "Cannot set chunked after data has been written on request");
     }
     // HTTP 1.0 does not support chunking so we ignore this if HTTP 1.0
     if (client.getOptions().getProtocolVersion() != io.vertx.core.http.HttpVersion.HTTP_1_0) {
       this.chunked = chunked;
     }
     return this;
   }
 }
Ejemplo n.º 5
0
  private void connected(ClientConnection conn) {
    conn.setCurrentRequest(this);
    this.conn = conn;
    this.metric =
        client
            .httpClientMetrics()
            .requestBegin(conn.metric(), conn.localAddress(), conn.remoteAddress(), this);

    // If anything was written or the request ended before we got the connection, then
    // we need to write it now

    if (pendingMaxSize != -1) {
      conn.doSetWriteQueueMaxSize(pendingMaxSize);
    }

    if (pendingChunks != null) {
      ByteBuf pending = pendingChunks;
      pendingChunks = null;

      if (completed) {
        // we also need to write the head so optimize this and write all out in once
        writeHeadWithContent(pending, true);

        conn.reportBytesWritten(written);

        if (respHandler != null) {
          conn.endRequest();
        }
      } else {
        writeHeadWithContent(pending, false);
      }
    } else {
      if (completed) {
        // we also need to write the head so optimize this and write all out in once
        writeHeadWithContent(Unpooled.EMPTY_BUFFER, true);

        conn.reportBytesWritten(written);

        if (respHandler != null) {
          conn.endRequest();
        }
      } else {
        if (writeHead) {
          writeHead();
        }
      }
    }
  }
Ejemplo n.º 6
0
 HttpClientRequestImpl(
     HttpClientImpl client,
     io.vertx.core.http.HttpMethod method,
     String host,
     int port,
     String relativeURI,
     VertxInternal vertx) {
   this.host = host;
   this.port = port;
   this.client = client;
   this.request =
       new DefaultHttpRequest(
           toNettyHttpVersion(client.getOptions().getProtocolVersion()),
           toNettyHttpMethod(method),
           relativeURI,
           false);
   this.chunked = false;
   this.method = method;
   this.vertx = vertx;
 }
Ejemplo n.º 7
0
 void reportResponseEnd(HttpClientResponseImpl resp) {
   HttpClientMetrics metrics = client.httpClientMetrics();
   if (metrics.isEnabled()) {
     metrics.responseEnd(metric, resp);
   }
 }
Ejemplo n.º 8
0
 private void cancelOutstandingTimeoutTimer() {
   if (currentTimeoutTimerId != -1) {
     client.getVertx().cancelTimer(currentTimeoutTimerId);
     currentTimeoutTimerId = -1;
   }
 }