private void configureProxyServer(ChannelPipeline clientPipeline) { if (proxyServerConfiguration != null && sslConfig != null) { if (proxyServerConfiguration.getProxyUsername() != null && proxyServerConfiguration.getProxyPassword() != null) { clientPipeline.addLast(Constants.PROXY_HANDLER, new HttpProxyHandler(proxyServerConfiguration.getInetSocketAddress(), proxyServerConfiguration.getProxyUsername(), proxyServerConfiguration.getProxyPassword())); } else { clientPipeline.addLast(Constants.PROXY_HANDLER, new HttpProxyHandler(proxyServerConfiguration.getInetSocketAddress())); } } }
private void setProxyAuthorizationHeader(HttpCarbonMessage httpOutboundRequest) { ByteBuf authz = Unpooled.copiedBuffer( senderConfiguration.getProxyServerConfiguration().getProxyUsername() + COLON + senderConfiguration.getProxyServerConfiguration().getProxyPassword(), CharsetUtil.UTF_8); ByteBuf authzBase64 = Base64.encode(authz, false); CharSequence authorization = new AsciiString("Basic " + authzBase64.toString(CharsetUtil.US_ASCII)); httpOutboundRequest.setHeader(HttpHeaderNames.PROXY_AUTHORIZATION.toString(), authorization); authz.release(); authzBase64.release(); } }
String proxyPassword = proxy.getStringField(HttpConstants.PROXY_PASSWORD); try { proxyServerConfiguration = new ProxyServerConfiguration(proxyHost, proxyPort); } catch (UnknownHostException e) { throw new BallerinaConnectorException("Failed to resolve host" + proxyHost, e); proxyServerConfiguration.setProxyUsername(proxyUserName); proxyServerConfiguration.setProxyPassword(proxyPassword);
@Override public Object makeObject() throws Exception { Bootstrap clientBootstrap = instantiateAndConfigBootStrap(eventLoopGroup, eventLoopClass, bootstrapConfiguration); ConnectionAvailabilityFuture connectionAvailabilityFuture = new ConnectionAvailabilityFuture(); HttpClientChannelInitializer httpClientChannelInitializer = instantiateAndConfigClientInitializer( senderConfiguration, clientBootstrap, httpRoute, connectionManager, connectionAvailabilityFuture); clientBootstrap.handler(httpClientChannelInitializer); // Connect to proxy server if proxy is enabled ChannelFuture channelFuture; if (senderConfiguration.getProxyServerConfiguration() != null && senderConfiguration.getScheme() .equals(HTTP_SCHEME)) { channelFuture = clientBootstrap.connect(new InetSocketAddress( senderConfiguration.getProxyServerConfiguration().getProxyHost(), senderConfiguration.getProxyServerConfiguration().getProxyPort() )); } else { channelFuture = clientBootstrap.connect(new InetSocketAddress(httpRoute.getHost(), httpRoute.getPort())); } connectionAvailabilityFuture.setSocketAvailabilityFuture(channelFuture); connectionAvailabilityFuture.setForceHttp2(senderConfiguration.isForceHttp2()); TargetChannel targetChannel = new TargetChannel(httpClientChannelInitializer, channelFuture, httpRoute, connectionAvailabilityFuture); httpClientChannelInitializer.setHttp2ClientChannel(targetChannel.getHttp2ClientChannel()); LOG.debug("Created channel: {}", httpRoute); return targetChannel; }
private void handleOutboundConnectionHeader(KeepAliveConfig keepAliveConfig, HttpCarbonMessage httpOutboundRequest) { switch (keepAliveConfig) { case AUTO: if (Float.valueOf(httpVersion) >= Constants.HTTP_1_1) { httpOutboundRequest .setHeader(HttpHeaderNames.CONNECTION.toString(), Constants.CONNECTION_KEEP_ALIVE); } else { httpOutboundRequest.setHeader(HttpHeaderNames.CONNECTION.toString(), Constants.CONNECTION_CLOSE); } break; case ALWAYS: httpOutboundRequest.setHeader(HttpHeaderNames.CONNECTION.toString(), Constants.CONNECTION_KEEP_ALIVE); break; case NEVER: httpOutboundRequest.setHeader(HttpHeaderNames.CONNECTION.toString(), Constants.CONNECTION_CLOSE); break; default: //Do nothing break; } // Add proxy-authorization header if proxy is enabled and scheme is http if (senderConfiguration.getScheme().equals(HTTP_SCHEME) && senderConfiguration.getProxyServerConfiguration() != null && senderConfiguration.getProxyServerConfiguration().getProxyUsername() != null && senderConfiguration.getProxyServerConfiguration().getProxyPassword() != null) { setProxyAuthorizationHeader(httpOutboundRequest); } }