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(); } }
private void startExecutingOutboundRequest(String protocol, ChannelFuture channelFuture) { if (protocol.equalsIgnoreCase(Constants.HTTP2_CLEARTEXT_PROTOCOL) || protocol.equalsIgnoreCase(Constants.HTTP2_TLS_PROTOCOL)) { prepareTargetChannelForHttp2(); } else { // Response for the upgrade request will arrive in stream 1, // so use 1 as the stream id. prepareTargetChannelForHttp(channelFuture); if (protocol.equalsIgnoreCase(Constants.HTTP_SCHEME) && senderConfiguration.getProxyServerConfiguration() != null) { httpOutboundRequest.setProperty(Constants.IS_PROXY_ENABLED, true); } targetChannel.writeContent(httpOutboundRequest); } }
@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); } }
public HttpClientChannelInitializer(SenderConfiguration senderConfiguration, HttpRoute httpRoute, ConnectionManager connectionManager, ConnectionAvailabilityFuture connectionAvailabilityFuture) { this.httpTraceLogEnabled = senderConfiguration.isHttpTraceLogEnabled(); this.keepAliveConfig = senderConfiguration.getKeepAliveConfig(); this.proxyServerConfiguration = senderConfiguration.getProxyServerConfiguration(); this.http2ConnectionManager = connectionManager.getHttp2ConnectionManager(); this.senderConfiguration = senderConfiguration; this.httpRoute = httpRoute; this.sslConfig = senderConfiguration.getClientSSLConfig(); this.connectionAvailabilityFuture = connectionAvailabilityFuture; String httpVersion = senderConfiguration.getHttpVersion(); if (Float.valueOf(httpVersion) == Constants.HTTP_2_0) { http2 = true; } connection = new DefaultHttp2Connection(false); clientFrameListener = new ClientFrameListener(); Http2FrameListener frameListener = new DelegatingDecompressorFrameListener(connection, clientFrameListener); Http2ConnectionHandlerBuilder connectionHandlerBuilder = new Http2ConnectionHandlerBuilder(); if (httpTraceLogEnabled) { connectionHandlerBuilder.frameLogger(new FrameLogger(TRACE, Constants.TRACE_LOG_UPSTREAM)); } http2ConnectionHandler = connectionHandlerBuilder.connection(connection).frameListener(frameListener).build(); http2TargetHandler = new Http2TargetHandler(connection, http2ConnectionHandler.encoder()); if (sslConfig != null) { sslHandlerFactory = new SSLHandlerFactory(sslConfig); } }