Search in sources :

Example 31 with HttpString

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

the class Http2ServerConnection method sendOutOfBandResponse.

@Override
public HttpServerExchange sendOutOfBandResponse(HttpServerExchange exchange) {
    if (exchange == null || !HttpContinue.requiresContinueResponse(exchange)) {
        throw UndertowMessages.MESSAGES.outOfBandResponseOnlyAllowedFor100Continue();
    }
    final HttpServerExchange newExchange = new HttpServerExchange(this);
    for (HttpString header : exchange.getRequestHeaders().getHeaderNames()) {
        newExchange.getRequestHeaders().putAll(header, exchange.getRequestHeaders().get(header));
    }
    newExchange.setProtocol(exchange.getProtocol());
    newExchange.setRequestMethod(exchange.getRequestMethod());
    exchange.setRequestURI(exchange.getRequestURI(), exchange.isHostIncludedInRequestURI());
    exchange.setRequestPath(exchange.getRequestPath());
    exchange.setRelativePath(exchange.getRelativePath());
    newExchange.setPersistent(true);
    Connectors.terminateRequest(newExchange);
    newExchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

        @Override
        public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
            HeaderMap headers = newExchange.getResponseHeaders();
            DateUtils.addDateHeaderIfRequired(exchange);
            headers.add(STATUS, exchange.getStatusCode());
            Connectors.flattenCookies(exchange);
            Http2HeadersStreamSinkChannel sink = new Http2HeadersStreamSinkChannel(channel, requestChannel.getStreamId(), headers);
            StreamSinkChannelWrappingConduit ret = new StreamSinkChannelWrappingConduit(sink);
            ret.setWriteReadyHandler(new WriteReadyHandler.ChannelListenerHandler(Connectors.getConduitSinkChannel(exchange)));
            return ret;
        }
    });
    continueSent = true;
    return newExchange;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) Http2HeadersStreamSinkChannel(io.undertow.protocols.http2.Http2HeadersStreamSinkChannel) HeaderMap(io.undertow.util.HeaderMap) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) StreamSinkChannelWrappingConduit(org.xnio.conduits.StreamSinkChannelWrappingConduit) HttpString(io.undertow.util.HttpString)

Example 32 with HttpString

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

the class AjpReadListener method createSourceConduit.

private StreamSourceConduit createSourceConduit(StreamSourceConduit underlyingConduit, AjpServerResponseConduit responseConduit, final HttpServerExchange exchange) {
    ReadDataStreamSourceConduit conduit = new ReadDataStreamSourceConduit(underlyingConduit, (AbstractServerConnection) exchange.getConnection());
    final HeaderMap requestHeaders = exchange.getRequestHeaders();
    HttpString transferEncoding = Headers.IDENTITY;
    Long length;
    final String teHeader = requestHeaders.getLast(Headers.TRANSFER_ENCODING);
    boolean hasTransferEncoding = teHeader != null;
    if (hasTransferEncoding) {
        transferEncoding = new HttpString(teHeader);
    }
    final String requestContentLength = requestHeaders.getFirst(Headers.CONTENT_LENGTH);
    if (hasTransferEncoding && !transferEncoding.equals(Headers.IDENTITY)) {
        //unknown length
        length = null;
    } else if (requestContentLength != null) {
        final long contentLength = Long.parseLong(requestContentLength);
        if (contentLength == 0L) {
            UndertowLogger.REQUEST_LOGGER.trace("No content, starting next request");
            // no content - immediately start the next request, returning an empty stream for this one
            Connectors.terminateRequest(httpServerExchange);
            return new EmptyStreamSourceConduit(conduit.getReadThread());
        } else {
            length = contentLength;
        }
    } else {
        UndertowLogger.REQUEST_LOGGER.trace("No content length or transfer coding, starting next request");
        // no content - immediately start the next request, returning an empty stream for this one
        Connectors.terminateRequest(exchange);
        return new EmptyStreamSourceConduit(conduit.getReadThread());
    }
    return new AjpServerRequestConduit(conduit, exchange, responseConduit, length, new ConduitListener<AjpServerRequestConduit>() {

        @Override
        public void handleEvent(AjpServerRequestConduit channel) {
            Connectors.terminateRequest(exchange);
        }
    });
}
Also used : EmptyStreamSourceConduit(io.undertow.conduits.EmptyStreamSourceConduit) HeaderMap(io.undertow.util.HeaderMap) ReadDataStreamSourceConduit(io.undertow.conduits.ReadDataStreamSourceConduit) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Example 33 with HttpString

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

the class MCMPHandler method processConfig.

/**
     * Process the node config.
     *
     * @param exchange the http server exchange
     * @param requestData the request data
     * @throws IOException
     */
private void processConfig(final HttpServerExchange exchange, final RequestData requestData) throws IOException {
    // Get the node builder
    List<String> hosts = null;
    List<String> contexts = null;
    final Balancer.BalancerBuilder balancer = Balancer.builder();
    final NodeConfig.NodeBuilder node = NodeConfig.builder(modCluster);
    final Iterator<HttpString> i = requestData.iterator();
    while (i.hasNext()) {
        final HttpString name = i.next();
        final String value = requestData.getFirst(name);
        UndertowLogger.ROOT_LOGGER.mcmpKeyValue(name, value);
        if (!checkString(value)) {
            processError(TYPESYNTAX, SBADFLD + name + SBADFLD1, exchange);
            return;
        }
        if (BALANCER.equals(name)) {
            node.setBalancer(value);
            balancer.setName(value);
        } else if (MAXATTEMPTS.equals(name)) {
            balancer.setMaxattempts(Integer.parseInt(value));
        } else if (STICKYSESSION.equals(name)) {
            if ("No".equalsIgnoreCase(value)) {
                balancer.setStickySession(false);
            }
        } else if (STICKYSESSIONCOOKIE.equals(name)) {
            balancer.setStickySessionCookie(value);
        } else if (STICKYSESSIONPATH.equals(name)) {
            balancer.setStickySessionPath(value);
        } else if (STICKYSESSIONREMOVE.equals(name)) {
            if ("Yes".equalsIgnoreCase(value)) {
                balancer.setStickySessionRemove(true);
            }
        } else if (STICKYSESSIONFORCE.equals(name)) {
            if ("no".equalsIgnoreCase(value)) {
                balancer.setStickySessionForce(false);
            }
        } else if (JVMROUTE.equals(name)) {
            node.setJvmRoute(value);
        } else if (DOMAIN.equals(name)) {
            node.setDomain(value);
        } else if (HOST.equals(name)) {
            node.setHostname(value);
        } else if (PORT.equals(name)) {
            node.setPort(Integer.parseInt(value));
        } else if (TYPE.equals(name)) {
            node.setType(value);
        } else if (REVERSED.equals(name)) {
            // ignore
            continue;
        } else if (FLUSH_PACKET.equals(name)) {
            if ("on".equalsIgnoreCase(value)) {
                node.setFlushPackets(true);
            } else if ("auto".equalsIgnoreCase(value)) {
                node.setFlushPackets(true);
            }
        } else if (FLUSH_WAIT.equals(name)) {
            node.setFlushwait(Integer.parseInt(value));
        } else if (MCMPConstants.PING.equals(name)) {
            node.setPing(Integer.parseInt(value));
        } else if (SMAX.equals(name)) {
            node.setSmax(Integer.parseInt(value));
        } else if (TTL.equals(name)) {
            node.setTtl(Integer.parseInt(value));
        } else if (TIMEOUT.equals(name)) {
            node.setTimeout(Integer.parseInt(value));
        } else if (CONTEXT.equals(name)) {
            final String[] context = value.split(",");
            contexts = Arrays.asList(context);
        } else if (ALIAS.equals(name)) {
            final String[] alias = value.split(",");
            hosts = Arrays.asList(alias);
        } else if (WAITWORKER.equals(name)) {
            node.setWaitWorker(Integer.parseInt(value));
        } else {
            processError(TYPESYNTAX, SBADFLD + name + SBADFLD1, exchange);
            return;
        }
    }
    final NodeConfig config;
    try {
        // Build the config
        config = node.build();
        if (container.addNode(config, balancer, exchange.getIoThread(), exchange.getConnection().getByteBufferPool())) {
            // Apparently this is hard to do in the C part, so maybe we should just remove this
            if (contexts != null && hosts != null) {
                for (final String context : contexts) {
                    container.enableContext(context, config.getJvmRoute(), hosts);
                }
            }
            processOK(exchange);
        } else {
            processError(MCMPErrorCode.NODE_STILL_EXISTS, exchange);
        }
    } catch (Exception e) {
        processError(MCMPErrorCode.CANT_UPDATE_NODE, exchange);
    }
}
Also used : HttpString(io.undertow.util.HttpString) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HttpString(io.undertow.util.HttpString)

Example 34 with HttpString

use of io.undertow.util.HttpString in project indy by Commonjava.

the class BasicAuthenticationOAuthTranslator method sendChallenge.

@Override
public ChallengeResult sendChallenge(final HttpServerExchange exchange, final SecurityContext securityContext) {
    logger.debug("BASIC sendChallenge");
    exchange.getResponseHeaders().add(new HttpString("WWW-Authenticate"), "BASIC realm=\"" + config.getRealm() + "\"");
    exchange.setResponseCode(401);
    return new ChallengeResult(true, 401);
}
Also used : HttpString(io.undertow.util.HttpString)

Example 35 with HttpString

use of io.undertow.util.HttpString in project indy by Commonjava.

the class HeaderDebugger method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("{} {}", exchange.getRequestMethod(), exchange.getRequestPath());
        logger.trace("FROM: {}", exchange.getSourceAddress());
        final HeaderMap headerMap = exchange.getRequestHeaders();
        final Collection<HttpString> names = headerMap.getHeaderNames();
        logger.trace("HEADERS: ");
        for (final HttpString name : names) {
            final HeaderValues values = headerMap.get(name);
            for (final String value : values) {
                logger.trace("{}: {}", name, value);
            }
        }
        logger.trace("COOKIES: ");
        final Map<String, Cookie> cookies = exchange.getRequestCookies();
        for (final String key : cookies.keySet()) {
            final Cookie cookie = cookies.get(key);
            if (cookie == null) {
                continue;
            }
            logger.trace("Cookie({}):\n  Domain: {}\n  Name: {}\n  Path: {}\n  Value: {}\n  Expires: {}\n  Max-Age: {}\n  Comment: {}\n  Version: {}\n\n", key, cookie.getDomain(), cookie.getName(), cookie.getPath(), cookie.getValue(), cookie.getExpires(), cookie.getMaxAge(), cookie.getComment(), cookie.getVersion());
        }
    }
    handler.handleRequest(exchange);
}
Also used : Cookie(io.undertow.server.handlers.Cookie) HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Aggregations

HttpString (io.undertow.util.HttpString)59 IOException (java.io.IOException)18 HttpServerExchange (io.undertow.server.HttpServerExchange)16 HeaderMap (io.undertow.util.HeaderMap)15 HttpHandler (io.undertow.server.HttpHandler)9 ByteBuffer (java.nio.ByteBuffer)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)6 HeaderValues (io.undertow.util.HeaderValues)6 ClientRequest (io.undertow.client.ClientRequest)5 Session (io.undertow.server.session.Session)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)4 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)4 SessionManager (io.undertow.server.session.SessionManager)4 List (java.util.List)4 TypeConverter (org.apache.camel.TypeConverter)4