Java Code Examples for io.undertow.server.HttpServerExchange#setPersistent()
The following examples show how to use
io.undertow.server.HttpServerExchange#setPersistent() .
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: HttpTransferEncoding.java From lams with GNU General Public License v2.0 | 6 votes |
private static StreamSinkConduit handleExplicitTransferEncoding(HttpServerExchange exchange, StreamSinkConduit channel, ConduitListener<StreamSinkConduit> finishListener, HeaderMap responseHeaders, String transferEncodingHeader, boolean headRequest) { HttpString transferEncoding = new HttpString(transferEncodingHeader); if (transferEncoding.equals(Headers.CHUNKED)) { if (headRequest) { return channel; } Boolean preChunked = exchange.getAttachment(HttpAttachments.PRE_CHUNKED_RESPONSE); if(preChunked != null && preChunked) { return new PreChunkedStreamSinkConduit(channel, finishListener, exchange); } else { return new ChunkedStreamSinkConduit(channel, exchange.getConnection().getByteBufferPool(), true, !exchange.isPersistent(), responseHeaders, finishListener, exchange); } } else { if (headRequest) { return channel; } log.trace("Cancelling persistence because response is identity with no content length"); // make it not persistent - very unfortunate for the next request handler really... exchange.setPersistent(false); responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString()); return new FinishableStreamSinkConduit(channel, terminateResponseListener(exchange)); } }
Example 2
Source File: FixedLengthStreamSourceConduit.java From lams with GNU General Public License v2.0 | 6 votes |
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 3
Source File: AionUndertowRpcHandler.java From aion with MIT License | 5 votes |
@Override public void handleRequest(HttpServerExchange exchange) { boolean isPost = Methods.POST.equals(exchange.getRequestMethod()); boolean isOptions = Methods.OPTIONS.equals(exchange.getRequestMethod()); // only support POST & OPTIONS requests if (!isPost && !isOptions) { exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED); exchange.setPersistent(false); // don't need to keep-alive connection in case of error. exchange.endExchange(); return; } // respond to cors-preflight request if (corsEnabled && isOptions) { addCorsHeaders(exchange); exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain"); exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "0"); exchange.getResponseSender().send(""); return; } /** respond to rpc call; {@link io.Undertow.BlockingReceiverImpl#receiveFullString} */ exchange.getRequestReceiver() .receiveFullString( (_exchange, body) -> { if (corsEnabled) addCorsHeaders(_exchange); _exchange .getResponseHeaders() .put(Headers.CONTENT_TYPE, "application/json"); _exchange.getResponseSender().send(rpcProcessor.process(body)); }); }
Example 4
Source File: HttpTransferEncoding.java From lams with GNU General Public License v2.0 | 5 votes |
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 5
Source File: HttpTransferEncoding.java From lams with GNU General Public License v2.0 | 5 votes |
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 6
Source File: HttpTransferEncoding.java From lams with GNU General Public License v2.0 | 5 votes |
private static StreamSinkConduit handleResponseConduit(HttpServerExchange exchange, boolean headRequest, StreamSinkConduit channel, HeaderMap responseHeaders, ConduitListener<StreamSinkConduit> finishListener, String transferEncodingHeader) { if (transferEncodingHeader == null) { if (exchange.isHttp11()) { if (exchange.isPersistent()) { responseHeaders.put(Headers.TRANSFER_ENCODING, Headers.CHUNKED.toString()); if (headRequest) { return channel; } return new ChunkedStreamSinkConduit(channel, exchange.getConnection().getByteBufferPool(), true, !exchange.isPersistent(), responseHeaders, finishListener, exchange); } else { if (headRequest) { return channel; } return new FinishableStreamSinkConduit(channel, finishListener); } } else { exchange.setPersistent(false); responseHeaders.put(Headers.CONNECTION, Headers.CLOSE.toString()); if (headRequest) { return channel; } return new FinishableStreamSinkConduit(channel, finishListener); } } else { //moved outside because this is rarely used //and makes the method small enough to be inlined return handleExplicitTransferEncoding(exchange, channel, finishListener, responseHeaders, transferEncodingHeader, headRequest); } }
Example 7
Source File: HttpServerConnection.java From lams with GNU General Public License v2.0 | 5 votes |
@Override protected StreamSinkConduit getSinkConduit(HttpServerExchange exchange, StreamSinkConduit conduit) { if(exchange.getRequestMethod().equals(Methods.CONNECT) && !connectHandled) { //make sure that any unhandled CONNECT requests result in a connection close exchange.setPersistent(false); exchange.getResponseHeaders().put(Headers.CONNECTION, "close"); } return HttpTransferEncoding.createSinkConduit(exchange); }
Example 8
Source File: MCMPHandler.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Handle a management+ request. * * @param method the http method * @param exchange the http server exchange * @throws Exception */ protected void handleRequest(final HttpString method, HttpServerExchange exchange) throws Exception { final RequestData requestData = parseFormData(exchange); boolean persistent = exchange.isPersistent(); exchange.setPersistent(false); //UNDERTOW-947 MCMP should not use persistent connections if (CONFIG.equals(method)) { processConfig(exchange, requestData); } else if (ENABLE_APP.equals(method)) { processCommand(exchange, requestData, MCMPAction.ENABLE); } else if (DISABLE_APP.equals(method)) { processCommand(exchange, requestData, MCMPAction.DISABLE); } else if (STOP_APP.equals(method)) { processCommand(exchange, requestData, MCMPAction.STOP); } else if (REMOVE_APP.equals(method)) { processCommand(exchange, requestData, MCMPAction.REMOVE); } else if (STATUS.equals(method)) { processStatus(exchange, requestData); } else if (INFO.equals(method)) { processInfo(exchange); } else if (DUMP.equals(method)) { processDump(exchange); } else if (PING.equals(method)) { processPing(exchange, requestData); } else { exchange.setPersistent(persistent); next.handleRequest(exchange); } }
Example 9
Source File: BlockingReceiverImpl.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public void error(HttpServerExchange exchange, IOException e) { if(!exchange.isResponseStarted()) { exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); } exchange.setPersistent(false); UndertowLogger.REQUEST_IO_LOGGER.ioException(e); exchange.endExchange(); }
Example 10
Source File: HttpTransferEncoding.java From lams with GNU General Public License v2.0 | 4 votes |
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 11
Source File: HttpTransferEncoding.java From lams with GNU General Public License v2.0 | 4 votes |
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 12
Source File: HttpContinue.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Sets a 417 response code and ends the exchange. * * @param exchange The exchange to reject */ public static void rejectExchange(final HttpServerExchange exchange) { exchange.setStatusCode(StatusCodes.EXPECTATION_FAILED); exchange.setPersistent(false); exchange.endExchange(); }