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");
}
}
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"));
}
}
}
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()));
}
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)))));
}
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));
}
Aggregations