public abstract static class DefaultContextImpl implements Context { private static final String durationProp = System.getProperty(DefaultContextImpl.class.getName() + ".durationMillis"); private static final long DURATION = durationProp != null ? Long.parseLong(durationProp) : 60_000L; private final ReentrantLock lock = new ReentrantLock(); private final long created; private final Map<String, Object> attachments = new HashMap<>(); private boolean valid = true; public DefaultContextImpl() { created = new Date().getTime(); } @Override public void invalidate() { attachments.clear(); valid = false; } @Override public final boolean isValid() { final boolean ret = valid && (new Date().getTime() - created) <= DURATION; if (!ret) invalidate(); return ret; } @Override public final Map<String, Object> getAttachments() { return attachments; } @Override public final ReentrantLock getLock() { return lock; } @Override public boolean watch() { return true; } }
/** * Handle the web socket handshake for the web socket specification <a href= * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">HyBi version 0</a> and * lower. This standard is really a rehash of <a * href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76" >hixie-76</a> and <a * href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75" >hixie-75</a>. * * <p>Browser request to the server: * * <pre> * GET /demo HTTP/1.1 * Upgrade: WebSocket * Connection: Upgrade * Host: example.com * Origin: http://example.com * Sec-WebSocket-Protocol: chat, sample * Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5 * Sec-WebSocket-Key2: 12998 5 Y3 1 .P00 * * ^n:ds[4U * </pre> * * <p>Server response: * * <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 channel Channel * @param req HTTP request */ @Override public ChannelFuture handshake(Channel channel, FullHttpRequest req, ChannelPromise promise) { if (logger.isDebugEnabled()) { logger.debug(String.format("Channel %s WS Version 00 server handshake", channel.id())); } // Serve the WebSocket handshake request. if (!Values.UPGRADE.equalsIgnoreCase(req.headers().get(CONNECTION)) || !WEBSOCKET.equalsIgnoreCase(req.headers().get(Names.UPGRADE))) { throw new WebSocketHandshakeException("not a WebSocket handshake request: missing upgrade"); } // Hixie 75 does not contain these headers while Hixie 76 does boolean isHixie76 = req.headers().contains(SEC_WEBSOCKET_KEY1) && req.headers().contains(SEC_WEBSOCKET_KEY2); // Create the WebSocket handshake response. FullHttpResponse res = new DefaultFullHttpResponse( HTTP_1_1, new HttpResponseStatus( 101, isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake")); res.headers().add(Names.UPGRADE, WEBSOCKET); res.headers().add(CONNECTION, Values.UPGRADE); // Fill in the headers and contents depending on handshake getMethod. if (isHixie76) { // New handshake getMethod with a challenge: res.headers().add(SEC_WEBSOCKET_ORIGIN, req.headers().get(ORIGIN)); res.headers().add(SEC_WEBSOCKET_LOCATION, uri()); String subprotocols = req.headers().get(SEC_WEBSOCKET_PROTOCOL); if (subprotocols != null) { String selectedSubprotocol = selectSubprotocol(subprotocols); if (selectedSubprotocol == null) { throw new WebSocketHandshakeException( "Requested subprotocol(s) not supported: " + subprotocols); } else { res.headers().add(SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); setSelectedSubprotocol(selectedSubprotocol); } } // Calculate the answer of the challenge. String key1 = req.headers().get(SEC_WEBSOCKET_KEY1); String key2 = req.headers().get(SEC_WEBSOCKET_KEY2); int a = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key1).replaceAll("")) / BEGINNING_SPACE.matcher(key1).replaceAll("").length()); int b = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key2).replaceAll("")) / BEGINNING_SPACE.matcher(key2).replaceAll("").length()); long c = req.data().readLong(); ByteBuf input = Unpooled.buffer(16); input.writeInt(a); input.writeInt(b); input.writeLong(c); res.data().writeBytes(WebSocketUtil.md5(input.array())); } else { // Old Hixie 75 handshake getMethod with no challenge: res.headers().add(WEBSOCKET_ORIGIN, req.headers().get(ORIGIN)); res.headers().add(WEBSOCKET_LOCATION, uri()); String protocol = req.headers().get(WEBSOCKET_PROTOCOL); if (protocol != null) { res.headers().add(WEBSOCKET_PROTOCOL, selectSubprotocol(protocol)); } } // Upgrade the connection and send the handshake response. channel.write(res, promise); promise.addListener( new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); if (p.get(HttpObjectAggregator.class) != null) { p.remove(HttpObjectAggregator.class); } p.replaceAndForward( HttpRequestDecoder.class, "wsdecoder", new WebSocket00FrameDecoder(maxFramePayloadLength())); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket00FrameEncoder()); } }); return promise; }