public void fillInRequest(HttpClientRequest req, String hostHeader) throws Exception { req.headers().put(HttpHeaders.Names.CONNECTION, "Upgrade"); req.headers().put(HttpHeaders.Names.UPGRADE, "WebSocket"); req.headers().put(HttpHeaders.Names.HOST, hostHeader); req.headers().put(HttpHeaders.Names.SEC_WEBSOCKET_KEY1, this.challenge.getKey1String()); req.headers().put(HttpHeaders.Names.SEC_WEBSOCKET_KEY2, this.challenge.getKey2String()); Buffer buff = new Buffer(6); buff.appendBytes(challenge.getKey3()); buff.appendByte((byte) '\r'); buff.appendByte((byte) '\n'); req.write(buff); }
public HttpResponse generateResponse(HttpRequest request, String serverOrigin) throws Exception { HttpResponse response = new DefaultHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus(101, "Web Socket Protocol Handshake - IETF-00")); 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)); 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; }