use of io.netty.handler.codec.http.LastHttpContent in project riposte by Nike-Inc.
the class RequestInfoImpl method addContentChunk.
/**
* {@inheritDoc}
*/
@Override
public int addContentChunk(HttpContent chunk) {
if (isCompleteRequestWithAllChunks) {
throw new IllegalStateException("Cannot add new content chunk - this RequestInfo is already marked as " + "representing the complete request with all chunks");
}
chunk.retain();
rawContentLengthInBytes += chunk.content().readableBytes();
// If content chunks will be released externally then there's no point in us holding on to them
if (!contentChunksWillBeReleasedExternally)
contentChunks.add(chunk);
if (chunk instanceof LastHttpContent) {
// to be set to true if content chunks are released externally.
if (!contentChunksWillBeReleasedExternally)
isCompleteRequestWithAllChunks = true;
HttpHeaders chunkTrailingHeaders = ((LastHttpContent) chunk).trailingHeaders();
//noinspection StatementWithEmptyBody
if (trailingHeaders == chunkTrailingHeaders) {
// Can happen during the constructor. We've already set the trailing headers to the chunk's trailing headers, so nothing to do here.
} else {
if (!trailingHeaders.isEmpty()) {
throw new IllegalStateException("Received the final chunk, but trailingHeaders was already " + "populated. This should not be possible.");
}
trailingHeaders.add(chunkTrailingHeaders);
}
}
return rawContentLengthInBytes;
}
use of io.netty.handler.codec.http.LastHttpContent in project async-http-client by AsyncHttpClient.
the class WebSocketHandler method handleRead.
@Override
public void handleRead(Channel channel, NettyResponseFuture<?> future, Object e) throws Exception {
if (e instanceof HttpResponse) {
HttpResponse response = (HttpResponse) e;
if (logger.isDebugEnabled()) {
HttpRequest httpRequest = future.getNettyRequest().getHttpRequest();
logger.debug("\n\nRequest {}\n\nResponse {}\n", httpRequest, response);
}
WebSocketUpgradeHandler handler = (WebSocketUpgradeHandler) future.getAsyncHandler();
HttpResponseStatus status = new NettyResponseStatus(future.getUri(), response, channel);
HttpResponseHeaders responseHeaders = new HttpResponseHeaders(response.headers());
if (!interceptors.exitAfterIntercept(channel, future, handler, response, status, responseHeaders)) {
switch(handler.onStatusReceived(status)) {
case CONTINUE:
upgrade(channel, future, handler, response, responseHeaders);
break;
default:
abort(channel, future, handler, status);
}
}
} else if (e instanceof WebSocketFrame) {
final WebSocketFrame frame = (WebSocketFrame) e;
WebSocketUpgradeHandler handler = (WebSocketUpgradeHandler) future.getAsyncHandler();
NettyWebSocket webSocket = handler.onCompleted();
// retain because we might buffer the frame
if (webSocket.isReady()) {
webSocket.handleFrame(frame);
} else {
// WebSocket hasn't been open yet, but upgrading the pipeline triggered a read and a frame was sent along the HTTP upgrade response
// as we want to keep sequential order (but can't notify user of open before upgrading so he doesn't to try send immediately), we have to buffer
webSocket.bufferFrame(frame);
}
} else if (!(e instanceof LastHttpContent)) {
// ignore, end of handshake response
logger.error("Invalid message {}", e);
}
}
use of io.netty.handler.codec.http.LastHttpContent in project Glowstone by GlowstoneMC.
the class HttpHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpResponse) {
HttpResponse response = (HttpResponse) msg;
int responseCode = response.status().code();
if (responseCode == HttpResponseStatus.NO_CONTENT.code()) {
done(ctx);
return;
}
if (responseCode != HttpResponseStatus.OK.code()) {
throw new IllegalStateException("Expected HTTP response 200 OK, got " + responseCode);
}
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
content.append(httpContent.content().toString(Charset.forName("UTF-8")));
if (msg instanceof LastHttpContent) {
done(ctx);
}
}
}
Aggregations