@Override public NettyHttpResponse header(String name, long value) { response.addHeader(name, value); return this; }
@Override public NettyHttpResponse header(String name, Date value) { response.addHeader(name, DateHelper.rfc1123Format(value)); return this; }
@Override public NettyHttpResponse header(String name, String value) { if (value == null) { response.removeHeader(name); } else { response.addHeader(name, value); } 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 void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setVersion("HIXIE-75"); res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, HttpHeaders.Values.UPGRADE); String origin = req.getHeader(ORIGIN); if (origin != null) { res.addHeader(WEBSOCKET_ORIGIN, origin); } res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req)); String protocol = req.getHeader(WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(WEBSOCKET_PROTOCOL, protocol); } }
@Override public void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setVersion("HIXIE-76"); res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, UPGRADE); res.addHeader(SEC_WEBSOCKET_ORIGIN, req.getHeader(ORIGIN)); res.addHeader(SEC_WEBSOCKET_LOCATION, getWebSocketLocation(req)); String protocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(SEC_WEBSOCKET_PROTOCOL, protocol); } // Calculate the answer of the challenge. String key1 = req.getHeader(SEC_WEBSOCKET_KEY1); String key2 = req.getHeader(SEC_WEBSOCKET_KEY2); int a = (int) (Long.parseLong(key1.replaceAll("[^0-9]", "")) / key1.replaceAll("[^ ]", "").length()); int b = (int) (Long.parseLong(key2.replaceAll("[^0-9]", "")) / key2.replaceAll("[^ ]", "").length()); long c = req.getContent().readLong(); ChannelBuffer input = ChannelBuffers.buffer(16); input.writeInt(a); input.writeInt(b); input.writeLong(c); ChannelBuffer output = ChannelBuffers.wrappedBuffer(MD5.digest(input.array())); res.setContent(output); }
@Override public void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setHybiWebSocketVersion(getHybiVersion()); if (getHybiVersion() < MIN_HYBI_VERSION) { res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED); res.setHeader(SEC_WEBSOCKET_VERSION, String.valueOf(MIN_HYBI_VERSION)); return; } String key = req.getHeader(SEC_WEBSOCKET_KEY); if (key == null) { res.setStatus(HttpResponseStatus.BAD_REQUEST); return; } String accept = Base64.encode(sha1(key + ACCEPT_GUID)); res.setStatus(new HttpResponseStatus(101, "Switching Protocols")); res.addHeader(UPGRADE, WEBSOCKET.toLowerCase()); res.addHeader(CONNECTION, UPGRADE); res.addHeader(SEC_WEBSOCKET_ACCEPT, accept); String webSocketProtocol = req.getHeader(SEC_WEBSOCKET_PROTOCOL); if (webSocketProtocol != null) { res.addHeader(SEC_WEBSOCKET_PROTOCOL, webSocketProtocol); } }
@Override public NettyHttpResponse header(String name, long value) { response.addHeader(name, value); return this; }
@Override public void addHeader(String name, Object value) { response.addHeader(name, value); }
private void addStandardResponseHeaders(HttpRequest request, HttpResponse response) { response.addHeader(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET); response.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.UPGRADE); response.addHeader(HttpHeaders.Names.LOCATION, HttpHeaders.Values.UPGRADE); response.addHeader(HttpHeaders.Names.SEC_WEBSOCKET_LOCATION, getHttpClientLocation(request)); String protocolRequested = request.getHeader(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL); if (StringUtils.isNotEmpty(protocolRequested)) { response.addHeader(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL, protocolRequested); } }
@Override public NettyHttpResponse header(String name, String value) { if (value == null) { response.removeHeader(name); } else { response.addHeader(name, value); } return this; }
@Override public NettyHttpResponse header(String name, Date value) { response.addHeader(name, DateHelper.rfc1123Format(value)); 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 void prepareHandshakeResponse(NettyWebSocketConnection webSocketConnection) { webSocketConnection.setVersion("HIXIE-75"); res.setStatus(new HttpResponseStatus(101, "Web Socket Protocol Handshake")); res.addHeader(UPGRADE, WEBSOCKET); res.addHeader(CONNECTION, HttpHeaders.Values.UPGRADE); String origin = req.getHeader(ORIGIN); if (origin != null) { res.addHeader(WEBSOCKET_ORIGIN, origin); } res.addHeader(WEBSOCKET_LOCATION, getWebSocketLocation(req)); String protocol = req.getHeader(WEBSOCKET_PROTOCOL); if (protocol != null) { res.addHeader(WEBSOCKET_PROTOCOL, protocol); } }
@Override public void temporaryRedirect(String location) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, TEMPORARY_REDIRECT); response.addHeader("Location", location); write(response); }
@Override public void movedPermanently(String location) { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, MOVED_PERMANENTLY); response.addHeader("Location", location); write(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.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; }
private HttpResponse processHybiHandshake(HttpRequest request) throws NoSuchAlgorithmException { String websocketKey = request.getHeader(ExtendedHttpHeaders.Names.SEC_WEBSOCKET_KEY); if (StringUtils.isEmpty(websocketKey)) { return new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN); } HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS); addStandardResponseHeaders(request, response); String websocketAcceptValue = computeHybiAcceptValue(websocketKey); response.addHeader(ExtendedHttpHeaders.Names.SEC_WEBSOCKET_ACCEPT, websocketAcceptValue); return response; }
private HttpResponse processHixieHandshake(HttpRequest request) throws NoSuchAlgorithmException { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS); addStandardResponseHeaders(request, response); response.addHeader(HttpHeaders.Names.SEC_WEBSOCKET_ORIGIN, request.getHeader(HttpHeaders.Names.ORIGIN)); ChannelBuffer responseContent = createHixieHandshakeContent(request); response.setContent(responseContent); return response; }