@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append(getClass().getSimpleName()); buf.append("(version: "); buf.append(getProtocolVersion().getText()); buf.append(", keepAlive: "); buf.append(HttpHeaders.isKeepAlive(this)); buf.append(", chunked: "); buf.append(isChunked()); buf.append(')'); buf.append(StringUtil.NEWLINE); appendHeaders(buf); // Remove the last newline. buf.setLength(buf.length() - StringUtil.NEWLINE.length()); return buf.toString(); }
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
private void handleHttpRequest(final ChannelHandlerContext ctx, MessageEvent messageEvent, HttpRequest httpRequest) { final NettyHttpRequest nettyHttpRequest = new NettyHttpRequest(messageEvent, httpRequest, id, timestamp); DefaultHttpResponse ok_200 = new DefaultHttpResponse(HTTP_1_1, OK); final NettyHttpResponse nettyHttpResponse = new NettyHttpResponse( ctx, ok_200, isKeepAlive(httpRequest), exceptionHandler); Iterator<HttpHandler> httpHandlers = this.httpHandlers.iterator(); final HttpControl control = new NettyHttpControl(httpHandlers, executor, ctx, nettyHttpRequest, nettyHttpResponse, httpRequest, ok_200, exceptionHandler, ioExceptionHandler); executor.execute(new Runnable() { @Override public void run() { try { control.nextHandler(nettyHttpRequest, nettyHttpResponse); } catch (Exception exception) { exceptionHandler.uncaughtException(Thread.currentThread(), WebbitException.fromException(exception, ctx.getChannel())); } } }); }
private void write(HttpResponse httpResponse) { ChannelFuture future = channel.write(httpResponse); if (!isKeepAlive(httpRequest)) { future.addListener(ChannelFutureListener.CLOSE); } this.interrupted = true; }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { if (!(msg instanceof org.jboss.netty.handler.codec.http.HttpRequest)) { return msg; } org.jboss.netty.handler.codec.http.HttpRequest request = (org.jboss.netty.handler.codec.http.HttpRequest) msg; boolean keepAlive = org.jboss.netty.handler.codec.http.HttpHeaders.isKeepAlive(request) & isKeepAlive; NettyHttpResponse response = new NettyHttpResponse(channel, keepAlive, request.getMethod()); ResteasyHttpHeaders headers = null; ResteasyUriInfo uriInfo = null; try { headers = NettyUtil.extractHttpHeaders(request); uriInfo = NettyUtil.extractUriInfo(request, servletMappingPrefix, proto); HttpRequest nettyRequest = new NettyHttpRequest(headers, uriInfo, request.getMethod().getName(), dispatcher, response, org.jboss.netty.handler.codec.http.HttpHeaders.is100ContinueExpected(request) ); ChannelBufferInputStream is = new ChannelBufferInputStream(request.getContent()); nettyRequest.setInputStream(is); return nettyRequest; } catch (Exception e) { response.sendError(400); // made it warn so that people can filter this. LogMessages.LOGGER.warn(Messages.MESSAGES.failedToParseRequest(), e); return null; } }
public static boolean isKeepAlive(HttpMessage message) { return HttpHeaders.isKeepAlive(message) && message.getProtocolVersion().equals(HttpVersion.HTTP_1_1); }
@Override public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception { if (e instanceof MessageEvent && ((MessageEvent)e).getMessage() instanceof HttpRequest) { HttpRequest request = (HttpRequest)((MessageEvent)e).getMessage(); CacheEntry ce = cache.get(request.getUri()); if (ce != null && ce.getExpires() > System.currentTimeMillis()) { ChannelFuture f = e.getChannel().write(ce.getContent()); f.addListener(ChannelFutureListener.CLOSE); if (!HttpHeaders.isKeepAlive(request)) { f.addListener(ChannelFutureListener.CLOSE); } return; } } super.handleUpstream(ctx, e); }
private void handleHttpRequest(final ChannelHandlerContext ctx, MessageEvent messageEvent, HttpRequest httpRequest) { final NettyHttpRequest nettyHttpRequest = new NettyHttpRequest(messageEvent, httpRequest, id, timestamp); DefaultHttpResponse ok_200 = new DefaultHttpResponse(HTTP_1_1, OK); final NettyHttpResponse nettyHttpResponse = new NettyHttpResponse( ctx, ok_200, isKeepAlive(httpRequest), exceptionHandler); Iterator<HttpHandler> httpHandlers = this.httpHandlers.iterator(); final HttpControl control = new NettyHttpControl(httpHandlers, executor, ctx, nettyHttpRequest, nettyHttpResponse, httpRequest, ok_200, exceptionHandler, ioExceptionHandler); executor.execute(new Runnable() { @Override public void run() { try { control.nextHandler(nettyHttpRequest, nettyHttpResponse); } catch (Exception exception) { exceptionHandler.uncaughtException(Thread.currentThread(), WebbitException.fromException(exception, ctx.getChannel())); } } }); }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Send the response and close the connection if necessary. if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { res.setHeader(CONNECTION, Values.CLOSE); ctx.getChannel().write(res).addListener(ChannelFutureListener.CLOSE); } else { ctx.getChannel().write(res); } }
private void writeResponse(HttpRequest request, HttpResponse response, Channel channel) { boolean keepAlive = isKeepAlive(request); if (keepAlive) { response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); response.setHeader(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } ChannelFuture future = channel.write(response); if (!keepAlive) { future.addListener(ChannelFutureListener.CLOSE); } } }
@Override protected void beforeProcess(Exchange exchange, MessageEvent messageEvent) { if (consumer.getConfiguration().isBridgeEndpoint()) { exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE); exchange.setProperty(Exchange.SKIP_WWW_FORM_URLENCODED, Boolean.TRUE); } HttpRequest request = (HttpRequest) messageEvent.getMessage(); // setup the connection property in case of the message header is removed boolean keepAlive = HttpHeaders.isKeepAlive(request); if (!keepAlive) { // Just make sure we close the connection this time. exchange.setProperty(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != 200) { //res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8)); //setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
/** Handle conditional connection close depending on keep-alive */ private void writeResponse(Channel channel, HttpRequest request, HttpResponse response) { response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); boolean hasKeepAliveHeader = KEEP_ALIVE.equalsIgnoreCase(request.getHeader(CONNECTION)); if (!request.getProtocolVersion().isKeepAliveDefault() && hasKeepAliveHeader) { response.setHeader(CONNECTION, KEEP_ALIVE); } ChannelFuture wf = channel.write(response); if (!HttpHeaders.isKeepAlive(request)) { wf.addListener(ChannelFutureListener.CLOSE); } }
@Override protected Object getRequestBody(Exchange exchange) throws Exception { // creating the url to use takes 2-steps String uri = NettyHttpHelper.createURL(exchange, getEndpoint()); URI u = NettyHttpHelper.createURI(exchange, uri, getEndpoint()); HttpRequest request = getEndpoint().getNettyHttpBinding().toNettyRequest(exchange.getIn(), u.toString(), getConfiguration()); String actualUri = request.getUri(); exchange.getIn().setHeader(Exchange.HTTP_URL, actualUri); // Need to check if we need to close the connection or not if (!HttpHeaders.isKeepAlive(request)) { // just want to make sure we close the channel if the keepAlive is not true exchange.setProperty(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, true); } if (getConfiguration().isBridgeEndpoint()) { // Need to remove the Host key as it should be not used when bridging/proxying exchange.getIn().removeHeader("host"); } return request; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { ChannelBuffer input; HttpRequest httpRequest = null; if (e.getMessage() instanceof HttpRequest) { httpRequest = ((HttpRequest)e.getMessage()); input = httpRequest.getContent(); } else { input = (ChannelBuffer)e.getMessage(); } ChannelBuffer output = ChannelBuffers.dynamicBuffer(responseSize); TProtocol protocol = protocolFactory.getProtocol(new TNettyChannelBuffer(input, output)); processor.process(protocol, protocol); if (httpRequest != null) { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setHeader("Content-Length", output.readableBytes()); response.setContent(output); ChannelFuture future = e.getChannel().write(response); if (!HttpHeaders.isKeepAlive(httpRequest)) { future.addListener(ChannelFutureListener.CLOSE); } } else { e.getChannel().write(output); } }
@Override public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception { final Channel channel = e.getChannel(); final HttpRequest request = (HttpRequest) e.getMessage(); final boolean keepAlive = isKeepAlive(request); final HttpVersion httpRequestVersion = request.getProtocolVersion(); final String origin = request.headers().get(Names.ORIGIN); // to allow for future changes, let's be at least a little strict in what we accept here. if (request.getMethod() != HttpMethod.POST) { writeResponse(channel, keepAlive, httpRequestVersion, METHOD_NOT_ALLOWED, origin); return; } final ChannelBuffer buffer = request.getContent(); if ("/gelf".equals(request.getUri())) { // send on to raw message handler writeResponse(channel, keepAlive, httpRequestVersion, ACCEPTED, origin); fireMessageReceived(ctx, buffer); } else { writeResponse(channel, keepAlive, httpRequestVersion, NOT_FOUND, origin); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != 200) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); HttpHeaders.setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != 200) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.getStatus().getCode() != HttpResponseStatus.OK.getCode()) { res.setContent( ChannelBuffers.copiedBuffer( res.getStatus().toString(), CharsetUtil.UTF_8)); setContentLength(res, res.getContent().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.getChannel().write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != HttpResponseStatus.OK.getCode()) { f.addListener(ChannelFutureListener.CLOSE); } } }
private void _write(String message) { if(!this.open) return; HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); res.addHeader(CONTENT_TYPE, "text/plain; charset=UTF-8"); res.addHeader("Access-Control-Allow-Origin", "*"); res.addHeader("Access-Control-Allow-Credentials", "true"); res.addHeader("Connection", "keep-alive"); res.setContent(ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8)); setContentLength(res, res.getContent().readableBytes()); // Send the response and close the connection if necessary. Channel chan = ctx.getChannel(); if(chan.isOpen()) { ChannelFuture f = chan.write(res); if (!isKeepAlive(req) || res.getStatus().getCode() != 200) { f.addListener(ChannelFutureListener.CLOSE); } } this.connected = false; }