/** * @deprecated Use {@link HttpUtil#setContentLength(HttpMessage, long)} instead. */ @Deprecated public static void setContentLength(HttpMessage message, long length) { HttpUtil.setContentLength(message, length); }
static void sendError(Channel ch, HttpResponseStatus status, CharSequence err) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); if (status.code() == METHOD_NOT_ALLOWED.code()) { // SockJS requires this resp.headers().set(io.vertx.core.http.HttpHeaders.ALLOW, io.vertx.core.http.HttpHeaders.GET); } if (err != null) { resp.content().writeBytes(err.toString().getBytes(CharsetUtil.UTF_8)); HttpUtil.setContentLength(resp, err.length()); } else { HttpUtil.setContentLength(resp, 0); } ch.writeAndFlush(resp); }
private void setContentLengthForKeepAlive(final FullHttpResponse response) { if (!isContentLengthSet(response)) { setContentLength(response, response.content().writerIndex()); } }
/** * Return that we need cannot not support the web socket version */ public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) { HttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); HttpUtil.setContentLength(res, 0); return channel.writeAndFlush(res, promise); } }
/** * Set final headers and fire a channel read event * * @param ctx The context to fire the event on * @param msg The message to send * @param release {@code true} to call release on the value if it is present. {@code false} to not call release. * @param stream the stream of the message which is being fired */ protected void fireChannelRead(ChannelHandlerContext ctx, FullHttpMessage msg, boolean release, Http2Stream stream) { removeMessage(stream, release); HttpUtil.setContentLength(msg, msg.content().readableBytes()); ctx.fireChannelRead(msg); }
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
protected int sendHttp1Response(ChannelHandlerContext ctx, HttpResponseStatus status, String resultStr, boolean isKeepAlive) { ByteBuf content = Unpooled.copiedBuffer(resultStr, RpcConstants.DEFAULT_CHARSET); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, status, content); res.headers().set(CONTENT_TYPE, "text/html; charset=" + RpcConstants.DEFAULT_CHARSET.displayName()); HttpUtil.setContentLength(res, content.readableBytes()); try { ChannelFuture f = ctx.channel().writeAndFlush(res); if (isKeepAlive) { HttpUtil.setKeepAlive(res, true); } else { HttpUtil.setKeepAlive(res, false); //set keepalive closed f.addListener(ChannelFutureListener.CLOSE); } } catch (Exception e2) { LOGGER.warn("Failed to send HTTP response to remote, cause by:", e2); } return content.readableBytes(); }
protected int sendHttp1Response(ChannelHandlerContext ctx, HttpResponseStatus status, String resultStr, boolean isKeepAlive) { ByteBuf content = Unpooled.copiedBuffer(resultStr, RpcConstants.DEFAULT_CHARSET); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, status, content); res.headers().set(CONTENT_TYPE, "text/html; charset=" + RpcConstants.DEFAULT_CHARSET.displayName()); HttpUtil.setContentLength(res, content.readableBytes()); try { ChannelFuture f = ctx.channel().writeAndFlush(res); if (isKeepAlive) { HttpUtil.setKeepAlive(res, true); } else { HttpUtil.setKeepAlive(res, false); //set keepalive closed f.addListener(ChannelFutureListener.CLOSE); } } catch (Exception e2) { LOGGER.warn("Failed to send HTTP response to remote, cause by:", e2); } return content.readableBytes(); }
private void encodeFullResponse(HttpResponse newRes, HttpContent content, List<Object> out) { int existingMessages = out.size(); encodeContent(content, out); if (HttpUtil.isContentLengthSet(newRes)) { // adjust the content-length header int messageSize = 0; for (int i = existingMessages; i < out.size(); i++) { Object item = out.get(i); if (item instanceof HttpContent) { messageSize += ((HttpContent) item).content().readableBytes(); } } HttpUtil.setContentLength(newRes, messageSize); } else { newRes.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); } }
private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, String type, ChannelPromise promise, HttpResponseStatus status) { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, status); res.headers().add(HttpHeaderNames.CONTENT_TYPE, type) .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); if (msg.getSessionId() != null) { res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId()); } String origin = channel.attr(ORIGIN).get(); addOriginHeaders(origin, res); HttpUtil.setContentLength(res, out.readableBytes()); // prevent XSS warnings on IE // https://github.com/LearnBoost/socket.io/pull/1333 String userAgent = channel.attr(EncoderHandler.USER_AGENT).get(); if (userAgent != null && (userAgent.contains(";MSIE") || userAgent.contains("Trident/"))) { res.headers().add("X-XSS-Protection", "0"); } sendMessage(msg, channel, out, res, promise); }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { // Handle a bad request. if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return; } // Allow only GET methods. if (req.method() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } // Send the index page if ("/".equals(req.uri()) || "/index.html".equals(req.uri())) { ByteBuf content = getContent(); FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); HttpUtil.setContentLength(res, content.readableBytes()); sendHttpResponse(ctx, req, res); } else { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND)); } ctx.pipeline().remove(this); }
HttpUtil.setContentLength(response, content.length()); response.headers().set(CONTENT_TYPE, "text/html");
private void handleWebSocketResponse(ChannelHandlerContext ctx, Outgoing out) { WebSocketHttpResponse response = (WebSocketHttpResponse) out.message; WebSocketServerHandshaker handshaker = response.handshakerFactory().newHandshaker(lastRequest); if (handshaker == null) { HttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); HttpUtil.setContentLength(res, 0); super.unbufferedWrite(ctx, new Outgoing(res, out.promise)); response.subscribe(new CancelledSubscriber<>()); } else { // First, insert new handlers in the chain after us for handling the websocket ChannelPipeline pipeline = ctx.pipeline(); HandlerPublisher<WebSocketFrame> publisher = new HandlerPublisher<>(ctx.executor(), WebSocketFrame.class); HandlerSubscriber<WebSocketFrame> subscriber = new HandlerSubscriber<>(ctx.executor()); pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-subscriber", subscriber); pipeline.addAfter(ctx.executor(), ctx.name(), "websocket-publisher", publisher); // Now remove ourselves from the chain ctx.pipeline().remove(ctx.name()); // Now do the handshake // Wrap the request in an empty request because we don't need the WebSocket handshaker ignoring the body, // we already have handled the body. handshaker.handshake(ctx.channel(), new EmptyHttpRequest(lastRequest)); // And hook up the subscriber/publishers response.subscribe(subscriber); publisher.subscribe(response); } }
HttpUtil.setContentLength(httpResponseWithEntity, 0); out.add(httpResponseWithEntity); } else { HttpUtil.setContentLength(fullHttpMessage, 0); out.add(fullHttpMessage); } else { HttpUtil.setContentLength(fullHttpMessage, fullHttpMessage.content().readableBytes()); removeMessage(streamId); out.add(fullHttpMessage); HttpUtil.setContentLength(fullHttpMessage, content.readableBytes()); removeMessage(streamId); out.add(fullHttpMessage);
HttpUtil.setContentLength(response, fileLength); setContentTypeHeader(response, file); setDateAndCacheHeaders(response, file);
static void sendError(Channel ch, HttpResponseStatus status, CharSequence err) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); if (status.code() == METHOD_NOT_ALLOWED.code()) { // SockJS requires this resp.headers().set(io.vertx.core.http.HttpHeaders.ALLOW, io.vertx.core.http.HttpHeaders.GET); } if (err != null) { resp.content().writeBytes(err.toString().getBytes(CharsetUtil.UTF_8)); HttpUtil.setContentLength(resp, err.length()); } else { HttpUtil.setContentLength(resp, 0); } ch.writeAndFlush(resp); }
private static void sendHttpResponse( ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); HttpUtil.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
/** * @deprecated Use {@link HttpUtil#setContentLength(HttpMessage, long)} instead. */ @Deprecated public static void setContentLength(HttpMessage message, long length) { HttpUtil.setContentLength(message, length); }
/** * Return that we need cannot not support the web socket version */ public static ChannelFuture sendUnsupportedVersionResponse(Channel channel, ChannelPromise promise) { HttpResponse res = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(HttpHeaderNames.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); HttpUtil.setContentLength(res, 0); return channel.writeAndFlush(res, promise); } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, FullHttpResponse res) { // Generate an error page if response status code is not OK (200). if (res.status().code() != 200) { res.content().writeBytes(res.status().toString().getBytes(StandardCharsets.UTF_8)); setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }