@Override public ByteBufferPool getBufferPool() { return exchange.getConnection().getByteBufferPool(); }
public RequestBodyPublisher(HttpServerExchange exchange, DataBufferFactory bufferFactory) { super(UndertowServerHttpRequest.this.getLogPrefix()); this.channel = exchange.getRequestChannel(); this.bufferFactory = bufferFactory; this.byteBufferPool = exchange.getConnection().getByteBufferPool(); }
@Override public PooledByteBuffer allocate() { return exchange.getConnection().getByteBufferPool().allocate(); }
public RequestBodyPublisher(HttpServerExchange exchange, DataBufferFactory bufferFactory) { super(UndertowServerHttpRequest.this.getLogPrefix()); this.channel = exchange.getRequestChannel(); this.bufferFactory = bufferFactory; this.byteBufferPool = exchange.getConnection().getByteBufferPool(); }
private ByteBuffer buffer() { ByteBuffer buffer = this.buffer; if (buffer != null) { return buffer; } this.pooledBuffer = exchange.getConnection().getByteBufferPool().allocate(); this.buffer = pooledBuffer.getBuffer(); return this.buffer; }
private void performTransfer(FileChannel source, IoCallback callback) { if (outputStream instanceof BufferWritableOutputStream) { try { ((BufferWritableOutputStream) outputStream).transferFrom(source); } catch (IOException e) { callback.onException(exchange, this, e); } } else { try (PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate()){ ByteBuffer buffer = pooled.getBuffer(); long pos = source.position(); long size = source.size(); while (size - pos > 0) { int ret = source.read(buffer); if (ret <= 0) { break; } pos += ret; outputStream.write(buffer.array(), buffer.arrayOffset(), ret); buffer.clear(); } if (pos != size) { throw new EOFException("Unexpected EOF reading file"); } } catch (IOException e) { callback.onException(exchange, this, e); } } }
@Override public void run() { if(shutdown || open == 0) { if(timerKey != null) { timerKey.remove(); } return; } if(pooled == null) { pooled = exchange.getConnection().getByteBufferPool().allocate(); pooled.getBuffer().put(":\n".getBytes(StandardCharsets.UTF_8)); pooled.getBuffer().flip(); writeListener.handleEvent(sink); } } }, keepAliveTime, TimeUnit.MILLISECONDS);
public UndertowInputStream(final HttpServerExchange exchange) { if (exchange.isRequestChannelAvailable()) { this.channel = exchange.getRequestChannel(); } else { this.channel = new EmptyStreamSourceChannel(exchange.getIoThread()); } this.bufferPool = exchange.getConnection().getByteBufferPool(); }
throw new IOException(UndertowMessages.MESSAGES.requestChannelAlreadyProvided()); try (PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate()){ ByteBuffer buf = pooled.getBuffer(); while (true) {
ByteBuffer[] bufs = null; while (bytes.hasRemaining()) { PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate(); if (bufs == null) { int noBufs = (bytes.remaining() + pooled.getBuffer().remaining() - 1) / pooled.getBuffer().remaining(); //round up division trick
public void send(final ReadableByteChannel source, final HttpServerExchange exchange, final IoCallback callback) { this.source = source; this.exchange = exchange; this.callback = callback; this.sender = exchange.getResponseSender(); ServerConnection connection = exchange.getConnection(); this.pooled = connection.getByteBufferPool().allocate(); this.bufferSize = connection.getBufferSize(); onComplete(exchange, sender); }
}, bindAddress, getUri(), exchange.getIoThread(), ssl, exchange.getConnection().getByteBufferPool(), options);
@Override public void getConnection(ProxyTarget target, HttpServerExchange exchange, ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit) { ClientConnection existing = exchange.getConnection().getAttachment(clientAttachmentKey); if (existing != null) { if (existing.isOpen()) { //this connection already has a client, re-use it callback.completed(exchange, new ProxyConnection(existing, uri.getPath() == null ? "/" : uri.getPath())); return; } else { exchange.getConnection().removeAttachment(clientAttachmentKey); } } client.connect(new ConnectNotifier(callback, exchange), uri, exchange.getIoThread(), exchange.getConnection().getByteBufferPool(), OptionMap.EMPTY); }
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))); }
private MultiPartUploadHandler(final HttpServerExchange exchange, final String boundary, final long maxIndividualFileSize, final long fileSizeThreshold, final String defaultEncoding) { this.exchange = exchange; this.maxIndividualFileSize = maxIndividualFileSize; this.defaultEncoding = defaultEncoding; this.fileSizeThreshold = fileSizeThreshold; this.data = new FormData(exchange.getConnection().getUndertowOptions().get(UndertowOptions.MAX_PARAMETERS, 1000)); String charset = defaultEncoding; String contentType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE); if (contentType != null) { String value = Headers.extractQuotedValueFromHeader(contentType, "charset"); if (value != null) { charset = value; } } this.parser = MultipartParser.beginParse(exchange.getConnection().getByteBufferPool(), this, boundary.getBytes(StandardCharsets.US_ASCII), charset); }
/** * Try to ping a server using the undertow client. * * @param connection the connection URI * @param callback the ping callback * @param exchange the http servers exchange * @param client the undertow client * @param xnioSsl the ssl setup * @param options the options */ static void pingHttpClient(URI connection, PingCallback callback, HttpServerExchange exchange, UndertowClient client, XnioSsl xnioSsl, OptionMap options) { final XnioIoThread thread = exchange.getIoThread(); final RequestExchangeListener exchangeListener = new RequestExchangeListener(callback, NodeHealthChecker.NO_CHECK, true); final Runnable r = new HttpClientPingTask(connection, exchangeListener, thread, client, xnioSsl, exchange.getConnection().getByteBufferPool(), options); exchange.dispatch(exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : thread, r); // Schedule timeout task scheduleCancelTask(exchange.getIoThread(), exchangeListener, 5, TimeUnit.SECONDS); }
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)); } }
@Override public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) { OptionMap undertowOptions = exchange.getConnection().getUndertowOptions(); Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions); Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() { @Override public void handleRequest(HttpServerExchange exchange) throws Exception { //if this header is present we don't actually process the rest of the handler chain //as the request was only to create the initial request if(exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) { exchange.endExchange(); return; } exchange.setProtocol(Protocols.HTTP_2_0); next.handleRequest(exchange); } }, undertowOptions, exchange.getConnection().getBufferSize(), null); channel.getReceiveSetter().set(receiveListener); receiveListener.handleInitialRequest(exchange, channel, data); channel.resumeReceives(); } });
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); } }
Transfer.initiateTransfer(result.getResponseChannel(), exchange.getResponseChannel(), ChannelListeners.closingChannelListener(), new HTTPTrailerChannelListener(result, exchange, exchange, proxyClientHandler, idempotentPredicate), handler, handler, exchange.getConnection().getByteBufferPool());