use of org.apache.hc.core5.http.nio.ContentEncoder in project httpcomponents-core by apache.
the class Http1IntegrationTest method testTruncatedChunk.
@Test
public void testTruncatedChunk() throws Exception {
final InetSocketAddress serverEndpoint = server.start(new InternalServerHttp1EventHandlerFactory(HttpProcessors.server(), (request, context) -> new MessageExchangeHandler<String>(new StringAsyncEntityConsumer()) {
@Override
protected void handle(final Message<HttpRequest, String> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
responseTrigger.submitResponse(new BasicResponseProducer(new StringAsyncEntityProducer("useful stuff")), context);
}
}, Http1Config.DEFAULT, CharCodingConfig.DEFAULT, DefaultConnectionReuseStrategy.INSTANCE, scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null, null, null) {
@Override
protected ServerHttp1StreamDuplexer createServerHttp1StreamDuplexer(final ProtocolIOSession ioSession, final HttpProcessor httpProcessor, final HandlerFactory<AsyncServerExchangeHandler> exchangeHandlerFactory, final Http1Config http1Config, final CharCodingConfig connectionConfig, final ConnectionReuseStrategy connectionReuseStrategy, final NHttpMessageParser<HttpRequest> incomingMessageParser, final NHttpMessageWriter<HttpResponse> outgoingMessageWriter, final ContentLengthStrategy incomingContentStrategy, final ContentLengthStrategy outgoingContentStrategy, final Http1StreamListener streamListener) {
return new ServerHttp1StreamDuplexer(ioSession, httpProcessor, exchangeHandlerFactory, scheme.id, http1Config, connectionConfig, connectionReuseStrategy, incomingMessageParser, outgoingMessageWriter, incomingContentStrategy, outgoingContentStrategy, streamListener) {
@Override
protected ContentEncoder createContentEncoder(final long len, final WritableByteChannel channel, final SessionOutputBuffer buffer, final BasicHttpTransportMetrics metrics) throws HttpException {
if (len == ContentLengthStrategy.CHUNKED) {
return new BrokenChunkEncoder(channel, buffer, metrics);
} else {
return super.createContentEncoder(len, channel, buffer, metrics);
}
}
};
}
});
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final AsyncRequestProducer requestProducer = new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello"));
final StringAsyncEntityConsumer entityConsumer = new StringAsyncEntityConsumer() {
@Override
public void releaseResources() {
// Do not clear internal content buffer
}
};
final BasicResponseConsumer<String> responseConsumer = new BasicResponseConsumer<>(entityConsumer);
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(requestProducer, responseConsumer, null);
final ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
final Throwable cause = exception.getCause();
Assertions.assertTrue(cause instanceof MalformedChunkCodingException);
Assertions.assertEquals("garbage", entityConsumer.generateContent());
}
use of org.apache.hc.core5.http.nio.ContentEncoder in project httpcomponents-core by apache.
the class AbstractHttp1StreamDuplexer method endOutputStream.
MessageDelineation endOutputStream(final List<? extends Header> trailers) throws IOException {
ioSession.getLock().lock();
try {
if (outgoingMessage == null) {
return MessageDelineation.NONE;
}
final ContentEncoder contentEncoder = outgoingMessage.getBody();
contentEncoder.complete(trailers);
ioSession.setEvent(SelectionKey.OP_WRITE);
outgoingMessage = null;
return contentEncoder instanceof ChunkEncoder ? MessageDelineation.CHUNK_CODED : MessageDelineation.MESSAGE_HEAD;
} finally {
ioSession.getLock().unlock();
}
}
use of org.apache.hc.core5.http.nio.ContentEncoder in project httpcomponents-core by apache.
the class AbstractHttp1StreamDuplexer method commitMessageHead.
void commitMessageHead(final OutgoingMessage messageHead, final boolean endStream, final FlushMode flushMode) throws HttpException, IOException {
ioSession.getLock().lock();
try {
outgoingMessageWriter.write(messageHead, outbuf);
updateOutputMetrics(messageHead, connMetrics);
if (!endStream) {
final ContentEncoder contentEncoder;
if (handleOutgoingMessage(messageHead)) {
final long len = outgoingContentStrategy.determineLength(messageHead);
contentEncoder = createContentEncoder(len, ioSession, outbuf, outTransportMetrics);
} else {
contentEncoder = null;
}
if (contentEncoder != null) {
outgoingMessage = new Message<>(messageHead, contentEncoder);
}
}
outgoingMessageWriter.reset();
if (flushMode == FlushMode.IMMEDIATE) {
outbuf.flush(ioSession);
}
ioSession.setEvent(EventMask.WRITE);
} finally {
ioSession.getLock().unlock();
}
}
use of org.apache.hc.core5.http.nio.ContentEncoder in project httpcomponents-core by apache.
the class AbstractHttp1StreamDuplexer method streamOutput.
int streamOutput(final ByteBuffer src) throws IOException {
ioSession.getLock().lock();
try {
if (outgoingMessage == null) {
throw new ClosedChannelException();
}
final ContentEncoder contentEncoder = outgoingMessage.getBody();
final int bytesWritten = contentEncoder.write(src);
if (bytesWritten > 0) {
ioSession.setEvent(SelectionKey.OP_WRITE);
}
return bytesWritten;
} finally {
ioSession.getLock().unlock();
}
}
Aggregations