io.undertow.util.Protocols Java Examples

The following examples show how to use io.undertow.util.Protocols. 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: HttpServletResponseImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void setTrailerFields(Supplier<Map<String, String>> supplier) {
    if(exchange.isResponseStarted()) {
        throw UndertowServletMessages.MESSAGES.responseAlreadyCommited();
    }
    if(exchange.getProtocol() == Protocols.HTTP_1_0) {
        throw UndertowServletMessages.MESSAGES.trailersNotSupported("HTTP/1.0 request");
    } else if(exchange.getProtocol() == Protocols.HTTP_1_1) {
        if(exchange.getResponseHeaders().contains(Headers.CONTENT_LENGTH)) {
            throw UndertowServletMessages.MESSAGES.trailersNotSupported("not chunked");
        }
    }
    this.trailerSupplier = supplier;
    exchange.putAttachment(HttpAttachments.RESPONSE_TRAILER_SUPPLIER, () -> {
        HeaderMap trailers = new HeaderMap();
        Map<String, String> map = supplier.get();
        for(Map.Entry<String, String> e : map.entrySet()) {
            trailers.put(new HttpString(e.getKey()), e.getValue());
        }
        return trailers;
    });
}
 
Example #2
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 #3
Source File: Http2ClientExchange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
ClientResponse createResponse(Http2StreamSourceChannel result) {
    HeaderMap headers = result.getHeaders();
    final String status = result.getHeaders().getFirst(Http2Channel.STATUS);
    int statusCode = Integer.parseInt(status);
    headers.remove(Http2Channel.STATUS);
    return new ClientResponse(statusCode, status.substring(3), Protocols.HTTP_2_0, headers);
}
 
Example #4
Source File: ServletInitialHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void dispatchMockRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    final DefaultByteBufferPool bufferPool = new DefaultByteBufferPool(false, 1024, 0, 0);
    MockServerConnection connection = new MockServerConnection(bufferPool);
    HttpServerExchange exchange = new HttpServerExchange(connection);
    exchange.setRequestScheme(request.getScheme());
    exchange.setRequestMethod(new HttpString(request.getMethod()));
    exchange.setProtocol(Protocols.HTTP_1_0);
    exchange.setResolvedPath(request.getContextPath());
    String relative;
    if (request.getPathInfo() == null) {
        relative = request.getServletPath();
    } else {
        relative = request.getServletPath() + request.getPathInfo();
    }
    exchange.setRelativePath(relative);
    final ServletPathMatch info = paths.getServletHandlerByPath(request.getServletPath());
    final HttpServletResponseImpl oResponse = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl oRequest = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), oRequest, oResponse, info);
    servletRequestContext.setServletRequest(request);
    servletRequestContext.setServletResponse(response);
    //set the max request size if applicable
    if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) {
        exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
    }
    exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);

    exchange.startBlocking(new ServletBlockingHttpExchange(exchange));
    servletRequestContext.setServletPathMatch(info);

    try {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new ServletException(e);
    }
}
 
Example #5
Source File: AjpClientConnection.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void initiateRequest(AjpClientExchange AjpClientExchange) {
    currentRequest = AjpClientExchange;
    ClientRequest request = AjpClientExchange.getRequest();

    String connectionString = request.getRequestHeaders().getFirst(CONNECTION);
    if (connectionString != null) {
        if (CLOSE.equalToString(connectionString)) {
            state |= CLOSE_REQ;
        }
    } else if (request.getProtocol() != Protocols.HTTP_1_1) {
        state |= CLOSE_REQ;
    }
    if (request.getRequestHeaders().contains(UPGRADE)) {
        state |= UPGRADE_REQUESTED;
    }

    long length = 0;
    String fixedLengthString = request.getRequestHeaders().getFirst(CONTENT_LENGTH);
    String transferEncodingString = request.getRequestHeaders().getLast(TRANSFER_ENCODING);

    if (fixedLengthString != null) {
        length = Long.parseLong(fixedLengthString);
    } else if (transferEncodingString != null) {
        length = -1;
    }

    AjpClientRequestClientStreamSinkChannel sinkChannel = connection.sendRequest(request.getMethod(), request.getPath(), request.getProtocol(), request.getRequestHeaders(), request, requestFinishListener);
    currentRequest.setRequestChannel(sinkChannel);

    AjpClientExchange.invokeReadReadyCallback(AjpClientExchange);
    if (length == 0) {
        //if there is no content we flush the response channel.
        //otherwise it is up to the user
        try {
            sinkChannel.shutdownWrites();
            if (!sinkChannel.flush()) {
                handleFailedFlush(sinkChannel);
            }
        } catch (Throwable t) {
            handleError((t instanceof IOException) ? (IOException) t : new IOException(t));
        }
    }
}
 
Example #6
Source File: HttpClientConnection.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void initiateRequest(HttpClientExchange httpClientExchange) {
    this.requestCount++;
    currentRequest = httpClientExchange;
    pendingResponse = new HttpResponseBuilder();
    ClientRequest request = httpClientExchange.getRequest();

    String connectionString = request.getRequestHeaders().getFirst(Headers.CONNECTION);
    if (connectionString != null) {
        if (Headers.CLOSE.equalToString(connectionString)) {
            state |= CLOSE_REQ;
        } else if (Headers.UPGRADE.equalToString(connectionString)) {
            state |= UPGRADE_REQUESTED;
        }
    } else if (request.getProtocol() != Protocols.HTTP_1_1) {
        state |= CLOSE_REQ;
    }
    if (request.getRequestHeaders().contains(Headers.UPGRADE)) {
        state |= UPGRADE_REQUESTED;
    }
    if(request.getMethod().equals(Methods.CONNECT)) {
        //we treat CONNECT like upgrade requests
        state |= UPGRADE_REQUESTED;
    }

    //setup the client request conduits
    final ConduitStreamSourceChannel sourceChannel = connection.getSourceChannel();
    sourceChannel.setReadListener(clientReadListener);
    sourceChannel.resumeReads();

    ConduitStreamSinkChannel sinkChannel = connection.getSinkChannel();
    StreamSinkConduit conduit = originalSinkConduit;
    HttpRequestConduit httpRequestConduit = new HttpRequestConduit(conduit, bufferPool, request);
    httpClientExchange.setRequestConduit(httpRequestConduit);
    conduit = httpRequestConduit;

    String fixedLengthString = request.getRequestHeaders().getFirst(Headers.CONTENT_LENGTH);
    String transferEncodingString = request.getRequestHeaders().getLast(Headers.TRANSFER_ENCODING);

    boolean hasContent = true;

    if (fixedLengthString != null) {
        try {
            long length = Long.parseLong(fixedLengthString);
            conduit = new ClientFixedLengthStreamSinkConduit(conduit, length, false, false, currentRequest);
            hasContent = length != 0;
        } catch (NumberFormatException e) {
            handleError(e);
            return;
        }
    } else if (transferEncodingString != null) {
        if (!transferEncodingString.toLowerCase(Locale.ENGLISH).contains(Headers.CHUNKED.toString())) {
            handleError(UndertowClientMessages.MESSAGES.unknownTransferEncoding(transferEncodingString));
            return;
        }
        conduit = new ChunkedStreamSinkConduit(conduit, httpClientExchange.getConnection().getBufferPool(), false, false, httpClientExchange.getRequest().getRequestHeaders(), requestFinishListener, httpClientExchange);
    } else {
        conduit = new ClientFixedLengthStreamSinkConduit(conduit, 0, false, false, currentRequest);
        hasContent = false;
    }
    sinkChannel.setConduit(conduit);

    httpClientExchange.invokeReadReadyCallback();
    if (!hasContent) {
        //if there is no content we flush the response channel.
        //otherwise it is up to the user
        try {
            sinkChannel.shutdownWrites();
            if (!sinkChannel.flush()) {
                sinkChannel.setWriteListener(ChannelListeners.flushingChannelListener(null, new ChannelExceptionHandler<ConduitStreamSinkChannel>() {
                    @Override
                    public void handleException(ConduitStreamSinkChannel channel, IOException exception) {
                        handleError(exception);
                    }
                }));
                sinkChannel.resumeWrites();
            }
        } catch (Throwable t) {
            handleError(t);
        }
    }
}
 
Example #7
Source File: HttpRequestParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void handle(ByteBuffer buffer, final ParseState currentState, final HttpServerExchange builder) throws BadRequestException {
    if (currentState.state == ParseState.VERB) {
        //fast path, we assume that it will parse fully so we avoid all the if statements

        //fast path HTTP GET requests, basically just assume all requests are get
        //and fall out to the state machine if it is not
        final int position = buffer.position();
        if (buffer.remaining() > 3
                && buffer.get(position) == 'G'
                && buffer.get(position + 1) == 'E'
                && buffer.get(position + 2) == 'T'
                && buffer.get(position + 3) == ' ') {
            buffer.position(position + 4);
            builder.setRequestMethod(Methods.GET);
            currentState.state = ParseState.PATH;
        } else {
            try {
                handleHttpVerb(buffer, currentState, builder);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException(e);
            }
        }
        handlePath(buffer, currentState, builder);
        boolean failed = false;
        if (buffer.remaining() > HTTP_LENGTH + 3) {
            int pos = buffer.position();
            for (int i = 0; i < HTTP_LENGTH; ++i) {
                if (HTTP[i] != buffer.get(pos + i)) {
                    failed = true;
                    break;
                }
            }
            if (!failed) {
                final byte b = buffer.get(pos + HTTP_LENGTH);
                final byte b2 = buffer.get(pos + HTTP_LENGTH + 1);
                final byte b3 = buffer.get(pos + HTTP_LENGTH + 2);
                if (b2 == '\r' && b3 == '\n') {
                    if (b == '1') {
                        builder.setProtocol(Protocols.HTTP_1_1);
                        buffer.position(pos + HTTP_LENGTH + 3);
                        currentState.state = ParseState.HEADER;
                    } else if (b == '0') {
                        builder.setProtocol(Protocols.HTTP_1_0);
                        buffer.position(pos + HTTP_LENGTH + 3);
                        currentState.state = ParseState.HEADER;
                    } else {
                        failed = true;
                    }
                } else {
                    failed = true;
                }
            }
        } else {
            failed = true;
        }
        if (failed) {
            handleHttpVersion(buffer, currentState, builder);
            handleAfterVersion(buffer, currentState);
        }

        while (currentState.state != ParseState.PARSE_COMPLETE && buffer.hasRemaining()) {
            handleHeader(buffer, currentState, builder);
            if (currentState.state == ParseState.HEADER_VALUE) {
                handleHeaderValue(buffer, currentState, builder);
            }
        }
        return;
    }
    handleStateful(buffer, currentState, builder);
}
 
Example #8
Source File: HttpServerExchange.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Determine whether this request conforms to HTTP 0.9.
 *
 * @return {@code true} if the request protocol is equal to {@link Protocols#HTTP_0_9}, {@code false} otherwise
 */
public boolean isHttp09() {
    return protocol.equals(Protocols.HTTP_0_9);
}
 
Example #9
Source File: HttpServerExchange.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Determine whether this request conforms to HTTP 1.0.
 *
 * @return {@code true} if the request protocol is equal to {@link Protocols#HTTP_1_0}, {@code false} otherwise
 */
public boolean isHttp10() {
    return protocol.equals(Protocols.HTTP_1_0);
}
 
Example #10
Source File: HttpServerExchange.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Determine whether this request conforms to HTTP 1.1.
 *
 * @return {@code true} if the request protocol is equal to {@link Protocols#HTTP_1_1}, {@code false} otherwise
 */
public boolean isHttp11() {
    return protocol.equals(Protocols.HTTP_1_1);
}