List of usage examples for io.vertx.core.http HttpClientRequest headers
@CacheReturn MultiMap headers();
From source file:de.braintags.netrelay.util.MultipartUtil.java
License:Open Source License
/** * Completes the request and writes the buffer to the connection * /*from w w w . j a va2s . c o m*/ */ public void finish(HttpClientRequest httpConn) { httpConn.headers().set("Content-Type", contentType + "; boundary=" + boundary); httpConn.headers().set("User-Agent", "CodeJava Agent"); httpConn.headers().set("Test", "Bonjour"); // writer.appendString(LINE_FEED); writer.appendString("--" + boundary + "--").appendString(LINE_FEED); httpConn.headers().set("content-length", String.valueOf(writer.length())); httpConn.write(writer); }
From source file:examples.HTTPExamples.java
License:Open Source License
public void example37(HttpClientRequest request) { // Write some headers using the headers() multimap MultiMap headers = request.headers(); headers.set("content-type", "application/json").set("other-header", "foo"); }
From source file:io.apiman.gateway.engine.vertx.polling.fetchers.HttpResourceFetcher.java
License:Apache License
@Override public void fetch(Handler<Buffer> resultHandler) { int port = uri.getPort(); if (port == -1) { if (isHttps) { port = 443;// w ww . j a v a2s. co m } else { port = 80; } } HttpClientRequest httpClientRequest = vertx.createHttpClient(new HttpClientOptions().setSsl(isHttps)) .get(port, uri.getHost(), uri.getPath(), clientResponse -> { if (clientResponse.statusCode() / 100 == 2) { clientResponse.handler(data -> { rawData.appendBuffer(data); }).endHandler(end -> resultHandler.handle(rawData)).exceptionHandler(exceptionHandler); } else { exceptionHandler.handle( new BadResponseCodeError("Unexpected response code when trying to retrieve config: " //$NON-NLS-1$ + clientResponse.statusCode())); } }).exceptionHandler(exceptionHandler); authenticator.authenticate(vertx, config, httpClientRequest.headers(), authResult -> { if (authResult.succeeded()) { // The client request is executed when HttpClientRequest#end is invoked. httpClientRequest.end(); } else { exceptionHandler.handle(authResult.cause()); } }); }
From source file:io.gravitee.gateway.services.healthcheck.http.HttpEndpointRuleHandler.java
License:Apache License
@Override public void handle(Long timer) { HttpEndpoint endpoint = (HttpEndpoint) rule.endpoint(); logger.debug("Running health-check for endpoint: {} [{}]", endpoint.getName(), endpoint.getTarget()); // Run request for each step for (io.gravitee.definition.model.services.healthcheck.Step step : rule.steps()) { try {// w w w . ja va 2s . c om URI hcRequestUri = create(endpoint.getTarget(), step.getRequest()); // Prepare HTTP client HttpClientOptions httpClientOptions = new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(false) .setTcpKeepAlive(false); if (endpoint.getHttpClientOptions() != null) { httpClientOptions .setIdleTimeout((int) (endpoint.getHttpClientOptions().getIdleTimeout() / 1000)) .setConnectTimeout((int) endpoint.getHttpClientOptions().getConnectTimeout()) .setTryUseCompression(endpoint.getHttpClientOptions().isUseCompression()); } // Configure HTTP proxy HttpProxy proxy = endpoint.getHttpProxy(); if (proxy != null && proxy.isEnabled()) { ProxyOptions proxyOptions = new ProxyOptions().setHost(proxy.getHost()).setPort(proxy.getPort()) .setUsername(proxy.getUsername()).setPassword(proxy.getPassword()) .setType(ProxyType.valueOf(proxy.getType().name())); httpClientOptions.setProxyOptions(proxyOptions); } HttpClientSslOptions sslOptions = endpoint.getHttpClientSslOptions(); if (HTTPS_SCHEME.equalsIgnoreCase(hcRequestUri.getScheme())) { // Configure SSL httpClientOptions.setSsl(true); if (sslOptions != null) { httpClientOptions.setVerifyHost(sslOptions.isHostnameVerifier()) .setTrustAll(sslOptions.isTrustAll()); // Client trust configuration if (!sslOptions.isTrustAll() && sslOptions.getTrustStore() != null) { switch (sslOptions.getTrustStore().getType()) { case PEM: PEMTrustStore pemTrustStore = (PEMTrustStore) sslOptions.getTrustStore(); PemTrustOptions pemTrustOptions = new PemTrustOptions(); if (pemTrustStore.getPath() != null && !pemTrustStore.getPath().isEmpty()) { pemTrustOptions.addCertPath(pemTrustStore.getPath()); } else if (pemTrustStore.getContent() != null && !pemTrustStore.getContent().isEmpty()) { pemTrustOptions.addCertValue( io.vertx.core.buffer.Buffer.buffer(pemTrustStore.getContent())); } else { throw new EndpointException( "Missing PEM certificate value for endpoint " + endpoint.getName()); } httpClientOptions.setPemTrustOptions(pemTrustOptions); break; case PKCS12: PKCS12TrustStore pkcs12TrustStore = (PKCS12TrustStore) sslOptions.getTrustStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12TrustStore.getPassword()); if (pkcs12TrustStore.getPath() != null && !pkcs12TrustStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12TrustStore.getPath()); } else if (pkcs12TrustStore.getContent() != null && !pkcs12TrustStore.getContent().isEmpty()) { pfxOptions.setValue( io.vertx.core.buffer.Buffer.buffer(pkcs12TrustStore.getContent())); } else { throw new EndpointException( "Missing PKCS12 value for endpoint " + endpoint.getName()); } httpClientOptions.setPfxTrustOptions(pfxOptions); break; case JKS: JKSTrustStore jksTrustStore = (JKSTrustStore) sslOptions.getTrustStore(); JksOptions jksOptions = new JksOptions(); jksOptions.setPassword(jksTrustStore.getPassword()); if (jksTrustStore.getPath() != null && !jksTrustStore.getPath().isEmpty()) { jksOptions.setPath(jksTrustStore.getPath()); } else if (jksTrustStore.getContent() != null && !jksTrustStore.getContent().isEmpty()) { jksOptions.setValue( io.vertx.core.buffer.Buffer.buffer(jksTrustStore.getContent())); } else { throw new EndpointException( "Missing JKS value for endpoint " + endpoint.getName()); } httpClientOptions.setTrustStoreOptions(jksOptions); break; } } // Client authentication configuration if (sslOptions.getKeyStore() != null) { switch (sslOptions.getKeyStore().getType()) { case PEM: PEMKeyStore pemKeyStore = (PEMKeyStore) sslOptions.getKeyStore(); PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions(); if (pemKeyStore.getCertPath() != null && !pemKeyStore.getCertPath().isEmpty()) { pemKeyCertOptions.setCertPath(pemKeyStore.getCertPath()); } else if (pemKeyStore.getCertContent() != null && !pemKeyStore.getCertContent().isEmpty()) { pemKeyCertOptions.setCertValue( io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getCertContent())); } if (pemKeyStore.getKeyPath() != null && !pemKeyStore.getKeyPath().isEmpty()) { pemKeyCertOptions.setKeyPath(pemKeyStore.getKeyPath()); } else if (pemKeyStore.getKeyContent() != null && !pemKeyStore.getKeyContent().isEmpty()) { pemKeyCertOptions.setKeyValue( io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getKeyContent())); } httpClientOptions.setPemKeyCertOptions(pemKeyCertOptions); break; case PKCS12: PKCS12KeyStore pkcs12KeyStore = (PKCS12KeyStore) sslOptions.getKeyStore(); PfxOptions pfxOptions = new PfxOptions(); pfxOptions.setPassword(pkcs12KeyStore.getPassword()); if (pkcs12KeyStore.getPath() != null && !pkcs12KeyStore.getPath().isEmpty()) { pfxOptions.setPath(pkcs12KeyStore.getPath()); } else if (pkcs12KeyStore.getContent() != null && !pkcs12KeyStore.getContent().isEmpty()) { pfxOptions.setValue( io.vertx.core.buffer.Buffer.buffer(pkcs12KeyStore.getContent())); } httpClientOptions.setPfxKeyCertOptions(pfxOptions); break; case JKS: JKSKeyStore jksKeyStore = (JKSKeyStore) sslOptions.getKeyStore(); JksOptions jksOptions = new JksOptions(); jksOptions.setPassword(jksKeyStore.getPassword()); if (jksKeyStore.getPath() != null && !jksKeyStore.getPath().isEmpty()) { jksOptions.setPath(jksKeyStore.getPath()); } else if (jksKeyStore.getContent() != null && !jksKeyStore.getContent().isEmpty()) { jksOptions .setValue(io.vertx.core.buffer.Buffer.buffer(jksKeyStore.getContent())); } httpClientOptions.setKeyStoreOptions(jksOptions); break; } } } } HttpClient httpClient = vertx.createHttpClient(httpClientOptions); final int port = hcRequestUri.getPort() != -1 ? hcRequestUri.getPort() : (HTTPS_SCHEME.equals(hcRequestUri.getScheme()) ? 443 : 80); String relativeUri = (hcRequestUri.getRawQuery() == null) ? hcRequestUri.getRawPath() : hcRequestUri.getRawPath() + '?' + hcRequestUri.getRawQuery(); // Run health-check HttpClientRequest healthRequest = httpClient.request( HttpMethod.valueOf(step.getRequest().getMethod().name().toUpperCase()), port, hcRequestUri.getHost(), relativeUri); // Set timeout on request if (endpoint.getHttpClientOptions() != null) { healthRequest.setTimeout(endpoint.getHttpClientOptions().getReadTimeout()); } // Prepare request if (step.getRequest().getHeaders() != null) { step.getRequest().getHeaders().forEach( httpHeader -> healthRequest.headers().set(httpHeader.getName(), httpHeader.getValue())); } final EndpointStatus.Builder healthBuilder = EndpointStatus .forEndpoint(rule.api(), endpoint.getName()).on(currentTimeMillis()); long startTime = currentTimeMillis(); Request request = new Request(); request.setMethod(step.getRequest().getMethod()); request.setUri(hcRequestUri.toString()); healthRequest.handler(response -> response.bodyHandler(buffer -> { long endTime = currentTimeMillis(); logger.debug("Health-check endpoint returns a response with a {} status code", response.statusCode()); String body = buffer.toString(); EndpointStatus.StepBuilder stepBuilder = validateAssertions(step, new EvaluableHttpResponse(response, body)); stepBuilder.request(request); stepBuilder.responseTime(endTime - startTime); Response healthResponse = new Response(); healthResponse.setStatus(response.statusCode()); // If validation fail, store request and response data if (!stepBuilder.isSuccess()) { request.setBody(step.getRequest().getBody()); if (step.getRequest().getHeaders() != null) { HttpHeaders reqHeaders = new HttpHeaders(); step.getRequest().getHeaders().forEach(httpHeader -> reqHeaders .put(httpHeader.getName(), Collections.singletonList(httpHeader.getValue()))); request.setHeaders(reqHeaders); } // Extract headers HttpHeaders headers = new HttpHeaders(); response.headers().names().forEach( headerName -> headers.put(headerName, response.headers().getAll(headerName))); healthResponse.setHeaders(headers); // Store body healthResponse.setBody(body); } stepBuilder.response(healthResponse); // Append step stepBuilder healthBuilder.step(stepBuilder.build()); report(healthBuilder.build()); // Close client httpClient.close(); })); healthRequest.exceptionHandler(event -> { long endTime = currentTimeMillis(); EndpointStatus.StepBuilder stepBuilder = EndpointStatus.forStep(step.getName()); stepBuilder.fail(event.getMessage()); Response healthResponse = new Response(); // Extract request information request.setBody(step.getRequest().getBody()); if (step.getRequest().getHeaders() != null) { HttpHeaders reqHeaders = new HttpHeaders(); step.getRequest().getHeaders().forEach(httpHeader -> reqHeaders.put(httpHeader.getName(), Collections.singletonList(httpHeader.getValue()))); request.setHeaders(reqHeaders); } if (event instanceof ConnectTimeoutException) { stepBuilder.fail(event.getMessage()); healthResponse.setStatus(HttpStatusCode.REQUEST_TIMEOUT_408); } else { healthResponse.setStatus(HttpStatusCode.SERVICE_UNAVAILABLE_503); } Step result = stepBuilder.build(); result.setResponse(healthResponse); result.setRequest(request); result.setResponseTime(endTime - startTime); // Append step result healthBuilder.step(result); report(healthBuilder.build()); try { // Close client httpClient.close(); } catch (IllegalStateException ise) { // Do not take care about exception when closing client } }); // Send request logger.debug("Execute health-check request: {}", healthRequest); if (step.getRequest().getBody() != null && !step.getRequest().getBody().isEmpty()) { healthRequest.end(step.getRequest().getBody()); } else { healthRequest.end(); } } catch (EndpointException ee) { logger.error("An error occurs while configuring the endpoint " + endpoint.getName() + ". Healthcheck is skipped for this endpoint.", ee); } catch (Exception ex) { logger.error("An unexpected error occurs", ex); } } }
From source file:io.gravitee.resource.oauth2.am.OAuth2AMResource.java
License:Apache License
@Override public void introspect(String accessToken, Handler<OAuth2Response> responseHandler) { HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(), context -> vertx.createHttpClient(httpClientOptions)); logger.debug("Introspect access token by requesting {}", introspectionEndpointURI); HttpClientRequest request = httpClient.post(introspectionEndpointURI); request.headers().add(HttpHeaders.AUTHORIZATION, introspectionEndpointAuthorization); request.headers().add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); request.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); request.handler(response -> response.bodyHandler(buffer -> { logger.debug("AM Introspection endpoint returns a response with a {} status code", response.statusCode());/*from w w w . j a v a 2 s . c o m*/ if (response.statusCode() == HttpStatusCode.OK_200) { // Introspection Response for AM v2 always returns HTTP 200 // with an "active" boolean indicator of whether or not the presented token is currently active. if (configuration().getVersion() == OAuth2ResourceConfiguration.Version.V2_X) { // retrieve active indicator JsonObject jsonObject = buffer.toJsonObject(); boolean active = jsonObject.getBoolean(INTROSPECTION_ACTIVE_INDICATOR, false); responseHandler.handle(new OAuth2Response(active, (active) ? buffer.toString() : "{\"error\": \"Invalid Access Token\"}")); } else { responseHandler.handle(new OAuth2Response(true, buffer.toString())); } } else { responseHandler.handle(new OAuth2Response(false, buffer.toString())); } })); request.exceptionHandler(event -> { logger.error("An error occurs while checking access_token", event); responseHandler.handle(new OAuth2Response(false, event.getMessage())); }); request.end("token=" + accessToken); }
From source file:io.gravitee.resource.oauth2.am.OAuth2AMResource.java
License:Apache License
@Override public void userInfo(String accessToken, Handler<UserInfoResponse> responseHandler) { HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(), context -> vertx.createHttpClient(httpClientOptions)); logger.debug("Get userinfo from {}", userInfoEndpointURI); HttpClientRequest request = httpClient.get(userInfoEndpointURI); request.headers().add(HttpHeaders.AUTHORIZATION, AUTHORIZATION_HEADER_BEARER_SCHEME + accessToken); request.headers().add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); request.handler(response -> response.bodyHandler(buffer -> { logger.debug("Userinfo endpoint returns a response with a {} status code", response.statusCode()); if (response.statusCode() == HttpStatusCode.OK_200) { responseHandler.handle(new UserInfoResponse(true, buffer.toString())); } else {// ww w . j a v a2 s . c om responseHandler.handle(new UserInfoResponse(false, buffer.toString())); } })); request.exceptionHandler(event -> { logger.error("An error occurs while getting userinfo from access_token", event); responseHandler.handle(new UserInfoResponse(false, event.getMessage())); }); request.end(); }
From source file:io.gravitee.resource.oauth2.generic.OAuth2GenericResource.java
License:Apache License
@Override public void introspect(String accessToken, Handler<OAuth2Response> responseHandler) { HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(), context -> vertx.createHttpClient(httpClientOptions)); OAuth2ResourceConfiguration configuration = configuration(); StringBuilder introspectionUriBuilder = new StringBuilder(introspectionEndpointURI); if (configuration.isTokenIsSuppliedByQueryParam()) { introspectionUriBuilder.append('?').append(configuration.getTokenQueryParamName()).append('=') .append(accessToken);//w w w . j a v a2 s. c o m } String introspectionEndpointURI = introspectionUriBuilder.toString(); logger.debug("Introspect access token by requesting {} [{}]", introspectionEndpointURI, configuration.getIntrospectionEndpointMethod()); HttpMethod httpMethod = HttpMethod.valueOf(configuration.getIntrospectionEndpointMethod().toUpperCase()); HttpClientRequest request = httpClient.requestAbs(httpMethod, introspectionEndpointURI); request.setTimeout(30000L); if (configuration().isUseClientAuthorizationHeader()) { String authorizationHeader = configuration.getClientAuthorizationHeaderName(); String authorizationValue = configuration.getClientAuthorizationHeaderScheme().trim() + AUTHORIZATION_HEADER_SCHEME_SEPARATOR + Base64.getEncoder().encodeToString( (configuration.getClientId() + AUTHORIZATION_HEADER_VALUE_BASE64_SEPARATOR + configuration.getClientSecret()).getBytes()); request.headers().add(authorizationHeader, authorizationValue); logger.debug("Set client authorization using HTTP header {} with value {}", authorizationHeader, authorizationValue); } // Set `Accept` header to ask for application/json content request.headers().add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); if (configuration.isTokenIsSuppliedByHttpHeader()) { request.headers().add(configuration.getTokenHeaderName(), accessToken); } request.handler(response -> response.bodyHandler(buffer -> { logger.debug("Introspection endpoint returns a response with a {} status code", response.statusCode()); if (response.statusCode() == HttpStatusCode.OK_200) { // According to RFC 7662 : Note that a properly formed and authorized query for an inactive or // otherwise invalid token (or a token the protected resource is not // allowed to know about) is not considered an error response by this // specification. In these cases, the authorization server MUST instead // respond with an introspection response with the "active" field set to // "false" as described in Section 2.2. String content = buffer.toString(); try { JsonNode introspectNode = MAPPER.readTree(content); JsonNode activeNode = introspectNode.get("active"); if (activeNode != null) { boolean isActive = activeNode.asBoolean(); responseHandler.handle(new OAuth2Response(isActive, content)); } else { responseHandler.handle(new OAuth2Response(true, content)); } } catch (IOException e) { logger.error("Unable to validate introspection endpoint payload: {}", content); responseHandler.handle(new OAuth2Response(false, content)); } } else { responseHandler.handle(new OAuth2Response(false, buffer.toString())); } })); request.exceptionHandler(event -> { logger.error("An error occurs while checking OAuth2 token", event); responseHandler.handle(new OAuth2Response(false, event.getMessage())); }); if (httpMethod == HttpMethod.POST && configuration.isTokenIsSuppliedByFormUrlEncoded()) { request.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); request.end(configuration.getTokenFormUrlEncodedName() + '=' + accessToken); } else { request.end(); } }
From source file:io.gravitee.resource.oauth2.generic.OAuth2GenericResource.java
License:Apache License
@Override public void userInfo(String accessToken, Handler<UserInfoResponse> responseHandler) { HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(), context -> vertx.createHttpClient(httpClientOptions)); OAuth2ResourceConfiguration configuration = configuration(); HttpMethod httpMethod = HttpMethod.valueOf(configuration.getUserInfoEndpointMethod().toUpperCase()); logger.debug("Get userinfo by requesting {} [{}]", userInfoEndpointURI, configuration.getUserInfoEndpointMethod()); HttpClientRequest request = httpClient.requestAbs(httpMethod, userInfoEndpointURI); request.headers().add(HttpHeaders.AUTHORIZATION, AUTHORIZATION_HEADER_BEARER_SCHEME + accessToken); request.handler(response -> response.bodyHandler(buffer -> { logger.debug("Userinfo endpoint returns a response with a {} status code", response.statusCode()); if (response.statusCode() == HttpStatusCode.OK_200) { responseHandler.handle(new UserInfoResponse(true, buffer.toString())); } else {/*w ww .j a v a 2 s . c om*/ responseHandler.handle(new UserInfoResponse(false, buffer.toString())); } })); request.exceptionHandler(event -> { logger.error("An error occurs while getting userinfo from access_token", event); responseHandler.handle(new UserInfoResponse(false, event.getMessage())); }); request.end(); }
From source file:io.helixservice.feature.restclient.RestRequest.java
License:Open Source License
/** * Execute the request, with the expected response body marshaled * to a specific object type./*from w w w .j a v a 2 s . c o m*/ * * @param responseType Type we expect the response to be marshaled to * @return RestResponse fluent interface * @throws SuspendExecution For Vert.x Sync */ public <T> RestResponse<T> asObject(Class<T> responseType) throws SuspendExecution { try { // Apply Params & Url Vars String modifiedUrlPath = addParameters(replaceUrlVars(urlPath)); // Do request HttpClientRequest request; if (useDefaultHostAndPort) { request = httpClient.get().request(io.vertx.core.http.HttpMethod.valueOf(method.name()), modifiedUrlPath); } else { request = httpClient.get().requestAbs(io.vertx.core.http.HttpMethod.valueOf(method.name()), modifiedUrlPath); } // Set timeout, if requested if (timeoutInMs != null) { request.setTimeout(timeoutInMs); } // With headers request.headers().addAll(VertxTypeConverter.toVertxMultiMap(headers)); // Write body if we need to Buffer body = Buffer.buffer(); if (requestBody.isPresent()) { request.setChunked(true); Message message = marshallerSupplier.get().marshal(requestBody); List<String> contentTypes = message.getContentTypes(); if (contentTypes != null && contentTypes.size() > 0) { request.putHeader("Content-Type", contentTypes); } body = body.appendBytes(message.getBody()); } // Wait for response with Vert.x Sync HttpClientResponse httpClientResponse = getHttpClientResponse(request, body); Buffer bodyBuffer = getBuffer(httpClientResponse); return new RestResponse<>(httpClientResponse, bodyBuffer, marshallerSupplier, responseType); } catch (URISyntaxException | UnsupportedEncodingException e) { throw new IllegalArgumentException("Unable to parse urlPath=" + urlPath, e); } }
From source file:io.nitor.api.backend.proxy.Proxy.java
License:Apache License
public void handle(RoutingContext routingContext) { final HttpServerRequest sreq = routingContext.request(); final boolean isTls = isOrigReqHttps || "https".equals(routingContext.request().scheme()); final boolean isHTTP2 = routingContext.request().version() == HTTP_2; final String chost = getRemoteAddress(routingContext); final ProxyTracer tracer = tracerFactory.get(); String reqId = sreq.headers().get(requestIdHeader); boolean hadRequestId = reqId != null; if (reqId == null) { reqId = Long.toString(requestId.getAndIncrement()); }//from www .j av a 2s. co m tracer.incomingRequestStart(routingContext, isTls, isHTTP2, chost, reqId); if (!hadRequestId) { sreq.headers().add(requestIdHeader, reqId); } HttpServerResponse sres = sreq.response(); sres.exceptionHandler(tracer::outgoingResponseException); routingContext.addHeadersEndHandler(tracer::outgoingResponseHeadersEnd); sres.bodyEndHandler(tracer::outgoingResponseBodyEnd); if (!isHTTP2) { sres.headers().add("keep-alive", keepAliveHeaderValue); sres.headers().add("connection", "keep-alive"); } sreq.exceptionHandler(t -> { tracer.incomingRequestException(t); routingContext.fail(new ProxyException(500, RejectReason.incomingRequestFail, t)); }); final State state = new State(); targetResolver.resolveNextHop(routingContext, nextHop -> { if (nextHop == null) { NullPointerException e = new NullPointerException("nextHop must not be null"); tracer.incomingRequestException(e); throw e; } tracer.nextHopResolved(nextHop); MultiMap sreqh = sreq.headers(); String origHost = null; if (isHTTP2) { origHost = sreqh.get(":authority"); } if (origHost == null) { origHost = sreqh.get("Host"); } if (origHost == null) { ProxyException e = new ProxyException(400, RejectReason.noHostHeader, null); tracer.incomingRequestException(e); routingContext.fail(e); return; } boolean isWebsocket = !isHTTP2 && "websocket".equals(sreqh.get("upgrade")); if (isWebsocket) { MultiMap creqh = new CaseInsensitiveHeaders(); propagateRequestHeaders(isTls, chost, sreqh, origHost, creqh); if (nextHop.hostHeader != null) { creqh.set("Host", nextHop.hostHeader); } else { creqh.remove("Host"); } tracer.outgoingWebsocketInitial(creqh); client.websocket(nextHop.socketPort, nextHop.socketHost, nextHop.uri, creqh, cws -> { // lol no headers copied final boolean[] isClosed = { false }; ServerWebSocket sws = sreq.upgrade(); tracer.websocketEstablished(); for (final WebSocketBase[] pair : new WebSocketBase[][] { { sws, cws }, { cws, sws } }) { pair[0].frameHandler(pair[1]::writeFrame).closeHandler(v -> { if (!isClosed[0]) { tracer.establishedWebsocketClosed(); isClosed[0] = true; pair[1].close(); } }).exceptionHandler(t -> { tracer.establishedWebsocketException(t); t.printStackTrace(); if (!isClosed[0]) { isClosed[0] = true; try { pair[1].close(); } catch (IllegalStateException e) { // whatever } } }); } }, t -> { tracer.outgoingWebsocketException(t); t.printStackTrace(); sres.setStatusCode(HttpResponseStatus.BAD_GATEWAY.code()); if (t instanceof WebSocketHandshakeRejectedException) { WebSocketHandshakeRejectedException e = (WebSocketHandshakeRejectedException) t; sres.setStatusCode(e.resp.status().code()); sres.setStatusMessage(e.resp.status().reasonPhrase()); MultiMap cresh = new HeadersAdaptor(e.resp.headers()); copyEndToEndHeaders(cresh, sres.headers()); sres.headers().add("keep-alive", keepAliveHeaderValue); sres.headers().add("connection", "keep-alive"); sres.headers().set("content-length", "0"); } tracer.outgoingResponseInitial(); tracer.outgoingResponseHeadersEnd(null); sres.end(); tracer.outgoingResponseBodyEnd(null); }); return; } String expectStr; state.expecting100 = null != (expectStr = sreq.headers().get("expect")) && expectStr.equalsIgnoreCase("100-continue"); HttpClientRequest creq = client.request(sreq.method(), nextHop.socketPort, nextHop.socketHost, nextHop.uri); creq.setTimeout(SECONDS.toMillis(clientReceiveTimeout)); creq.handler(cres -> { cres.exceptionHandler(t -> { tracer.incomingResponseException(t); if (!state.serverFinished) { state.clientFinished = true; state.serverFinished = true; routingContext.fail(new ProxyException(502, RejectReason.incomingResponseFail, t)); } }); tracer.incomingResponseStart(cres); sres.setStatusCode(cres.statusCode()); sres.setStatusMessage(cres.statusMessage()); MultiMap headers = cres.headers(); copyEndToEndHeaders(headers, sres.headers()); final boolean reqCompletedBeforeResponse = state.requestComplete; if (state.expecting100) { log.info("Got " + cres.statusCode() + " instead of 100 Continue"); if (!isHTTP2) { if (/* state.receivedRequestBodyBefore100 && */ !reqCompletedBeforeResponse) { // TODO investigate whether vertx is able to handle the server request correctly without us closing the conn // but actually the client might have data in transit.. log.info( "Client might have started streaming data anyway, so request message boundary is lost. Continue streaming, but close server connection after response complete."); sres.headers().set("connection", "close"); } else { log.info( "Client had streamed the complete data anyway. Can carry on without closing server conn."); } } } if (!isHTTP2) { if (!sres.headers().contains("connection") || !sres.headers().get("connection").contains("close")) { sres.headers().add("keep-alive", keepAliveHeaderValue); sres.headers().add("connection", "keep-alive"); } } if (!headers.contains("content-length")) { sres.setChunked(true); } tracer.outgoingResponseInitial(); cres.endHandler(v -> { tracer.incomingResponseEnd(); state.clientFinished = true; if (!state.serverFinished) { state.serverFinished = true; sres.end(); } if (state.expecting100 && /* state.receivedRequestBodyBefore100 && */ !reqCompletedBeforeResponse) { log.info( "Client had started streaming data anyway, so request message boundary is lost. Close client connection."); creq.connection().close(); } }); pump.start(cres, sres, tracer); }); creq.exceptionHandler(t -> { tracer.outgoingRequestException(t); if (!state.serverFinished) { state.clientFinished = true; state.serverFinished = true; routingContext.fail(new ProxyException(502, RejectReason.outgoingRequestFail, t)); } }); MultiMap creqh = creq.headers(); propagateRequestHeaders(isTls, chost, sreqh, origHost, creqh); creq.headers().addAll(addHeaders); if (nextHop.hostHeader != null) { creq.setHost(nextHop.hostHeader); } else { creqh.remove("host"); } if (sreqh.getAll("transfer-encoding").stream().anyMatch(v -> v.equals("chunked"))) { creq.setChunked(true); } sres.closeHandler(v -> { if (!state.clientFinished) { state.clientFinished = true; tracer.incomingConnectionPrematurelyClosed(); HttpConnection connection = creq.connection(); if (connection != null) { connection.close(); } // else TODO } if (!state.serverFinished) { state.serverFinished = true; routingContext.fail(new ProxyException(0, RejectReason.outgoingResponseFail, null)); } }); tracer.outgoingRequestInitial(creq); if (sreq.isEnded()) { state.requestComplete = true; Buffer body = routingContext.getBody(); if (body == null || body.length() == 0) { creq.end(); } else { if (!creq.isChunked()) { creq.putHeader("content-length", Integer.toString(body.length())); } creq.end(routingContext.getBody()); } tracer.incomingRequestEnd(); } else { sreq.endHandler(v -> { state.requestComplete = true; try { creq.end(); } catch (IllegalStateException ex) { // ignore - nothing can be done - the request is already complete/closed - TODO log? } tracer.incomingRequestEnd(); }); ReadStream<Buffer> sreqStream; if (state.expecting100) { log.debug("Expect: 100"); creq.continueHandler(v -> { // no longer expecting 100, it's like a normal not-expecting-100 request from now on state.expecting100 = false; // since we received 100 Continue, we know the server agrees to accept all the request body, so we can assume we are forgiven for sending data early state.receivedRequestBodyBefore100 = false; log.info("Got 100, propagating"); sres.writeContinue(); }); // in this case we must flush request headers before the body is sent creq.sendHead(); sreqStream = new ReadStreamWrapper<Buffer>(sreq) { final LazyHandlerWrapper<Buffer> handlerWrapper = new LazyHandlerWrapper<Buffer>( super::handler, null) { @Override public void handle(Buffer event) { log.info("Got first request body chunk"); if (state.expecting100) { log.info("Got request body before '100 Continue'"); // data received despite not having yet recived 100-continue state.receivedRequestBodyBefore100 = true; } deactivate(); wrapped.handle(event); } }; @Override public ReadStream<Buffer> handler(Handler<Buffer> handler) { return handlerWrapper.handler(handler, this); } }; } else { log.debug("Not expect-100"); sreqStream = sreq; } pump.start(sreqStream, creq, tracer); } }); }