List of usage examples for io.netty.handler.codec.http HttpHeaderNames UPGRADE
AsciiString UPGRADE
To view the source code for io.netty.handler.codec.http HttpHeaderNames UPGRADE.
Click Source Link
From source file:com.linecorp.armeria.server.http.Http1RequestDecoder.java
License:Apache License
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof UpgradeEvent) { // Generate the initial Http2Settings frame, // so that the next handler knows the protocol upgrade occurred as well. ctx.fireChannelRead(DEFAULT_HTTP2_SETTINGS); // Continue handling the upgrade request after the upgrade is complete. final FullHttpRequest nettyReq = ((UpgradeEvent) evt).upgradeRequest(); // Remove the headers related with the upgrade. nettyReq.headers().remove(HttpHeaderNames.CONNECTION); nettyReq.headers().remove(HttpHeaderNames.UPGRADE); nettyReq.headers().remove(Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER); if (logger.isDebugEnabled()) { logger.debug("{} Handling the pre-upgrade request ({}): {} {} {} ({}B)", ctx.channel(), ((UpgradeEvent) evt).protocol(), nettyReq.method(), nettyReq.uri(), nettyReq.protocolVersion(), nettyReq.content().readableBytes()); }/*from w w w .j a v a 2 s . com*/ channelRead(ctx, nettyReq); channelReadComplete(ctx); return; } ctx.fireUserEventTriggered(evt); }
From source file:eastwind.webpush.WebPushHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws JsonProcessingException { // Handle a bad request. if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req,/*from ww w .java 2 s.com*/ new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); return; } // Allow only GET methods. if (req.method() != HttpMethod.GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); return; } String uid = null; String uuid = null; Channel channel = ctx.channel(); int q = req.uri().indexOf("?"); String path = req.uri(); if (q != -1) { path = path.substring(0, q); } List<String> l = Lists.newLinkedList(Splitter.on("/").omitEmptyStrings().trimResults().split(path)); if (l.size() >= 2) { uid = l.get(0); uuid = l.get(1); Session s = sessionManager.get(uid, uuid); if (s == null) { logger.info("expired:{}-{}", uid, uuid); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)); } if (l.size() == 2) { s.setChannel(channel); s.trySendMessages(); } else { String oper = l.get(2); handleHttpOper(ctx, req, s, oper); } } else { String params = ""; if (q != -1) { String uri = req.uri(); if (q < uri.length() - 1) { params = uri.substring(q + 1); } } try { uid = action.active(channel.remoteAddress(), params); } catch (Throwable th) { logger.warn("active:", th); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.copiedBuffer(th.getClass().getName(), Charset.forName("utf-8")))); return; } if (uid != null) { uuid = sessionManager.create(uid).getUuid(); logger.info("active:{}-{}", uid, uuid); SessionGroup sg = sessionManager.get(uid); timer.newTimeout(new SessionCleaner(sg), lost, TimeUnit.MILLISECONDS); } // websocket if ("Upgrade".equals(req.headers().get(HttpHeaderNames.CONNECTION)) && "websocket".equals(req.headers().get(HttpHeaderNames.UPGRADE))) { // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( getWebSocketLocation(req, ""), null, true); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(channel); return; } else { handshaker.handshake(channel, req); UserLite.set(channel, new UserLite(uid, uuid)); } } else { String content = String.format("{\"uid\":\"%s\", \"uuid\":\"%s\"}", uid, uuid); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.copiedBuffer(content, Charset.forName("utf-8")))); } } }
From source file:lee.study.handle.WebSocketServerHandler.java
License:Apache License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { // Handle a bad request. if (!req.decoderResult().isSuccess()) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST)); return;/* w w w . ja va 2 s .c om*/ } // Allow only GET methods. if (req.method() != GET) { sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN)); return; } if ("/favicon.ico".equals(req.uri())) { FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); sendHttpResponse(ctx, req, res); return; } if (HttpHeaderValues.WEBSOCKET.toString().equals(req.headers().get(HttpHeaderNames.UPGRADE))) { // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( getWebSocketLocation(req), null, true, 5 * 1024 * 1024); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { handshaker.handshake(ctx.channel(), req); Hero hero = new Hero(); // Room.room.put(ctx, hero); } } else { FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND); sendHttpResponse(ctx, req, res); return; } }
From source file:net.anyflow.menton.http.HttpRequestRouter.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (HttpHeaderValues.WEBSOCKET.toString().equalsIgnoreCase(request.headers().get(HttpHeaderNames.UPGRADE)) && HttpHeaderValues.UPGRADE.toString() .equalsIgnoreCase(request.headers().get(HttpHeaderNames.CONNECTION))) { if (ctx.pipeline().get(WebsocketFrameHandler.class) == null) { logger.error("No WebSocket Handler available."); ctx.channel()/* w w w . ja va 2 s. c om*/ .writeAndFlush( new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN)) .addListener(ChannelFutureListener.CLOSE); return; } ctx.fireChannelRead(request.retain()); return; } if (HttpUtil.is100ContinueExpected(request)) { ctx.write(new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE)); return; } HttpResponse response = HttpResponse.createServerDefault(request.headers().get(HttpHeaderNames.COOKIE)); String requestPath = new URI(request.uri()).getPath(); if (isWebResourcePath(requestPath)) { handleWebResourceRequest(ctx, request, response, requestPath); } else { try { processRequest(ctx, request, response); } catch (URISyntaxException e) { response.setStatus(HttpResponseStatus.NOT_FOUND); logger.info("unexcepted URI : {}", request.uri()); } catch (Exception e) { response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); logger.error("Unknown exception was thrown in business logic handler.\r\n" + e.getMessage(), e); } } }
From source file:org.thingsplode.synapse.endpoint.handlers.HttpRequestHandler.java
License:Apache License
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest) throws Exception { //todo: support for API keys ///endpoints/json?api_key=565656 try {/*from w w w. ja v a 2 s.c om*/ // Handle a bad request. if (!httpRequest.decoderResult().isSuccess()) { HttpResponseHandler.sendError(ctx, HttpResponseStatus.BAD_REQUEST, "Could not decode request.", httpRequest); return; } if (httpRequest.method().equals(HttpMethod.HEAD) || httpRequest.method().equals(HttpMethod.PATCH) || httpRequest.method().equals(HttpMethod.TRACE) || httpRequest.method().equals(HttpMethod.CONNECT) || httpRequest.method().equals(HttpMethod.OPTIONS)) { HttpResponseHandler.sendError(ctx, HttpResponseStatus.FORBIDDEN, "Method forbidden (The following are not supported: HEAD, PATCH, TRACE, CONNECT, OPTIONS).", httpRequest); return; } //check websocket upgrade request String upgradeHeader = httpRequest.headers().get(HttpHeaderNames.UPGRADE); if (!Util.isEmpty(upgradeHeader) && UPGRADE_TO_WEBSOCKET.equalsIgnoreCase(upgradeHeader)) { //case websocket upgrade request is detected -> Prepare websocket handshake upgradeToWebsocket(ctx, httpRequest); return; } else { //case simple http request Request request = new Request(prepareHeader(ctx, httpRequest)); //no information about the object type / it will be processed in a later stage //todo: with requestbodytype header value early deserialization would be possible, however not beneficial in routing cases request.setBody(httpRequest.content()); ctx.fireChannelRead(request); } } catch (Exception ex) { logger.error("Channel read error: " + ex.getMessage(), ex); HttpResponseHandler.sendError(ctx, HttpResponseStatus.INTERNAL_SERVER_ERROR, ex.getClass().getSimpleName() + ": " + ex.getMessage(), httpRequest); } }
From source file:reactor.ipc.netty.http.server.HttpServerOperations.java
License:Open Source License
@Override public boolean isWebsocket() { return requestHeaders().contains(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true); }