Esempio n. 1
0
  @Override
  public void exchangeEvent(
      final HttpServerExchange exchange,
      final ExchangeCompletionListener.NextListener nextListener) {
    connection.resetChannel();
    final HttpServerConnection connection = this.connection;
    if (exchange.isPersistent() && !exchange.isUpgrade()) {
      newRequest();
      StreamConnection channel = connection.getChannel();
      if (connection.getExtraBytes() == null) {
        // if we are not pipelining we just register a listener

        channel.getSourceChannel().getReadSetter().set(this);
        channel.getSourceChannel().resumeReads();
      } else {
        if (channel.getSourceChannel().isReadResumed()) {
          channel.getSourceChannel().suspendReads();
        }
        if (exchange.isInIoThread()) {
          channel.getIoThread().execute(this);
        } else {
          Executor executor = exchange.getDispatchExecutor();
          if (executor == null) {
            executor = exchange.getConnection().getWorker();
          }
          executor.execute(this);
        }
      }
    }
    nextListener.proceed();
  }
Esempio n. 2
0
 HttpReadListener(final HttpServerConnection connection, final HttpRequestParser parser) {
   this.connection = connection;
   this.parser = parser;
   maxRequestSize =
       connection
           .getUndertowOptions()
           .get(UndertowOptions.MAX_HEADER_SIZE, UndertowOptions.DEFAULT_MAX_HEADER_SIZE);
   this.maxEntitySize = connection.getUndertowOptions().get(UndertowOptions.MAX_ENTITY_SIZE, 0);
 }
Esempio n. 3
0
 private void sendExpect100(Map headers, RequestLine requestLine)
     throws TransformerException, IOException {
   // respond with status code 100, for Expect handshake
   // according to rfc 2616 and http 1.1
   // the processing will continue and the request will be fully
   // read immediately after
   if (HttpConstants.HTTP11.equals(headers.get(HttpConnector.HTTP_VERSION_PROPERTY))) {
     // just in case we have something other than String in
     // the headers map
     String expectHeaderValue =
         ObjectUtils.toString(headers.get(HttpConstants.HEADER_EXPECT)).toLowerCase();
     if (HttpConstants.HEADER_EXPECT_CONTINUE_REQUEST_VALUE.equals(expectHeaderValue)) {
       HttpResponse expected = new HttpResponse();
       expected.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_CONTINUE);
       final DefaultMuleEvent event =
           new DefaultMuleEvent(
               new DefaultMuleMessage(expected),
               endpoint,
               new DefaultMuleSession(service, connector.getMuleContext()),
               true);
       RequestContext.setEvent(event);
       conn.writeResponse(transformResponse(expected));
     }
   }
 }
Esempio n. 4
0
    public void run() {
      long keepAliveTimeout = ((TcpConnector) connector).getKeepAliveTimeout();

      try {
        do {
          conn.setKeepAlive(false);

          // Only add a monitor if the timeout has been set
          if (keepAliveTimeout > 0) {
            ((HttpConnector) connector)
                .getKeepAliveMonitor()
                .addExpirable(keepAliveTimeout, TimeUnit.MILLISECONDS, this);
          }

          HttpRequest request = conn.readRequest();
          if (request == null) {
            break;
          }

          // Ensure that we drop any monitors, we'll add again for the next request
          ((HttpConnector) connector).getKeepAliveMonitor().removeExpirable(this);

          conn.writeResponse(processRequest(request));

          if (request.getBody() != null) {
            request.getBody().close();
          }
        } while (conn.isKeepAlive());
      } catch (Exception e) {
        handleException(e);
      } finally {
        logger.debug("Closing HTTP connection.");

        if (conn.isOpen()) {
          conn.close();
          conn = null;

          // Ensure that we drop any monitors
          ((HttpConnector) connector).getKeepAliveMonitor().removeExpirable(this);
        }
      }
    }
Esempio n. 5
0
  public void handleEvent(final StreamSourceChannel channel) {

    Pooled<ByteBuffer> existing = connection.getExtraBytes();

    final Pooled<ByteBuffer> pooled =
        existing == null ? connection.getBufferPool().allocate() : existing;
    final ByteBuffer buffer = pooled.getResource();
    boolean free = true;

    try {
      int res;
      do {
        if (existing == null) {
          buffer.clear();
          try {
            res = channel.read(buffer);
          } catch (IOException e) {
            UndertowLogger.REQUEST_IO_LOGGER.debug("Error reading request", e);
            IoUtils.safeClose(connection);
            return;
          }
        } else {
          res = buffer.remaining();
        }

        if (res == 0) {
          if (!channel.isReadResumed()) {
            channel.getReadSetter().set(this);
            channel.resumeReads();
          }
          return;
        } else if (res == -1) {
          try {
            channel.suspendReads();
            channel.shutdownReads();
            final StreamSinkChannel responseChannel = this.connection.getChannel().getSinkChannel();
            responseChannel.shutdownWrites();
            // will return false if there's a response queued ahead of this one, so we'll set up a
            // listener then
            if (!responseChannel.flush()) {
              responseChannel
                  .getWriteSetter()
                  .set(ChannelListeners.flushingChannelListener(null, null));
              responseChannel.resumeWrites();
            }
          } catch (IOException e) {
            UndertowLogger.REQUEST_IO_LOGGER.debug("Error reading request", e);
            // f**k it, it's all ruined
            IoUtils.safeClose(channel);
            return;
          }
          return;
        }
        if (existing != null) {
          existing = null;
          connection.setExtraBytes(null);
        } else {
          buffer.flip();
        }
        parser.handle(buffer, state, httpServerExchange);
        if (buffer.hasRemaining()) {
          free = false;
          connection.setExtraBytes(pooled);
        }
        int total = read + res;
        read = total;
        if (read > maxRequestSize) {
          UndertowLogger.REQUEST_LOGGER.requestHeaderWasTooLarge(
              connection.getPeerAddress(), maxRequestSize);
          IoUtils.safeClose(connection);
          return;
        }
      } while (!state.isComplete());

      // we remove ourselves as the read listener from the channel;
      // if the http handler doesn't set any then reads will suspend, which is the right thing to do
      channel.getReadSetter().set(null);
      channel.suspendReads();

      final HttpServerExchange httpServerExchange = this.httpServerExchange;
      httpServerExchange.putAttachment(
          UndertowOptions.ATTACHMENT_KEY, connection.getUndertowOptions());
      httpServerExchange.setRequestScheme(connection.getSslSession() != null ? "https" : "http");
      this.httpServerExchange = null;
      HttpTransferEncoding.setupRequest(httpServerExchange);
      HttpHandlers.executeRootHandler(
          connection.getRootHandler(),
          httpServerExchange,
          Thread.currentThread() instanceof XnioExecutor);
    } catch (Exception e) {
      sendBadRequestAndClose(connection.getChannel(), e);
      return;
    } finally {
      if (free) pooled.free();
    }
  }
Esempio n. 6
0
 @Override
 public void run() {
   handleEvent(connection.getChannel().getSourceChannel());
 }
Esempio n. 7
0
 public void release() {
   conn.close();
   conn = null;
 }
Esempio n. 8
0
 public void expired() {
   if (conn.isOpen()) {
     conn.close();
   }
 }