io.netty.handler.codec.http.HttpHeaders.Values Java Examples
The following examples show how to use
io.netty.handler.codec.http.HttpHeaders.Values.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebSocketClientHandshaker07.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * <p> * Process server response: * </p> * * <pre> * HTTP/1.1 101 Switching Protocols * Upgrade: websocket * Connection: Upgrade * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= * Sec-WebSocket-Protocol: chat * </pre> * * @param response * HTTP response returned from the server for the request sent by beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override protected void verify(FullHttpResponse response) { final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS; final HttpHeaders headers = response.headers(); if (!response.getStatus().equals(status)) { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.getStatus()); } String upgrade = headers.get(Names.UPGRADE); if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } String connection = headers.get(Names.CONNECTION); if (!Values.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); if (accept == null || !accept.equals(expectedChallengeResponseString)) { throw new WebSocketHandshakeException(String.format( "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString)); } }
Example #2
Source File: SpdyServerHandler.java From netty-cookbook with Apache License 2.0 | 6 votes |
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; if (is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); } boolean keepAlive = isKeepAlive(req); ByteBuf content = RESPONSE_BYTES.duplicate(); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, content); response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (!keepAlive) { ctx.write(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); ctx.write(response); } } }
Example #3
Source File: WebSocketClientHandshaker08.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * <p> * Process server response: * </p> * * <pre> * HTTP/1.1 101 Switching Protocols * Upgrade: websocket * Connection: Upgrade * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= * Sec-WebSocket-Protocol: chat * </pre> * * @param response * HTTP response returned from the server for the request sent by beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override protected void verify(FullHttpResponse response) { final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS; final HttpHeaders headers = response.headers(); if (!response.getStatus().equals(status)) { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.getStatus()); } String upgrade = headers.get(Names.UPGRADE); if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } String connection = headers.get(Names.CONNECTION); if (!Values.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); if (accept == null || !accept.equals(expectedChallengeResponseString)) { throw new WebSocketHandshakeException(String.format( "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString)); } }
Example #4
Source File: WebSocketClientHandshaker13.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * <p> * Process server response: * </p> * * <pre> * HTTP/1.1 101 Switching Protocols * Upgrade: websocket * Connection: Upgrade * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= * Sec-WebSocket-Protocol: chat * </pre> * * @param response * HTTP response returned from the server for the request sent by beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override protected void verify(FullHttpResponse response) { final HttpResponseStatus status = HttpResponseStatus.SWITCHING_PROTOCOLS; final HttpHeaders headers = response.headers(); if (!response.getStatus().equals(status)) { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.getStatus()); } String upgrade = headers.get(Names.UPGRADE); if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } String connection = headers.get(Names.CONNECTION); if (!Values.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); if (accept == null || !accept.equals(expectedChallengeResponseString)) { throw new WebSocketHandshakeException(String.format( "Invalid challenge. Actual: %s. Expected: %s", accept, expectedChallengeResponseString)); } }
Example #5
Source File: HttpServerHandler.java From java-specialagent with Apache License 2.0 | 6 votes |
@Override public void channelRead(final ChannelHandlerContext handlerContext, final Object message) { if (message instanceof HttpRequest) { final HttpRequest request = (HttpRequest)message; if (HttpUtil.is100ContinueExpected(request)) handlerContext.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); final boolean keepAlive = HttpUtil.isKeepAlive(request); final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (keepAlive) { response.headers().set(CONNECTION, Values.KEEP_ALIVE); handlerContext.write(response); } else { handlerContext.write(response).addListener(ChannelFutureListener.CLOSE); } } }
Example #6
Source File: HttpServerHandler.java From java-specialagent with Apache License 2.0 | 6 votes |
@Override public void channelRead(final ChannelHandlerContext handlerContext, final Object message) { if (message instanceof HttpRequest) { final HttpRequest request = (HttpRequest)message; if (HttpUtil.is100ContinueExpected(request)) handlerContext.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); final boolean keepAlive = HttpUtil.isKeepAlive(request); final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT)); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); if (keepAlive) { response.headers().set(CONNECTION, Values.KEEP_ALIVE); handlerContext.write(response); } else { handlerContext.write(response).addListener(ChannelFutureListener.CLOSE); } } }
Example #7
Source File: HttpContentEncoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Override protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception { String acceptedEncoding = msg.headers().get(HttpHeaders.Names.ACCEPT_ENCODING); if (acceptedEncoding == null) { acceptedEncoding = HttpHeaders.Values.IDENTITY; } acceptEncodingQueue.add(acceptedEncoding); out.add(ReferenceCountUtil.retain(msg)); }
Example #8
Source File: NettyHttpServerHandler.java From DDMQ with Apache License 2.0 | 5 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { BackupState backupState = BackupState.ERROR; if (msg instanceof FullHttpRequest) { FullHttpRequest req = (FullHttpRequest) msg; if (req.getUri().equals("/chronos/backup")) { backupState = BackupDB.backup(); LOGGER.info("backupState:{}", backupState.getDesc()); RestoreState restoreState = BackupDB.restore(); LOGGER.info("restoreState:{}", restoreState.getDesc()); } } else { LOGGER.error("request is not FullHttpRequest"); } FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(backupState.getDesc().getBytes("utf-8"))); response.headers().set(Names.CONTENT_TYPE, "text/plain;charset=UTF-8"); response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(Names.CONNECTION, Values.KEEP_ALIVE); ctx.write(response); ctx.flush(); }
Example #9
Source File: NettyHttpServerHandler.java From DDMQ with Apache License 2.0 | 5 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { BackupState backupState = BackupState.ERROR; if (msg instanceof FullHttpRequest) { FullHttpRequest req = (FullHttpRequest) msg; if (req.getUri().equals("/chronos/backup")) { backupState = BackupDB.backup(); LOGGER.info("backupState:{}", backupState.getDesc()); RestoreState restoreState = BackupDB.restore(); LOGGER.info("restoreState:{}", restoreState.getDesc()); } } else { LOGGER.error("request is not FullHttpRequest"); } FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(backupState.getDesc().getBytes("utf-8"))); response.headers().set(Names.CONTENT_TYPE, "text/plain;charset=UTF-8"); response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(Names.CONNECTION, Values.KEEP_ALIVE); ctx.write(response); ctx.flush(); }
Example #10
Source File: WebSocketClientHandshaker00.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
/** * <p> * Process server response: * </p> * * <pre> * HTTP/1.1 101 WebSocket Protocol Handshake * Upgrade: WebSocket * Connection: Upgrade * Sec-WebSocket-Origin: http://example.com * Sec-WebSocket-Location: ws://example.com/demo * Sec-WebSocket-Protocol: sample * * 8jKS'y:G*Co,Wxa- * </pre> * * @param response * HTTP response returned from the server for the request sent by beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override protected void verify(FullHttpResponse response) { final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake"); if (!response.getStatus().equals(status)) { throw new WebSocketHandshakeException("Invalid handshake response getStatus: " + response.getStatus()); } HttpHeaders headers = response.headers(); String upgrade = headers.get(Names.UPGRADE); if (!Values.WEBSOCKET.equalsIgnoreCase(upgrade)) { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } String connection = headers.get(Names.CONNECTION); if (!Values.UPGRADE.equalsIgnoreCase(connection)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); } ByteBuf challenge = response.content(); if (!challenge.equals(expectedChallengeResponseBytes)) { throw new WebSocketHandshakeException("Invalid challenge"); } }
Example #11
Source File: HttpContentEncoderTest.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Test public void testChunkedContent() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder()); ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[3]))); ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[2]))); ch.writeOutbound(new DefaultLastHttpContent(Unpooled.wrappedBuffer(new byte[1]))); HttpContent chunk; chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("3")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("2")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1")); assertThat(chunk, is(instanceOf(HttpContent.class))); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().isReadable(), is(false)); assertThat(chunk, is(instanceOf(LastHttpContent.class))); chunk.release(); assertThat(ch.readOutbound(), is(nullValue())); }
Example #12
Source File: HttpContentEncoderTest.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Test public void testChunkedContentWithTrailingHeader() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new TestEncoder()); ch.writeInbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[3]))); ch.writeOutbound(new DefaultHttpContent(Unpooled.wrappedBuffer(new byte[2]))); LastHttpContent content = new DefaultLastHttpContent(Unpooled.wrappedBuffer(new byte[1])); content.trailingHeaders().set("X-Test", "Netty"); ch.writeOutbound(content); HttpContent chunk; chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("3")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("2")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().toString(CharsetUtil.US_ASCII), is("1")); assertThat(chunk, is(instanceOf(HttpContent.class))); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().isReadable(), is(false)); assertThat(chunk, is(instanceOf(LastHttpContent.class))); assertEquals("Netty", ((LastHttpContent) chunk).trailingHeaders().get("X-Test")); chunk.release(); assertThat(ch.readOutbound(), is(nullValue())); }
Example #13
Source File: HttpContentCompressorTest.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
@Test public void testChunkedContent() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor()); ch.writeInbound(newRequest()); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); ch.writeOutbound(new DefaultHttpContent(Unpooled.copiedBuffer("Hell", CharsetUtil.US_ASCII))); ch.writeOutbound(new DefaultHttpContent(Unpooled.copiedBuffer("o, w", CharsetUtil.US_ASCII))); ch.writeOutbound(new DefaultLastHttpContent(Unpooled.copiedBuffer("orld", CharsetUtil.US_ASCII))); HttpContent chunk; chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("1f8b0800000000000000f248cdc901000000ffff")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("cad7512807000000ffff")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("ca2fca4901000000ffff")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("0300c2a99ae70c000000")); assertThat(chunk, is(instanceOf(HttpContent.class))); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().isReadable(), is(false)); assertThat(chunk, is(instanceOf(LastHttpContent.class))); chunk.release(); assertThat(ch.readOutbound(), is(nullValue())); }
Example #14
Source File: WebSocketClientHandshaker13.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
/** * /** * <p> * Sends the opening request to the server: * </p> * * <pre> * GET /chat HTTP/1.1 * Host: server.example.com * Upgrade: websocket * Connection: Upgrade * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== * Sec-WebSocket-Origin: http://example.com * Sec-WebSocket-Protocol: chat, superchat * Sec-WebSocket-Version: 13 * </pre> * */ @Override protected FullHttpRequest newHandshakeRequest() { // Get path URI wsURL = uri(); String path = wsURL.getPath(); if (wsURL.getQuery() != null && !wsURL.getQuery().isEmpty()) { path = wsURL.getPath() + '?' + wsURL.getQuery(); } if (path == null || path.isEmpty()) { path = "/"; } // Get 16 bit nonce and base 64 encode it byte[] nonce = WebSocketUtil.randomBytes(16); String key = WebSocketUtil.base64(nonce); String acceptSeed = key + MAGIC_GUID; byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII)); expectedChallengeResponseString = WebSocketUtil.base64(sha1); if (logger.isDebugEnabled()) { logger.debug( "WebSocket version 13 client handshake key: {}, expected response: {}", key, expectedChallengeResponseString); } // Format request int wsPort = wsURL.getPort(); // check if the URI contained a port if not set the correct one depending on the schema. // See https://github.com/netty/netty/pull/1558 if (wsPort == -1) { if ("wss".equals(wsURL.getScheme())) { wsPort = 443; } else { wsPort = 80; } } FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); headers.add(Names.UPGRADE, Values.WEBSOCKET.toLowerCase()) .add(Names.CONNECTION, Values.UPGRADE) .add(Names.SEC_WEBSOCKET_KEY, key) .add(Names.HOST, wsURL.getHost() + ':' + wsPort); String originValue = "http://" + wsURL.getHost(); if (wsPort != 80 && wsPort != 443) { // if the port is not standard (80/443) its needed to add the port to the header. // See http://tools.ietf.org/html/rfc6454#section-6.2 originValue = originValue + ':' + wsPort; } headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } headers.add(Names.SEC_WEBSOCKET_VERSION, "13"); if (customHeaders != null) { headers.add(customHeaders); } return request; }
Example #15
Source File: WebSocketClientHandshaker07.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
/** * /** * <p> * Sends the opening request to the server: * </p> * * <pre> * GET /chat HTTP/1.1 * Host: server.example.com * Upgrade: websocket * Connection: Upgrade * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== * Sec-WebSocket-Origin: http://example.com * Sec-WebSocket-Protocol: chat, superchat * Sec-WebSocket-Version: 7 * </pre> * */ @Override protected FullHttpRequest newHandshakeRequest() { // Get path URI wsURL = uri(); String path = wsURL.getPath(); if (wsURL.getQuery() != null && !wsURL.getQuery().isEmpty()) { path = wsURL.getPath() + '?' + wsURL.getQuery(); } if (path == null || path.isEmpty()) { path = "/"; } // Get 16 bit nonce and base 64 encode it byte[] nonce = WebSocketUtil.randomBytes(16); String key = WebSocketUtil.base64(nonce); String acceptSeed = key + MAGIC_GUID; byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII)); expectedChallengeResponseString = WebSocketUtil.base64(sha1); if (logger.isDebugEnabled()) { logger.debug( "WebSocket version 07 client handshake key: {}, expected response: {}", key, expectedChallengeResponseString); } // Format request FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); headers.add(Names.UPGRADE, Values.WEBSOCKET.toLowerCase()) .add(Names.CONNECTION, Values.UPGRADE) .add(Names.SEC_WEBSOCKET_KEY, key) .add(Names.HOST, wsURL.getHost()); int wsPort = wsURL.getPort(); String originValue = "http://" + wsURL.getHost(); if (wsPort != 80 && wsPort != 443) { // if the port is not standard (80/443) its needed to add the port to the header. // See http://tools.ietf.org/html/rfc6454#section-6.2 originValue = originValue + ':' + wsPort; } headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } headers.add(Names.SEC_WEBSOCKET_VERSION, "7"); if (customHeaders != null) { headers.add(customHeaders); } return request; }
Example #16
Source File: WebSocketClientHandshaker08.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
/** * /** * <p> * Sends the opening request to the server: * </p> * * <pre> * GET /chat HTTP/1.1 * Host: server.example.com * Upgrade: websocket * Connection: Upgrade * Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== * Sec-WebSocket-Origin: http://example.com * Sec-WebSocket-Protocol: chat, superchat * Sec-WebSocket-Version: 8 * </pre> * */ @Override protected FullHttpRequest newHandshakeRequest() { // Get path URI wsURL = uri(); String path = wsURL.getPath(); if (wsURL.getQuery() != null && !wsURL.getQuery().isEmpty()) { path = wsURL.getPath() + '?' + wsURL.getQuery(); } if (path == null || path.isEmpty()) { path = "/"; } // Get 16 bit nonce and base 64 encode it byte[] nonce = WebSocketUtil.randomBytes(16); String key = WebSocketUtil.base64(nonce); String acceptSeed = key + MAGIC_GUID; byte[] sha1 = WebSocketUtil.sha1(acceptSeed.getBytes(CharsetUtil.US_ASCII)); expectedChallengeResponseString = WebSocketUtil.base64(sha1); if (logger.isDebugEnabled()) { logger.debug( "WebSocket version 08 client handshake key: {}, expected response: {}", key, expectedChallengeResponseString); } // Format request FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); HttpHeaders headers = request.headers(); headers.add(Names.UPGRADE, Values.WEBSOCKET.toLowerCase()) .add(Names.CONNECTION, Values.UPGRADE) .add(Names.SEC_WEBSOCKET_KEY, key) .add(Names.HOST, wsURL.getHost()); int wsPort = wsURL.getPort(); String originValue = "http://" + wsURL.getHost(); if (wsPort != 80 && wsPort != 443) { // if the port is not standard (80/443) its needed to add the port to the header. // See http://tools.ietf.org/html/rfc6454#section-6.2 originValue = originValue + ':' + wsPort; } headers.add(Names.SEC_WEBSOCKET_ORIGIN, originValue); String expectedSubprotocol = expectedSubprotocol(); if (expectedSubprotocol != null && !expectedSubprotocol.isEmpty()) { headers.add(Names.SEC_WEBSOCKET_PROTOCOL, expectedSubprotocol); } headers.add(Names.SEC_WEBSOCKET_VERSION, "8"); if (customHeaders != null) { headers.add(customHeaders); } return request; }
Example #17
Source File: HttpContentCompressorTest.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
@Test public void testChunkedContentWithTrailingHeader() throws Exception { EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor()); ch.writeInbound(newRequest()); HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); res.headers().set(Names.TRANSFER_ENCODING, Values.CHUNKED); ch.writeOutbound(res); assertEncodedResponse(ch); ch.writeOutbound(new DefaultHttpContent(Unpooled.copiedBuffer("Hell", CharsetUtil.US_ASCII))); ch.writeOutbound(new DefaultHttpContent(Unpooled.copiedBuffer("o, w", CharsetUtil.US_ASCII))); LastHttpContent content = new DefaultLastHttpContent(Unpooled.copiedBuffer("orld", CharsetUtil.US_ASCII)); content.trailingHeaders().set("X-Test", "Netty"); ch.writeOutbound(content); HttpContent chunk; chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("1f8b0800000000000000f248cdc901000000ffff")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("cad7512807000000ffff")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("ca2fca4901000000ffff")); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(ByteBufUtil.hexDump(chunk.content()), is("0300c2a99ae70c000000")); assertThat(chunk, is(instanceOf(HttpContent.class))); chunk.release(); chunk = (HttpContent) ch.readOutbound(); assertThat(chunk.content().isReadable(), is(false)); assertThat(chunk, is(instanceOf(LastHttpContent.class))); assertEquals("Netty", ((LastHttpContent) chunk).trailingHeaders().get("X-Test")); chunk.release(); assertThat(ch.readOutbound(), is(nullValue())); }