use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.
the class CachingExecBase method generateViaHeader.
String generateViaHeader(final HttpMessage msg) {
if (msg.getVersion() == null) {
msg.setVersion(HttpVersion.DEFAULT);
}
final ProtocolVersion pv = msg.getVersion();
final String existingEntry = viaHeaders.get(msg.getVersion());
if (existingEntry != null) {
return existingEntry;
}
final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.hc.client5", getClass().getClassLoader());
final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
final String value;
final int major = pv.getMajor();
final int minor = pv.getMinor();
if (URIScheme.HTTP.same(pv.getProtocol())) {
value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", major, minor, release);
} else {
value = String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getProtocol(), major, minor, release);
}
viaHeaders.put(pv, value);
return value;
}
use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.
the class RequestExpectContinue method process.
@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
Args.notNull(request, "HTTP request");
if (!request.containsHeader(HttpHeaders.EXPECT)) {
final ProtocolVersion version = request.getVersion() != null ? request.getVersion() : HttpVersion.HTTP_1_1;
// Do not send the expect header if request body is known to be empty
if (entity != null && entity.getContentLength() != 0 && !version.lessEquals(HttpVersion.HTTP_1_0)) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig config = clientContext.getRequestConfig();
if (config.isExpectContinueEnabled()) {
request.addHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE);
}
}
}
}
use of org.apache.hc.core5.http.ProtocolVersion in project opentelemetry-java-instrumentation by open-telemetry.
the class ApacheHttpClientHttpAttributesGetter method flavor.
@Override
@Nullable
public String flavor(HttpRequest request, @Nullable HttpResponse response) {
ProtocolVersion protocolVersion = getVersion(request, response);
if (protocolVersion == null) {
return null;
}
String protocol = protocolVersion.getProtocol();
if (!protocol.equals("HTTP")) {
return null;
}
int major = protocolVersion.getMajor();
int minor = protocolVersion.getMinor();
if (major == 1 && minor == 0) {
return SemanticAttributes.HttpFlavorValues.HTTP_1_0;
}
if (major == 1 && minor == 1) {
return SemanticAttributes.HttpFlavorValues.HTTP_1_1;
}
if (major == 2 && minor == 0) {
return SemanticAttributes.HttpFlavorValues.HTTP_2_0;
}
logger.log(Level.FINE, "unexpected http protocol version: {0}", protocolVersion);
return null;
}
use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.
the class PoolingAsyncClientConnectionManager method lease.
@Override
public Future<AsyncConnectionEndpoint> lease(final String id, final HttpRoute route, final Object state, final Timeout requestTimeout, final FutureCallback<AsyncConnectionEndpoint> callback) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} endpoint lease request ({}) {}", id, requestTimeout, ConnPoolSupport.formatStats(route, state, pool));
}
return new Future<AsyncConnectionEndpoint>() {
final ConnectionConfig connectionConfig = resolveConnectionConfig(route);
final BasicFuture<AsyncConnectionEndpoint> resultFuture = new BasicFuture<>(callback);
final Future<PoolEntry<HttpRoute, ManagedAsyncClientConnection>> leaseFuture = pool.lease(route, state, requestTimeout, new FutureCallback<PoolEntry<HttpRoute, ManagedAsyncClientConnection>>() {
@Override
public void completed(final PoolEntry<HttpRoute, ManagedAsyncClientConnection> poolEntry) {
if (poolEntry.hasConnection()) {
final TimeValue timeToLive = connectionConfig.getTimeToLive();
if (TimeValue.isNonNegative(timeToLive)) {
final Deadline deadline = Deadline.calculate(poolEntry.getCreated(), timeToLive);
if (deadline.isExpired()) {
poolEntry.discardConnection(CloseMode.GRACEFUL);
}
}
}
if (poolEntry.hasConnection()) {
final ManagedAsyncClientConnection connection = poolEntry.getConnection();
final TimeValue timeValue = connectionConfig.getValidateAfterInactivity();
if (connection.isOpen() && TimeValue.isNonNegative(timeValue)) {
final Deadline deadline = Deadline.calculate(poolEntry.getUpdated(), timeValue);
if (deadline.isExpired()) {
final ProtocolVersion protocolVersion = connection.getProtocolVersion();
if (protocolVersion != null && protocolVersion.greaterEquals(HttpVersion.HTTP_2_0)) {
connection.submitCommand(new PingCommand(new BasicPingHandler(result -> {
if (result == null || !result) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} connection {} is stale", id, ConnPoolSupport.getId(connection));
}
poolEntry.discardConnection(CloseMode.GRACEFUL);
}
leaseCompleted(poolEntry);
})), Command.Priority.IMMEDIATE);
return;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("{} connection {} is closed", id, ConnPoolSupport.getId(connection));
}
poolEntry.discardConnection(CloseMode.IMMEDIATE);
}
}
}
}
leaseCompleted(poolEntry);
}
void leaseCompleted(final PoolEntry<HttpRoute, ManagedAsyncClientConnection> poolEntry) {
final ManagedAsyncClientConnection connection = poolEntry.getConnection();
if (connection != null) {
connection.activate();
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} endpoint leased {}", id, ConnPoolSupport.formatStats(route, state, pool));
}
final AsyncConnectionEndpoint endpoint = new InternalConnectionEndpoint(poolEntry);
if (LOG.isDebugEnabled()) {
LOG.debug("{} acquired {}", id, ConnPoolSupport.getId(endpoint));
}
resultFuture.completed(endpoint);
}
@Override
public void failed(final Exception ex) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} endpoint lease failed", id);
}
resultFuture.failed(ex);
}
@Override
public void cancelled() {
if (LOG.isDebugEnabled()) {
LOG.debug("{} endpoint lease cancelled", id);
}
resultFuture.cancel();
}
});
@Override
public AsyncConnectionEndpoint get() throws InterruptedException, ExecutionException {
return resultFuture.get();
}
@Override
public AsyncConnectionEndpoint get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return resultFuture.get(timeout, unit);
}
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
return leaseFuture.cancel(mayInterruptIfRunning);
}
@Override
public boolean isDone() {
return resultFuture.isDone();
}
@Override
public boolean isCancelled() {
return resultFuture.isCancelled();
}
};
}
use of org.apache.hc.core5.http.ProtocolVersion in project httpcomponents-client by apache.
the class PoolingAsyncClientConnectionManager method connect.
@Override
public Future<AsyncConnectionEndpoint> connect(final AsyncConnectionEndpoint endpoint, final ConnectionInitiator connectionInitiator, final Timeout timeout, final Object attachment, final HttpContext context, final FutureCallback<AsyncConnectionEndpoint> callback) {
Args.notNull(endpoint, "Endpoint");
Args.notNull(connectionInitiator, "Connection initiator");
final InternalConnectionEndpoint internalEndpoint = cast(endpoint);
final ComplexFuture<AsyncConnectionEndpoint> resultFuture = new ComplexFuture<>(callback);
if (internalEndpoint.isConnected()) {
resultFuture.completed(endpoint);
return resultFuture;
}
final PoolEntry<HttpRoute, ManagedAsyncClientConnection> poolEntry = internalEndpoint.getPoolEntry();
final HttpRoute route = poolEntry.getRoute();
final HttpHost host;
if (route.getProxyHost() != null) {
host = route.getProxyHost();
} else {
host = route.getTargetHost();
}
final InetSocketAddress localAddress = route.getLocalSocketAddress();
final ConnectionConfig connectionConfig = resolveConnectionConfig(route);
final TlsConfig tlsConfig = resolveTlsConfig(host, attachment);
final Timeout connectTimeout = timeout != null ? timeout : connectionConfig.getConnectTimeout();
if (LOG.isDebugEnabled()) {
LOG.debug("{} connecting endpoint to {} ({})", ConnPoolSupport.getId(endpoint), host, connectTimeout);
}
final Future<ManagedAsyncClientConnection> connectFuture = connectionOperator.connect(connectionInitiator, host, localAddress, connectTimeout, route.isTunnelled() ? TlsConfig.copy(tlsConfig).setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_1).build() : tlsConfig, context, new FutureCallback<ManagedAsyncClientConnection>() {
@Override
public void completed(final ManagedAsyncClientConnection connection) {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("{} connected {}", ConnPoolSupport.getId(endpoint), ConnPoolSupport.getId(connection));
}
final ProtocolVersion protocolVersion = connection.getProtocolVersion();
context.setProtocolVersion(protocolVersion);
final Timeout socketTimeout = connectionConfig.getSocketTimeout();
if (socketTimeout != null) {
connection.setSocketTimeout(socketTimeout);
}
poolEntry.assignConnection(connection);
resultFuture.completed(internalEndpoint);
} catch (final RuntimeException ex) {
resultFuture.failed(ex);
}
}
@Override
public void failed(final Exception ex) {
resultFuture.failed(ex);
}
@Override
public void cancelled() {
resultFuture.cancel();
}
});
resultFuture.setDependency(connectFuture);
return resultFuture;
}
Aggregations