HTTP is working, still need to do HTTPS though

This commit is contained in:
md_5 2013-07-04 11:32:36 +10:00
parent 87884ad084
commit 2cbea83c02
3 changed files with 64 additions and 13 deletions

View File

@ -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() );
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 );
}
}

View File

@ -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<HttpObject>
{
@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();
}
}
}
}

View File

@ -26,5 +26,6 @@ public class HttpInitializer extends ChannelInitializer<Channel>
ch.pipeline().addLast( "ssl", new SslHandler( engine ) );
}
ch.pipeline().addLast( "http", new HttpClientCodec() );
ch.pipeline().addLast( "handler", new HttpHandler() );
}
}