public void requestDataSent() { if(http2UpgradeReceived) { doHttp2Upgrade(); } }
@Override public void handleEvent(StreamSinkConduit channel) { if(currentRequest != null) { currentRequest.terminateRequest(); } } };
@Override public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioWorker worker, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) { connect(listener, null, uri, worker, ssl, bufferPool, options); }
public void handle(final ByteBuffer buffer, final ResponseParseState currentState, final HttpResponseBuilder builder) throws BadRequestException { handleHttpVersion(buffer, currentState, builder); if (!buffer.hasRemaining()) { return; handleStatusCode(buffer, currentState, builder); if (!buffer.hasRemaining()) { return; handleReasonPhrase(buffer, currentState, builder); if (!buffer.hasRemaining()) { return; handleAfterReasonPhrase(buffer, currentState, builder); if (!buffer.hasRemaining()) { return; handleHeader(buffer, currentState, builder); if (!buffer.hasRemaining()) { return; handleHeaderValue(buffer, currentState, builder); if (!buffer.hasRemaining()) { return;
protected void doHttp2Upgrade() { try { StreamConnection connectedStreamChannel = this.performUpgrade(); Http2Channel http2Channel = new Http2Channel(connectedStreamChannel, null, bufferPool, null, true, true, options); Http2ClientConnection http2ClientConnection = new Http2ClientConnection(http2Channel, currentRequest.getResponseCallback(), currentRequest.getRequest(), currentRequest.getRequest().getRequestHeaders().getFirst(Headers.HOST), clientStatistics, false); http2ClientConnection.getCloseSetter().set(new ChannelListener<ClientConnection>() { @Override public void handleEvent(ClientConnection channel) { ChannelListeners.invokeChannelListener(HttpClientConnection.this, HttpClientConnection.this.closeSetter.get()); } }); http2Delegate = http2ClientConnection; connectedStreamChannel.getSourceChannel().wakeupReads(); //make sure the read listener is immediately invoked, as it may not happen if data is pushed back currentRequest = null; pendingResponse = null; } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); safeClose(this); } }
void terminateRequest() { if(anyAreSet(state, REQUEST_TERMINATED)) { return; } log.debugf("request terminated for request to %s %s", clientConnection.getPeerAddress(), getRequest().getPath()); state |= REQUEST_TERMINATED; clientConnection.requestDataSent(); if (anyAreSet(state, RESPONSE_TERMINATED)) { clientConnection.exchangeDone(); } }
void terminateResponse() { if(anyAreSet(state, RESPONSE_TERMINATED)) { return; } log.debugf("response terminated for request to %s %s", clientConnection.getPeerAddress(), getRequest().getPath()); state |= RESPONSE_TERMINATED; if (anyAreSet(state, REQUEST_TERMINATED)) { clientConnection.exchangeDone(); } }
public void handleEvent(StreamConnection channel) { log.debugf("connection to %s closed", getPeerAddress()); HttpClientConnection.this.state |= CLOSED; ChannelListeners.invokeChannelListener(HttpClientConnection.this, closeSetter.get()); try { if (pooledBuffer != null) { pooledBuffer.close(); } } catch (Throwable ignored){} for(ChannelListener<ClientConnection> listener : closeListeners) { listener.handleEvent(HttpClientConnection.this); } HttpClientExchange pending = pendingQueue.poll(); while (pending != null) { pending.setFailed(new ClosedChannelException()); pending = pendingQueue.poll(); } if(currentRequest != null) { currentRequest.setFailed(new ClosedChannelException()); currentRequest = null; pendingResponse = null; } } });
@Override public StreamSourceChannel getResponseChannel() { return new DetachableStreamSourceChannel(clientConnection.getConnection().getSourceChannel()) { @Override protected boolean isFinished() { return anyAreSet(state, RESPONSE_TERMINATED); } }; }
@Override public void handleEvent(StreamSourceConduit channel) { if(currentRequest != null) { currentRequest.terminateResponse(); } } };
public long write(final ByteBuffer[] srcs) throws IOException { return write(srcs, 0, srcs.length); }
private void handleError(Throwable exception) { if (exception instanceof IOException) { handleError((IOException) exception); } else { handleError(new IOException(exception)); } } private void handleError(IOException exception) {
/** * Parses the reason phrase. This is called from the generated bytecode. * * @param buffer The buffer * @param state The current state * @param builder The exchange builder * @return The number of bytes remaining */ @SuppressWarnings("unused") final void handleReasonPhrase(ByteBuffer buffer, ResponseParseState state, HttpResponseBuilder builder) { StringBuilder stringBuilder = state.stringBuilder; while (buffer.hasRemaining()) { final char next = (char) buffer.get(); if (next == '\n' || next == '\r') { builder.setReasonPhrase(stringBuilder.toString()); state.state = ResponseParseState.AFTER_REASON_PHRASE; state.stringBuilder.setLength(0); state.parseState = 0; state.leftOver = (byte) next; state.pos = 0; state.nextHeader = null; return; } else { stringBuilder.append(next); } } }
void setFailed(IOException e) { this.failedReason = e; if (readyCallback != null) { readyCallback.failed(e); readyCallback = null; } if (responseCallback != null) { responseCallback.failed(e); responseCallback = null; } if(requestConduit != null) { requestConduit.freeBuffers(); } }
/** * Parses the status code. This is called from the generated bytecode. * * @param buffer The buffer * @param state The current state * @param builder The exchange builder * @return The number of bytes remaining */ @SuppressWarnings("unused") final void handleStatusCode(ByteBuffer buffer, ResponseParseState state, HttpResponseBuilder builder) { StringBuilder stringBuilder = state.stringBuilder; while (buffer.hasRemaining()) { final char next = (char) buffer.get(); if (next == ' ' || next == '\t') { builder.setStatusCode(Integer.parseInt(stringBuilder.toString())); state.state = ResponseParseState.REASON_PHRASE; state.stringBuilder.setLength(0); state.parseState = 0; state.pos = 0; state.nextHeader = null; return; } else { stringBuilder.append(next); } } }
private void handleError(IOException exception) { UndertowLogger.REQUEST_IO_LOGGER.ioException(exception); currentRequest.setFailed(exception); currentRequest = null; pendingResponse = null; safeClose(connection); }
public void close() throws IOException { log.debugf("close called on connection to %s", getPeerAddress()); if(http2Delegate != null) { http2Delegate.close(); } if (anyAreSet(state, CLOSED)) { return; } state |= CLOSED | CLOSE_REQ; ConnectionUtils.cleanClose(connection); }
@Override protected void channelFinished() { exchange.terminateRequest(); } }
@Override public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) { connect(listener, null, uri, ioThread, ssl, bufferPool, options); }
@Override public StreamSinkChannel getRequestChannel() { return new DetachableStreamSinkChannel(clientConnection.getConnection().getSinkChannel()) { @Override protected boolean isFinished() { return anyAreSet(state, REQUEST_TERMINATED); } }; }