private static StreamSourceConduit fixedLengthStreamSourceConduitWrapper(final long contentLength, final StreamSourceConduit conduit, final HttpServerExchange exchange) { return new FixedLengthStreamSourceConduit(conduit, contentLength, fixedLengthDrainListener(exchange), exchange); }
public long transferTo(final long count, final ByteBuffer throughBuffer, final StreamSinkChannel target) throws IOException { if (count == 0L) { return 0L; } long val = state; checkMaxSize(val); if (anyAreSet(val, FLAG_CLOSED | FLAG_FINISHED) || allAreClear(val, MASK_COUNT)) { if (allAreClear(val, FLAG_FINISHED)) { invokeFinishListener(); } return -1; } long res = 0L; try { return res = next.transferTo(min(count, val & MASK_COUNT), throughBuffer, target); } catch (IOException | RuntimeException | Error e) { IoUtils.safeClose(exchange.getConnection()); throw e; } finally { exitRead(res + throughBuffer.remaining()); } }
@Override public void terminateReads() throws IOException { long val = enterShutdownReads(); if (allAreSet(val, FLAG_CLOSED)) { return; } exitShutdownReads(val); }
return 0L; } else if (length == 1) { return read(dsts[offset]); checkMaxSize(val); if (allAreSet(val, FLAG_CLOSED) || allAreClear(val, MASK_COUNT)) { if (allAreClear(val, FLAG_FINISHED)) { invokeFinishListener(); throw e; } finally { exitRead(res);
private void exitShutdownReads(long oldVal) { if (!allAreClear(oldVal, MASK_COUNT)) { invokeFinishListener(); } }
public void handleEvent(final FixedLengthStreamSourceConduit fixedLengthConduit) { long remaining = fixedLengthConduit.getRemaining(); if (remaining > 0L) { UndertowLogger.REQUEST_LOGGER.requestWasNotFullyConsumed(); exchange.setPersistent(false); } Connectors.terminateRequest(exchange); } };
public long read(final ByteBuffer[] dsts) throws IOException { return read(dsts, 0, dsts.length); }
return 0L; } else if (length == 1) { return read(dsts[offset]); checkMaxSize(val); if (allAreSet(val, FLAG_CLOSED) || allAreClear(val, MASK_COUNT)) { if (allAreClear(val, FLAG_FINISHED)) { invokeFinishListener(); throw e; } finally { exitRead(res);
/** * Exit a read method. * * @param consumed the number of bytes consumed by this call (may be 0) */ private void exitRead(long consumed) throws IOException { long oldVal = state; if(consumed == -1) { if (anyAreSet(oldVal, MASK_COUNT)) { invokeFinishListener(); state &= ~MASK_COUNT; throw UndertowMessages.MESSAGES.couldNotReadContentLengthData(); } return; } long newVal = oldVal - consumed; state = newVal; }
public void handleEvent(final FixedLengthStreamSourceConduit fixedLengthConduit) { long remaining = fixedLengthConduit.getRemaining(); if (remaining > 0L) { UndertowLogger.REQUEST_LOGGER.requestWasNotFullyConsumed(); exchange.setPersistent(false); } Connectors.terminateRequest(exchange); } };
public long read(final ByteBuffer[] dsts) throws IOException { return read(dsts, 0, dsts.length); }
public long transferTo(final long position, final long count, final FileChannel target) throws IOException { long val = state; checkMaxSize(val); if (anyAreSet(val, FLAG_CLOSED | FLAG_FINISHED) || allAreClear(val, MASK_COUNT)) { if (allAreClear(val, FLAG_FINISHED)) { invokeFinishListener(); } return -1L; } long res = 0L; try { return res = next.transferTo(position, min(count, val & MASK_COUNT), target); } catch (IOException | RuntimeException | Error e) { IoUtils.safeClose(exchange.getConnection()); throw e; } finally { exitRead(res); } }
return 0L; } else if (length == 1) { return read(dsts[offset]); checkMaxSize(val); if (allAreSet(val, FLAG_CLOSED) || allAreClear(val, MASK_COUNT)) { if (allAreClear(val, FLAG_FINISHED)) { invokeFinishListener(); throw e; } finally { exitRead(res);
@Override public void terminateReads() throws IOException { long val = enterShutdownReads(); if (allAreSet(val, FLAG_CLOSED)) { return; } exitShutdownReads(val); }
private void exitShutdownReads(long oldVal) { if (!allAreClear(oldVal, MASK_COUNT)) { invokeFinishListener(); } }
private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) { String encoding = response.getResponseHeaders().getLast(Headers.TRANSFER_ENCODING); boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding)); String length = response.getResponseHeaders().getFirst(Headers.CONTENT_LENGTH); if (exchange.getRequest().getMethod().equals(Methods.HEAD)) { connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener)); } else if (chunked) { connection.getSourceChannel().setConduit(new ChunkedStreamSourceConduit(connection.getSourceChannel().getConduit(), pushBackStreamSourceConduit, bufferPool, responseFinishedListener, exchange, connection)); } else if (length != null) { try { long contentLength = Long.parseLong(length); connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener)); } catch (NumberFormatException e) { handleError(e); throw e; } } else if (response.getProtocol().equals(Protocols.HTTP_1_1) && !Connectors.isEntityBodyAllowed(response.getResponseCode())) { connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener)); } else { connection.getSourceChannel().setConduit(new FinishableStreamSourceConduit(connection.getSourceChannel().getConduit(), responseFinishedListener)); state |= CLOSE_REQ; } }
public void handleEvent(final FixedLengthStreamSourceConduit fixedLengthConduit) { long remaining = fixedLengthConduit.getRemaining(); if (remaining > 0L) { UndertowLogger.REQUEST_LOGGER.requestWasNotFullyConsumed(); exchange.setPersistent(false); } Connectors.terminateRequest(exchange); } };
public long read(final ByteBuffer[] dsts) throws IOException { return read(dsts, 0, dsts.length); }
public int read(final ByteBuffer dst) throws IOException { long val = state; checkMaxSize(val); if (allAreSet(val, FLAG_CLOSED) || allAreClear(val, MASK_COUNT)) { if (allAreClear(val, FLAG_FINISHED)) { invokeFinishListener(); throw e; } finally { exitRead(res);
@Override public void terminateReads() throws IOException { long val = enterShutdownReads(); if (allAreSet(val, FLAG_CLOSED)) { return; } exitShutdownReads(val); }