diff --git a/http/src/main/java/net/md_5/bungee/http/HttpClient.java b/http/src/main/java/net/md_5/bungee/http/HttpClient.java index 9dd1b5a28..5fe1fbbac 100644 --- a/http/src/main/java/net/md_5/bungee/http/HttpClient.java +++ b/http/src/main/java/net/md_5/bungee/http/HttpClient.java @@ -2,7 +2,10 @@ package net.md_5.bungee.http; import com.google.common.base.Preconditions; import io.netty.bootstrap.Bootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders; @@ -10,7 +13,6 @@ import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpVersion; import java.net.URI; -import java.net.URISyntaxException; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @@ -19,20 +21,18 @@ public class HttpClient private final EventLoopGroup eventLoop; + public static void main(String[] args) + { + new HttpClient( new NioEventLoopGroup( 1 ) ).get( "https://session.minecraft.net/" ); + } + public void get(String url) { - URI uri = null; - try - { - uri = new URI( url ); - } catch ( URISyntaxException ex ) - { - throw new IllegalArgumentException( "Could not parse url " + url, ex ); - } + final URI uri = URI.create( url ); Preconditions.checkNotNull( uri.getScheme(), "scheme" ); Preconditions.checkNotNull( uri.getHost(), "host" ); - boolean ssl = false; + boolean ssl = uri.getScheme().equals( "https" ); int port = uri.getPort(); if ( port == -1 ) { @@ -49,9 +49,23 @@ public class HttpClient } } - new Bootstrap().channel( NioSocketChannel.class ).group( eventLoop ).handler( new HttpInitializer( url, port, ssl ) ).remoteAddress( uri.getHost(), port ).connect(); + ChannelFutureListener future = new ChannelFutureListener() + { + @Override + public void operationComplete(ChannelFuture future) throws Exception + { + if ( future.isSuccess() ) + { + HttpRequest request = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath() ); + request.headers().set( HttpHeaders.Names.HOST, uri.getHost() ); - HttpRequest request = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath() ); - request.headers().set( HttpHeaders.Names.HOST, uri.getHost() ); + future.channel().write( request ); + } else + { + } + } + }; + + new Bootstrap().channel( NioSocketChannel.class ).group( eventLoop ).handler( new HttpInitializer( url, port, ssl ) ).remoteAddress( uri.getHost(), port ).connect().addListener( future ); } } diff --git a/http/src/main/java/net/md_5/bungee/http/HttpHandler.java b/http/src/main/java/net/md_5/bungee/http/HttpHandler.java new file mode 100644 index 000000000..dba073b43 --- /dev/null +++ b/http/src/main/java/net/md_5/bungee/http/HttpHandler.java @@ -0,0 +1,36 @@ +package net.md_5.bungee.http; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.handler.codec.http.HttpContent; +import io.netty.handler.codec.http.HttpObject; +import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.netty.handler.codec.http.LastHttpContent; +import java.nio.charset.Charset; + +public class HttpHandler extends SimpleChannelInboundHandler +{ + + @Override + protected void messageReceived(ChannelHandlerContext ctx, HttpObject msg) throws Exception + { + if ( msg instanceof HttpResponse ) + { + HttpResponse response = (HttpResponse) msg; + if ( response.getStatus() != HttpResponseStatus.OK ) + { + } + } + if ( msg instanceof HttpContent ) + { + HttpContent content = (HttpContent) msg; + String s = content.content().toString( Charset.forName( "UTF-8" ) ); + + if ( msg instanceof LastHttpContent ) + { + ctx.channel().close(); + } + } + } +} diff --git a/http/src/main/java/net/md_5/bungee/http/HttpInitializer.java b/http/src/main/java/net/md_5/bungee/http/HttpInitializer.java index c2a09a492..2ff7e6edf 100644 --- a/http/src/main/java/net/md_5/bungee/http/HttpInitializer.java +++ b/http/src/main/java/net/md_5/bungee/http/HttpInitializer.java @@ -26,5 +26,6 @@ public class HttpInitializer extends ChannelInitializer ch.pipeline().addLast( "ssl", new SslHandler( engine ) ); } ch.pipeline().addLast( "http", new HttpClientCodec() ); + ch.pipeline().addLast( "handler", new HttpHandler() ); } }