List of usage examples for io.netty.handler.codec.http HttpContent content
ByteBuf content();
From source file:HttpUploadClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) { if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; System.err.println("STATUS: " + response.getStatus()); System.err.println("VERSION: " + response.getProtocolVersion()); if (!response.headers().isEmpty()) { for (String name : response.headers().names()) { for (String value : response.headers().getAll(name)) { System.err.println("HEADER: " + name + " = " + value); }//from w w w .j a va 2 s .c om } } if (response.getStatus().code() == 200 && response.headers() .contains(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED, true)) { readingChunks = true; System.err.println("CHUNKED CONTENT {"); } else { System.err.println("CONTENT {"); } } if (msg instanceof HttpContent) { HttpContent chunk = (HttpContent) msg; System.err.println(chunk.content().toString(CharsetUtil.UTF_8)); if (chunk instanceof LastHttpContent) { if (readingChunks) { System.err.println("} END OF CHUNKED CONTENT"); } else { System.err.println("} END OF CONTENT"); } readingChunks = false; } else { System.err.println(chunk.content().toString(CharsetUtil.UTF_8)); } } }
From source file:bzh.ygu.fun.chitchat.HttpChitChatServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest request = this.request = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx);/* w w w . j a v a 2 s . co m*/ } String uri = request.uri(); HttpMethod method = request.method(); Root root = Root.getInstance(); buf.setLength(0); contentBuf.setLength(0); if (method == HttpMethod.POST) { if (uri.equals("/chitchat")) { currentAction = "Add"; } } if (method == HttpMethod.GET) { if (uri.startsWith(LATEST)) { latestFromWho = decode(uri.substring(LATEST_SIZE)); currentAction = "Latest"; Message m = root.getLatest(latestFromWho); if (m != null) { //{"author":"Iron Man", "text":"We have a Hulk !", "thread":3,"createdAt":1404736639715} buf.append(m.toJSON()); } } if (uri.startsWith(THREADCALL)) { currentAction = "Thread"; String threadId = uri.substring(THREADCALL_SIZE); MessageThread mt = root.getMessageThread(threadId); if (mt != null) { //{"author":"Iron Man", "text":"We have a Hulk !", "thread":3,"createdAt":1404736639715} buf.append(mt.toJSON()); } } if (uri.startsWith(SEARCH)) { String stringToSearch = decode(uri.substring(SEARCH_SIZE)); currentAction = "search"; List<Message> lm = root.search(stringToSearch); //[{"author":"Loki", "text":"I have an army !", "thread":3, //"createdAt":1404736639710}, {"author":"Iron Man", "text":"We have a Hulk !", // "thread":3, "createdAt":1404736639715}] buf.append("["); if (!lm.isEmpty()) { Iterator<Message> it = lm.iterator(); Message m = it.next(); buf.append(m.toJSON()); while (it.hasNext()) { m = it.next(); buf.append(", "); buf.append(m.toJSON()); } } buf.append("]"); } } } if (msg instanceof HttpContent) { Root root = Root.getInstance(); HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { contentBuf.append(content.toString(CharsetUtil.UTF_8)); } if (msg instanceof LastHttpContent) { // buf.append("END OF CONTENT\r\n"); if (currentAction.equals("Add")) { addMessageFromContent(contentBuf.toString(), root); currentAction = "None"; } LastHttpContent trailer = (LastHttpContent) msg; if (!writeResponse(trailer, ctx)) { // If keep-alive is off, close the connection once the content is fully written. ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } } } }
From source file:ccwihr.client.t1.HttpUploadClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) { if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; System.err.println("STATUS: " + response.status()); System.err.println("VERSION: " + response.protocolVersion()); if (!response.headers().isEmpty()) { for (CharSequence name : response.headers().names()) { for (CharSequence value : response.headers().getAll(name)) { System.err.println("HEADER: " + name + " = " + value); }//w w w . j a v a 2s. c o m } } if (response.status().code() == 200 && HttpUtil.isTransferEncodingChunked(response)) { readingChunks = true; System.err.println("CHUNKED CONTENT {"); } else { System.err.println("CONTENT {"); } } if (msg instanceof HttpContent) { HttpContent chunk = (HttpContent) msg; System.err.println(chunk.content().toString(CharsetUtil.UTF_8)); if (chunk instanceof LastHttpContent) { if (readingChunks) { System.err.println("} END OF CHUNKED CONTENT"); } else { System.err.println("} END OF CONTENT"); } readingChunks = false; } else { System.err.println(chunk.content().toString(CharsetUtil.UTF_8)); } } }
From source file:ch07.handlers.HttpSnoopServerHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { HttpRequest request = this.request = (HttpRequest) msg; if (HttpHeaders.is100ContinueExpected(request)) { send100Continue(ctx);/*from w w w . j a v a 2 s .c om*/ } buf.setLength(0); buf.append("WELCOME TO THE WILD WILD WEB SERVER\r\n"); buf.append("===================================\r\n"); buf.append("VERSION: ").append(request.protocolVersion()).append("\r\n"); buf.append("HOSTNAME: ").append(HttpHeaders.getHost(request, "unknown")).append("\r\n"); buf.append("REQUEST_URI: ").append(request.uri()).append("\r\n\r\n"); HttpHeaders headers = request.headers(); if (!headers.isEmpty()) { for (Map.Entry<String, String> h : headers) { String key = h.getKey(); String value = h.getValue(); buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n"); } buf.append("\r\n"); } QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.uri()); Map<String, List<String>> params = queryStringDecoder.parameters(); if (!params.isEmpty()) { for (Entry<String, List<String>> p : params.entrySet()) { String key = p.getKey(); List<String> vals = p.getValue(); for (String val : vals) { buf.append("PARAM: ").append(key).append(" = ").append(val).append("\r\n"); } } buf.append("\r\n"); } appendDecoderResult(buf, request); } if (msg instanceof HttpContent) { HttpContent httpContent = (HttpContent) msg; ByteBuf content = httpContent.content(); if (content.isReadable()) { buf.append("CONTENT: "); buf.append(content.toString(CharsetUtil.UTF_8)); buf.append("\r\n"); appendDecoderResult(buf, request); } if (msg instanceof LastHttpContent) { buf.append("END OF CONTENT\r\n"); LastHttpContent trailer = (LastHttpContent) msg; if (!trailer.trailingHeaders().isEmpty()) { buf.append("\r\n"); for (String name : trailer.trailingHeaders().names()) { for (String value : trailer.trailingHeaders().getAll(name)) { buf.append("TRAILING HEADER: "); buf.append(name).append(" = ").append(value).append("\r\n"); } } buf.append("\r\n"); } if (!writeResponse(trailer, ctx)) { // If keep-alive is off, close the connection once the content is fully written. ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } } } }
From source file:cn.dennishucd.nettyhttpserver.HttpServerHandler.java
License:Apache License
@SuppressWarnings("deprecation") @Override//w w w . j a va 2 s .c om public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { request = (HttpRequest) msg; if (HttpUtil.is100ContinueExpected(request)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); } if (!request.uri().equalsIgnoreCase(URL)) { sendError(ctx, NOT_FOUND); return; } } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; ByteBuf buf = content.content(); UserToken ut = CTools.JSONStr2Object(buf.toString(io.netty.util.CharsetUtil.UTF_8), UserToken.class); logger.info("request body: " + buf.toString(io.netty.util.CharsetUtil.UTF_8)); buf.release(); boolean keepAlive = HttpUtil.isKeepAlive(request); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT.getBytes())); response.headers().set(CONTENT_TYPE, "application/json"); response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes()); logger.info("response body: " + CONTENT); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, KEEP_ALIVE); ctx.write(response); } } else { ctx.fireChannelRead(msg); } }
From source file:cn.wantedonline.puppy.httpserver.component.HttpObjectAggregator.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception { AggregatedFullHttpMessage currentMessage = this.currentMessage; if (msg instanceof HttpMessage) { tooLongFrameFound = false;/*w w w .j a v a 2s . c om*/ assert currentMessage == null; HttpMessage m = (HttpMessage) msg; // Handle the 'Expect: 100-continue' header if necessary. if (is100ContinueExpected(m)) { if (HttpHeaders.getContentLength(m, 0) > maxContentLength) { tooLongFrameFound = true; final ChannelFuture future = ctx.writeAndFlush(EXPECTATION_FAILED.duplicate().retain()); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.fireExceptionCaught(future.cause()); } } }); if (closeOnExpectationFailed) { future.addListener(ChannelFutureListener.CLOSE); } ctx.pipeline().fireUserEventTriggered(HttpExpectationFailedEvent.INSTANCE); return; } ctx.writeAndFlush(CONTINUE.duplicate().retain()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.fireExceptionCaught(future.cause()); } } }); } if (!m.getDecoderResult().isSuccess()) { removeTransferEncodingChunked(m); out.add(toFullMessage(m)); this.currentMessage = null; return; } if (msg instanceof HttpRequest) { HttpRequest header = (HttpRequest) msg; this.currentMessage = currentMessage = new AggregatedFullHttpRequest(header, ctx.alloc().compositeBuffer(maxCumulationBufferComponents), null); } else if (msg instanceof HttpResponse) { HttpResponse header = (HttpResponse) msg; this.currentMessage = currentMessage = new AggregatedFullHttpResponse(header, Unpooled.compositeBuffer(maxCumulationBufferComponents), null); } else { throw new Error(); } // A streamed message - initialize the cumulative buffer, and wait for incoming chunks. removeTransferEncodingChunked(currentMessage); } else if (msg instanceof HttpContent) { if (tooLongFrameFound) { if (msg instanceof LastHttpContent) { this.currentMessage = null; } // already detect the too long frame so just discard the content return; } assert currentMessage != null; // Merge the received chunk into the content of the current message. HttpContent chunk = (HttpContent) msg; CompositeByteBuf content = (CompositeByteBuf) currentMessage.content(); if (content.readableBytes() > maxContentLength - chunk.content().readableBytes()) { tooLongFrameFound = true; // release current message to prevent leaks currentMessage.release(); this.currentMessage = null; throw new TooLongFrameException("HTTP content length exceeded " + maxContentLength + " bytes."); } // Append the content of the chunk if (chunk.content().isReadable()) { chunk.retain(); content.addComponent(chunk.content()); content.writerIndex(content.writerIndex() + chunk.content().readableBytes()); } final boolean last; if (!chunk.getDecoderResult().isSuccess()) { currentMessage.setDecoderResult(DecoderResult.failure(chunk.getDecoderResult().cause())); last = true; } else { last = chunk instanceof LastHttpContent; } if (last) { this.currentMessage = null; // Merge trailing headers into the message. if (chunk instanceof LastHttpContent) { LastHttpContent trailer = (LastHttpContent) chunk; currentMessage.setTrailingHeaders(trailer.trailingHeaders()); } else { currentMessage.setTrailingHeaders(new DefaultHttpHeaders()); } // Set the 'Content-Length' header. If one isn't already set. // This is important as HEAD responses will use a 'Content-Length' header which // does not match the actual body, but the number of bytes that would be // transmitted if a GET would have been used. // // See rfc2616 14.13 Content-Length if (!isContentLengthSet(currentMessage)) { currentMessage.headers().set(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(content.readableBytes())); } // All done out.add(currentMessage); } } else { throw new Error(); } }
From source file:co.freeside.betamax.proxy.netty.NettyMessageAdapter.java
License:Apache License
public void append(HttpContent chunk) throws IOException { body.addComponent(copiedBuffer(chunk.content())); body.writerIndex(body.writerIndex() + chunk.content().readableBytes()); }
From source file:com.aerofs.baseline.http.HttpRequestHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (ctx.channel().closeFuture().isDone()) { LOGGER.warn("{}: drop http message - channel closed", Channels.getHexText(ctx)); return;//from w w w . jav a 2 s . c o m } if (!(msg instanceof HttpObject)) { super.channelRead(ctx, msg); } // // netty http decoding: // // 1. normal case: HttpRequest (headers), HttpContent (0+), LastHttpContent (trailing headers) // NOTE: with chunked transfer encoding or content-length non-zero you get an arbitrary number of HttpContent objects // 2. error case (failed to decode an HTTP message): HttpRequest with NETTY_HTTP_DECODING_FAILED_URI // if (msg instanceof HttpRequest) { // this is the first object netty generates: // an HttpRequest containing a number of headers // we should not have another request pending Preconditions.checkState(pendingRequest == null, "previous request pending:%s", pendingRequest); // cast it HttpRequest nettyRequest = (HttpRequest) msg; // get the request id String requestId = nettyRequest.headers().get(Headers.REQUEST_TRACING_HEADER); Preconditions.checkState(requestId != null, "http request on %s has no request id", Channels.getHexText(ctx)); // check if http decoding failed and if so, abort early if (nettyRequest.uri().equals(NETTY_HTTP_DECODING_FAILED_URI)) { LOGGER.warn("{}: [{}] fail http decoding", Channels.getHexText(ctx), requestId); ctx.read(); return; } // get a few headers we really care about HttpVersion httpVersion = nettyRequest.protocolVersion(); boolean keepAlive = HttpHeaders.isKeepAlive(nettyRequest); boolean transferEncodingChunked = HttpHeaders.isTransferEncodingChunked(nettyRequest); boolean continueExpected = HttpHeaders.is100ContinueExpected(nettyRequest); long contentLength = HttpHeaders.getContentLength(nettyRequest, ZERO_CONTENT_LENGTH); boolean hasContent = transferEncodingChunked || contentLength > ZERO_CONTENT_LENGTH; LOGGER.trace("{}: [{}] rq:{} ka:{} ck:{} ce:{} cl:{}", Channels.getHexText(ctx), requestId, nettyRequest, keepAlive, transferEncodingChunked, continueExpected, contentLength); // create the input stream used to read content ContentInputStream entityInputStream; if (hasContent) { entityInputStream = new EntityInputStream(httpVersion, continueExpected, ctx); } else { entityInputStream = EmptyEntityInputStream.EMPTY_ENTITY_INPUT_STREAM; } // create the object with which to write the response body PendingRequest pendingRequest = new PendingRequest(requestId, httpVersion, keepAlive, entityInputStream, ctx); // create the jersey request object final ContainerRequest jerseyRequest = new ContainerRequest(baseUri, URI.create(nettyRequest.uri()), nettyRequest.method().name(), DEFAULT_SECURITY_CONTEXT, PROPERTIES_DELEGATE); jerseyRequest.setProperty(RequestProperties.REQUEST_CONTEXT_CHANNEL_ID_PROPERTY, new ChannelId(Channels.getHexText(ctx))); jerseyRequest.setProperty(RequestProperties.REQUEST_CONTEXT_REQUEST_ID_PROPERTY, new RequestId(requestId)); jerseyRequest.header(Headers.REQUEST_TRACING_HEADER, requestId); // add request id to headers copyHeaders(nettyRequest.headers(), jerseyRequest); // copy headers from message jerseyRequest.setEntityStream(entityInputStream); jerseyRequest.setWriter(pendingRequest); // now we've got all the initial headers and are waiting for the entity this.pendingRequest = pendingRequest; // store the runnable that we want jersey to execute saveRequestRunnable(() -> { // all throwables caught by jersey internally - // handled by the ResponseWriter below // if, for some reason there's some weird error it'll be handled // by the default exception handler, which kills the process applicationHandler.handle(jerseyRequest); }); // IMPORTANT: // we pass this request up to be processed by // jersey before we've read any content. This allows // the resource to read from the InputStream // directly, OR, to use an @Consumes annotation with an // input objectType to invoke the appropriate parser // // since the request is consumed *before* the content // has been received readers may block. to prevent the // IO thread from blocking we have to execute all // request processing in an application threadpool if (hasContent) { submitPendingRunnable(); } // indicate that we want to keep reading // this is always the case when we receive headers // because we want to receive everything until // LastHttpContent ctx.read(); } else { // after receiving the http headers we get // a series of HttpContent objects that represent // the entity or a set of chunks // we should have received the headers already Preconditions.checkState(pendingRequest != null, "no pending request"); // we're not expecting anything other than content objects right now Preconditions.checkArgument(msg instanceof HttpContent, "HttpContent expected, not %s", msg.getClass().getSimpleName()); // handle the content HttpContent content = (HttpContent) msg; boolean last = msg instanceof LastHttpContent; LOGGER.trace("{}: [{}] handling content:{} last:{}", Channels.getHexText(ctx), pendingRequest.requestId, content.content().readableBytes(), last); pendingRequest.entityInputStream.addBuffer(content.content(), last); // transfers ownership to the HttpContentInputStream // FIXME (AG): support trailing headers // if it's the last piece of content, then we're done if (last) { // submit the request to jersey if we haven't yet if (savedRequestRunnable != null) { submitPendingRunnable(); } } } }
From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) { assert msg instanceof FullHttpResponse; l.info(" Got a message from server !!! {}", msg); ++state;/* w w w. j ava 2s .com*/ if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; System.out.println("STATUS: " + response.getStatus()); System.out.println("VERSION: " + response.getProtocolVersion()); System.out.println(); if (!response.headers().isEmpty()) { for (String name : response.headers().names()) { for (String value : response.headers().getAll(name)) { System.out.println("HEADER: " + name + " = " + value); } } System.out.println(); } if (HttpHeaders.isTransferEncodingChunked(response)) { System.out.println("CHUNKED CONTENT {"); } else { System.out.println("CONTENT {"); } } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; System.out.print(content.content().toString(CharsetUtil.UTF_8)); System.out.flush(); if (content instanceof LastHttpContent) { System.out.println("} END OF CONTENT"); } } switch (state) { case 1: FullHttpResponse resp = (FullHttpResponse) msg; for (String cookiestr : resp.headers().getAll(HttpHeaders.Names.SET_COOKIE)) { Set<Cookie> tmp = CookieDecoder.decode(cookiestr); sessCookies = tmp; } //login( ctx.channel(), credential ); pubEvent(ctx.channel(), event, (FullHttpResponse) msg); break; case 2: getEvents(ctx.channel(), (FullHttpResponse) msg); break; case 3: logout(ctx.channel()); break; default: ctx.close(); break; } }
From source file:com.andrewkroh.cisco.xmlservices.XmlResponseChannelHandler.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { IpPhone phone = ctx.channel().attr(phoneAttributeKey).get(); if (msg instanceof HttpResponse && LOGGER.isDebugEnabled()) { HttpResponse response = (HttpResponse) msg; StringBuilder responseInfo = new StringBuilder(); responseInfo.append("Source="); responseInfo.append(phone.getHostname()); responseInfo.append(", "); responseInfo.append("Status="); responseInfo.append(response.getStatus()); responseInfo.append(", "); responseInfo.append("Version="); responseInfo.append(response.getProtocolVersion()); for (String name : response.headers().names()) { for (String value : response.headers().getAll(name)) { responseInfo.append(", "); responseInfo.append(name); responseInfo.append('='); responseInfo.append(value); }//from w w w . j a v a 2s . c o m } LOGGER.debug(responseInfo.toString()); } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; SettableFuture<XmlPushResponse> responseFuture = ctx.channel().attr(responseAttributeKey).get(); // The default charset for HTTP is ISO-8859-1. None // of the Cisco phones I've seen to date were actually // setting the charset so use the default. We could // improve here by checking the header for the value. String xmlContent = content.content().toString(CharsetUtil.ISO_8859_1); CiscoIPPhoneResponse xmlResponse = XmlMarshaller.unmarshal(xmlContent, CiscoIPPhoneResponse.class); responseFuture.set(new DefaultXmlPushResponse(phone, xmlResponse)); // Cleanup: ctx.close(); ctx.channel().close(); } }