private static final FullHttpResponse http404() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
public final void sendHttpResponse(HttpRequest req, ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); resp.headers().add("Content-Length", "0"); final boolean closeConn = ((status != OK) || (! HttpUtil.isKeepAlive(req))); if (closeConn) { resp.headers().add(HttpHeaderNames.CONNECTION, "Close"); } final ChannelFuture cf = ctx.channel().writeAndFlush(resp); if (closeConn) { cf.addListener(ChannelFutureListener.CLOSE); } }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", StandardCharsets.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
@Override public void failure(Throwable error) { ctx.writeAndFlush(new DefaultFullHttpResponse(req.protocolVersion(), HttpResponseStatus.INTERNAL_SERVER_ERROR)) .addListener(ChannelFutureListener.CLOSE); }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { var response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
/** * 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); } }
/** * @param conversionService The conversion service */ @SuppressWarnings("MagicNumber") public NettyMutableHttpResponse(ConversionService conversionService) { this.nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); this.headers = new NettyHttpHeaders(nettyResponse.headers(), conversionService); this.attributes = new MutableConvertibleValuesMap<>(new ConcurrentHashMap<>(4), conversionService); this.conversionService = conversionService; }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", StandardCharsets.UTF_8)); response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8"); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof WebSocketHandshakeException) { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.wrappedBuffer(cause.getMessage().getBytes())); ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.fireExceptionCaught(cause); ctx.close(); } }
private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) { var response = new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8)); response.headers().set(HttpConst.CONTENT_TYPE, Const.CONTENT_TYPE_TEXT); // Close the connection as soon as the error message is sent. ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
private static final FullHttpResponse http404() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest request, HttpResponseStatus status, PushUserAuth userAuth) { final FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); resp.headers().add("Content-Length", "0"); final ChannelFuture cf = ctx.channel().writeAndFlush(resp); if (!HttpUtil.isKeepAlive(request)) { cf.addListener(ChannelFutureListener.CLOSE); } logPushEvent(request, status, userAuth); }
private static void forbidden(final ChannelHandlerContext ctx, final HttpRequest request) { HttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(), FORBIDDEN); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, HttpHeaderValues.ZERO); release(request); respond(ctx, request, response); }
public FullHttpResponse toFullResponse() { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.valueOf(this.version.text()), HttpResponseStatus.valueOf(this.status)); for (Map.Entry<String, String[]> entry : getHeaders().entrySet()) { String key = entry.getKey(); for (String value : entry.getValue()) { response.headers().add(key, value); } } if (this.content != null) { response.content().writeBytes(this.content.getContent()); } return response; } }
private void writeErrorResponse(int statusCode, String message, ChannelHandlerContext ctx) { FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(statusCode), Unpooled.copiedBuffer(message, Charset.forName(SentinelConfig.charset()))); httpResponse.headers().set("Content-Type", "text/plain; charset=" + SentinelConfig.charset()); ctx.write(httpResponse); ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { if (!ExceptionHandler.isResetByPeer(cause)) { log.error(cause.getMessage(), cause); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(500)); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } }
private static void sendListing(ChannelHandlerContext ctx, String uri, List<FileMeta> listFiles, String dirPath) { var response = new DefaultFullHttpResponse(HTTP_1_1, OK); response.headers().set(HttpConst.CONTENT_TYPE, "text/html; charset=UTF-8"); StringBuilder buf = new StringBuilder() .append("<!DOCTYPE html>\r\n") ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
private static final FullHttpResponse http200(String result) { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(result.getBytes())); HttpHeaders httpHeaders = response.headers(); httpHeaders.set(HttpHeaders.Names.CONTENT_TYPE, "text/plain"); httpHeaders.set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes()); return response; }
public final void sendHttpResponse(HttpRequest req, ChannelHandlerContext ctx, HttpResponseStatus status) { FullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, status); resp.headers().add("Content-Length", "0"); final boolean closeConn = ((status != OK) || (! HttpUtil.isKeepAlive(req))); if (closeConn) { resp.headers().add(HttpHeaderNames.CONNECTION, "Close"); } final ChannelFuture cf = ctx.channel().writeAndFlush(resp); if (closeConn) { cf.addListener(ChannelFutureListener.CLOSE); } }
private FullHttpResponse createResponseByByteBuf(Response response, ByteBuf byteBuf) { Map<String, String> headers = response.headers(); var httpResponse = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.valueOf(response.statusCode()), byteBuf); httpResponse.headers().set(CONTENT_LENGTH, httpResponse.content().readableBytes()); setDefaultHeaders(httpResponse.headers()); if (response.cookiesRaw().size() > 0) { this.appendCookie(response, httpResponse); } for (Map.Entry<String, String> next : headers.entrySet()) { httpResponse.headers().set(HttpConst.getAsciiString(next.getKey()), next.getValue()); } return httpResponse; }