use of io.netty.handler.codec.http.DefaultHttpRequest in project Glowstone by GlowstoneMC.
the class HttpClient method connect.
/**
* Opens a URL.
*
* @param url the URL to download
* @param eventLoop an {@link EventLoop} that will receive the response body
* @param callback a callback to handle the response or any error
*/
public void connect(String url, EventLoop eventLoop, HttpCallback callback) {
URI uri = URI.create(url);
String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
int port = uri.getPort();
SslContext sslCtx = null;
if ("https".equalsIgnoreCase(scheme)) {
if (port == -1) {
port = 443;
}
try {
sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} catch (SSLException e) {
callback.error(e);
return;
}
} else if ("http".equalsIgnoreCase(scheme)) {
if (port == -1) {
port = 80;
}
} else {
throw new IllegalArgumentException("Only http(s) is supported!");
}
new Bootstrap().group(eventLoop).resolver(resolverGroup).channel(Networking.bestSocketChannel()).handler(new HttpChannelInitializer(sslCtx, callback)).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000).connect(InetSocketAddress.createUnresolved(host, port)).addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
String path = uri.getRawPath() + (uri.getRawQuery() == null ? "" : "?" + uri.getRawQuery());
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
request.headers().set(HttpHeaderNames.HOST, host);
future.channel().writeAndFlush(request);
} else {
callback.error(future.cause());
}
});
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project crate by crate.
the class HttpAuthUpstreamHandlerTest method testNotNoHbaConfig.
@Test
public void testNotNoHbaConfig() throws Exception {
HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY, authService);
EmbeddedChannel ch = new EmbeddedChannel(handler);
DefaultHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
request.headers().add(HttpHeaderNames.AUTHORIZATION.toString(), "Basic QWxhZGRpbjpPcGVuU2VzYW1l");
request.headers().add("X-Real-Ip", "10.1.0.100");
ch.writeInbound(request);
ch.releaseInbound();
assertFalse(handler.authorized());
assertUnauthorized(ch.readOutbound(), "No valid auth.host_based.config entry found for host \"10.1.0.100\", user \"Aladdin\", protocol \"http\". Did you enable TLS in your client?\n");
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project crate by crate.
the class HttpAuthUpstreamHandlerTest method testAuthorized.
@Test
public void testAuthorized() throws Exception {
HttpAuthUpstreamHandler handler = new HttpAuthUpstreamHandler(Settings.EMPTY, new AlwaysOKAuthentication(userName -> User.CRATE_USER));
EmbeddedChannel ch = new EmbeddedChannel(handler);
DefaultHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/_sql");
ch.writeInbound(request);
ch.releaseInbound();
assertThat(handler.authorized(), is(true));
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project graylog2-server by Graylog2.
the class HttpHandlerTest method successfullyProcessOPTIONSRequest.
@Test
public void successfullyProcessOPTIONSRequest() {
final HttpRequest httpRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.OPTIONS, "/gelf");
httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
httpRequest.headers().add(HttpHeaderNames.ORIGIN, "http://example.com");
httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
channel.writeInbound(httpRequest);
channel.finish();
final HttpResponse httpResponse = channel.readOutbound();
assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.OK);
final HttpHeaders headers = httpResponse.headers();
assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://example.com");
assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS)).isEqualTo("true");
assertThat(headers.get(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS)).isEqualTo("Authorization, Content-Type");
}
use of io.netty.handler.codec.http.DefaultHttpRequest in project rest.li by linkedin.
the class NettyRequestAdapter method toNettyRequest.
/**
* Adapts a StreamRequest to Netty's HttpRequest
* @param request R2 stream request
* @return Adapted HttpRequest.
*/
public static HttpRequest toNettyRequest(StreamRequest request) throws Exception {
HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
URL url = new URL(request.getURI().toString());
String path = url.getFile();
// it MUST be given as "/" (the server root).
if (path.isEmpty()) {
path = "/";
}
HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
nettyRequest.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
// that contains a Transfer-Encoding header field.
if (entry.getKey().equalsIgnoreCase(HttpHeaderNames.CONTENT_LENGTH.toString())) {
continue;
}
nettyRequest.headers().set(entry.getKey(), entry.getValue());
}
nettyRequest.headers().set(HttpHeaderNames.HOST, url.getAuthority());
// RFC 6265
// When the user agent generates an HTTP/1.1 request, the user agent MUST
// NOT attach more than one Cookie header field.
String encodedCookieHeaderValues = CookieUtil.clientEncode(request.getCookies());
if (encodedCookieHeaderValues != null) {
nettyRequest.headers().set(HttpConstants.REQUEST_COOKIE_HEADER_NAME, encodedCookieHeaderValues);
}
return nettyRequest;
}
Aggregations