Search in sources :

Example 1 with HeaderMap

use of io.undertow.util.HeaderMap in project camel by apache.

the class UndertowProducer method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    ClientConnection connection = null;
    try {
        final UndertowClient client = UndertowClient.getInstance();
        IoFuture<ClientConnection> connect = client.connect(endpoint.getHttpURI(), worker, pool, options);
        // creating the url to use takes 2-steps
        final String exchangeUri = UndertowHelper.createURL(exchange, getEndpoint());
        final URI uri = UndertowHelper.createURI(exchange, exchangeUri, getEndpoint());
        final String pathAndQuery = URISupport.pathAndQueryOf(uri);
        // what http method to use
        HttpString method = UndertowHelper.createMethod(exchange, endpoint, exchange.getIn().getBody() != null);
        ClientRequest request = new ClientRequest();
        request.setProtocol(Protocols.HTTP_1_1);
        request.setPath(pathAndQuery);
        request.setMethod(method);
        final HeaderMap requestHeaders = request.getRequestHeaders();
        // Set the Host header
        Message message = exchange.getIn();
        final String host = message.getHeader("Host", String.class);
        requestHeaders.put(Headers.HOST, Optional.ofNullable(host).orElseGet(() -> uri.getAuthority()));
        Object body = getRequestBody(request, exchange);
        TypeConverter tc = endpoint.getCamelContext().getTypeConverter();
        ByteBuffer bodyAsByte = tc.tryConvertTo(ByteBuffer.class, body);
        if (body != null) {
            requestHeaders.put(Headers.CONTENT_LENGTH, bodyAsByte.array().length);
        }
        if (getEndpoint().getCookieHandler() != null) {
            Map<String, List<String>> cookieHeaders = getEndpoint().getCookieHandler().loadCookies(exchange, uri);
            for (Map.Entry<String, List<String>> entry : cookieHeaders.entrySet()) {
                requestHeaders.putAll(new HttpString(entry.getKey()), entry.getValue());
            }
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Executing http {} method: {}", method, pathAndQuery);
        }
        connection = connect.get();
        connection.sendRequest(request, new UndertowProducerCallback(connection, bodyAsByte, exchange, callback));
    } catch (Exception e) {
        IOHelper.close(connection);
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // use async routing engine
    return false;
}
Also used : Message(org.apache.camel.Message) UndertowClient(io.undertow.client.UndertowClient) HttpString(io.undertow.util.HttpString) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) TypeConverter(org.apache.camel.TypeConverter) HeaderMap(io.undertow.util.HeaderMap) ClientConnection(io.undertow.client.ClientConnection) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) OptionMap(org.xnio.OptionMap) HeaderMap(io.undertow.util.HeaderMap) Map(java.util.Map) ClientRequest(io.undertow.client.ClientRequest) HttpString(io.undertow.util.HttpString)

Example 2 with HeaderMap

use of io.undertow.util.HeaderMap in project wildfly by wildfly.

the class DigestAuthenticationMechanism method sendChallenge.

@Override
public ChallengeResult sendChallenge(final HttpServerExchange exchange, final SecurityContext securityContext) {
    DigestContext context = exchange.getAttachment(DigestContext.ATTACHMENT_KEY);
    boolean stale = context == null ? false : context.isStale();
    StringBuilder rb = new StringBuilder(DIGEST_PREFIX);
    rb.append(Headers.REALM.toString()).append("=\"").append(realmName).append("\",");
    rb.append(Headers.DOMAIN.toString()).append("=\"").append(domain).append("\",");
    // based on security constraints.
    rb.append(Headers.NONCE.toString()).append("=\"").append(nonceManager.nextNonce(null, exchange)).append("\",");
    // Not currently using OPAQUE as it offers no integrity, used for session data leaves it vulnerable to
    // session fixation type issues as well.
    rb.append(Headers.OPAQUE.toString()).append("=\"00000000000000000000000000000000\"");
    if (stale) {
        rb.append(",stale=true");
    }
    if (supportedAlgorithms.size() > 0) {
        // This header will need to be repeated once for each algorithm.
        rb.append(",").append(Headers.ALGORITHM.toString()).append("=%s");
    }
    if (qopString != null) {
        rb.append(",").append(Headers.QOP.toString()).append("=\"").append(qopString).append("\"");
    }
    String theChallenge = rb.toString();
    HeaderMap responseHeader = exchange.getResponseHeaders();
    if (supportedAlgorithms.isEmpty()) {
        responseHeader.add(WWW_AUTHENTICATE, theChallenge);
    } else {
        for (DigestAlgorithm current : supportedAlgorithms) {
            responseHeader.add(WWW_AUTHENTICATE, String.format(theChallenge, current.getToken()));
        }
    }
    return new ChallengeResult(true, UNAUTHORIZED);
}
Also used : HeaderMap(io.undertow.util.HeaderMap) DigestAlgorithm(io.undertow.security.idm.DigestAlgorithm)

Example 3 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class LotsOfHeadersRequestTestCase method setup.

@BeforeClass
public static void setup() {
    Assume.assumeFalse(DefaultServer.isH2upgrade());
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            HeaderMap headers = exchange.getRequestHeaders();
            for (HeaderValues header : headers) {
                for (String val : header) {
                    exchange.getResponseHeaders().put(HttpString.tryFromString(header.getHeaderName().toString()), val);
                }
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) HttpString(io.undertow.util.HttpString) BeforeClass(org.junit.BeforeClass)

Example 4 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class DigestAuthenticationMechanism method sendAuthenticationInfoHeader.

public void sendAuthenticationInfoHeader(final HttpServerExchange exchange) {
    DigestContext context = exchange.getAttachment(DigestContext.ATTACHMENT_KEY);
    DigestQop qop = context.getQop();
    String currentNonce = context.getNonce();
    String nextNonce = nonceManager.nextNonce(currentNonce, exchange);
    if (qop != null || !nextNonce.equals(currentNonce)) {
        StringBuilder sb = new StringBuilder();
        sb.append(NEXT_NONCE).append("=\"").append(nextNonce).append("\"");
        if (qop != null) {
            Map<DigestAuthorizationToken, String> parsedHeader = context.getParsedHeader();
            sb.append(",").append(Headers.QOP.toString()).append("=\"").append(qop.getToken()).append("\"");
            byte[] ha1 = context.getHa1();
            byte[] ha2;
            if (qop == DigestQop.AUTH) {
                ha2 = createHA2Auth(context);
            } else {
                ha2 = createHA2AuthInt();
            }
            String rspauth = new String(createRFC2617RequestDigest(ha1, ha2, context), StandardCharsets.UTF_8);
            sb.append(",").append(Headers.RESPONSE_AUTH.toString()).append("=\"").append(rspauth).append("\"");
            sb.append(",").append(Headers.CNONCE.toString()).append("=\"").append(parsedHeader.get(DigestAuthorizationToken.CNONCE)).append("\"");
            sb.append(",").append(Headers.NONCE_COUNT.toString()).append("=").append(parsedHeader.get(DigestAuthorizationToken.NONCE_COUNT));
        }
        HeaderMap responseHeader = exchange.getResponseHeaders();
        responseHeader.add(AUTHENTICATION_INFO, sb.toString());
    }
    exchange.removeAttachment(DigestContext.ATTACHMENT_KEY);
}
Also used : HeaderMap(io.undertow.util.HeaderMap)

Example 5 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class HttpServerExchange method upgradeChannel.

/**
     * Upgrade the channel to a raw socket. This method set the response code to 101, and then marks both the
     * request and response as terminated, which means that once the current request is completed the raw channel
     * can be obtained from {@link io.undertow.server.protocol.http.HttpServerConnection#getChannel()}
     *
     * @param productName the product name to report to the client
     * @throws IllegalStateException if a response or upgrade was already sent, or if the request body is already being
     *                               read
     */
public HttpServerExchange upgradeChannel(String productName, final HttpUpgradeListener listener) {
    if (!connection.isUpgradeSupported()) {
        throw UndertowMessages.MESSAGES.upgradeNotSupported();
    }
    UndertowLogger.REQUEST_LOGGER.debugf("Upgrading request %s", this);
    connection.setUpgradeListener(listener);
    setStatusCode(StatusCodes.SWITCHING_PROTOCOLS);
    final HeaderMap headers = getResponseHeaders();
    headers.put(Headers.UPGRADE, productName);
    headers.put(Headers.CONNECTION, Headers.UPGRADE_STRING);
    return this;
}
Also used : HeaderMap(io.undertow.util.HeaderMap)

Aggregations

HeaderMap (io.undertow.util.HeaderMap)30 HttpString (io.undertow.util.HttpString)17 HeaderValues (io.undertow.util.HeaderValues)9 ByteBuffer (java.nio.ByteBuffer)7 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)6 HttpServerExchange (io.undertow.server.HttpServerExchange)6 IOException (java.io.IOException)6 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)3 HttpHandler (io.undertow.server.HttpHandler)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BeforeClass (org.junit.BeforeClass)3 ClientRequest (io.undertow.client.ClientRequest)2 DigestAlgorithm (io.undertow.security.idm.DigestAlgorithm)2 ImmediatePooledByteBuffer (io.undertow.util.ImmediatePooledByteBuffer)2 ParameterLimitException (io.undertow.util.ParameterLimitException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2