io.undertow.server.Connectors Java Examples

The following examples show how to use io.undertow.server.Connectors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpClientConnection.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) {
    String encoding = response.getResponseHeaders().getLast(Headers.TRANSFER_ENCODING);
    boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding));
    String length = response.getResponseHeaders().getFirst(Headers.CONTENT_LENGTH);
    if (exchange.getRequest().getMethod().equals(Methods.HEAD)) {
        connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
    } else if (chunked) {
        connection.getSourceChannel().setConduit(new ChunkedStreamSourceConduit(connection.getSourceChannel().getConduit(), pushBackStreamSourceConduit, bufferPool, responseFinishedListener, exchange, connection));
    } else if (length != null) {
        try {
            long contentLength = Long.parseLong(length);
            connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener));
        } catch (NumberFormatException e) {
            handleError(e);
            throw e;
        }
    } else if (response.getProtocol().equals(Protocols.HTTP_1_1) && !Connectors.isEntityBodyAllowed(response.getResponseCode())) {
        connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener));
    } else {
        connection.getSourceChannel().setConduit(new FinishableStreamSourceConduit(connection.getSourceChannel().getConduit(), responseFinishedListener));
        state |= CLOSE_REQ;
    }
}
 
Example #2
Source File: ChunkedStreamSourceConduit.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void updateRemainingAllowed(final int written) throws IOException {
    if (remainingAllowed == Long.MIN_VALUE) {
        if (exchange == null) {
            return;
        } else {
            long maxEntitySize = exchange.getMaxEntitySize();
            if (maxEntitySize <= 0) {
                return;
            }
            remainingAllowed = maxEntitySize;
        }
    }
    remainingAllowed -= written;
    if (remainingAllowed < 0) {
        //max entity size is exceeded
        Connectors.terminateRequest(exchange);
        closed = true;
        exchange.setPersistent(false);
        finishListener.handleEvent(this);
        throw UndertowMessages.MESSAGES.requestEntityWasTooLarge(exchange.getMaxEntitySize());
    }
}
 
Example #3
Source File: FixedLengthStreamSourceConduit.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void checkMaxSize(long state) throws IOException {
    if (anyAreClear(state, FLAG_LENGTH_CHECKED)) {
        HttpServerExchange exchange = this.exchange;
        if (exchange != null) {
            if (exchange.getMaxEntitySize() > 0 && exchange.getMaxEntitySize() < (state & MASK_COUNT)) {
                //max entity size is exceeded
                //we need to forcibly close the read side
                Connectors.terminateRequest(exchange);
                exchange.setPersistent(false);
                finishListener.handleEvent(this);
                this.state |= FLAG_FINISHED | FLAG_CLOSED;
                throw UndertowMessages.MESSAGES.requestEntityWasTooLarge(exchange.getMaxEntitySize());
            }
        }
        this.state |= FLAG_LENGTH_CHECKED;
    }
}
 
Example #4
Source File: AccessLogCompletionListenerTest.java    From galeb with Apache License 2.0 5 votes vote down vote up
@Test
public void getJsonObjectTest() {
    environmentVariables.set("HOSTNAME", "hostname.localenv");
    environmentVariables.set("LOGGING_TAGS", "GALEB,OTHER");

    AccessLogCompletionListener accessLogCompletionListener = new AccessLogCompletionListener();

    HttpServerExchange httpServerExchange = new HttpServerExchange(Mockito.mock(ServerConnection.class), getRequestHeaders(), null, 0);
    httpServerExchange.setSourceAddress(new InetSocketAddress("1.2.3.4", 44444));
    httpServerExchange.setRequestMethod(HttpString.tryFromString("GET"));
    httpServerExchange.setRequestURI("/test");
    httpServerExchange.setProtocol(HttpString.tryFromString("HTTP"));
    httpServerExchange.setStatusCode(200);
    Connectors.setRequestStartTime(httpServerExchange);

    JsonObject jsonObject = accessLogCompletionListener.getJsonObject(httpServerExchange);

    Assert.assertEquals("1", jsonObject.getAsJsonPrimitive("@version").getAsString());
    Assert.assertEquals("hostname.localenv", jsonObject.getAsJsonPrimitive("host").getAsString());
    Assert.assertEquals(AccessLogCompletionListener.SHORT_MESSAGE, jsonObject.getAsJsonPrimitive("short_message").getAsString());
    Assert.assertEquals("vhost.host.virtual", jsonObject.getAsJsonPrimitive("vhost").getAsString());
    Assert.assertEquals("GALEB,OTHER,ACCESS", jsonObject.getAsJsonPrimitive("_tags").getAsString());
    Assert.assertEquals("1.2.3.4", jsonObject.getAsJsonPrimitive("remote_addr").getAsString());
    Assert.assertEquals("GET", jsonObject.getAsJsonPrimitive("request_method").getAsString());
    Assert.assertEquals("/test", jsonObject.getAsJsonPrimitive("request_uri").getAsString());
    Assert.assertEquals("HTTP", jsonObject.getAsJsonPrimitive("server_protocol").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_referer").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_x_mobile_group").getAsString());
    Assert.assertEquals("600", jsonObject.getAsJsonPrimitive("status").getAsString());
    Assert.assertNotNull(jsonObject.getAsJsonPrimitive("body_bytes_sent").getAsString());
    Assert.assertNotNull(jsonObject.getAsJsonPrimitive("request_time").getAsString());
    Assert.assertEquals("UNKNOWN_TARGET", jsonObject.getAsJsonPrimitive("upstream_addr").getAsString());
    Assert.assertEquals("200", jsonObject.getAsJsonPrimitive("upstream_status").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("upstream_response_length").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_user_agent").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("request_id_final").getAsString());
    Assert.assertEquals("-", jsonObject.getAsJsonPrimitive("http_x_forwarded_for").getAsString());
}
 
Example #5
Source File: RequestLimit.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void handleRequest(final HttpServerExchange exchange, final HttpHandler next) throws Exception {
    int oldVal, newVal;
    do {
        oldVal = requests;
        if (oldVal >= max) {
            exchange.dispatch(SameThreadExecutor.INSTANCE, new Runnable() {
                @Override
                public void run() {
                    //we have to try again in the sync block
                    //we need to have already dispatched for thread safety reasons
                    synchronized (RequestLimit.this) {
                        int oldVal, newVal;
                        do {
                            oldVal = requests;
                            if (oldVal >= max) {
                                if (!queue.offer(new SuspendedRequest(exchange, next))) {
                                    Connectors.executeRootHandler(failureHandler, exchange);
                                }
                                return;
                            }
                            newVal = oldVal + 1;
                        } while (!requestsUpdater.compareAndSet(RequestLimit.this, oldVal, newVal));
                        exchange.addExchangeCompleteListener(COMPLETION_LISTENER);
                        exchange.dispatch(next);
                    }
                }
            });
            return;
        }
        newVal = oldVal + 1;
    } while (!requestsUpdater.compareAndSet(this, oldVal, newVal));
    exchange.addExchangeCompleteListener(COMPLETION_LISTENER);
    next.handleRequest(exchange);
}
 
Example #6
Source File: Http2ServerConnection.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected StreamSinkConduit getSinkConduit(HttpServerExchange exchange, StreamSinkConduit conduit) {
    HeaderMap headers = responseChannel.getHeaders();
    DateUtils.addDateHeaderIfRequired(exchange);
    headers.add(STATUS, exchange.getStatusCode());
    Connectors.flattenCookies(exchange);
    return originalSinkConduit;
}
 
Example #7
Source File: UndertowNetworkDispatcher.java    From actframework with Apache License 2.0 5 votes vote down vote up
@Override
public void keep() {
    if (!this.dispatched) {
        Connectors.resetRequestChannel(exchange);
        exchange.getRequestChannel().resumeReads();
    }
}
 
Example #8
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static ConduitListener<FixedLengthStreamSourceConduit> fixedLengthDrainListener(final HttpServerExchange exchange) {
    return new ConduitListener<FixedLengthStreamSourceConduit>() {
        public void handleEvent(final FixedLengthStreamSourceConduit fixedLengthConduit) {
            long remaining = fixedLengthConduit.getRemaining();
            if (remaining > 0L) {
                UndertowLogger.REQUEST_LOGGER.requestWasNotFullyConsumed();
                exchange.setPersistent(false);
            }
            Connectors.terminateRequest(exchange);
        }
    };
}
 
Example #9
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static ConduitListener<ChunkedStreamSourceConduit> chunkedDrainListener(final HttpServerExchange exchange) {
    return new ConduitListener<ChunkedStreamSourceConduit>() {
        public void handleEvent(final ChunkedStreamSourceConduit chunkedStreamSourceConduit) {
            if (!chunkedStreamSourceConduit.isFinished()) {
                UndertowLogger.REQUEST_LOGGER.requestWasNotFullyConsumed();
                exchange.setPersistent(false);
            }
            Connectors.terminateRequest(exchange);
        }
    };
}
 
Example #10
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static ConduitListener<StreamSinkConduit> terminateResponseListener(final HttpServerExchange exchange) {
    return new ConduitListener<StreamSinkConduit>() {
        public void handleEvent(final StreamSinkConduit channel) {
            Connectors.terminateResponse(exchange);
        }
    };
}
 
Example #11
Source File: Methods.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static HttpString fromString(String method) {
    HttpString res = METHODS.get(method);
    if(res == null) {
        HttpString httpString = new HttpString(method);
        Connectors.verifyToken(httpString);
        return httpString;
    }
    return res;
}
 
Example #12
Source File: RequestLimit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void handleRequest(final HttpServerExchange exchange, final HttpHandler next) throws Exception {
    int oldVal, newVal;
    do {
        oldVal = requests;
        if (oldVal >= max) {
            exchange.dispatch(SameThreadExecutor.INSTANCE, new Runnable() {
                @Override
                public void run() {
                    //we have to try again in the sync block
                    //we need to have already dispatched for thread safety reasons
                    synchronized (RequestLimit.this) {
                        int oldVal, newVal;
                        do {
                            oldVal = requests;
                            if (oldVal >= max) {
                                if (!queue.offer(new SuspendedRequest(exchange, next))) {
                                    Connectors.executeRootHandler(failureHandler, exchange);
                                }
                                return;
                            }
                            newVal = oldVal + 1;
                        } while (!requestsUpdater.compareAndSet(RequestLimit.this, oldVal, newVal));
                        exchange.addExchangeCompleteListener(COMPLETION_LISTENER);
                        exchange.dispatch(next);
                    }
                }
            });
            return;
        }
        newVal = oldVal + 1;
    } while (!requestsUpdater.compareAndSet(this, oldVal, newVal));
    exchange.addExchangeCompleteListener(COMPLETION_LISTENER);
    next.handleRequest(exchange);
}
 
Example #13
Source File: GzipStreamSinkConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public GzipStreamSinkConduit(
        ConduitFactory<StreamSinkConduit> conduitFactory,
        HttpServerExchange exchange,
        ObjectPool deflaterPool) {
    super(conduitFactory, exchange, deflaterPool);
    writeHeader();
    Connectors.updateResponseBytesSent(exchange, HEADER.length);
}
 
Example #14
Source File: DeflatingStreamSinkConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public DeflatingStreamSinkConduit(final ConduitFactory<StreamSinkConduit> conduitFactory, final HttpServerExchange exchange, ObjectPool<Deflater> deflaterPool) {
    this.pooledObject = deflaterPool.allocate();
    this.deflater = pooledObject.getObject();
    this.currentBuffer = exchange.getConnection().getByteBufferPool().allocate();
    this.exchange = exchange;
    this.conduitFactory = conduitFactory;
    setWriteReadyHandler(new WriteReadyHandler.ChannelListenerHandler<>(Connectors.getConduitSinkChannel(exchange)));
}
 
Example #15
Source File: DeflatingStreamSinkConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int write(final ByteBuffer src) throws IOException {
    if (anyAreSet(state, SHUTDOWN | CLOSED) || currentBuffer == null) {
        throw new ClosedChannelException();
    }
    try {
        if (!performFlushIfRequired()) {
            return 0;
        }
        if (src.remaining() == 0) {
            return 0;
        }
        //we may already have some input, if so compress it
        if (!deflater.needsInput()) {
            deflateData(false);
            if (!deflater.needsInput()) {
                return 0;
            }
        }
        byte[] data = new byte[src.remaining()];
        src.get(data);
        preDeflate(data);
        deflater.setInput(data);
        Connectors.updateResponseBytesSent(exchange, 0 - data.length);
        deflateData(false);
        return data.length;
    } catch (IOException | RuntimeException | Error e) {
        freeBuffer();
        throw e;
    }
}
 
Example #16
Source File: AsyncRequestBodyReceiver.java    From divolte-collector with Apache License 2.0 4 votes vote down vote up
private void receive(final BiConsumer<InputStream, Integer> callback,
                     final HttpServerExchange exchange,
                     final Optional<Integer> contentLength,
                     final int provisionChunks) {
    logger.debug("Starting to receive request body.");
    final StreamSourceChannel channel = exchange.getRequestChannel();
    if (channel == null) {
        throw UndertowMessages.MESSAGES.requestChannelAlreadyProvided();
    }

    /*
     * NOTE: We could make this use buffers from Undertow's / XNio's buffer pool,
     * instead of allocating our own. On G1, I think it makes more sense to
     * allocate the buffers without pooling, though. As they are fixed size and
     * < 1MB, allocation and pooling shouldn't differ that much performance-wise.
     * Undertow's buffer pool consists of native buffer in most cases, while these
     * buffers are used exclusively on heap after receiving the bytes from the
     * socket.
     */
    ChunkyByteBuffer.fill(channel, provisionChunks, contentLength.isPresent() ? provisionChunks : maximumChunks,
                          new ChunkyByteBuffer.CompletionHandler() {
        @Override
        public void overflow() {
            if (logger.isWarnEnabled()) {
                logger.warn("Request body too large; rejecting request from {}", getPeerHost(exchange));
            }
            rejectLargeRequest(exchange);
        }
        @Override
        public void failed(final Throwable e) {
            if (logger.isWarnEnabled()) {
                logger.warn("Error while reading request body received from " + getPeerHost(exchange), e);
            }
            Connectors.executeRootHandler(exchange -> exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR)
                                                              .endExchange(),
                                          exchange);
        }
        @Override
        public void completed(final InputStream body, final int bodyLength) {
            // Sanity check that the body size matches the content-length header, if it
            // was supplied.
            if (contentLength.map(l -> l != bodyLength).orElse(false)) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Bad request from {}; content length ({}) didn't match actual body length ({}).",
                                getPeerHost(exchange), contentLength.orElse(-1), bodyLength);
                }
                exchange.setStatusCode(StatusCodes.BAD_REQUEST)
                        .endExchange();
                return;
            }
            // First bump the global default for the number of chunks that we pre-allocate.
            final int newNumChunks = calculateChunks(bodyLength);
            int currentNumChunks;
            while ((currentNumChunks = preallocateChunks.get()) < newNumChunks) {
                if (preallocateChunks.compareAndSet(currentNumChunks, newNumChunks)) {
                    logger.info("Updated default body buffer size from {} to {}",
                                 currentNumChunks * ChunkyByteBuffer.CHUNK_SIZE,
                                 newNumChunks * ChunkyByteBuffer.CHUNK_SIZE);
                    break;
                }
            }
            // Pass the body on to the upstream handler.
            callback.accept(body, bodyLength);
        }
    });
}
 
Example #17
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
static StreamSinkConduit createSinkConduit(final HttpServerExchange exchange) {
    DateUtils.addDateHeaderIfRequired(exchange);

    boolean headRequest = exchange.getRequestMethod().equals(Methods.HEAD);
    HttpServerConnection serverConnection = (HttpServerConnection) exchange.getConnection();

    HttpResponseConduit responseConduit = serverConnection.getResponseConduit();
    responseConduit.reset(exchange);
    StreamSinkConduit channel = responseConduit;
    if (headRequest) {
        //if this is a head request we add a head channel underneath the content encoding channel
        //this will just discard the data
        //we still go through with the rest of the logic, to make sure all headers are set correctly
        channel = new HeadStreamSinkConduit(channel, terminateResponseListener(exchange));
    } else if(!Connectors.isEntityBodyAllowed(exchange)) {
        //we are not allowed to send an entity body for some requests
        exchange.getResponseHeaders().remove(Headers.CONTENT_LENGTH);
        exchange.getResponseHeaders().remove(Headers.TRANSFER_ENCODING);
        channel = new HeadStreamSinkConduit(channel, terminateResponseListener(exchange));
        return channel;
    }

    final HeaderMap responseHeaders = exchange.getResponseHeaders();
    // test to see if we're still persistent
    String connection = responseHeaders.getFirst(Headers.CONNECTION);
    if (!exchange.isPersistent()) {
        responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString());
    } else if (exchange.isPersistent() && connection != null) {
        if (HttpString.tryFromString(connection).equals(Headers.CLOSE)) {
            exchange.setPersistent(false);
        }
    } else if (exchange.getConnection().getUndertowOptions().get(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, true)) {
        responseHeaders.put(Headers.CONNECTION, Headers.KEEP_ALIVE.toString());
    }
    //according to the HTTP RFC we should ignore content length if a transfer coding is specified
    final String transferEncodingHeader = responseHeaders.getLast(Headers.TRANSFER_ENCODING);
    if(transferEncodingHeader == null) {
        final String contentLengthHeader = responseHeaders.getFirst(Headers.CONTENT_LENGTH);
        if (contentLengthHeader != null) {
            StreamSinkConduit res = handleFixedLength(exchange, headRequest, channel, responseHeaders, contentLengthHeader, serverConnection);
            if (res != null) {
                return res;
            }
        }
    } else {
        responseHeaders.remove(Headers.CONTENT_LENGTH); //if there is a transfer-encoding header we remove content length if present
    }
    return handleResponseConduit(exchange, headRequest, channel, responseHeaders, terminateResponseListener(exchange), transferEncodingHeader);
}
 
Example #18
Source File: HttpTransferEncoding.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static void setupRequest(final HttpServerExchange exchange) {
    final HeaderMap requestHeaders = exchange.getRequestHeaders();
    final String connectionHeader = requestHeaders.getFirst(Headers.CONNECTION);
    final String transferEncodingHeader = requestHeaders.getLast(Headers.TRANSFER_ENCODING);
    final String contentLengthHeader = requestHeaders.getFirst(Headers.CONTENT_LENGTH);

    final HttpServerConnection connection = (HttpServerConnection) exchange.getConnection();
    //if we are already using the pipelineing buffer add it to the exchange
    PipeliningBufferingStreamSinkConduit pipeliningBuffer = connection.getPipelineBuffer();
    if (pipeliningBuffer != null) {
        pipeliningBuffer.setupPipelineBuffer(exchange);
    }
    ConduitStreamSourceChannel sourceChannel = connection.getChannel().getSourceChannel();
    sourceChannel.setConduit(connection.getReadDataStreamSourceConduit());

    boolean persistentConnection = persistentConnection(exchange, connectionHeader);

    if (transferEncodingHeader == null && contentLengthHeader == null) {
        if (persistentConnection
                && connection.getExtraBytes() != null
                && pipeliningBuffer == null
                && connection.getUndertowOptions().get(UndertowOptions.BUFFER_PIPELINED_DATA, false)) {
            pipeliningBuffer = new PipeliningBufferingStreamSinkConduit(connection.getOriginalSinkConduit(), connection.getByteBufferPool());
            connection.setPipelineBuffer(pipeliningBuffer);
            pipeliningBuffer.setupPipelineBuffer(exchange);
        }
        // no content - immediately start the next request, returning an empty stream for this one
        Connectors.terminateRequest(exchange);
    } else {
        persistentConnection = handleRequestEncoding(exchange, transferEncodingHeader, contentLengthHeader, connection, pipeliningBuffer, persistentConnection);
    }

    exchange.setPersistent(persistentConnection);

    if (!exchange.isRequestComplete() || connection.getExtraBytes() != null) {
        //if there is more data we suspend reads
        sourceChannel.setReadListener(null);
        sourceChannel.suspendReads();
    }

}
 
Example #19
Source File: Http2HeaderBlockParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void emitHeader(HttpString name, String value, boolean neverIndex) throws HpackException {
    if(maxHeaderListSize > 0) {
        headerSize += (name.length() + value.length() + 32);
        if (headerSize > maxHeaderListSize) {
            throw new HpackException(UndertowMessages.MESSAGES.headerBlockTooLarge(), Http2Channel.ERROR_PROTOCOL_ERROR);
        }
    }
    if(maxHeaders > 0 && headerMap.size() > maxHeaders) {
        return;
    }
    headerMap.add(name, value);

    if(name.length() == 0) {
        throw UndertowMessages.MESSAGES.invalidHeader();
    }
    if(name.equals(Headers.TRANSFER_ENCODING)) {
        throw new HpackException(Http2Channel.ERROR_PROTOCOL_ERROR);
    }
    if(name.byteAt(0) == ':') {
        if(client) {
            if(!name.equals(Http2Channel.STATUS)) {
                invalid = true;
            }
        } else {
            if(!SERVER_HEADERS.contains(name)) {
                invalid = true;
            }
        }
        if(!processingPseudoHeaders) {
            throw new HpackException(UndertowMessages.MESSAGES.pseudoHeaderInWrongOrder(name), Http2Channel.ERROR_PROTOCOL_ERROR);
        }
    } else {
        processingPseudoHeaders = false;
    }
    for(int i = 0; i < name.length(); ++i) {
        byte c = name.byteAt(i);
        if(c>= 'A' && c <= 'Z') {
            invalid = true;
            UndertowLogger.REQUEST_LOGGER.debugf("Malformed request, header %s contains uppercase characters", name);
        } else if(c != ':' && !Connectors.isValidTokenCharacter(c)) {
            invalid = true;
            UndertowLogger.REQUEST_LOGGER.debugf("Malformed request, header %s contains invalid token character", name);
        }
    }

}