Search in sources :

Example 16 with ProxyOptions

use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.

the class ProxyErrorTest method proxyTest.

private void proxyTest(int error, String username, String url, Handler<HttpClientResponse> assertResponse, boolean completeOnException) throws Exception {
    startProxy(error, username);
    final HttpClientOptions options = new HttpClientOptions().setSsl(url.startsWith("https")).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()));
    HttpClient client = vertx.createHttpClient(options);
    client.getAbs(url, assertResponse).exceptionHandler(e -> {
        if (completeOnException) {
            testComplete();
        } else {
            fail(e);
        }
    }).end();
    await();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HttpClientResponse(io.vertx.core.http.HttpClientResponse) ProxyOptions(io.vertx.core.net.ProxyOptions) ProxyType(io.vertx.core.net.ProxyType) HttpProxy(io.vertx.test.core.HttpProxy) Test(org.junit.Test) HttpClientOptions(io.vertx.core.http.HttpClientOptions) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) VertxTestBase(io.vertx.test.core.VertxTestBase) LoggerFactory(io.vertx.core.logging.LoggerFactory) HttpClient(io.vertx.core.http.HttpClient) ProxyOptions(io.vertx.core.net.ProxyOptions) HttpClient(io.vertx.core.http.HttpClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 17 with ProxyOptions

use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.

the class ProxyOptionsTest method testCopyProxyOptions.

@Test
public void testCopyProxyOptions() {
    ProxyOptions options = new ProxyOptions();
    options.setType(randType);
    options.setHost(randHost);
    options.setPort(randPort);
    options.setUsername(randUsername);
    options.setPassword(randPassword);
    ProxyOptions copy = new ProxyOptions(options);
    assertEquals(randType, copy.getType());
    assertEquals(randPort, copy.getPort());
    assertEquals(randHost, copy.getHost());
    assertEquals(randUsername, copy.getUsername());
    assertEquals(randPassword, copy.getPassword());
}
Also used : ProxyOptions(io.vertx.core.net.ProxyOptions) Test(org.junit.Test)

Example 18 with ProxyOptions

use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.

the class ProxyOptionsTest method testOptionsJson.

@Test
public void testOptionsJson() {
    JsonObject json = new JsonObject();
    json.put("type", randType.toString()).put("host", randHost).put("port", randPort).put("username", randUsername).put("password", randPassword);
    ProxyOptions options = new ProxyOptions(json);
    assertEquals(randType, options.getType());
    assertEquals(randPort, options.getPort());
    assertEquals(randHost, options.getHost());
    assertEquals(randUsername, options.getUsername());
    assertEquals(randPassword, options.getPassword());
}
Also used : ProxyOptions(io.vertx.core.net.ProxyOptions) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 19 with ProxyOptions

use of io.vertx.core.net.ProxyOptions in project vert.x by eclipse.

the class HttpClientImpl method createRequest.

private HttpClientRequest createRequest(HttpMethod method, String host, int port, Boolean ssl, String relativeURI, MultiMap headers) {
    Objects.requireNonNull(method, "no null method accepted");
    Objects.requireNonNull(host, "no null host accepted");
    Objects.requireNonNull(relativeURI, "no null relativeURI accepted");
    checkClosed();
    HttpClientRequest req;
    if (useProxy) {
        relativeURI = "http://" + host + (port != 80 ? ":" + port : "") + relativeURI;
        ProxyOptions proxyOptions = options.getProxyOptions();
        if (proxyOptions.getUsername() != null && proxyOptions.getPassword() != null) {
            if (headers == null) {
                headers = MultiMap.caseInsensitiveMultiMap();
            }
            headers.add("Proxy-Authorization", "Basic " + Base64.getEncoder().encodeToString((proxyOptions.getUsername() + ":" + proxyOptions.getPassword()).getBytes()));
        }
        req = new HttpClientRequestImpl(this, ssl != null ? ssl : options.isSsl(), method, proxyOptions.getHost(), proxyOptions.getPort(), relativeURI, vertx);
        req.setHost(host + (port != 80 ? ":" + port : ""));
    } else {
        req = new HttpClientRequestImpl(this, ssl != null ? ssl : options.isSsl(), method, host, port, relativeURI, vertx);
    }
    if (headers != null) {
        req.headers().setAll(headers);
    }
    return req;
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) ProxyOptions(io.vertx.core.net.ProxyOptions)

Example 20 with ProxyOptions

use of io.vertx.core.net.ProxyOptions in project gravitee-gateway by gravitee-io.

the class HttpEndpointRuleHandler method handle.

@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 {
            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);
            }
            // Configure TLS if required
            HttpClientSslOptions sslOptions = endpoint.getHttpClientSslOptions();
            if (sslOptions != null && sslOptions.isEnabled()) {
                httpClientOptions.setSsl(sslOptions.isEnabled()).setVerifyHost(sslOptions.isHostnameVerifier()).setTrustAll(sslOptions.isTrustAll());
                if (sslOptions.getPem() != null && !sslOptions.getPem().isEmpty()) {
                    httpClientOptions.setPemTrustOptions(new PemTrustOptions().addCertValue(io.vertx.core.buffer.Buffer.buffer(sslOptions.getPem())));
                }
            } else if (HTTPS_SCHEME.equalsIgnoreCase(hcRequestUri.getScheme())) {
                // SSL is not configured but the endpoint scheme is HTTPS so let's enable the SSL on Vert.x HTTP client
                // automatically
                httpClientOptions.setSsl(true).setTrustAll(true);
            }
            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(System.currentTimeMillis());
            long startTime = System.currentTimeMillis();
            Request request = new Request();
            request.setMethod(step.getRequest().getMethod());
            request.setUri(hcRequestUri.toString());
            healthRequest.handler(response -> response.bodyHandler(buffer -> {
                long endTime = System.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 = System.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 (Exception ex) {
            logger.error("An unexpected error occurs", ex);
        }
    }
}
Also used : Step(io.gravitee.reporter.api.health.Step) HttpHeaders(io.gravitee.common.http.HttpHeaders) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) EndpointStatus(io.gravitee.reporter.api.health.EndpointStatus) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) EvaluationException(io.gravitee.gateway.services.healthcheck.eval.EvaluationException) HttpClientSslOptions(io.gravitee.definition.model.HttpClientSslOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Response(io.gravitee.reporter.api.common.Response) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) EndpointRule(io.gravitee.gateway.services.healthcheck.EndpointRule) EndpointStatusDecorator(io.gravitee.gateway.services.healthcheck.EndpointStatusDecorator) HttpClientOptions(io.vertx.core.http.HttpClientOptions) URI(java.net.URI) PemTrustOptions(io.vertx.core.net.PemTrustOptions) Request(io.gravitee.reporter.api.common.Request) ProxyOptions(io.vertx.core.net.ProxyOptions) Logger(org.slf4j.Logger) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) Iterator(java.util.Iterator) HttpProxy(io.gravitee.definition.model.HttpProxy) Vertx(io.vertx.core.Vertx) EvaluableHttpResponse(io.gravitee.gateway.services.healthcheck.http.el.EvaluableHttpResponse) ProxyType(io.vertx.core.net.ProxyType) AssertionEvaluation(io.gravitee.gateway.services.healthcheck.eval.assertion.AssertionEvaluation) HttpMethod(io.vertx.core.http.HttpMethod) Pattern(java.util.regex.Pattern) Handler(io.vertx.core.Handler) Collections(java.util.Collections) HttpClient(io.vertx.core.http.HttpClient) HttpHeaders(io.gravitee.common.http.HttpHeaders) ProxyOptions(io.vertx.core.net.ProxyOptions) Step(io.gravitee.reporter.api.health.Step) URI(java.net.URI) HttpClientOptions(io.vertx.core.http.HttpClientOptions) HttpClientRequest(io.vertx.core.http.HttpClientRequest) Request(io.gravitee.reporter.api.common.Request) HttpClientSslOptions(io.gravitee.definition.model.HttpClientSslOptions) EvaluableHttpResponse(io.gravitee.gateway.services.healthcheck.http.el.EvaluableHttpResponse) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) PemTrustOptions(io.vertx.core.net.PemTrustOptions) HttpEndpoint(io.gravitee.definition.model.endpoint.HttpEndpoint) URISyntaxException(java.net.URISyntaxException) EvaluationException(io.gravitee.gateway.services.healthcheck.eval.EvaluationException) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException) HttpProxy(io.gravitee.definition.model.HttpProxy) EndpointStatus(io.gravitee.reporter.api.health.EndpointStatus) Response(io.gravitee.reporter.api.common.Response) EvaluableHttpResponse(io.gravitee.gateway.services.healthcheck.http.el.EvaluableHttpResponse) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClient(io.vertx.core.http.HttpClient) ConnectTimeoutException(io.netty.channel.ConnectTimeoutException)

Aggregations

ProxyOptions (io.vertx.core.net.ProxyOptions)28 Test (org.junit.Test)15 HttpClientOptions (io.vertx.core.http.HttpClientOptions)7 SocketAddress (io.vertx.core.net.SocketAddress)5 JsonObject (io.vertx.core.json.JsonObject)4 Handler (io.vertx.core.Handler)3 HttpClientRequest (io.vertx.core.http.HttpClientRequest)3 ProxyType (io.vertx.core.net.ProxyType)3 HttpClientSslOptions (io.gravitee.definition.model.HttpClientSslOptions)2 HttpProxy (io.gravitee.definition.model.HttpProxy)2 HttpClient (io.vertx.core.http.HttpClient)2 ContextInternal (io.vertx.core.impl.ContextInternal)2 Endpoint (io.vertx.core.net.impl.pool.Endpoint)2 SSLCustom (org.apache.servicecomb.foundation.ssl.SSLCustom)2 SSLOption (org.apache.servicecomb.foundation.ssl.SSLOption)2 SSLOptionFactory (org.apache.servicecomb.foundation.ssl.SSLOptionFactory)2 HttpHeaders (io.gravitee.common.http.HttpHeaders)1 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)1 HttpEndpoint (io.gravitee.definition.model.endpoint.HttpEndpoint)1 EndpointRule (io.gravitee.gateway.services.healthcheck.EndpointRule)1