response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); final int failAt = RANDOM.nextInt(allBytes.length);
@Test public void simpleMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); long chunkNum = 0; while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk, ++chunkNum); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(RANDOM.nextInt(128), allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void simpleSingleStreamTest() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent(new BigEndianHeapChannelBuffer(allBytes)); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(RANDOM.nextInt(128), allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void alignedMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); long chunkNum = 0; while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk, ++chunkNum); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); for (byte[] bytes : BYTE_LIST) { final byte[] expectedBytes = new byte[bytes.length]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); Assert.assertArrayEquals(expectedBytes, bytes); } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test(expected = TesterException.class) public void testExceptionalSingleStream() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent( new BigEndianHeapChannelBuffer(allBytes) { @Override public void getBytes(int index, byte[] dst, int dstIndex, int length) { if (dstIndex + length >= allBytes.length) { throw new TesterException(); } super.getBytes(index, dst, dstIndex, length); } } ); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response, null); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final byte[] buff = new byte[allBytes.length]; fillBuff(stream, buff); }
@Override public NettyHttpResponse chunked() { response.setHeader(Names.TRANSFER_ENCODING, Values.CHUNKED); response.setChunked(true); ctx.getChannel().write(response); return this; }
private void performEventSourceHandshake(ChannelHandler eventSourceConnectionHandler) { nettyHttpResponse.setStatus(HttpResponseStatus.OK); nettyHttpResponse.addHeader("Content-Type", "text/event-stream"); nettyHttpResponse.addHeader("Transfer-Encoding", "identity"); nettyHttpResponse.addHeader("Connection", "keep-alive"); nettyHttpResponse.addHeader("Cache-Control", "no-cache"); nettyHttpResponse.setChunked(false); ctx.getChannel().write(nettyHttpResponse); getReadyToSendEventSourceMessages(eventSourceConnectionHandler); }
@Override public NettyHttpResponse chunked() { response.setHeader(Names.TRANSFER_ENCODING, Values.CHUNKED); response.setChunked(true); ctx.getChannel().write(response); return this; }
private void sendError(String err, HttpResponseStatus status, Channel ch) { HttpResponse resp = new DefaultHttpResponse(HTTP_1_1, status); resp.setChunked(false); if (status.getCode() == METHOD_NOT_ALLOWED.getCode()) { // SockJS requires this resp.setHeader("allow", "GET"); } if (err != null) { ChannelBuffer buff = ChannelBuffers.copiedBuffer(err.getBytes(Charset.forName("UTF-8"))); resp.setHeader("Content-Length", err.length()); resp.setContent(buff); } else { resp.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0"); } ch.write(resp); }
private void performEventSourceHandshake(ChannelHandler eventSourceConnectionHandler) { nettyHttpResponse.setStatus(HttpResponseStatus.OK); nettyHttpResponse.addHeader("Content-Type", "text/event-stream"); nettyHttpResponse.addHeader("Transfer-Encoding", "identity"); nettyHttpResponse.addHeader("Connection", "keep-alive"); nettyHttpResponse.addHeader("Cache-Control", "no-cache"); nettyHttpResponse.setChunked(false); ctx.getChannel().write(nettyHttpResponse); getReadyToSendEventSourceMessages(eventSourceConnectionHandler); }
@Test public void simpleMultiStreamTest() throws IOException { Iterator<byte[]> it = BYTE_LIST.iterator(); SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(true); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response); while (it.hasNext()) { final DefaultHttpChunk chunk = new DefaultHttpChunk(new BigEndianHeapChannelBuffer(it.next())); clientResponse = responseHandler.handleChunk(clientResponse, chunk); } clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(Math.abs(RANDOM.nextInt()) % 128, allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Test public void simpleSingleStreamTest() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent(new BigEndianHeapChannelBuffer(allBytes)); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final InputStream expectedStream = new ByteArrayInputStream(allBytes); int read = 0; while (read < allBytes.length) { final byte[] expectedBytes = new byte[Math.min(Math.abs(RANDOM.nextInt()) % 128, allBytes.length - read)]; final byte[] actualBytes = new byte[expectedBytes.length]; fillBuff(stream, actualBytes); fillBuff(expectedStream, expectedBytes); Assert.assertArrayEquals(expectedBytes, actualBytes); read += expectedBytes.length; } Assert.assertEquals(allBytes.length, responseHandler.getByteCount()); }
@Override public HttpResponse generateResponse(HttpRequest request) throws Exception { HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus( 101, "Web Socket Protocol Handshake - IETF-07" ) ); String origin = request.getHeader( Names.ORIGIN ); if (origin != null) { response.addHeader( Names.ORIGIN, origin ); } response.addHeader( Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation( request ) ); String protocol = request.getHeader( Names.SEC_WEBSOCKET_PROTOCOL ); if (protocol != null) { response.addHeader( Names.SEC_WEBSOCKET_PROTOCOL, protocol ); } String key = request.getHeader( "Sec-WebSocket-Key" ); String solution = Ietf07WebSocketChallenge.solve( key ); response.addHeader( "Sec-WebSocket-Accept", solution ); response.setChunked( false ); return response; }
@Override public HttpResponse generateResponse(HttpRequest request) throws Exception { HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus( 101, "Web Socket Protocol Handshake - IETF-07" ) ); String origin = request.getHeader( Names.ORIGIN ); if (origin != null) { response.addHeader( Names.SEC_WEBSOCKET_ORIGIN, origin ); } response.addHeader( Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation( request ) ); String protocol = request.getHeader( Names.SEC_WEBSOCKET_PROTOCOL ); if (protocol != null) { response.addHeader( Names.SEC_WEBSOCKET_PROTOCOL, protocol ); } String key = request.getHeader( "Sec-WebSocket-Key" ); String solution = Ietf07WebSocketChallenge.solve( key ); response.addHeader( "Sec-WebSocket-Accept", solution ); response.setChunked( false ); return response; }
@Override public HttpResponse generateResponse(HttpRequest request) throws Exception { HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus( 101, "Web Socket Protocol Handshake - IETF-07" ) ); String origin = request.getHeader( Names.ORIGIN ); if (origin != null) { response.addHeader( Names.SEC_WEBSOCKET_ORIGIN, origin ); } response.addHeader( Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation( request ) ); String protocol = request.getHeader( Names.SEC_WEBSOCKET_PROTOCOL ); if (protocol != null) { response.addHeader( Names.SEC_WEBSOCKET_PROTOCOL, protocol ); } String key = request.getHeader( "Sec-WebSocket-Key" ); String solution = Ietf07WebSocketChallenge.solve( key ); response.addHeader( "Sec-WebSocket-Accept", solution ); response.setChunked( false ); return response; }
public HttpResponse generateResponse(HttpRequest request, String serverOrigin) throws Exception { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols")); response.addHeader(HttpHeaders.Names.UPGRADE, "WebSocket"); response.addHeader(HttpHeaders.Names.CONNECTION, "Upgrade"); String origin = request.getHeader(Names.ORIGIN); if (origin == null) { origin = serverOrigin; } response.addHeader(Names.SEC_WEBSOCKET_ORIGIN, origin); response.addHeader(Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation(request, serverOrigin)); String protocol = request.getHeader(Names.SEC_WEBSOCKET_PROTOCOL); if (protocol != null) { response.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol); } String key = request.getHeader("Sec-WebSocket-Key"); String solution = WebSocketChallenge08.solve(key); response.addHeader("Sec-WebSocket-Accept", solution); response.setChunked(false); return response; }
public HttpResponse generateResponse(HttpRequest request, String serverOrigin) throws Exception { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols")); response.addHeader(Names.UPGRADE, "WebSocket"); response.addHeader(HttpHeaders.Names.CONNECTION, "Upgrade"); String origin = request.getHeader(Names.ORIGIN); if (origin != null) { response.addHeader(Names.SEC_WEBSOCKET_ORIGIN, origin); } response.addHeader(Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation(request, serverOrigin)); String protocol = request.getHeader(Names.SEC_WEBSOCKET_PROTOCOL); if (protocol != null) { response.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol); } String key = request.getHeader("Sec-WebSocket-Key"); String solution = WebSocketChallenge08.solve(key); response.addHeader("Sec-WebSocket-Accept", solution); response.setChunked(false); return response; }
@Test(expected = TesterException.class) public void testExceptionalSingleStream() throws IOException { SequenceInputStreamResponseHandler responseHandler = new SequenceInputStreamResponseHandler(); final HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setChunked(false); response.setContent( new BigEndianHeapChannelBuffer(allBytes) { @Override public void getBytes(int index, byte[] dst, int dstIndex, int length) { if (dstIndex + length >= allBytes.length) { throw new TesterException(); } super.getBytes(index, dst, dstIndex, length); } } ); ClientResponse<InputStream> clientResponse = responseHandler.handleResponse(response); clientResponse = responseHandler.done(clientResponse); final InputStream stream = clientResponse.getObj(); final byte[] buff = new byte[allBytes.length]; fillBuff(stream, buff); }
@Override public HttpResponse generateResponse(HttpRequest request) throws Exception { HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus( 101, "Web Socket Protocol Handshake - IETF-00" ) ); String origin = request.getHeader( Names.ORIGIN ); if (origin != null) { response.addHeader( Names.SEC_WEBSOCKET_ORIGIN, request.getHeader( Names.ORIGIN ) ); } response.addHeader( Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation( request ) ); String protocol = request.getHeader( Names.SEC_WEBSOCKET_PROTOCOL ); if (protocol != null) { response.addHeader( Names.SEC_WEBSOCKET_PROTOCOL, protocol ); } // Calculate the answer of the challenge. String key1 = request.getHeader( Names.SEC_WEBSOCKET_KEY1 ); String key2 = request.getHeader( Names.SEC_WEBSOCKET_KEY2 ); byte[] key3 = new byte[8]; request.getContent().readBytes( key3 ); byte[] solution = Ietf00WebSocketChallenge.solve( key1, key2, key3 ); ChannelBuffer buffer = ChannelBuffers.dynamicBuffer( solution.length + 2 ); buffer.writeBytes( solution ); response.setContent( buffer ); response.setChunked( false ); return response; }
public HttpResponse generateResponse(HttpRequest request, String serverOrigin) throws Exception { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(101, "WebSocket Protocol Handshake")); response.addHeader(HttpHeaders.Names.CONNECTION, "Upgrade"); response.addHeader(HttpHeaders.Names.UPGRADE, "WebSocket"); String origin = request.getHeader(Names.ORIGIN); if (origin == null) { origin = serverOrigin; } response.addHeader(Names.SEC_WEBSOCKET_ORIGIN, origin); response.addHeader(Names.SEC_WEBSOCKET_LOCATION, getWebSocketLocation(request, serverOrigin)); String protocol = request.getHeader(Names.SEC_WEBSOCKET_PROTOCOL); if (protocol != null) { response.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, protocol); } // Calculate the answer of the challenge. String key1 = request.getHeader(Names.SEC_WEBSOCKET_KEY1); String key2 = request.getHeader(Names.SEC_WEBSOCKET_KEY2); byte[] key3 = new byte[8]; request.getContent().readBytes(key3); byte[] solution = WebSocketChallenge00.solve(key1, key2, key3); ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(solution.length + 2); buffer.writeBytes(solution); response.addHeader("Content-Length", buffer.readableBytes()); response.setContent(buffer); response.setChunked(false); return response; }