if (response.status().code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code() && response.headers().get(HttpHeaderNames.UPGRADE).equals(ACTIVEMQ_REMOTING)) { String accept = response.headers().get(SEC_ACTIVEMQ_REMOTING_ACCEPT); String expectedResponse = createExpectedResponse(MAGIC_NUMBER, ctx.channel().attr(REMOTING_KEY).get()); ctx.close(); latch.countDown(); ctx.close();
protected void captureResponseHeaderSize(HttpResponse httpResponse) { String statusLine = httpResponse.getProtocolVersion().toString() + ' ' + httpResponse.getStatus().toString(); // +2 => CRLF after status line, +4 => header/data separation long responseHeadersSize = statusLine.length() + 6; HttpHeaders headers = httpResponse.headers(); responseHeadersSize += BrowserMobHttpUtil.getHeaderSize(headers); harEntry.getResponse().setHeadersSize(responseHeadersSize); }
@Override public FullHttpResponse setStatus(HttpResponseStatus status) { ((HttpResponse) message).setStatus(status); return this; }
@Override protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmpty) { if (!isAlwaysEmpty && method == HttpMethod.CONNECT && msg.status().codeClass() == HttpStatusClass.SUCCESS) { // Stripping Transfer-Encoding: // See https://tools.ietf.org/html/rfc7230#section-3.3.1 msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); return; } super.sanitizeHeadersBeforeEncode(msg, isAlwaysEmpty); }
protected boolean isContentAlwaysEmpty(HttpMessage msg) { if (msg instanceof HttpResponse) { HttpResponse res = (HttpResponse) msg; int code = res.status().code(); // Correctly handle return codes of 1xx. // // See: // - http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html Section 4.4 // - https://github.com/netty/netty/issues/222 if (code >= 100 && code < 200) { // One exception: Hixie 76 websocket handshake response return !(code == 101 && !res.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_ACCEPT) && res.headers().contains(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true)); } switch (code) { case 204: case 304: return true; } } return false; }
@Override protected void sanitizeHeadersBeforeEncode(HttpResponse msg, boolean isAlwaysEmpty) { if (isAlwaysEmpty) { HttpResponseStatus status = msg.status(); if (status.codeClass() == HttpStatusClass.INFORMATIONAL || status.code() == HttpResponseStatus.NO_CONTENT.code()) { // Stripping Content-Length: // See https://tools.ietf.org/html/rfc7230#section-3.3.2 msg.headers().remove(HttpHeaderNames.CONTENT_LENGTH); // Stripping Transfer-Encoding: // See https://tools.ietf.org/html/rfc7230#section-3.3.1 msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); } else if (status.code() == HttpResponseStatus.RESET_CONTENT.code()) { // Stripping Transfer-Encoding: msg.headers().remove(HttpHeaderNames.TRANSFER_ENCODING); // Set Content-Length: 0 // https://httpstatuses.com/205 msg.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0); } } }
private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { ctx.pipeline().addBefore(BridgeRuntime.class.getName(), "encoder", new HttpResponseEncoder()); // Generate an error page if response status code is not OK (200). if (res.getStatus().code() != 200) { FullHttpResponse response = (FullHttpResponse) res; response.content().writeBytes(Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8)); io.netty.handler.codec.http.HttpUtil.setContentLength(res, response.content().readableBytes()); io.netty.handler.codec.http.HttpUtil.setKeepAlive(res, false); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.getStatus().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
for (Map.Entry<String, String> entry : response.headers().entries()) { jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue()); if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) || HttpUtil.isTransferEncodingChunked(response)) { ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(Future<? super Void> future) throws Exception { if (content.isReadable()) { byte[] bytes = new byte[content.readableBytes()]; content.getBytes(content.readerIndex(), bytes); isList.add(new ByteArrayInputStream(bytes));
switch (response.status().code()) { case 200: case 201: case 204: ctx.fireChannelRead(byteBuf); break; default: errorBody.writeBytes(byteBuf); try { switch (response.status().code()) { case 101: case 200: case 301: case 302: if (response.headers().contains(HttpHeaderNames.LOCATION)) { String location = response.headers().get(HttpHeaderNames.LOCATION); HttpRequest redirected = requestProvider.getHttpRequest(location); ctx.channel().writeAndFlush(redirected); throw new InternalServerErrorException(getBodyAsMessage(errorBody)); default: throw new DockerException(getBodyAsMessage(errorBody), response.status().code());
HttpUtil.setContentLength(response, content.length()); response.headers().set(CONTENT_TYPE, "text/html"); if (HttpUtil.isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); ByteBuf buf = ctx.alloc().buffer(content.length()); buf.writeBytes(content.getBytes()); ctx.write(buf); lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!HttpUtil.isKeepAlive(request)) {
if (msg instanceof HttpResponse) { HttpResponse resp = (HttpResponse) msg; HttpResponseStatus status = resp.status(); if (status.code() != 101) { handshaker = null; close(); handleException(new WebsocketRejectedException(status.code())); return; response = new DefaultFullHttpResponse(resp.protocolVersion(), status); response.headers().add(resp.headers()); response.content().writeBytes(((HttpContent) msg).content()); if (msg instanceof LastHttpContent) { response.trailingHeaders().add(((LastHttpContent) msg).trailingHeaders()); try { handshakeComplete(ctx, response); chctx.pipeline().remove(HandshakeInboundHandler.this); for (;;) { Object m = buffered.poll(); break; ctx.fireChannelRead(m);
if (this.contentSizeThreshold > 0) { if (headers instanceof HttpContent && ((HttpContent) headers).content().readableBytes() < contentSizeThreshold) { return null; String contentEncoding = headers.headers().get(HttpHeaderNames.CONTENT_ENCODING); if (contentEncoding != null) { new EmbeddedChannel(ctx.channel().id(), ctx.channel().metadata().hasDisconnect(), ctx.channel().config(), ZlibCodecFactory.newZlibEncoder( wrapper, compressionLevel, windowBits, memLevel)));
private static void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) { ChannelFuture f = ctx.channel().writeAndFlush(res); if (!isKeepAlive(req) || res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
@Override public void onResponse(HttpResponse httpResponse) { HttpResponseMessage response = HttpResponseMessage .from(request) .setStatusCode(httpResponse.status().code()) .setReasonPhrase(httpResponse.status().reasonPhrase()); for (Map.Entry<String, String> entry : httpResponse.headers()) { response.addHeader(entry.getKey(), entry.getValue()); } if (httpResponse instanceof FullHttpResponse) { ByteBuf content = ((FullHttpResponse) httpResponse).content(); if (content != null && content.readableBytes() > 0) { byte[] body = new byte[content.readableBytes()]; content.readBytes(body); response.body = body; response.addHeader(CONTENT_LENGTH.toString(), Integer.toString(response.body.length)); } } response.send(); Logs.HTTP.info("send proxy request success end request={}, response={}", request, response); }
if (msg instanceof HttpResponse) { final HttpResponse nettyRes = (HttpResponse) msg; final DecoderResult decoderResult = nettyRes.decoderResult(); if (!decoderResult.isSuccess()) { fail(ctx, new ProtocolViolationException(decoderResult.cause())); if (!HttpUtil.isKeepAlive(nettyRes)) { disconnectWhenFinished(); if (nettyRes.status().codeClass() == HttpStatusClass.INFORMATIONAL) { state = State.NEED_INFORMATIONAL_DATA; } else { res.scheduleTimeout(channel().eventLoop()); res.write(ArmeriaHttpUtil.toArmeria(nettyRes)); } else { final int dataLength = data.readableBytes(); if (dataLength > 0) { assert res != null; if (!trailingHeaders.isEmpty()) { res.write(ArmeriaHttpUtil.toArmeria(trailingHeaders)); ctx.close();
String ifModifiedSince = request.headers().get(IF_MODIFIED_SINCE); if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) { SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US); HttpUtil.setContentLength(response, fileLength); setContentTypeHeader(response, file); setDateAndCacheHeaders(response, file); if (HttpUtil.isKeepAlive(request)) { response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); if (ctx.pipeline().get(SslHandler.class) == null) { sendFileFuture = ctx.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength), ctx.newProgressivePromise()); lastContentFuture = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT); if (!HttpUtil.isKeepAlive(request)) {
if (message != null) responseStatus = new HttpResponseStatus(status, message); setStatus(status); responseStatus = HttpResponseStatus.valueOf(status); setStatus(status); if (message != null) ByteBuf byteBuf = ctx.alloc().buffer(); byteBuf.writeBytes(message.getBytes()); response.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); if (message == null) response.headers().add(HttpHeaderNames.CONTENT_LENGTH, 0); else response.headers().add(HttpHeaderNames.CONTENT_LENGTH, message.getBytes().length); ctx.writeAndFlush(response); committed = true;
private void write(XHROptionsMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK); res.headers().add(HttpHeaderNames.SET_COOKIE, "io=" + msg.getSessionId()) .add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE) .add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, HttpHeaderNames.CONTENT_TYPE); String origin = ctx.channel().attr(ORIGIN).get(); addOriginHeaders(origin, res); ByteBuf out = encoder.allocateBuffer(ctx.alloc()); sendMessage(msg, ctx.channel(), out, res, promise); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (needsToFilterUpgradeResponse && msg instanceof HttpResponse) { needsToFilterUpgradeResponse = false; final HttpResponse res = (HttpResponse) msg; if (res.status().code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code()) { final HttpHeaders headers = res.headers(); if (!headers.contains(HttpHeaderNames.UPGRADE)) { headers.set(HttpHeaderNames.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME); } } if (!needsToFilterUpgradeRequest) { ctx.pipeline().remove(this); } } ctx.fireChannelRead(msg); }