/** * Return that we need cannot not support the web socket version * * @param channel * Channel */ public ChannelFuture sendUnsupportedWebSocketVersionResponse(Channel channel) { HttpResponse res = new DefaultHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.SWITCHING_PROTOCOLS); res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED); res.headers().set(Names.SEC_WEBSOCKET_VERSION, WebSocketVersion.V13.toHttpHeaderValue()); return channel.write(res); }
@Override public NettyHttpResponse status(int status) { response.setStatus(HttpResponseStatus.valueOf(status)); return this; }
res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS); res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase()); res.headers().add(Names.CONNECTION, Names.UPGRADE);
res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS); res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase()); res.headers().add(Names.CONNECTION, Names.UPGRADE);
res.setStatus(HttpResponseStatus.SWITCHING_PROTOCOLS); res.headers().add(Names.UPGRADE, WEBSOCKET.toLowerCase()); res.headers().add(Names.CONNECTION, Names.UPGRADE);
@Override public NettyHttpResponse error(Throwable error) { if (error instanceof TooLongFrameException) { response.setStatus(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); } else { response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); } String message = getStackTrace(error); header("Content-Type", "text/plain"); content(message); flushResponse(); exceptionHandler.uncaughtException(Thread.currentThread(), WebbitException.fromException(error, ctx.getChannel())); return this; }
@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); } }
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-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.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 NettyHttpResponse status(int status) { response.setStatus(HttpResponseStatus.valueOf(status)); return this; }
@Override public NettyHttpResponse error(Throwable error) { if (error instanceof TooLongFrameException) { response.setStatus(HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE); } else { response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); } String message = getStackTrace(error); header("Content-Type", "text/plain"); content(message); flushResponse(); exceptionHandler.uncaughtException(Thread.currentThread(), WebbitException.fromException(error, ctx.getChannel())); return this; }
@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); } }
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-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); }
if (uri.equals("/")) { final HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setStatus(MOVED_PERMANENTLY); setHeader(response, LOCATION, "/fake-stream");
public void handle(HttpRequest request, HttpResponse response) { QueryStringDecoder qsd = new QueryStringDecoder(request.getUri()); String path = qsd.getPath(); if (!path.matches(".*/iframe[0-9-.a-z_]*.html")) { response.setStatus(HttpResponseStatus.NOT_FOUND); response.setContent(ChannelBuffers.copiedBuffer("Not found", CharsetUtil.UTF_8)); return; } response.setHeader(HttpHeaders.Names.SET_COOKIE, "JSESSIONID=dummy; path=/"); if (request.containsHeader(HttpHeaders.Names.IF_NONE_MATCH)) { response.setStatus(HttpResponseStatus.NOT_MODIFIED); response.removeHeader(HttpHeaders.Names.CONTENT_TYPE); } else { response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/html; charset=UTF-8"); response.setHeader(HttpHeaders.Names.CACHE_CONTROL, "max-age=31536000, public"); response.setHeader(HttpHeaders.Names.EXPIRES, "FIXME"); // FIXME: Fix this response.removeHeader(HttpHeaders.Names.SET_COOKIE); response.setContent(content); } response.setHeader(HttpHeaders.Names.ETAG, etag); }
public void prepareNettyResponse(HttpResponse nettyResponse) { nettyResponse.headers().add("Accept-Ranges", "bytes"); if(unsatisfiable) { nettyResponse.setStatus(HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE); nettyResponse.headers().set("Content-Range", "bytes " + 0 + "-" + (fileLength-1) + "/" + fileLength); nettyResponse.headers().set("Content-length", 0); } else { nettyResponse.setStatus(HttpResponseStatus.PARTIAL_CONTENT); if(byteRanges.length == 1) { ByteRange range = byteRanges[0]; nettyResponse.headers().set("Content-Range", "bytes " + range.start + "-" + range.end + "/" + fileLength); } else { nettyResponse.headers().set("Content-type", "multipart/byteranges; boundary="+DEFAULT_SEPARATOR); } long length = 0; for(ByteRange range: byteRanges) { length += range.computeTotalLength(); } nettyResponse.headers().set("Content-length", length); } }
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.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); } }