Skip to content

Commit

Permalink
Fixed compression support for h2c protocol
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
reta committed Oct 26, 2022
1 parent d96da50 commit 845d6ea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG]: flaky test index/80_geo_point/Single point test([#4860](https://github.com/opensearch-project/OpenSearch/pull/4860))
- Fix bug in SlicedInputStream with zero length ([#4863](https://github.com/opensearch-project/OpenSearch/pull/4863))
- Fix a bug on handling an invalid array value for point type field #4900([#4900](https://github.com/opensearch-project/OpenSearch/pull/4900))
- Fixed compression support for h2c protocol ([#4944](https://github.com/opensearch-project/OpenSearch/pull/4944))

### Security
- CVE-2022-25857 org.yaml:snakeyaml DOS vulnerability ([#4341](https://github.com/opensearch-project/OpenSearch/pull/4341))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@

package org.opensearch.client;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.entity.GzipCompressingEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import static org.hamcrest.Matchers.equalTo;

Expand All @@ -62,4 +71,32 @@ public void testCompressesResponseIfRequested() throws IOException {
assertEquals(SAMPLE_DOCUMENT, searchResponse.getHits().getHits()[0].getSourceAsString());
}

/**
* The default CloseableHttpAsyncClient does not support compression out of the box (so that applies to RestClient
* and RestHighLevelClient). To check the compression works on both sides, crafting the request using CloseableHttpClient
* instead which uses compression by default.
*/
public void testCompressesRequest() throws IOException, URISyntaxException {
try (CloseableHttpClient client = HttpClients.custom().build()) {
final Node node = client().getNodes().iterator().next();
final URI baseUri = new URI(node.getHost().toURI());

final HttpPut index = new HttpPut(baseUri.resolve("/company/_doc/1"));
index.setEntity(new GzipCompressingEntity(new StringEntity(SAMPLE_DOCUMENT, ContentType.APPLICATION_JSON)));
try (CloseableHttpResponse response = client.execute(index)) {
assertThat(response.getCode(), equalTo(201));
}

final HttpGet refresh = new HttpGet(baseUri.resolve("/_refresh"));
try (CloseableHttpResponse response = client.execute(refresh)) {
assertThat(response.getCode(), equalTo(200));
}

final HttpPost search = new HttpPost(baseUri.resolve("/_search"));
index.setEntity(new GzipCompressingEntity(new StringEntity("{}", ContentType.APPLICATION_JSON)));
try (CloseableHttpResponse response = client.execute(search)) {
assertThat(response.getCode(), equalTo(200));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,9 @@ protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws E
// If this handler is hit then no upgrade has been attempted and the client is just talking HTTP
final ChannelPipeline pipeline = ctx.pipeline();
pipeline.addAfter(ctx.name(), "handler", getRequestHandler());
pipeline.replace(this, "aggregator", aggregator);
pipeline.replace(this, "decoder_compress", new HttpContentDecompressor());

ch.pipeline().addLast("decoder_compress", new HttpContentDecompressor());
ch.pipeline().addLast("encoder", new HttpResponseEncoder());
pipeline.addAfter("decoder_compress", "aggregator", aggregator);
if (handlingSettings.isCompression()) {
ch.pipeline()
.addAfter("aggregator", "encoder_compress", new HttpContentCompressor(handlingSettings.getCompressionLevel()));
Expand Down

0 comments on commit 845d6ea

Please sign in to comment.