use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project weicoder by wdcode.
the class WebSocketHandler method http.
/*
* http请求
*/
private void http(ChannelHandlerContext ctx, FullHttpRequest req) {
// 请求不成功
if (!req.decoderResult().isSuccess()) {
send(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
// 只允许get请求
if (req.method() != HttpMethod.GET) {
send(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN));
return;
}
// 处理websocket
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(String.format(WEBSOCKET_PATH, req.headers().get(HttpHeaderNames.HOST)), null, true, 5 * 1024 * 1024);
WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
} else {
handshaker.handshake(ctx.channel(), req);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project java by wavefrontHQ.
the class ChannelUtils method makeResponse.
/**
* Create {@link FullHttpResponse} based on provided status and body contents.
*
* @param status response status.
* @param contents response body.
* @return http response object
*/
public static HttpResponse makeResponse(final HttpResponseStatus status, final Object contents) {
final FullHttpResponse response;
if (contents instanceof JsonNode) {
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer(contents.toString(), CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
} else if (contents instanceof CharSequence) {
response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer((CharSequence) contents, CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_PLAIN);
} else {
throw new IllegalArgumentException("Unexpected response content type, JsonNode or " + "CharSequence expected: " + contents.getClass().getName());
}
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project proxyee-down by monkeyWie.
the class HttpHandlerUtil method buildJson.
public static FullHttpResponse buildJson(Object obj, Include include) {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, AsciiString.cached("application/json; charset=utf-8"));
if (obj != null) {
try {
ObjectMapper objectMapper = new ObjectMapper();
if (include != null) {
objectMapper.setSerializationInclusion(include);
}
String content = objectMapper.writeValueAsString(obj);
response.content().writeBytes(content.getBytes(Charset.forName("utf-8")));
} catch (JsonProcessingException e) {
response.setStatus(HttpResponseStatus.SERVICE_UNAVAILABLE);
}
}
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project reactor-netty by reactor.
the class HttpClientTest method prematureCancel.
@Test
void prematureCancel() {
Sinks.Many<Void> signal = Sinks.unsafe().many().unicast().onBackpressureError();
disposableServer = TcpServer.create().host("localhost").port(0).handle((in, out) -> {
signal.tryEmitComplete().orThrow();
return out.withConnection(c -> c.addHandlerFirst(new HttpResponseEncoder())).sendObject(Mono.delay(Duration.ofSeconds(2)).map(t -> new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.PROCESSING))).neverComplete();
}).wiretap(true).bindNow(Duration.ofSeconds(30));
StepVerifier.create(createHttpClientForContextWithAddress().get().uri("/").responseContent().timeout(signal.asFlux())).verifyError(TimeoutException.class);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project reactor-netty by reactor.
the class HttpServerOperations method sendDecodingFailures.
static void sendDecodingFailures(ChannelHandlerContext ctx, ConnectionObserver listener, boolean secure, Throwable t, Object msg) {
Connection conn = Connection.from(ctx.channel());
Throwable cause = t.getCause() != null ? t.getCause() : t;
if (log.isWarnEnabled()) {
log.warn(format(ctx.channel(), "Decoding failed: " + msg + " : "), cause);
}
ReferenceCountUtil.release(msg);
HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, cause instanceof TooLongFrameException ? HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE : HttpResponseStatus.BAD_REQUEST);
response.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, 0).set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
HttpRequest request = null;
if (msg instanceof HttpRequest) {
request = (HttpRequest) msg;
}
listener.onStateChange(new FailedHttpServerRequest(conn, listener, request, response, secure), REQUEST_DECODING_FAILED);
}
Aggregations