|
| 1 | +/* |
| 2 | + * Copyright 2024 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.internal; |
| 18 | + |
| 19 | +import com.google.api.client.http.LowLevelHttpRequest; |
| 20 | +import com.google.api.client.http.LowLevelHttpResponse; |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.SocketTimeoutException; |
| 25 | +import java.util.concurrent.CancellationException; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ExecutionException; |
| 28 | +import java.util.concurrent.Future; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.TimeoutException; |
| 31 | + |
| 32 | +import org.apache.hc.client5.http.ConnectTimeoutException; |
| 33 | +import org.apache.hc.client5.http.HttpHostConnectException; |
| 34 | +import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; |
| 35 | +import org.apache.hc.client5.http.async.methods.SimpleHttpResponse; |
| 36 | +import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder; |
| 37 | +import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer; |
| 38 | +import org.apache.hc.client5.http.config.RequestConfig; |
| 39 | +import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
| 40 | +import org.apache.hc.core5.concurrent.FutureCallback; |
| 41 | +import org.apache.hc.core5.http.nio.support.BasicRequestProducer; |
| 42 | +import org.apache.hc.core5.http2.H2StreamResetException; |
| 43 | +import org.apache.hc.core5.util.Timeout; |
| 44 | + |
| 45 | +final class ApacheHttp2Request extends LowLevelHttpRequest { |
| 46 | + private final CloseableHttpAsyncClient httpAsyncClient; |
| 47 | + private final SimpleRequestBuilder requestBuilder; |
| 48 | + private SimpleHttpRequest request; |
| 49 | + private final RequestConfig.Builder requestConfig; |
| 50 | + private int writeTimeout; |
| 51 | + private ApacheHttp2AsyncEntityProducer entityProducer; |
| 52 | + |
| 53 | + ApacheHttp2Request( |
| 54 | + CloseableHttpAsyncClient httpAsyncClient, SimpleRequestBuilder requestBuilder) { |
| 55 | + this.httpAsyncClient = httpAsyncClient; |
| 56 | + this.requestBuilder = requestBuilder; |
| 57 | + this.writeTimeout = 0; |
| 58 | + |
| 59 | + this.requestConfig = RequestConfig.custom() |
| 60 | + .setRedirectsEnabled(false); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void addHeader(String name, String value) { |
| 65 | + requestBuilder.addHeader(name, value); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void setTimeout(int connectionTimeout, int readTimeout) throws IOException { |
| 70 | + requestConfig |
| 71 | + .setConnectTimeout(Timeout.ofMilliseconds(connectionTimeout)) |
| 72 | + .setResponseTimeout(Timeout.ofMilliseconds(readTimeout)); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void setWriteTimeout(int writeTimeout) throws IOException { |
| 77 | + this.writeTimeout = writeTimeout; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public LowLevelHttpResponse execute() throws IOException { |
| 82 | + // Set request configs |
| 83 | + requestBuilder.setRequestConfig(requestConfig.build()); |
| 84 | + |
| 85 | + // Build request |
| 86 | + request = requestBuilder.build(); |
| 87 | + |
| 88 | + // Make Producer |
| 89 | + CompletableFuture<Void> writeFuture = new CompletableFuture<>(); |
| 90 | + entityProducer = new ApacheHttp2AsyncEntityProducer(this, writeFuture); |
| 91 | + |
| 92 | + // Execute |
| 93 | + final Future<SimpleHttpResponse> responseFuture = httpAsyncClient.execute( |
| 94 | + new BasicRequestProducer(request, entityProducer), |
| 95 | + SimpleResponseConsumer.create(), |
| 96 | + new FutureCallback<SimpleHttpResponse>() { |
| 97 | + @Override |
| 98 | + public void completed(final SimpleHttpResponse response) { |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void failed(final Exception exception) { |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void cancelled() { |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + // Wait for write |
| 111 | + try { |
| 112 | + if (writeTimeout != 0) { |
| 113 | + writeFuture.get(writeTimeout, TimeUnit.MILLISECONDS); |
| 114 | + } |
| 115 | + } catch (TimeoutException e) { |
| 116 | + throw new IOException("Write Timeout", e.getCause()); |
| 117 | + } catch (Exception e) { |
| 118 | + throw new IOException("Exception in write", e.getCause()); |
| 119 | + } |
| 120 | + |
| 121 | + // Wait for response |
| 122 | + try { |
| 123 | + final SimpleHttpResponse response = responseFuture.get(); |
| 124 | + return new ApacheHttp2Response(response); |
| 125 | + } catch (ExecutionException e) { |
| 126 | + if (e.getCause() instanceof ConnectTimeoutException |
| 127 | + || e.getCause() instanceof SocketTimeoutException) { |
| 128 | + throw new IOException("Connection Timeout", e.getCause()); |
| 129 | + } else if (e.getCause() instanceof HttpHostConnectException) { |
| 130 | + throw new IOException("Connection exception in request", e.getCause()); |
| 131 | + } else if (e.getCause() instanceof H2StreamResetException) { |
| 132 | + throw new IOException("Stream exception in request", e.getCause()); |
| 133 | + } else { |
| 134 | + throw new IOException("Unknown exception in request", e); |
| 135 | + } |
| 136 | + } catch (InterruptedException e) { |
| 137 | + throw new IOException("Request Interrupted", e); |
| 138 | + } catch (CancellationException e) { |
| 139 | + throw new IOException("Request Cancelled", e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @VisibleForTesting |
| 144 | + ApacheHttp2AsyncEntityProducer getEntityProducer() { |
| 145 | + return entityProducer; |
| 146 | + } |
| 147 | +} |
0 commit comments