in common/src/main/java/org/apache/asyncweb/common/codec/HttpResponseDecodingState.java [163:261]
protected DecodingState finishDecode(List<Object> childProducts,
ProtocolDecoderOutput out) throws Exception {
Map<String, List<String>> headers = (Map<String, List<String>>) childProducts
.get(0);
// Parse cookies
List<String> cookies = headers.get(HttpHeaderConstants.KEY_SET_COOKIE);
if (cookies != null && !cookies.isEmpty()) {
for (String cookie : cookies) {
response.addCookie(parseCookie(cookie));
}
}
response.setHeaders(headers);
if (LOG.isDebugEnabled()) {
LOG.debug("Decoded header: " + response.getHeaders());
}
// Select appropriate body decoding state.
boolean isChunked = false;
if (response.getProtocolVersion() == HttpVersion.HTTP_1_1) {
LOG.debug("Request is HTTP 1/1. Checking for transfer coding");
isChunked = isChunked(response);
} else {
LOG.debug("Request is not HTTP 1/1. Using content length");
}
DecodingState nextState;
if (isChunked) {
LOG.debug("Using chunked decoder for request");
nextState = new ChunkedBodyDecodingState() {
@Override
protected DecodingState finishDecode(
List<Object> childProducts,
ProtocolDecoderOutput out) throws Exception {
if (childProducts.size() != 1) {
int chunkSize = 0;
for (Object product : childProducts) {
IoBuffer chunk = (IoBuffer) product;
chunkSize += chunk.remaining();
}
IoBuffer body = IoBuffer.allocate(chunkSize);
for (Object product : childProducts) {
IoBuffer chunk = (IoBuffer) product;
body.put(chunk);
}
body.flip();
response.setContent(body);
} else {
response.setContent((IoBuffer) childProducts.get(0));
}
out.write(response);
return null;
}
};
} else {
int length = getContentLength(response);
if (length > 0) {
if (LOG.isDebugEnabled()) {
LOG.debug(
"Using fixed length decoder for request with " +
"length " + length);
}
// TODO max length limitation.
nextState = new FixedLengthDecodingState(length) {
@Override
protected DecodingState finishDecode(IoBuffer readData,
ProtocolDecoderOutput out) throws Exception {
response.setContent(readData);
out.write(response);
return null;
}
};
} else if (length < 0) {
if (LOG.isDebugEnabled()) {
LOG.debug(
"Using consume-to-disconnection decoder for " +
"request with unspecified length.");
}
// FIXME hard-coded max length.
nextState = new ConsumeToEndOfSessionDecodingState(1048576) {
@Override
protected DecodingState finishDecode(IoBuffer readData,
ProtocolDecoderOutput out) throws Exception {
response.setContent(readData);
out.write(response);
return null;
}
};
} else {
LOG.debug("No entity body for this request");
out.write(response);
nextState = null;
}
}
return nextState;
}