Exemplo n.º 1
0
    public void handleEvent(StreamSourceChannel channel) {

      HttpResponseBuilder builder = pendingResponse;
      final Pooled<ByteBuffer> pooled = bufferPool.allocate();
      final ByteBuffer buffer = pooled.getResource();
      boolean free = true;

      try {

        if (builder == null) {
          // read ready when no request pending
          buffer.clear();
          try {
            int res = channel.read(buffer);
            if (res == -1) {
              UndertowLogger.CLIENT_LOGGER.debugf(
                  "Connection to %s was closed by the target server", connection.getPeerAddress());
              IoUtils.safeClose(HttpClientConnection.this);
            } else if (res != 0) {
              UndertowLogger.CLIENT_LOGGER.debugf(
                  "Target server %s sent unexpected data when no request pending, closing connection",
                  connection.getPeerAddress());
              IoUtils.safeClose(HttpClientConnection.this);
            }
            // otherwise it is a spurious notification
          } catch (IOException e) {
            if (UndertowLogger.CLIENT_LOGGER.isDebugEnabled()) {
              UndertowLogger.CLIENT_LOGGER.debugf(e, "Connection closed with IOException");
            }
            safeClose(connection);
          }
          return;
        }
        final ResponseParseState state = builder.getParseState();
        int res;
        do {
          buffer.clear();
          try {
            res = channel.read(buffer);
          } catch (IOException e) {
            if (UndertowLogger.CLIENT_LOGGER.isDebugEnabled()) {
              UndertowLogger.CLIENT_LOGGER.debugf(e, "Connection closed with IOException");
            }
            safeClose(channel);
            return;
          }

          if (res == 0) {
            if (!channel.isReadResumed()) {
              channel.getReadSetter().set(this);
              channel.resumeReads();
            }
            return;
          } else if (res == -1) {
            channel.suspendReads();
            IoUtils.safeClose(HttpClientConnection.this);
            try {
              final StreamSinkChannel requestChannel = connection.getSinkChannel();
              requestChannel.shutdownWrites();
              // will return false if there's a response queued ahead of this one, so we'll set up a
              // listener then
              if (!requestChannel.flush()) {
                requestChannel
                    .getWriteSetter()
                    .set(ChannelListeners.flushingChannelListener(null, null));
                requestChannel.resumeWrites();
              }
              // Cancel the current active request
              currentRequest.setFailed(new IOException(MESSAGES.connectionClosed()));
            } catch (IOException e) {
              if (UndertowLogger.CLIENT_LOGGER.isDebugEnabled()) {
                UndertowLogger.CLIENT_LOGGER.debugf(
                    e, "Connection closed with IOException when attempting to shut down reads");
              }
              IoUtils.safeClose(channel);
              // Cancel the current active request
              currentRequest.setFailed(e);
              return;
            }
            return;
          }

          buffer.flip();

          HttpResponseParser.INSTANCE.handle(buffer, state, builder);
          if (buffer.hasRemaining()) {
            free = false;
            pushBackStreamSourceConduit.pushBack(pooled);
          }

        } while (!state.isComplete());

        final ClientResponse response = builder.build();

        String connectionString = response.getResponseHeaders().getFirst(CONNECTION);

        // check if an upgrade worked
        if (anyAreSet(HttpClientConnection.this.state, UPGRADE_REQUESTED)) {
          if ((connectionString == null || !UPGRADE.equalToString(connectionString))
              && !response.getResponseHeaders().contains(UPGRADE)) {
            // just unset the upgrade requested flag
            HttpClientConnection.this.state &= ~UPGRADE_REQUESTED;
          }
        }

        if (connectionString != null) {
          if (HttpString.tryFromString(connectionString).equals(Headers.CLOSE)) {
            HttpClientConnection.this.state |= CLOSE_REQ;
          }
        }

        if (builder.getStatusCode() == 100) {
          pendingResponse = new HttpResponseBuilder();
          currentRequest.setContinueResponse(response);
        } else {
          prepareResponseChannel(response, currentRequest);
          channel.getReadSetter().set(null);
          channel.suspendReads();
          pendingResponse = null;
          currentRequest.setResponse(response);
        }

      } catch (Exception e) {
        UndertowLogger.CLIENT_LOGGER.exceptionProcessingRequest(e);
        IoUtils.safeClose(connection);
        currentRequest.setFailed(new IOException(e));
      } finally {
        if (free) pooled.free();
      }
    }