@Override public String reason() { return this.nettyHttpResponse.status().reasonPhrase(); }
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); } }
@Override public HttpStatus getStatus() { return HttpStatus.valueOf(nettyResponse.status().code()); }
private void setStatusCode(HttpResponse httpResponse, FullHttpResponse fullHttpResponse) { HttpResponseStatus status = fullHttpResponse.status(); httpResponse.withStatusCode(status.code()); httpResponse.withReasonPhrase(status.reasonPhrase()); }
@Override public boolean mustSendImmediately(FullHttpMessage msg) { if (msg instanceof FullHttpResponse) { return ((FullHttpResponse) msg).status().codeClass() == HttpStatusClass.INFORMATIONAL; } if (msg instanceof FullHttpRequest) { return msg.headers().contains(HttpHeaderNames.EXPECT); } return false; }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); LOGGER.debug("WebSocket Client connected!"); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; LOGGER.debug("WebSocket Client received text message: " + textFrame.text()); String textReceived = textFrame.text(); callback.call(textReceived); } else if (frame instanceof CloseWebSocketFrame) { isConnected = false; LOGGER.debug("WebSocket Client received closing"); ch.close(); } }
@Override public void write(HttpRequest<?> request, MutableHttpResponse<?> response, ChannelHandlerContext context) { if (response instanceof NettyMutableHttpResponse) { FullHttpResponse nettyResponse = ((NettyMutableHttpResponse) response).getNativeResponse(); // Write the request data context.write(new DefaultHttpResponse(nettyResponse.protocolVersion(), nettyResponse.status(), nettyResponse.headers()), context.voidPromise()); context.writeAndFlush(new HttpChunkedInput(new ChunkedStream(getInputStream()))); } else { throw new IllegalArgumentException("Unsupported response type. Not a Netty response: " + response); } } }
context.write(new DefaultHttpResponse(nettyResponse.protocolVersion(), nettyResponse.status(), headers), context.voidPromise());
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) { Channel ch = ctx.channel(); if (msg instanceof FullHttpResponse) { FullHttpResponse httpResponse = (FullHttpResponse) msg; final SettableFuture<String> registrationFuture = ch.attr(REGISTRATION_FUTURE).get(); if (httpResponse.headers().contains(UPGRADE, WEBSOCKET, true) && !handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, httpResponse); final String clientRegistrationId = httpResponse.headers().get("X-CLIENT-REGISTRATION-ID"); registrationFuture.set(clientRegistrationId); mockServerLogger.trace("web socket client " + clientRegistrationId + " connected!"); } else if (httpResponse.status().equals(HttpResponseStatus.NOT_IMPLEMENTED)) { String message = readRequestBody(httpResponse); registrationFuture.setException(new WebSocketException(message)); mockServerLogger.warn(message); } else { registrationFuture.setException(new WebSocketException("Unsupported web socket message " + new FullHttpResponseToMockServerResponse().mapMockServerResponseToFullHttpResponse(httpResponse))); } } else if (msg instanceof WebSocketFrame) { WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { webSocketClient.receivedTextWebSocketFrame((TextWebSocketFrame) frame); } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.content().retain())); } else if (frame instanceof CloseWebSocketFrame) { mockServerLogger.trace("web socket client received request to close"); ch.close(); } } }
Argument<B> bodyType, boolean errorStatus) { this.status = HttpStatus.valueOf(fullHttpResponse.status().code()); this.headers = new NettyHttpHeaders(fullHttpResponse.headers(), ConversionService.SHARED); this.attributes = new MutableConvertibleValuesMap<>();
public static HttpResponse newResponse(final FullHttpResponse response) { return builder() .withVersion(toHttpProtocolVersion(response.protocolVersion())) .withStatus(response.status().code()) .withHeaders(toHeaders(response)) .withContent(content() .withContent(new ByteBufInputStream(response.content())) .build()) .build(); }
NettyByteBuffer data = new NettyByteBuffer(content); try { if (msg.status() == HttpResponseStatus.OK) {
NettyByteBuffer data = new NettyByteBuffer(content); try { if (msg.status() == HttpResponseStatus.OK) {
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); } }
@Override protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { final Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { // web socket client connected handshaker.finishHandshake(ch, (FullHttpResponse) msg); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) msg; throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } // a close frame doesn't mean much here. errors raised from closed channels will mark the host as dead final WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof PingWebSocketFrame) { ctx.writeAndFlush(new PongWebSocketFrame()); }else if (frame instanceof PongWebSocketFrame) { logger.debug("Received response from keep-alive request"); } else if (frame instanceof BinaryWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof CloseWebSocketFrame) ch.close(); }
final FullHttpResponse response = (FullHttpResponse) msg; httpReceive.setStatusCode(response.status().code()) .setStatusText(response.status().codeAsText().toString()); .setErrMsg(cause.getMessage()) .setThrowable(cause); } else if (response.status().code() != 200) { httpReceive.setHaveError(true) .setErrMsg("本次请求响应码不是200,是" + response.status().code());