|
63 | 63 | import java.net.NoRouteToHostException; |
64 | 64 | import java.net.URI; |
65 | 65 | import java.net.URISyntaxException; |
| 66 | +import java.net.URLEncoder; |
66 | 67 | import java.net.UnknownHostException; |
67 | 68 | import java.nio.charset.StandardCharsets; |
68 | 69 | import java.security.NoSuchAlgorithmException; |
@@ -338,13 +339,14 @@ public ClassicHttpResponse executeRequest(ClickHouseNode server, Map<String, Obj |
338 | 339 | .build(); |
339 | 340 | req.setConfig(httpReqConfig); |
340 | 341 | // setting entity. wrapping if compression is enabled |
341 | | - req.setEntity(wrapEntity(new EntityTemplate(-1, CONTENT_TYPE, null, writeCallback), false)); |
| 342 | + req.setEntity(wrapEntity(new EntityTemplate(-1, CONTENT_TYPE, null, writeCallback), HttpStatus.SC_OK, false)); |
342 | 343 |
|
343 | 344 | HttpClientContext context = HttpClientContext.create(); |
344 | 345 |
|
345 | 346 | try { |
346 | 347 | ClassicHttpResponse httpResponse = httpClient.executeOpen(null, req, context); |
347 | | - httpResponse.setEntity(wrapEntity(httpResponse.getEntity(), true)); |
| 348 | + httpResponse.setEntity(wrapEntity(httpResponse.getEntity(), httpResponse.getCode(), true)); |
| 349 | + |
348 | 350 | if (httpResponse.getCode() == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { |
349 | 351 | throw new ClientMisconfigurationException("Proxy authentication required. Please check your proxy settings."); |
350 | 352 | } else if (httpResponse.getCode() == HttpStatus.SC_BAD_GATEWAY) { |
@@ -395,11 +397,12 @@ private void addHeaders(HttpPost req, Map<String, String> chConfig, Map<String, |
395 | 397 | req.addHeader(ClickHouseHttpProto.HEADER_QUERY_ID, requestConfig.get(ClickHouseClientOption.QUERY_ID.getKey()).toString()); |
396 | 398 | } |
397 | 399 | } |
398 | | - req.addHeader(ClickHouseHttpProto.HEADER_DB_USER, chConfig.get(ClickHouseDefaults.USER.getKey())); |
399 | 400 | if (MapUtils.getFlag(chConfig, "ssl_authentication", false)) { |
| 401 | + req.addHeader(ClickHouseHttpProto.HEADER_DB_USER, chConfig.get(ClickHouseDefaults.USER.getKey())); |
400 | 402 | req.addHeader(ClickHouseHttpProto.HEADER_SSL_CERT_AUTH, "on"); |
401 | 403 | } else { |
402 | | - req.addHeader(ClickHouseHttpProto.HEADER_DB_PASSWORD, chConfig.get(ClickHouseDefaults.PASSWORD.getKey())); |
| 404 | + req.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString( |
| 405 | + (chConfig.get(ClickHouseDefaults.USER.getKey()) + ":" + chConfig.get(ClickHouseDefaults.PASSWORD.getKey())).getBytes(StandardCharsets.UTF_8))); |
403 | 406 | } |
404 | 407 | if (proxyAuthHeaderValue != null) { |
405 | 408 | req.addHeader(HttpHeaders.PROXY_AUTHORIZATION, proxyAuthHeaderValue); |
@@ -487,15 +490,26 @@ private void addQueryParams(URIBuilder req, Map<String, String> chConfig, Map<St |
487 | 490 | } |
488 | 491 | } |
489 | 492 |
|
490 | | - private HttpEntity wrapEntity(HttpEntity httpEntity, boolean isResponse) { |
491 | | - boolean serverCompression = chConfiguration.getOrDefault(ClickHouseClientOption.COMPRESS.getKey(), "false").equalsIgnoreCase("true"); |
492 | | - boolean clientCompression = chConfiguration.getOrDefault(ClickHouseClientOption.DECOMPRESS.getKey(), "false").equalsIgnoreCase("true"); |
493 | | - boolean useHttpCompression = chConfiguration.getOrDefault("client.use_http_compression", "false").equalsIgnoreCase("true"); |
494 | | - if (serverCompression || clientCompression) { |
495 | | - return new LZ4Entity(httpEntity, useHttpCompression, serverCompression, clientCompression, |
496 | | - MapUtils.getInt(chConfiguration, "compression.lz4.uncompressed_buffer_size"), isResponse); |
497 | | - } else { |
498 | | - return httpEntity; |
| 493 | + private HttpEntity wrapEntity(HttpEntity httpEntity, int httpStatus, boolean isResponse) { |
| 494 | + |
| 495 | + switch (httpStatus) { |
| 496 | + case HttpStatus.SC_OK: |
| 497 | + case HttpStatus.SC_CREATED: |
| 498 | + case HttpStatus.SC_ACCEPTED: |
| 499 | + case HttpStatus.SC_NO_CONTENT: |
| 500 | + case HttpStatus.SC_PARTIAL_CONTENT: |
| 501 | + case HttpStatus.SC_RESET_CONTENT: |
| 502 | + case HttpStatus.SC_NOT_MODIFIED: |
| 503 | + case HttpStatus.SC_BAD_REQUEST: |
| 504 | + boolean serverCompression = chConfiguration.getOrDefault(ClickHouseClientOption.COMPRESS.getKey(), "false").equalsIgnoreCase("true"); |
| 505 | + boolean clientCompression = chConfiguration.getOrDefault(ClickHouseClientOption.DECOMPRESS.getKey(), "false").equalsIgnoreCase("true"); |
| 506 | + boolean useHttpCompression = chConfiguration.getOrDefault("client.use_http_compression", "false").equalsIgnoreCase("true"); |
| 507 | + if (serverCompression || clientCompression) { |
| 508 | + return new LZ4Entity(httpEntity, useHttpCompression, serverCompression, clientCompression, |
| 509 | + MapUtils.getInt(chConfiguration, "compression.lz4.uncompressed_buffer_size"), isResponse); |
| 510 | + } |
| 511 | + default: |
| 512 | + return httpEntity; |
499 | 513 | } |
500 | 514 | } |
501 | 515 |
|
|
0 commit comments