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