Search in sources :

Example 1 with IncomingEntityDetails

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

the class ClientH2StreamHandler method consumeHeader.

@Override
public void consumeHeader(final List<Header> headers, final boolean endStream) throws HttpException, IOException {
    if (done.get()) {
        throw new ProtocolException("Unexpected message headers");
    }
    switch(responseState) {
        case HEADERS:
            final HttpResponse response = DefaultH2ResponseConverter.INSTANCE.convert(headers);
            final int status = response.getCode();
            if (status < HttpStatus.SC_INFORMATIONAL) {
                throw new ProtocolException("Invalid response: " + new StatusLine(response));
            }
            if (status > HttpStatus.SC_CONTINUE && status < HttpStatus.SC_SUCCESS) {
                exchangeHandler.consumeInformation(response, context);
            }
            if (requestState == MessageState.ACK) {
                if (status == HttpStatus.SC_CONTINUE || status >= HttpStatus.SC_SUCCESS) {
                    requestState = MessageState.BODY;
                    exchangeHandler.produce(dataChannel);
                }
            }
            if (status < HttpStatus.SC_SUCCESS) {
                return;
            }
            final EntityDetails entityDetails = endStream ? null : new IncomingEntityDetails(response, -1);
            context.setAttribute(HttpCoreContext.HTTP_RESPONSE, response);
            httpProcessor.process(response, entityDetails, context);
            connMetrics.incrementResponseCount();
            exchangeHandler.consumeResponse(response, entityDetails, context);
            responseState = endStream ? MessageState.COMPLETE : MessageState.BODY;
            break;
        case BODY:
            responseState = MessageState.COMPLETE;
            exchangeHandler.streamEnd(headers);
            break;
        default:
            throw new ProtocolException("Unexpected message headers");
    }
}
Also used : StatusLine(org.apache.hc.core5.http.message.StatusLine) ProtocolException(org.apache.hc.core5.http.ProtocolException) EntityDetails(org.apache.hc.core5.http.EntityDetails) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails) HttpResponse(org.apache.hc.core5.http.HttpResponse) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails)

Example 2 with IncomingEntityDetails

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

the class AbstractHttp1StreamDuplexer method onInput.

public final void onInput(final ByteBuffer src) throws HttpException, IOException {
    if (src != null) {
        inbuf.put(src);
    }
    if (connState.compareTo(ConnectionState.GRACEFUL_SHUTDOWN) >= 0 && inbuf.hasData() && inputIdle()) {
        ioSession.clearEvent(SelectionKey.OP_READ);
        return;
    }
    boolean endOfStream = false;
    if (incomingMessage == null) {
        final int bytesRead = inbuf.fill(ioSession);
        if (bytesRead > 0) {
            inTransportMetrics.incrementBytesTransferred(bytesRead);
        }
        endOfStream = bytesRead == -1;
    }
    do {
        if (incomingMessage == null) {
            final IncomingMessage messageHead = parseMessageHead(endOfStream);
            if (messageHead != null) {
                this.version = messageHead.getVersion();
                updateInputMetrics(messageHead, connMetrics);
                final ContentDecoder contentDecoder;
                if (handleIncomingMessage(messageHead)) {
                    final long len = incomingContentStrategy.determineLength(messageHead);
                    contentDecoder = createContentDecoder(len, ioSession, inbuf, inTransportMetrics);
                    consumeHeader(messageHead, contentDecoder != null ? new IncomingEntityDetails(messageHead, len) : null);
                } else {
                    consumeHeader(messageHead, null);
                    contentDecoder = null;
                }
                capacityWindow = new CapacityWindow(http1Config.getInitialWindowSize(), ioSession);
                if (contentDecoder != null) {
                    incomingMessage = new Message<>(messageHead, contentDecoder);
                } else {
                    inputEnd();
                    if (connState.compareTo(ConnectionState.ACTIVE) == 0) {
                        ioSession.setEvent(SelectionKey.OP_READ);
                    }
                }
            } else {
                break;
            }
        }
        if (incomingMessage != null) {
            final ContentDecoder contentDecoder = incomingMessage.getBody();
            // At present the consumer can be forced to consume data
            // over its declared capacity in order to avoid having
            // unprocessed message body content stuck in the session
            // input buffer
            final int bytesRead = contentDecoder.read(contentBuffer);
            if (bytesRead > 0) {
                contentBuffer.flip();
                consumeData(contentBuffer);
                contentBuffer.clear();
                final int capacity = capacityWindow.removeCapacity(bytesRead);
                if (capacity <= 0) {
                    if (!contentDecoder.isCompleted()) {
                        updateCapacity(capacityWindow);
                    }
                }
            }
            if (contentDecoder.isCompleted()) {
                dataEnd(contentDecoder.getTrailers());
                capacityWindow.close();
                incomingMessage = null;
                ioSession.setEvent(SelectionKey.OP_READ);
                inputEnd();
            }
            if (bytesRead == 0) {
                break;
            }
        }
    } while (inbuf.hasData());
    if (endOfStream && !inbuf.hasData()) {
        if (outputIdle() && inputIdle()) {
            requestShutdown(CloseMode.GRACEFUL);
        } else {
            shutdownSession(new ConnectionClosedException("Connection closed by peer"));
        }
    }
}
Also used : ContentDecoder(org.apache.hc.core5.http.nio.ContentDecoder) IncomingEntityDetails(org.apache.hc.core5.http.impl.IncomingEntityDetails) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException)

Example 3 with IncomingEntityDetails

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

the class IncomingEntityDetailsTest method getContentLength.

@Test
public void getContentLength() {
    final MessageHeaders messageHeaders = new HeaderGroup();
    final HeaderGroup headerGroup = new HeaderGroup();
    final Header header = new BasicHeader("name", "value");
    headerGroup.addHeader(header);
    final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
    assertAll(() -> assertEquals(-1, incomingEntityDetails.getContentLength()), () -> assertTrue(incomingEntityDetails.isChunked()));
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HeaderGroup(org.apache.hc.core5.http.message.HeaderGroup) MessageHeaders(org.apache.hc.core5.http.MessageHeaders) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 4 with IncomingEntityDetails

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

the class IncomingEntityDetailsTest method getTrailerNames.

@Test
public void getTrailerNames() {
    final HeaderGroup messageHeaders = new HeaderGroup();
    final Header header = new BasicHeader(HttpHeaders.TRAILER, "a, b, c, c");
    messageHeaders.setHeaders(header);
    final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
    final Set<String> incomingSet = incomingEntityDetails.getTrailerNames();
    assertAll(() -> assertFalse(incomingSet.isEmpty()), () -> assertTrue(incomingSet.containsAll(Stream.of("a", "b", "c").collect(Collectors.toCollection(HashSet::new)))));
}
Also used : Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HeaderGroup(org.apache.hc.core5.http.message.HeaderGroup) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with IncomingEntityDetails

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

the class IncomingEntityDetailsTest method getContentLengthEmpty.

@Test
public void getContentLengthEmpty() {
    final MessageHeaders messageHeaders = new HeaderGroup();
    final IncomingEntityDetails incomingEntityDetails = new IncomingEntityDetails(messageHeaders);
    assertAll(() -> assertEquals(-1, incomingEntityDetails.getContentLength()), () -> assertNull(incomingEntityDetails.getContentType()), () -> assertNull(incomingEntityDetails.getContentEncoding()), () -> assertEquals(incomingEntityDetails.getTrailerNames().size(), 0));
}
Also used : HeaderGroup(org.apache.hc.core5.http.message.HeaderGroup) MessageHeaders(org.apache.hc.core5.http.MessageHeaders) Test(org.junit.jupiter.api.Test)

Aggregations

IncomingEntityDetails (org.apache.hc.core5.http.impl.IncomingEntityDetails)4 EntityDetails (org.apache.hc.core5.http.EntityDetails)3 ProtocolException (org.apache.hc.core5.http.ProtocolException)3 HeaderGroup (org.apache.hc.core5.http.message.HeaderGroup)3 Test (org.junit.jupiter.api.Test)3 Header (org.apache.hc.core5.http.Header)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 MessageHeaders (org.apache.hc.core5.http.MessageHeaders)2 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)2 HashSet (java.util.HashSet)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 StatusLine (org.apache.hc.core5.http.message.StatusLine)1 AsyncResponseProducer (org.apache.hc.core5.http.nio.AsyncResponseProducer)1 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)1 ContentDecoder (org.apache.hc.core5.http.nio.ContentDecoder)1 BasicResponseProducer (org.apache.hc.core5.http.nio.support.BasicResponseProducer)1 ImmediateResponseExchangeHandler (org.apache.hc.core5.http.nio.support.ImmediateResponseExchangeHandler)1 H2StreamResetException (org.apache.hc.core5.http2.H2StreamResetException)1