Refine search
@SuppressWarnings("unchecked") @Override public <T> T upgrade(final Class<T> type) { if (type == NativeWebSocket.class) { String protocol = ifSecure("wss", "ws"); String webSocketURL = protocol + "://" + req.headers().get(HttpHeaderNames.HOST) + path; WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( webSocketURL, null, true, wsMaxMessageSize); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); NettyWebSocket result = new NettyWebSocket(ctx, handshaker, (ws) -> { handshaker.handshake(ctx.channel(), (FullHttpRequest) req) .addListener(FIRE_EXCEPTION_ON_FAILURE) .addListener(payload -> ws.connect()) .addListener(FIRE_EXCEPTION_ON_FAILURE); }); ctx.channel().attr(NettyWebSocket.KEY).set(result); return (T) result; } else if (type == Sse.class) { NettySse sse = new NettySse(ctx, responseHeaders); return (T) sse; } else if (type == NativePushPromise.class) { return (T) new NettyPush(ctx, req.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()), header("host").orElse(ip()), ifSecure("https", "http")); } throw new UnsupportedOperationException("Not Supported: " + type); }
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) { String protocol = "ws"; if (cp.get(SslHandler.class) != null) { // SSL in use so use Secure WebSockets protocol = "wss"; } String host = req.headers().get(HttpHeaderNames.HOST); return protocol + "://" + host + path; } }
@SuppressWarnings("deprecation") private SpdySynStreamFrame createSynStreamFrame(HttpRequest httpRequest) throws Exception { final HttpHeaders httpHeaders = httpRequest.headers(); int streamId = httpHeaders.getInt(SpdyHttpHeaders.Names.STREAM_ID); int associatedToStreamId = httpHeaders.getInt(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID, 0); byte priority = (byte) httpHeaders.getInt(SpdyHttpHeaders.Names.PRIORITY, 0); CharSequence scheme = httpHeaders.get(SpdyHttpHeaders.Names.SCHEME); httpHeaders.remove(SpdyHttpHeaders.Names.STREAM_ID); httpHeaders.remove(SpdyHttpHeaders.Names.ASSOCIATED_TO_STREAM_ID); CharSequence host = httpHeaders.get(HttpHeaderNames.HOST); httpHeaders.remove(HttpHeaderNames.HOST); frameHeaders.set(SpdyHeaders.HttpNames.HOST, host);
static String getWebSocketLocation(HttpRequest req, boolean ssl) throws Exception { String prefix; if (ssl) { prefix = "ws://"; } else { prefix = "wss://"; } URI uri = new URI(req.uri()); String path = uri.getRawPath(); String loc = prefix + req.headers().get(HttpHeaderNames.HOST) + path; String query = uri.getRawQuery(); if (query != null) { loc += "?" + query; } return loc; }
/** * Check if the given request is a multipart request * @return True if the request is a Multipart request */ public static boolean isMultipart(HttpRequest request) { if (request.headers().contains(HttpHeaderNames.CONTENT_TYPE)) { return getMultipartDataBoundary(request.headers().get(HttpHeaderNames.CONTENT_TYPE)) != null; } else { return false; } }
private void prepareRequestHeaders(HttpRequest request, String hostHeader, boolean chunked) { HttpHeaders headers = request.headers(); headers.remove(TRANSFER_ENCODING); if (!headers.contains(HOST)) { request.headers().set(HOST, hostHeader); } if (chunked) { HttpUtil.setTransferEncodingChunked(request, true); } if (conn.options.isTryUseCompression() && request.headers().get(ACCEPT_ENCODING) == null) { // if compression should be used but nothing is specified by the user support deflate and gzip. request.headers().set(ACCEPT_ENCODING, DEFLATE_GZIP); } if (!conn.options.isKeepAlive() && conn.options.getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_1) { request.headers().set(CONNECTION, CLOSE); } else if (conn.options.isKeepAlive() && conn.options.getProtocolVersion() == io.vertx.core.http.HttpVersion.HTTP_1_0) { request.headers().set(CONNECTION, KEEP_ALIVE); } }
@Override public List<org.jooby.Cookie> cookies() { if (this.cookies == null) { String cookieString = req.headers().get(HttpHeaderNames.COOKIE); if (cookieString != null) { this.cookies = ServerCookieDecoder.STRICT.decode(cookieString).stream() .map(this::cookie) .collect(Collectors.toList()); } else { this.cookies = Collections.emptyList(); } } return this.cookies; }
@Override protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception { CharSequence acceptedEncoding = msg.headers().get(HttpHeaderNames.ACCEPT_ENCODING); if (acceptedEncoding == null) { acceptedEncoding = HttpContentDecoder.IDENTITY; } HttpMethod meth = msg.method(); if (meth == HttpMethod.HEAD) { acceptedEncoding = ZERO_LENGTH_HEAD; } else if (meth == HttpMethod.CONNECT) { acceptedEncoding = ZERO_LENGTH_CONNECT; } acceptEncodingQueue.add(acceptedEncoding); out.add(ReferenceCountUtil.retain(msg)); }
public static User validateAuth(UserDao userDao, HttpRequest req) throws IllegalAccessException { String auth = req.headers().get(HttpHeaderNames.AUTHORIZATION); if (auth != null) { try { String encodedAuth = auth.substring("Basic ".length()); String decoded = new String(java.util.Base64.getDecoder().decode(encodedAuth)); String[] userAndPass = decoded.split(":"); String user = userAndPass[0].toLowerCase(); String pass = userAndPass[1]; User superUser = userDao.getSuperAdmin(); String passHash = SHA256Util.makeHash(pass, user); log.info("Header auth attempt. User: {}, pass: {}", user, pass); if (superUser != null && superUser.email.equals(user) && superUser.pass.equals(passHash)) { return superUser; } else { throw new IllegalAccessException("Authentication failed."); } } catch (IllegalAccessException iae) { log.error("Error invoking OTA handler. {}", iae.getMessage()); throw iae; } catch (Exception e) { log.error("Error invoking OTA handler."); } } return null; } }
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { if (msg instanceof HttpRequest) { request = (HttpRequest) msg; final String origin = request.headers().get(HttpHeaderNames.ORIGIN); config = getForOrigin(origin); if (isPreflightRequest(request)) { handlePreflight(ctx, request); return; } if (isShortCircuit && !(origin == null || config != null)) { forbidden(ctx, request); return; } } ctx.fireChannelRead(msg); }
if (msg instanceof HttpRequest) { HttpRequest request = (HttpRequest) msg; if (request.headers().contains(io.vertx.core.http.HttpHeaders.UPGRADE, Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, true)) { String connection = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION); int found = 0; if (connection != null && connection.length() > 0) { String settingsHeader = request.headers().get(Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER); if (settingsHeader != null) { Http2Settings settings = HttpUtils.decodeSettings(settingsHeader); headers.method(request.method().name()); headers.path(request.uri()); headers.authority(request.headers().get("host")); headers.scheme("http"); request.headers().remove("http2-settings"); request.headers().remove("host"); request.headers().forEach(header -> headers.set(header.getKey().toLowerCase(), header.getValue())); ctx.fireChannelRead(new DefaultHttp2HeadersFrame(headers, false)); } else {
@Override public void channelRead0(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof HttpRequest) { ctx.channel().attr(NettyRequest.NEED_FLUSH).set(true); HttpRequest req = (HttpRequest) msg; ctx.channel().attr(PATH).set(req.method().name() + " " + req.uri()); if (HttpUtil.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.CONTINUE)); } boolean keepAlive = HttpUtil.isKeepAlive(req); try { String streamId = req.headers().get(STREAM_ID); HttpHeaders headers = new DefaultHttpHeaders(); handler.handle( new NettyRequest(ctx, req, headers, tmpdir, wsMaxMessageSize), new NettyResponse(ctx, headers, bufferSize, keepAlive, streamId)); } catch (Throwable ex) { exceptionCaught(ctx, ex); } } else if (msg instanceof WebSocketFrame) { Attribute<NettyWebSocket> ws = ctx.channel().attr(NettyWebSocket.KEY); ws.get().handle(msg); } }