@Override public HttpClientOptions createHttpClientOptions() { HttpVersion ver = ServiceRegistryConfig.INSTANCE.getHttpVersion(); HttpClientOptions httpClientOptions = new HttpClientOptions(); httpClientOptions.setProtocolVersion(ver); httpClientOptions.setConnectTimeout(ServiceRegistryConfig.INSTANCE.getConnectionTimeout()); httpClientOptions.setIdleTimeout(ServiceRegistryConfig.INSTANCE.getIdleWatchTimeout()); if (ver == HttpVersion.HTTP_2) { LOGGER.debug("service center ws client protocol version is HTTP/2"); httpClientOptions.setHttp2ClearTextUpgrade(false); } if (ServiceRegistryConfig.INSTANCE.isSsl()) { LOGGER.debug("service center ws client performs requests over TLS"); VertxTLSBuilder.buildHttpClientOptions(SSL_KEY, httpClientOptions); } return httpClientOptions; }
@Override public HttpClientOptions createHttpClientOptions() { HttpVersion ver = ServiceRegistryConfig.INSTANCE.getHttpVersion(); HttpClientOptions httpClientOptions = new HttpClientOptions(); httpClientOptions.setProtocolVersion(ver); httpClientOptions.setConnectTimeout(ServiceRegistryConfig.INSTANCE.getConnectionTimeout()); httpClientOptions.setIdleTimeout(ServiceRegistryConfig.INSTANCE.getIdleConnectionTimeout()); if (ServiceRegistryConfig.INSTANCE.isProxyEnable()) { ProxyOptions proxy = new ProxyOptions(); proxy.setHost(ServiceRegistryConfig.INSTANCE.getProxyHost()); proxy.setPort(ServiceRegistryConfig.INSTANCE.getProxyPort()); proxy.setUsername(ServiceRegistryConfig.INSTANCE.getProxyUsername()); proxy.setPassword(ServiceRegistryConfig.INSTANCE.getProxyPasswd()); httpClientOptions.setProxyOptions(proxy); } if (ver == HttpVersion.HTTP_2) { LOGGER.debug("service center client protocol version is HTTP/2"); httpClientOptions.setHttp2ClearTextUpgrade(false); } if (ServiceRegistryConfig.INSTANCE.isSsl()) { LOGGER.debug("service center client performs requests over TLS"); VertxTLSBuilder.buildHttpClientOptions(SSL_KEY, httpClientOptions); } return httpClientOptions; }
@Test public void testPrivateMethodCreateHttpClientOptions() { MicroserviceFactory microserviceFactory = new MicroserviceFactory(); Microservice microservice = microserviceFactory.create("app", "ms"); oClient.registerMicroservice(microservice); oClient.registerMicroserviceInstance(microservice.getInstance()); new MockUp<ServiceRegistryConfig>() { @Mock public HttpVersion getHttpVersion() { return HttpVersion.HTTP_2; } @Mock public boolean isSsl() { return true; } }; try { oClient.init(); HttpClientOptions httpClientOptions = Deencapsulation.invoke(oClient, "createHttpClientOptions"); Assert.assertNotNull(httpClientOptions); Assert.assertEquals(80, httpClientOptions.getDefaultPort()); } catch (Exception e) { Assert.assertNotNull(e); } }
@Test public void getFormattedElement() { AccessLogParam param = new AccessLogParam(); RoutingContext mockContext = Mockito.mock(RoutingContext.class); HttpServerRequest request = Mockito.mock(HttpServerRequest.class); String uri = "/test/uri"; param.setRoutingContext(mockContext); Mockito.when(mockContext.request()).thenReturn(request); Mockito.when(request.method()).thenReturn(HttpMethod.DELETE); Mockito.when(request.path()).thenReturn(uri); Mockito.when(request.version()).thenReturn(HttpVersion.HTTP_1_1); String result = ELEMENT.getFormattedElement(param); assertEquals("\"DELETE " + uri + " HTTP/1.1\"", result); }
@Test public void getFormattedElement() { AccessLogParam param = new AccessLogParam(); RoutingContext context = Mockito.mock(RoutingContext.class); HttpServerRequest request = Mockito.mock(HttpServerRequest.class); param.setRoutingContext(context); Mockito.when(context.request()).thenReturn(request); Mockito.when(request.version()).thenReturn(HttpVersion.HTTP_1_1); String result = new VersionOrProtocolElement().getFormattedElement(param); assertEquals("HTTP/1.1", result); Mockito.when(request.version()).thenReturn(HttpVersion.HTTP_1_0); result = new VersionOrProtocolElement().getFormattedElement(param); assertEquals("HTTP/1.0", result); Mockito.when(request.version()).thenReturn(HttpVersion.HTTP_2); result = new VersionOrProtocolElement().getFormattedElement(param); assertEquals("HTTP/2.0", result); }
@Override public void handle(RoutingContext context) { // common logging data long timestamp = System.currentTimeMillis(); String remoteClient = getClientAddress(context.request().remoteAddress()); HttpMethod method = context.request().method(); String uri = context.request().uri(); HttpVersion version = context.request().version(); if (immediate) { log(context, timestamp, remoteClient, version, method, uri); } else { context.addBodyEndHandler(v -> log(context, timestamp, remoteClient, version, method, uri)); } context.next(); }
@Test public void testIsKeepAlive() throws Exception { MultiMap headers = MultiMap.caseInsensitiveMultiMap(); HttpServerRequest req = mock(HttpServerRequest.class); when(req.headers()).thenReturn(headers); // Connection header set. headers.add(HeaderNames.CONNECTION, HttpUtils.CLOSE); assertThat(HttpUtils.isKeepAlive(req)).isFalse(); headers.add(HeaderNames.CONNECTION, HttpUtils.KEEP_ALIVE); assertThat(HttpUtils.isKeepAlive(req)).isTrue(); // Unset connection header headers.clear(); when(req.version()).thenReturn(HttpVersion.HTTP_1_1); assertThat(HttpUtils.isKeepAlive(req)).isTrue(); when(req.version()).thenReturn(HttpVersion.HTTP_1_0); assertThat(HttpUtils.isKeepAlive(req)).isFalse(); }
@Test public void testHttpClientPool() { new MockUp<ServiceRegistryConfig>() { @Mock public HttpVersion getHttpVersion() { return HttpVersion.HTTP_2; } @Mock public boolean isSsl() { return true; } }; HttpClientPool.INSTANCE.create(); }
@Test public void testWebsocketClientPool() { new MockUp<ServiceRegistryConfig>() { @Mock public HttpVersion getHttpVersion() { return HttpVersion.HTTP_2; } @Mock public boolean isSsl() { return true; } }; WebsocketClientPool.INSTANCE.create(); }
private String getStringVersion(HttpVersion version) { switch (version) { case HTTP_2: return "HTTP/2.0"; case HTTP_1_0: return "HTTP/1.0"; case HTTP_1_1: return "HTTP/1.1"; default: return EMPTY_RESULT; } }
@SuppressWarnings({"unchecked", "rawtypes"}) public TypedParamInjectorRegistry(Config config) { map = new HashMap<>(); registerInjector(Vertx.class, new VertxParamInjector()); registerInjector(Session.class, new SessionParamInjector()); registerInjector(RoutingContext.class, new RoutingContextParamInjector()); registerInjector(Payload.class, new PayloadParamInjector()); registerInjector(PaginationContext.class, new PaginationContextParamInjector()); registerInjector(EventBus.class, new EventBusParamInjector()); registerInjector(ResourceBundle.class, new ResourceBundleParamInjector(config)); registerInjector(HttpServerRequest.class, new RequestParamInjector()); registerInjector(HttpServerResponse.class, new ResponseParamInjector()); registerInjector(SocketAddress.class, new SocketAddressParamInjector()); registerInjector(HttpVersion.class, new HttpVersionParamInjector()); }
public void sendFrame(String body) { if (log.isTraceEnabled()) log.trace("XHR sending frame"); if (!headersWritten) { HttpServerResponse resp = rc.response(); resp.putHeader("Content-Type", "application/javascript; charset=UTF-8"); setJSESSIONID(options, rc); setCORS(rc); if (rc.request().version() != HttpVersion.HTTP_1_0) { resp.setChunked(true); } else { resp.putHeader("Content-Length", "0"); } headersWritten = true; } }
@Test public void testNoHttp2Push() throws Exception { stat.setWebRoot("webroot/somedir3"); router.route().handler(stat); HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions() .setUseAlpn(true) .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))); http2Server.requestHandler(router::accept).listen(8443); HttpClientOptions options = new HttpClientOptions() .setSsl(true) .setUseAlpn(true) .setProtocolVersion(HttpVersion.HTTP_2) .setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem")); HttpClient client = vertx.createHttpClient(options); HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> { assertEquals(200, resp.statusCode()); assertEquals(HttpVersion.HTTP_2, resp.version()); resp.bodyHandler(this::assertNotNull); testComplete(); }); request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> { fail(); })); request.end(); await(); }
@Test public void testHttp2Push() throws Exception { List<Http2PushMapping> mappings = new ArrayList<>(); mappings.add(new Http2PushMapping("style.css", "style", false)); mappings.add(new Http2PushMapping("coin.png", "image", false)); stat.setHttp2PushMapping(mappings) .setWebRoot("webroot/somedir3"); router.route().handler(stat); HttpServer http2Server = vertx.createHttpServer(new HttpServerOptions() .setUseAlpn(true) .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("tls/server-key.pem").setCertPath("tls/server-cert.pem"))); http2Server.requestHandler(router::accept).listen(8443); HttpClientOptions options = new HttpClientOptions() .setSsl(true) .setUseAlpn(true) .setProtocolVersion(HttpVersion.HTTP_2) .setPemTrustOptions(new PemTrustOptions().addCertPath("tls/server-cert.pem")); HttpClient client = vertx.createHttpClient(options); HttpClientRequest request = client.get(8443, "localhost", "/testLinkPreload.html", resp -> { assertEquals(200, resp.statusCode()); assertEquals(HttpVersion.HTTP_2, resp.version()); resp.bodyHandler(this::assertNotNull); }); CountDownLatch latch = new CountDownLatch(2); request.pushHandler(pushedReq -> pushedReq.handler(pushedResp -> { assertNotNull(pushedResp); pushedResp.bodyHandler(this::assertNotNull); latch.countDown(); })); request.end(); latch.await(); }
/** * Checks whether the given request should be closed or not once completed. * * @param request the request * @return {@code true} if the connection is marked as {@literal keep-alive}, and so must not be closed. {@code * false} otherwise. Notice that if not set in the request, the default value depends on the HTTP version. */ public static boolean isKeepAlive(HttpServerRequest request) { String connection = request.headers().get(HeaderNames.CONNECTION); if (connection != null && connection.equalsIgnoreCase(CLOSE)) { return false; } if (request.version() == HttpVersion.HTTP_1_1) { return !CLOSE.equalsIgnoreCase(connection); } else { return KEEP_ALIVE.equalsIgnoreCase(connection); } }
public static void buildResponse(HttpServerResponse httpServerResponse, ApiResponse amanResponse, HttpVersion httpVersion) { amanResponse.getHeaders().forEach(e -> { if (httpVersion == HttpVersion.HTTP_1_0 || httpVersion == HttpVersion.HTTP_1_1 || !e.getKey().equals("Connection")) { httpServerResponse.headers().add(e.getKey(), e.getValue()); } }); httpServerResponse.setStatusCode(amanResponse.getCode()); httpServerResponse.setStatusMessage(amanResponse.getMessage() == null ? "" : amanResponse.getMessage()); }
public HttpVersion getHttpVersion() { DynamicStringProperty property = DynamicPropertyFactory.getInstance() .getStringProperty("cse.service.registry.client.httpVersion", "HTTP_1_1"); return HttpVersion.valueOf(property.get()); }
@Override public HttpVersion version() { return HttpVersion.HTTP_1_1; }
@Test public void testVersion(TestContext context) { context.assertEquals(HttpVersion.HTTP_1_1, request.version()); }
@Override public List<HttpVersion> getAlpnVersions() { return options.getAlpnVersions(); }
public HttpClientRequestWithFutureResponse sendHead(Handler<HttpVersion> completionHandler) { request.sendHead(completionHandler); return this; }
@Override public HttpClientRequest sendHead(final Handler<HttpVersion> aHandler) { return myRequest.sendHead(aHandler); }
@Override public HttpVersion version() { // TODO Auto-generated method stub return null; }
@Override public HttpVersion version() { return httpServerRequest.version(); }
@Override public HttpVersion version() { return serverRequest.version(); }
@Override public HttpVersion resolve(RoutingContext context) { return context.request().version(); }
@GET("/httpVersion") @ContentType("application/json") public JsonObject getNetSocket(HttpVersion version) { return new JsonObject() .put("version", version.toString()); }
@Override public WebClientOptions setProtocolVersion(HttpVersion protocolVersion) { return (WebClientOptions) super.setProtocolVersion(protocolVersion); }
@Override public WebClientOptions setAlpnVersions(List<HttpVersion> alpnVersions) { return (WebClientOptions) super.setAlpnVersions(alpnVersions); }
@Override public HttpVersion version() { return resp.version(); }
@Override protected HttpClientOptions getHttpClientOptions() { return super.getHttpClientOptions().setProtocolVersion(HttpVersion.HTTP_2); }
public HttpVersion getProtocolVersion() { return protocolVersion; }
public ServerOptions setProtocolVersion(HttpVersion protocolVersion) { this.protocolVersion = protocolVersion; return this; }
@CodeTranslate public void enumValueFromConstructor() throws Exception { ServerOptions obj = new ServerOptions().setProtocolVersion(HttpVersion.HTTP_2); DataObjectTest.o = obj; }
@CodeTranslate public void enumValueFromIdentifier() throws Exception { ServerOptions obj = new ServerOptions(); obj.setProtocolVersion(HttpVersion.HTTP_2); DataObjectTest.o = obj; }
@Override public HttpVersion version() { return request.version(); }
/** * Set the protocol version. * * @param protocolVersion the protocol version * @return a reference to this, so the API can be used fluently */ @Override public ConsulClientOptions setProtocolVersion(HttpVersion protocolVersion) { return (ConsulClientOptions) super.setProtocolVersion(protocolVersion); }
/** * Set the list of protocol versions to provide to the server during the Application-Layer Protocol Negotiation. * When the list is empty, the client provides a best effort list according to {@link #setProtocolVersion}: * * <ul> * <li>{@link HttpVersion#HTTP_2}: [ "h2", "http/1.1" ]</li> * <li>otherwise: [{@link #getProtocolVersion()}]</li> * </ul> * * @param alpnVersions the versions * @return a reference to this, so the API can be used fluently */ @Override public ConsulClientOptions setAlpnVersions(List<HttpVersion> alpnVersions) { return (ConsulClientOptions) super.setAlpnVersions(alpnVersions); }
/** * @return the version of the response */ @CacheReturn HttpVersion version();