use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project vert.x by eclipse.
the class Http1xClientConnection method toWebSocket.
synchronized void toWebSocket(ContextInternal context, String requestURI, MultiMap headers, boolean allowOriginHeader, WebsocketVersion vers, List<String> subProtocols, int maxWebSocketFrameSize, Promise<WebSocket> promise) {
try {
URI wsuri = new URI(requestURI);
if (!wsuri.isAbsolute()) {
// Netty requires an absolute url
wsuri = new URI((ssl ? "https:" : "http:") + "//" + server.host() + ":" + server.port() + requestURI);
}
WebSocketVersion version = WebSocketVersion.valueOf((vers == null ? WebSocketVersion.V13 : vers).toString());
HttpHeaders nettyHeaders;
if (headers != null) {
nettyHeaders = new DefaultHttpHeaders();
for (Map.Entry<String, String> entry : headers) {
nettyHeaders.add(entry.getKey(), entry.getValue());
}
} else {
nettyHeaders = null;
}
ChannelPipeline p = chctx.channel().pipeline();
ArrayList<WebSocketClientExtensionHandshaker> extensionHandshakers = initializeWebSocketExtensionHandshakers(client.getOptions());
if (!extensionHandshakers.isEmpty()) {
p.addBefore("handler", "webSocketsExtensionsHandler", new WebSocketClientExtensionHandler(extensionHandshakers.toArray(new WebSocketClientExtensionHandshaker[0])));
}
String subp = null;
if (subProtocols != null) {
subp = String.join(",", subProtocols);
}
WebSocketClientHandshaker handshaker = newHandshaker(wsuri, version, subp, !extensionHandshakers.isEmpty(), allowOriginHeader, nettyHeaders, maxWebSocketFrameSize, !options.isSendUnmaskedFrames());
WebSocketHandshakeInboundHandler handshakeInboundHandler = new WebSocketHandshakeInboundHandler(handshaker, ar -> {
AsyncResult<WebSocket> wsRes = ar.map(v -> {
WebSocketImpl w = new WebSocketImpl(context, Http1xClientConnection.this, version != WebSocketVersion.V00, options.getWebSocketClosingTimeout(), options.getMaxWebSocketFrameSize(), options.getMaxWebSocketMessageSize());
w.subProtocol(handshaker.actualSubprotocol());
return w;
});
if (ar.failed()) {
close();
} else {
webSocket = (WebSocketImpl) wsRes.result();
webSocket.registerHandler(vertx.eventBus());
log.debug("WebSocket handshake complete");
HttpClientMetrics metrics = client.metrics();
if (metrics != null) {
webSocket.setMetric(metrics.connected(webSocket));
}
}
getContext().emit(wsRes, res -> {
if (res.succeeded()) {
webSocket.headers(ar.result());
}
promise.handle(res);
if (res.succeeded()) {
webSocket.headers(null);
}
});
});
p.addBefore("handler", "handshakeCompleter", handshakeInboundHandler);
handshaker.handshake(chctx.channel());
} catch (Exception e) {
handleException(e);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project vert.x by eclipse.
the class Http1xClientConnection method createRequest.
private HttpRequest createRequest(HttpMethod method, String uri, MultiMap headerMap, String authority, boolean chunked, ByteBuf buf, boolean end) {
HttpRequest request = new DefaultHttpRequest(HttpUtils.toNettyHttpVersion(version), method.toNetty(), uri, false);
HttpHeaders headers = request.headers();
if (headerMap != null) {
for (Map.Entry<String, String> header : headerMap) {
headers.add(header.getKey(), header.getValue());
}
}
if (!headers.contains(HOST)) {
request.headers().set(HOST, authority);
} else {
headers.remove(TRANSFER_ENCODING);
}
if (chunked) {
HttpUtil.setTransferEncodingChunked(request, true);
}
if (options.isTryUseCompression() && request.headers().get(ACCEPT_ENCODING) == null) {
// if compression should be used but nothing is specified by the user support deflate and gzip.
request.headers().set(ACCEPT_ENCODING, DEFLATE_GZIP);
}
if (!options.isKeepAlive() && options.getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_1) {
request.headers().set(CONNECTION, CLOSE);
} else if (options.isKeepAlive() && options.getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_0) {
request.headers().set(CONNECTION, KEEP_ALIVE);
}
if (end) {
if (buf != null) {
request = new AssembledFullHttpRequest(request, buf);
} else {
request = new AssembledFullHttpRequest(request);
}
} else if (buf != null) {
request = new AssembledHttpRequest(request, buf);
}
return request;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project graylog2-server by Graylog2.
the class HttpHandlerTest method withCustomContentType.
@Test
public void withCustomContentType() {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "foo/bar");
httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
httpRequest.content().writeBytes(GELF_MESSAGE);
channel.writeInbound(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.readOutbound();
assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project graylog2-server by Graylog2.
the class HttpHandlerTest method withJSONContentType.
@Test
public void withJSONContentType() {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
httpRequest.content().writeBytes(GELF_MESSAGE);
channel.writeInbound(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.readOutbound();
assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpHeaders in project graylog2-server by Graylog2.
the class HttpHandlerTest method withKeepalive.
@Test
public void withKeepalive() {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/gelf");
httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
httpRequest.content().writeBytes(GELF_MESSAGE);
channel.writeInbound(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.readOutbound();
assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.KEEP_ALIVE.toString());
}
Aggregations