private NetSocket getUpgradedNetSocket(HttpServerRequest req, String path) { assertEquals(path, req.path()); assertEquals("Upgrade", req.headers().get("Connection")); NetSocket sock = req.netSocket(); String secHeader = req.headers().get("Sec-WebSocket-Key"); String tmp = secHeader + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; String encoded = sha1(tmp); sock.write( "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + encoded + "\r\n" + "\r\n"); return sock; }
@Override public void handle(RoutingContext context) { HttpServerRequest req = context.request(); String remainingPath = Utils.pathOffset(req.path(), context); if (req.method() != HttpMethod.GET && req.method() != HttpMethod.POST) { context.next(); } JSONAware json = null; try { // Check access policy requestHandler.checkAccess( req.remoteAddress().host(), req.remoteAddress().host(), getOriginOrReferer(req)); if (req.method() == HttpMethod.GET) { json = requestHandler.handleGetRequest(req.uri(), remainingPath, getParams(req.params())); } else { Arguments.require( context.getBody() != null, "Missing body, make sure that BodyHandler is used before"); // TODO how to get Stream ? InputStream inputStream = new ByteBufInputStream(context.getBody().getByteBuf()); json = requestHandler.handlePostRequest( req.uri(), inputStream, StandardCharsets.UTF_8.name(), getParams(req.params())); } } catch (Throwable exp) { json = requestHandler.handleThrowable( exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp); } finally { if (json == null) json = requestHandler.handleThrowable( new Exception("Internal error while handling an exception")); context .response() .setStatusCode(getStatusCode(json)) .putHeader(HttpHeaders.CONTENT_TYPE, contentType) .end(json.toJSONString()); } }
public HttpRequest createRequest( final HttpServerRequest request, final Buffer buffer, final CopyOnWriteArrayList<HttpResponseDecorator> decorators, final HttpResponseCreator httpResponseCreator) { final MultiMap<String, String> headers = request.headers().size() == 0 ? MultiMap.empty() : new MultiMapWrapper(request.headers()); final String contentType = request.headers().get("Content-Type"); final byte[] body = HttpContentTypes.isFormContentType(contentType) || buffer == null ? new byte[0] : buffer.getBytes(); final MultiMap<String, String> params = buildParams(request, contentType); final HttpRequestBuilder httpRequestBuilder = HttpRequestBuilder.httpRequestBuilder(); final String requestPath = request.path(); httpRequestBuilder .setId(requestId.incrementAndGet()) .setUri(requestPath) .setMethod(request.method().toString()) .setParams(params) .setBodyBytes(body) .setRemoteAddress(request.remoteAddress().toString()) .setResponse( createResponse( requestPath, headers, params, request.response(), decorators, httpResponseCreator)) .setTimestamp(time == 0L ? Timer.timer().now() : time) .setHeaders(headers); return httpRequestBuilder.build(); }