Search in sources :

Example 1 with MalformedChunkCodingException

use of org.apache.hc.core5.http.MalformedChunkCodingException 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());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) CharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig) BasicAsyncServerExpectationDecorator(org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) AbstractClassicEntityProducer(org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityProducer) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) Future(java.util.concurrent.Future) Map(java.util.Map) DigestingEntityProducer(org.apache.hc.core5.http.nio.entity.DigestingEntityProducer) AbstractClassicServerExchangeHandler(org.apache.hc.core5.http.nio.support.classic.AbstractClassicServerExchangeHandler) RequestConnControl(org.apache.hc.core5.http.protocol.RequestConnControl) CancellationException(java.util.concurrent.CancellationException) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) HandlerFactory(org.apache.hc.core5.http.nio.HandlerFactory) ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) ConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy) Timeout(org.apache.hc.core5.util.Timeout) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) HeaderElements(org.apache.hc.core5.http.HeaderElements) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) AsyncServerRequestHandler(org.apache.hc.core5.http.nio.AsyncServerRequestHandler) RunWith(org.junit.runner.RunWith) InterruptedIOException(java.io.InterruptedIOException) NHttpMessageParser(org.apache.hc.core5.http.nio.NHttpMessageParser) EntityDetails(org.apache.hc.core5.http.EntityDetails) HttpVersion(org.apache.hc.core5.http.HttpVersion) StringTokenizer(java.util.StringTokenizer) RequestTargetHost(org.apache.hc.core5.http.protocol.RequestTargetHost) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NHttpMessageWriter(org.apache.hc.core5.http.nio.NHttpMessageWriter) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) AsyncResponseProducer(org.apache.hc.core5.http.nio.AsyncResponseProducer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) ImmediateResponseExchangeHandler(org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler) BufferedWriter(java.io.BufferedWriter) IOException(java.io.IOException) Test(org.junit.Test) InputStreamReader(java.io.InputStreamReader) URIScheme(org.apache.hc.core5.http.URIScheme) ExecutionException(java.util.concurrent.ExecutionException) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpHeaders(org.apache.hc.core5.http.HttpHeaders) AfterEach(org.junit.jupiter.api.AfterEach) HttpHost(org.apache.hc.core5.http.HttpHost) HttpRequest(org.apache.hc.core5.http.HttpRequest) ContentType(org.apache.hc.core5.http.ContentType) WritableByteChannel(java.nio.channels.WritableByteChannel) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) BufferedReader(java.io.BufferedReader) HttpStatus(org.apache.hc.core5.http.HttpStatus) AsyncEntityProducers(org.apache.hc.core5.http.nio.entity.AsyncEntityProducers) CoreMatchers(org.hamcrest.CoreMatchers) AsyncRequestBuilder(org.apache.hc.core5.http.nio.support.AsyncRequestBuilder) RequestContent(org.apache.hc.core5.http.protocol.RequestContent) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ByteBuffer(java.nio.ByteBuffer) AbstractClassicEntityConsumer(org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer) HttpProcessors(org.apache.hc.core5.http.impl.HttpProcessors) URI(java.net.URI) Http1Config(org.apache.hc.core5.http.config.Http1Config) EnableRuleMigrationSupport(org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) RequestValidateHost(org.apache.hc.core5.http.protocol.RequestValidateHost) Parameterized(org.junit.runners.Parameterized) Message(org.apache.hc.core5.http.Message) TimeValue(org.apache.hc.core5.util.TimeValue) Collection(java.util.Collection) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) InetSocketAddress(java.net.InetSocketAddress) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) List(java.util.List) ContentEncoder(org.apache.hc.core5.http.nio.ContentEncoder) SSLTestContexts(org.apache.hc.core5.testing.SSLTestContexts) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ProtocolIOSession(org.apache.hc.core5.reactor.ProtocolIOSession) Queue(java.util.Queue) DefaultConnectionReuseStrategy(org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy) AbstractContentEncoder(org.apache.hc.core5.http.impl.nio.AbstractContentEncoder) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResponseChannel(org.apache.hc.core5.http.nio.ResponseChannel) TextUtils(org.apache.hc.core5.util.TextUtils) AsyncRequestConsumer(org.apache.hc.core5.http.nio.AsyncRequestConsumer) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) ServerHttp1StreamDuplexer(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexer) HttpResponse(org.apache.hc.core5.http.HttpResponse) LinkedList(java.util.LinkedList) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) OutputStream(java.io.OutputStream) HttpException(org.apache.hc.core5.http.HttpException) Logger(org.slf4j.Logger) Header(org.apache.hc.core5.http.Header) ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicRequestConsumer(org.apache.hc.core5.http.nio.support.BasicRequestConsumer) AbstractServerExchangeHandler(org.apache.hc.core5.http.nio.support.AbstractServerExchangeHandler) DigestingEntityConsumer(org.apache.hc.core5.http.nio.entity.DigestingEntityConsumer) AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) IOSession(org.apache.hc.core5.reactor.IOSession) Method(org.apache.hc.core5.http.Method) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) Assertions(org.junit.jupiter.api.Assertions) InputStream(java.io.InputStream) CharCodingConfig(org.apache.hc.core5.http.config.CharCodingConfig) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) ProtocolIOSession(org.apache.hc.core5.reactor.ProtocolIOSession) HttpProcessor(org.apache.hc.core5.http.protocol.HttpProcessor) DefaultHttpProcessor(org.apache.hc.core5.http.protocol.DefaultHttpProcessor) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) HttpException(org.apache.hc.core5.http.HttpException) ExecutionException(java.util.concurrent.ExecutionException) AsyncServerExchangeHandler(org.apache.hc.core5.http.nio.AsyncServerExchangeHandler) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) ServerHttp1StreamDuplexer(org.apache.hc.core5.http.impl.nio.ServerHttp1StreamDuplexer) ContentEncoder(org.apache.hc.core5.http.nio.ContentEncoder) AbstractContentEncoder(org.apache.hc.core5.http.impl.nio.AbstractContentEncoder) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) WritableByteChannel(java.nio.channels.WritableByteChannel) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ConnectionReuseStrategy(org.apache.hc.core5.http.ConnectionReuseStrategy) DefaultConnectionReuseStrategy(org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy) BasicResponseProducer(org.apache.hc.core5.http.nio.support.BasicResponseProducer) Http1Config(org.apache.hc.core5.http.config.Http1Config) Test(org.junit.Test)

Example 2 with MalformedChunkCodingException

use of org.apache.hc.core5.http.MalformedChunkCodingException in project httpcomponents-core by apache.

the class ChunkedInputStream method getChunkSize.

/**
 * Expects the stream to start with a chunksize in hex with optional
 * comments after a semicolon. The line must end with a CRLF: "a3; some
 * comment\r\n" Positions the stream at the start of the next line.
 */
private long getChunkSize() throws IOException {
    final State st = this.state;
    switch(st) {
        case CHUNK_CRLF:
            lineBuffer.clear();
            final int bytesRead1 = this.buffer.readLine(lineBuffer, inputStream);
            if (bytesRead1 == -1) {
                throw new MalformedChunkCodingException("CRLF expected at end of chunk");
            }
            if (!lineBuffer.isEmpty()) {
                throw new MalformedChunkCodingException("Unexpected content at the end of chunk");
            }
            state = State.CHUNK_LEN;
        // $FALL-THROUGH$
        case CHUNK_LEN:
            lineBuffer.clear();
            final int bytesRead2 = this.buffer.readLine(lineBuffer, inputStream);
            if (bytesRead2 == -1) {
                throw new ConnectionClosedException("Premature end of chunk coded message body: closing chunk expected");
            }
            int separator = lineBuffer.indexOf(';');
            if (separator < 0) {
                separator = lineBuffer.length();
            }
            final String s = this.lineBuffer.substringTrimmed(0, separator);
            try {
                return Long.parseLong(s, 16);
            } catch (final NumberFormatException e) {
                throw new MalformedChunkCodingException("Bad chunk header: " + s);
            }
        default:
            throw new IllegalStateException("Inconsistent codec state");
    }
}
Also used : MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException)

Example 3 with MalformedChunkCodingException

use of org.apache.hc.core5.http.MalformedChunkCodingException in project httpcomponents-core by apache.

the class ChunkDecoder method readChunkHead.

private void readChunkHead() throws IOException {
    if (this.lineBuf == null) {
        this.lineBuf = new CharArrayBuffer(32);
    } else {
        this.lineBuf.clear();
    }
    if (this.endOfChunk) {
        if (this.buffer.readLine(this.lineBuf, this.endOfStream)) {
            if (!this.lineBuf.isEmpty()) {
                throw new MalformedChunkCodingException("CRLF expected at end of chunk");
            }
        } else {
            if (this.buffer.length() > 2 || this.endOfStream) {
                throw new MalformedChunkCodingException("CRLF expected at end of chunk");
            }
            return;
        }
        this.endOfChunk = false;
    }
    final boolean lineComplete = this.buffer.readLine(this.lineBuf, this.endOfStream);
    final int maxLineLen = this.http1Config.getMaxLineLength();
    if (maxLineLen > 0 && (this.lineBuf.length() > maxLineLen || (!lineComplete && this.buffer.length() > maxLineLen))) {
        throw new MessageConstraintException("Maximum line length limit exceeded");
    }
    if (lineComplete) {
        int separator = this.lineBuf.indexOf(';');
        if (separator < 0) {
            separator = this.lineBuf.length();
        }
        final String s = this.lineBuf.substringTrimmed(0, separator);
        try {
            this.chunkSize = Long.parseLong(s, 16);
        } catch (final NumberFormatException e) {
            throw new MalformedChunkCodingException("Bad chunk header: " + s);
        }
        this.pos = 0L;
    } else if (this.endOfStream) {
        throw new ConnectionClosedException("Premature end of chunk coded message body: closing chunk expected");
    }
}
Also used : MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Example 4 with MalformedChunkCodingException

use of org.apache.hc.core5.http.MalformedChunkCodingException in project httpcomponents-core by apache.

the class TestChunkDecoder method testMissingClosingChunk.

@Test
public void testMissingClosingChunk() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345\r\n";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    Assertions.assertThrows(ConnectionClosedException.class, () -> {
        long bytesRead = 0;
        try {
            while (dst.hasRemaining() && !decoder.isCompleted()) {
                final int i = decoder.read(dst);
                if (i > 0) {
                    bytesRead += i;
                }
            }
        } catch (final MalformedChunkCodingException ex) {
            Assertions.assertEquals(26L, bytesRead);
            Assertions.assertEquals("12345678901234561234512345", CodecTestUtils.convert(dst));
            Assertions.assertTrue(decoder.isCompleted());
            throw ex;
        }
    });
}
Also used : MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

MalformedChunkCodingException (org.apache.hc.core5.http.MalformedChunkCodingException)4 ByteBuffer (java.nio.ByteBuffer)2 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)2 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 InterruptedIOException (java.io.InterruptedIOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 Charset (java.nio.charset.Charset)1 StandardCharsets (java.nio.charset.StandardCharsets)1