@Override protected HttpMessage createMessage(String[] initialLine) throws Exception { return new DefaultHttpResponse( RtspVersions.valueOf(initialLine[0]), new HttpResponseStatus(Integer.valueOf(initialLine[1]), initialLine[2])); }
@Override protected HttpMessage createMessage(String[] initialLine) { return new DefaultHttpResponse( HttpVersion.valueOf(initialLine[0]), new HttpResponseStatus(Integer.valueOf(initialLine[1]), initialLine[2])); }
return new HttpResponseStatus(code, reasonPhrase + " (" + code + ')');
/** * Returns the {@link HttpResponseStatus} represented by the HTTP response status header. */ public static HttpResponseStatus getStatus(int spdyVersion, SpdyHeadersFrame frame) { try { String status = frame.headers().get(HttpNames.STATUS); int space = status.indexOf(' '); if (space == -1) { return HttpResponseStatus.valueOf(Integer.parseInt(status)); } else { int code = Integer.parseInt(status.substring(0, space)); String reasonPhrase = status.substring(space + 1); HttpResponseStatus responseStatus = HttpResponseStatus.valueOf(code); if (responseStatus.getReasonPhrase().equals(reasonPhrase)) { return responseStatus; } else { return new HttpResponseStatus(code, reasonPhrase); } } } catch (Exception e) { return null; } }
final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake");
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, isHixie76 ? "WebSocket Protocol Handshake" : "Web Socket Protocol Handshake")); res.headers().add(Names.UPGRADE, WEBSOCKET);
@Override public void sendError(int status, String message) throws IOException { if (committed) { throw new IllegalStateException(); } HttpResponseStatus responseStatus = null; if (message != null) { responseStatus = new HttpResponseStatus(status, message); } else { responseStatus = HttpResponseStatus.valueOf(status); } DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, responseStatus); if (keepAlive) { // Add keep alive and content length if needed response.headers() .add(Names.CONNECTION, Values.KEEP_ALIVE) .add(Names.CONTENT_LENGTH, 0); } channel.write(response); committed = true; }
@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 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 encode(Channel channel) throws Exception { HttpChannelConfig httpConfig = (HttpChannelConfig) channel.getConfig(); ChannelBufferFactory bufferFactory = httpConfig.getBufferFactory(); int code = Integer.parseInt(codeEncoder.encode(bufferFactory).toString(US_ASCII)); String reason = reasonEncoder.encode(bufferFactory).toString(US_ASCII); HttpResponseStatus status = new HttpResponseStatus(code, reason); httpConfig.setStatus(status); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!handshakeCompleted) { HttpResponse response = (HttpResponse)e.getMessage(); final HttpResponseStatus status = new HttpResponseStatus(101, "Web Socket Protocol Handshake"); final boolean validStatus = response.getStatus().equals(status); final boolean validUpgrade = response.getHeader(Names.UPGRADE).equals(Values.WEBSOCKET); final boolean validConnection = response.getHeader(Names.CONNECTION).equals(Values.UPGRADE); if (!validStatus || !validUpgrade || !validConnection) { throw new WebSocketException("Invalid handshake response"); } handshakeCompleted = true; ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocketFrameDecoder()); callback.onConnect(this); return; } if (e.getMessage() instanceof HttpResponse) { HttpResponse response = (HttpResponse) e.getMessage(); throw new WebSocketException("Unexpected HttpResponse (status=" + response.getStatus() + ", content=" + response.getContent().toString(CharsetUtil.UTF_8) + ")"); } DefaultWebSocketFrame frame = (DefaultWebSocketFrame)e.getMessage(); callback.onMessage(this, frame); }
@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; }
@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; }
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; }
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; }
private void writeHead() { if (!headWritten) { HttpResponseStatus status = statusMessage == null ? HttpResponseStatus.valueOf(statusCode) : new HttpResponseStatus(statusCode, statusMessage); response.setStatus(status); if (version == HttpVersion.HTTP_1_0 && keepAlive) { response.setHeader("Connection", "Keep-Alive"); } writeHeaders(); if (chunked) { response.setHeader(Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); } else if (version != HttpVersion.HTTP_1_0 && !contentLengthSet()) { response.setHeader(Names.CONTENT_LENGTH, "0"); } channelFuture = conn.write(response); headWritten = true; } }
@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 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); } }