public void run() { String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); String host = uri.getHost() == null ? "localhost" : uri.getHost(); int port = uri.getPort(); if (port == -1) { if (scheme.equalsIgnoreCase("http")) { port = 80; } else if (scheme.equalsIgnoreCase("https")) { port = 443; } } if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) { logger.error("Only HTTP(S) is supported."); return; } boolean ssl = scheme.equalsIgnoreCase("https"); // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool())); // Set up the event pipeline factory. bootstrap.setPipelineFactory(new HttpSnoopClientPipelineFactory(ssl)); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().getChannel(); if (!future.isSuccess()) { future.getCause().printStackTrace(); bootstrap.releaseExternalResources(); return; } // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); request.setHeader(HttpHeaders.Names.HOST, host); request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); // Set some example cookies. CookieEncoder httpCookieEncoder = new CookieEncoder(false); httpCookieEncoder.addCookie("my-cookie", "foo"); httpCookieEncoder.addCookie("another-cookie", "bar"); request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); // Send the HTTP request. channel.write(request); // Wait for the server to close the connection. channel.getCloseFuture().awaitUninterruptibly(); // Shut down executor threads to exit. bootstrap.releaseExternalResources(); }
private static HttpRequest createRequestTemplate(String host, String tunnelId, String uri) { HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, createCompleteUri(host, uri)); request.setHeader(HttpHeaders.Names.HOST, host); request.setHeader(HttpHeaders.Names.USER_AGENT, USER_AGENT); if (tunnelId != null) { request.setHeader(HttpHeaders.Names.COOKIE, tunnelId); } return request; }
public static HttpRequest createSendDataRequest(String host, String cookie, ChannelBuffer data) { HttpRequest request = createRequestTemplate(host, cookie, CLIENT_SEND_REQUEST_URI); request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, Long.toString(data.readableBytes())); request.setContent(data); return request; }
@Test public void testPerformOpeningHandshake() { Channel channelMock = EasyMock.createMock(Channel.class); DefaultChannelPipeline pipeline = createPipeline(channelMock); EasyMock.expect(channelMock.pipeline()).andReturn(pipeline); // capture the http response in order to verify the headers Capture<HttpResponse> res = new Capture<HttpResponse>(); EasyMock.expect(channelMock.write(capture(res))) .andReturn(new DefaultChannelFuture(channelMock, true)); replay(channelMock); HttpRequest req = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/chat"); req.setHeader(Names.HOST, "server.example.com"); req.setHeader(Names.UPGRADE, WEBSOCKET.toLowerCase()); req.setHeader(Names.CONNECTION, "Upgrade"); req.setHeader(Names.SEC_WEBSOCKET_KEY, "dGhlIHNhbXBsZSBub25jZQ=="); req.setHeader(Names.SEC_WEBSOCKET_ORIGIN, "http://example.com"); req.setHeader(Names.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); req.setHeader(Names.SEC_WEBSOCKET_VERSION, "13"); WebSocketServerHandshaker13 handsaker = new WebSocketServerHandshaker13("ws://example.com/chat", "chat", false, Integer.MAX_VALUE); handsaker.handshake(channelMock, req); Assert.assertEquals( "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=", res.getValue().getHeader(Names.SEC_WEBSOCKET_ACCEPT)); Assert.assertEquals("chat", res.getValue().getHeader(Names.SEC_WEBSOCKET_PROTOCOL)); }
private static void setNoData(HttpRequest request) { request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0"); request.setContent(null); }