-
Notifications
You must be signed in to change notification settings - Fork 577
/
Copy pathHttpUrlConnectionImpl.java
335 lines (298 loc) · 14.6 KB
/
HttpUrlConnectionImpl.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.clickhouse.client.http;
import com.clickhouse.client.ClickHouseChecker;
import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseConfig;
import com.clickhouse.client.ClickHouseFormat;
import com.clickhouse.client.ClickHouseInputStream;
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseOutputStream;
import com.clickhouse.client.ClickHouseRequest;
import com.clickhouse.client.ClickHouseSslContextProvider;
import com.clickhouse.client.ClickHouseUtils;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.config.ClickHouseSslMode;
import com.clickhouse.client.data.ClickHouseExternalTable;
import com.clickhouse.client.http.config.ClickHouseHttpOption;
import com.clickhouse.client.logging.Logger;
import com.clickhouse.client.logging.LoggerFactory;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
public class HttpUrlConnectionImpl extends ClickHouseHttpConnection {
private static final Logger log = LoggerFactory.getLogger(HttpUrlConnectionImpl.class);
private static final byte[] HEADER_CONTENT_DISPOSITION = "content-disposition: form-data; name=\""
.getBytes(StandardCharsets.US_ASCII);
private static final byte[] HEADER_OCTET_STREAM = "content-type: application/octet-stream\r\n"
.getBytes(StandardCharsets.US_ASCII);
private static final byte[] HEADER_BINARY_ENCODING = "content-transfer-encoding: binary\r\n\r\n"
.getBytes(StandardCharsets.US_ASCII);
private static final byte[] DOUBLE_DASH = new byte[] { '-', '-' };
private static final byte[] END_OF_NAME = new byte[] { '"', '\r', '\n' };
private static final byte[] LINE_PREFIX = new byte[] { '\r', '\n', '-', '-' };
private static final byte[] LINE_SUFFIX = new byte[] { '\r', '\n' };
private static final byte[] SUFFIX_QUERY = "query\"\r\n\r\n".getBytes(StandardCharsets.US_ASCII);
private static final byte[] SUFFIX_FORMAT = "_format\"\r\n\r\n".getBytes(StandardCharsets.US_ASCII);
private static final byte[] SUFFIX_STRUCTURE = "_structure\"\r\n\r\n".getBytes(StandardCharsets.US_ASCII);
private static final byte[] SUFFIX_FILENAME = "\"; filename=\"".getBytes(StandardCharsets.US_ASCII);
private final HttpURLConnection conn;
private ClickHouseHttpResponse buildResponse(Runnable postCloseAction) throws IOException {
// X-ClickHouse-Server-Display-Name: xxx
// X-ClickHouse-Query-Id: xxx
// X-ClickHouse-Format: RowBinaryWithNamesAndTypes
// X-ClickHouse-Timezone: UTC
// X-ClickHouse-Summary:
// {"read_rows":"0","read_bytes":"0","written_rows":"0","written_bytes":"0","total_rows_to_read":"0"}
String displayName = getResponseHeader("X-ClickHouse-Server-Display-Name", server.getHost());
String queryId = getResponseHeader("X-ClickHouse-Query-Id", "");
String summary = getResponseHeader("X-ClickHouse-Summary", "{}");
ClickHouseConfig c = config;
ClickHouseFormat format = c.getFormat();
TimeZone timeZone = c.getServerTimeZone();
boolean hasOutputFile = output != null && output.getUnderlyingFile().isAvailable();
boolean hasQueryResult = false;
// queryId, format and timeZone are only available for queries
if (!ClickHouseChecker.isNullOrEmpty(queryId)) {
String value = getResponseHeader("X-ClickHouse-Format", "");
if (!ClickHouseChecker.isNullOrEmpty(value)) {
format = ClickHouseFormat.valueOf(value);
hasQueryResult = true;
}
value = getResponseHeader("X-ClickHouse-Timezone", "");
timeZone = !ClickHouseChecker.isNullOrEmpty(value) ? TimeZone.getTimeZone(value)
: timeZone;
}
final InputStream source;
final Runnable action;
if (output != null) {
source = ClickHouseInputStream.empty();
action = () -> {
try (OutputStream o = output) {
ClickHouseInputStream.pipe(conn.getInputStream(), o, c.getWriteBufferSize());
if (postCloseAction != null) {
postCloseAction.run();
}
} catch (IOException e) {
throw new UncheckedIOException("Failed to redirect response to given output stream", e);
}
};
} else {
source = conn.getInputStream();
action = postCloseAction;
}
return new ClickHouseHttpResponse(this,
hasOutputFile ? ClickHouseInputStream.of(source, c.getReadBufferSize(), action)
: (hasQueryResult ? ClickHouseClient.getAsyncResponseInputStream(c, source, action)
: ClickHouseClient.getResponseInputStream(c, source, action)),
displayName, queryId, summary, format, timeZone);
}
@SuppressWarnings("lgtm[java/unsafe-hostname-verification]")
private HttpURLConnection newConnection(String url, boolean post) throws IOException {
HttpURLConnection newConn = config.isUseNoProxy()
? (HttpURLConnection) new URL(url).openConnection(Proxy.NO_PROXY)
: (HttpURLConnection) new URL(url).openConnection();
if ((newConn instanceof HttpsURLConnection) && config.isSsl()) {
HttpsURLConnection secureConn = (HttpsURLConnection) newConn;
SSLContext sslContext = ClickHouseSslContextProvider.getProvider().getSslContext(SSLContext.class, config)
.orElse(null);
HostnameVerifier verifier = config.getSslMode() == ClickHouseSslMode.STRICT
? HttpsURLConnection.getDefaultHostnameVerifier()
: (hostname, session) -> true;
secureConn.setHostnameVerifier(verifier);
secureConn.setSSLSocketFactory(sslContext.getSocketFactory());
}
if (post) {
newConn.setInstanceFollowRedirects(true);
newConn.setRequestMethod("POST");
}
newConn.setUseCaches(false);
newConn.setAllowUserInteraction(false);
newConn.setDoInput(true);
newConn.setDoOutput(true);
newConn.setConnectTimeout(config.getConnectionTimeout());
newConn.setReadTimeout(config.getSocketTimeout());
return newConn;
}
private String getResponseHeader(String header, String defaultValue) {
String value = conn.getHeaderField(header);
return value != null ? value : defaultValue;
}
private void setHeaders(HttpURLConnection conn, Map<String, String> headers) {
headers = mergeHeaders(headers);
if (headers != null && !headers.isEmpty()) {
for (Entry<String, String> header : headers.entrySet()) {
conn.setRequestProperty(header.getKey(), header.getValue());
}
}
}
private void checkResponse(HttpURLConnection conn) throws IOException {
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
String errorCode = conn.getHeaderField("X-ClickHouse-Exception-Code");
// String encoding = conn.getHeaderField("Content-Encoding");
String serverName = conn.getHeaderField("X-ClickHouse-Server-Display-Name");
InputStream errorInput = conn.getErrorStream();
if (errorInput == null) {
// TODO follow redirects?
throw new ConnectException(ClickHouseUtils.format("HTTP response %d %s", conn.getResponseCode(),
conn.getResponseMessage()));
}
String errorMsg;
int bufferSize = (int) ClickHouseClientOption.BUFFER_SIZE.getDefaultValue();
ByteArrayOutputStream output = new ByteArrayOutputStream(bufferSize);
ClickHouseInputStream.pipe(errorInput, output, bufferSize);
byte[] bytes = output.toByteArray();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
ClickHouseClient.getResponseInputStream(config, new ByteArrayInputStream(bytes), null),
StandardCharsets.UTF_8))) {
StringBuilder builder = new StringBuilder();
while ((errorMsg = reader.readLine()) != null) {
builder.append(errorMsg).append('\n');
}
errorMsg = builder.toString();
} catch (IOException e) {
log.debug("Failed to read error message[code=%s] from server [%s] due to: %s", errorCode, serverName,
e.getMessage());
errorMsg = new String(bytes, StandardCharsets.UTF_8);
}
throw new IOException(errorMsg);
}
}
protected HttpUrlConnectionImpl(ClickHouseNode server, ClickHouseRequest<?> request, ExecutorService executor)
throws IOException {
super(server, request);
conn = newConnection(url, true);
}
@Override
protected boolean isReusable() {
return false;
}
@Override
protected ClickHouseHttpResponse post(String sql, ClickHouseInputStream data, List<ClickHouseExternalTable> tables,
String url, Map<String, String> headers, ClickHouseConfig config, Runnable postCloseAction)
throws IOException {
Charset ascii = StandardCharsets.US_ASCII;
byte[] boundary = null;
if (tables != null && !tables.isEmpty()) {
String uuid = rm.createUniqueId();
conn.setRequestProperty("content-type", "multipart/form-data; boundary=".concat(uuid));
boundary = uuid.getBytes(ascii);
} else {
conn.setRequestProperty("content-type", "text/plain; charset=UTF-8");
}
setHeaders(conn, headers);
ClickHouseConfig c = config;
final boolean hasFile = data != null && data.getUnderlyingFile().isAvailable();
final boolean hasInput = data != null || boundary != null;
if (hasInput) {
conn.setChunkedStreamingMode(config.getRequestChunkSize());
} else {
// TODO conn.setFixedLengthStreamingMode(contentLength);
}
try (ClickHouseOutputStream out = hasFile
? ClickHouseOutputStream.of(conn.getOutputStream(), config.getWriteBufferSize())
: (hasInput
? ClickHouseClient.getAsyncRequestOutputStream(config, conn.getOutputStream(), null) // latch::countDown)
: ClickHouseClient.getRequestOutputStream(c, conn.getOutputStream(), null))) {
Charset utf8 = StandardCharsets.UTF_8;
byte[] sqlBytes = hasFile ? new byte[0] : sql.getBytes(utf8);
if (boundary != null) {
out.writeBytes(LINE_PREFIX);
out.writeBytes(boundary);
out.writeBytes(LINE_SUFFIX);
out.writeBytes(HEADER_CONTENT_DISPOSITION);
out.writeBytes(SUFFIX_QUERY);
out.writeBytes(sqlBytes);
for (ClickHouseExternalTable t : tables) {
byte[] tableName = t.getName().getBytes(utf8);
for (int i = 0; i < 3; i++) {
out.writeBytes(LINE_PREFIX);
out.writeBytes(boundary);
out.writeBytes(LINE_SUFFIX);
out.writeBytes(HEADER_CONTENT_DISPOSITION);
out.writeBytes(tableName);
if (i == 0) {
out.writeBytes(SUFFIX_FORMAT);
out.writeBytes(t.getFormat().name().getBytes(ascii));
} else if (i == 1) {
out.writeBytes(SUFFIX_STRUCTURE);
out.writeBytes(t.getStructure().getBytes(utf8));
} else {
out.writeBytes(SUFFIX_FILENAME);
out.writeBytes(tableName);
out.writeBytes(END_OF_NAME);
break;
}
}
out.writeBytes(HEADER_OCTET_STREAM);
out.writeBytes(HEADER_BINARY_ENCODING);
ClickHouseInputStream.pipe(t.getContent(), out, c.getWriteBufferSize());
}
out.writeBytes(LINE_PREFIX);
out.writeBytes(boundary);
out.writeBytes(DOUBLE_DASH);
out.writeBytes(LINE_SUFFIX);
} else {
out.writeBytes(sqlBytes);
if (data != null && data.available() > 0) {
// append \n
if (sqlBytes.length > 0 && sqlBytes[sqlBytes.length - 1] != (byte) '\n') {
out.write(10);
}
ClickHouseInputStream.pipe(data, out, c.getWriteBufferSize());
}
}
}
checkResponse(conn);
return buildResponse(postCloseAction);
}
@Override
public boolean ping(int timeout) {
String response = config.getStrOption(ClickHouseHttpOption.DEFAULT_RESPONSE);
String url = null;
HttpURLConnection c = null;
try {
url = getBaseUrl().concat("ping");
c = newConnection(url, false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
checkResponse(c);
int size = 12;
try (ByteArrayOutputStream out = new ByteArrayOutputStream(size)) {
ClickHouseInputStream.pipe(c.getInputStream(), out, size);
c.disconnect();
c = null;
return response.equals(new String(out.toByteArray(), StandardCharsets.UTF_8));
}
} catch (IOException e) {
log.debug("Failed to ping url %s due to: %s", url, e.getMessage());
} finally {
if (c != null) {
c.disconnect();
}
}
return false;
}
@Override
public void close() {
conn.disconnect();
}
}