Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2026 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 java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
import org.apache.hc.core5.http.nio.CapacityChannel;

public class ApacheHttp2AsyncEntityConsumer implements AsyncEntityConsumer<ApacheHttp2Entity> {

private EntityDetails entityDetails;
private FutureCallback<ApacheHttp2Entity> resultCallback;
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

@Override
public void streamStart(
final EntityDetails entityDetails,
final FutureCallback<ApacheHttp2Entity> resultCallback)
throws HttpException, IOException {
this.entityDetails = entityDetails;
this.resultCallback = resultCallback;
}

@Override
public void updateCapacity(CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}

@Override
public void consume(ByteBuffer src) throws IOException {
if (src.hasArray()) {
buffer.write(src.array(), src.arrayOffset() + src.position(), src.remaining());
src.position(src.limit());
} else {
byte[] bytes = new byte[src.remaining()];
src.get(bytes);
buffer.write(bytes);
}
}

@Override
public void streamEnd(List<? extends Header> trailers) throws HttpException, IOException {
if (resultCallback != null) {
resultCallback.completed(getContent());
}
}

@Override
public void failed(Exception cause) {
if (resultCallback != null) {
resultCallback.failed(cause);
}
}

@Override
public void releaseResources() {
buffer.reset();
}

@Override
public ApacheHttp2Entity getContent() {
return new ApacheHttp2Entity(buffer.toByteArray(), entityDetails);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/google/firebase/internal/ApacheHttp2Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2026 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 org.apache.hc.core5.http.EntityDetails;

public class ApacheHttp2Entity {
private final byte[] content;
private final EntityDetails entityDetails;

public ApacheHttp2Entity(byte[] content, EntityDetails entityDetails) {
this.content = content;
this.entityDetails = entityDetails;
}
Comment on lines +25 to +28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To ensure ApacheHttp2Entity is immutable, the constructor should create a defensive copy of the content byte array. This prevents the internal state of the object from being modified by external code that might hold a reference to the original array.

Suggested change
public ApacheHttp2Entity(byte[] content, EntityDetails entityDetails) {
this.content = content;
this.entityDetails = entityDetails;
}
public ApacheHttp2Entity(byte[] content, EntityDetails entityDetails) {
this.content = (content != null) ? content.clone() : null;
this.entityDetails = entityDetails;
}


public byte[] getContent() {
return content;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getContent() method returns the internal byte[] array directly. To preserve the immutability of this object, it's better to return a defensive copy of the array. This prevents callers from modifying the internal state of the ApacheHttp2Entity instance.

Suggested change
return content;
return content == null ? null : content.clone();

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is all internal I don't think we need to be this strict. It's also wrapped later in a ByteArrayInputStream before it's accessible by the google http client.

}

public EntityDetails getEntityDetails() {
return entityDetails;
}
}
28 changes: 19 additions & 9 deletions src/main/java/com/google/firebase/internal/ApacheHttp2Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
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.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.async.methods.SimpleResponseConsumer;
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;

Expand All @@ -49,6 +50,7 @@ final class ApacheHttp2Request extends LowLevelHttpRequest {
private final RequestConfig.Builder requestConfig;
private int writeTimeout;
private ApacheHttp2AsyncEntityProducer entityProducer;
private ApacheHttp2AsyncEntityConsumer entityConsumer;

ApacheHttp2Request(
CloseableHttpAsyncClient httpAsyncClient, SimpleRequestBuilder requestBuilder) {
Expand Down Expand Up @@ -85,17 +87,20 @@ public LowLevelHttpResponse execute() throws IOException {
// Build request
request = requestBuilder.build();

// Make Producer
// Make Entity Producer
CompletableFuture<Void> writeFuture = new CompletableFuture<>();
entityProducer = new ApacheHttp2AsyncEntityProducer(this, writeFuture);

// Make Entity Consumer
entityConsumer = new ApacheHttp2AsyncEntityConsumer();

// Execute
final Future<SimpleHttpResponse> responseFuture = httpAsyncClient.execute(
final Future<Message<HttpResponse, ApacheHttp2Entity>> responseFuture = httpAsyncClient.execute(
new BasicRequestProducer(request, entityProducer),
SimpleResponseConsumer.create(),
new FutureCallback<SimpleHttpResponse>() {
new BasicResponseConsumer<ApacheHttp2Entity>(entityConsumer),
new FutureCallback<Message<HttpResponse, ApacheHttp2Entity>>() {
@Override
public void completed(final SimpleHttpResponse response) {
public void completed(final Message<HttpResponse, ApacheHttp2Entity> response) {
}

@Override
Expand All @@ -120,10 +125,10 @@ public void cancelled() {

// Wait for response
try {
final SimpleHttpResponse response = responseFuture.get();
final Message<HttpResponse, ApacheHttp2Entity> response = responseFuture.get();
return new ApacheHttp2Response(response);
} catch (ExecutionException e) {
if (e.getCause() instanceof ConnectTimeoutException
if (e.getCause() instanceof ConnectTimeoutException
|| e.getCause() instanceof SocketTimeoutException) {
throw new IOException("Connection Timeout", e.getCause());
} else if (e.getCause() instanceof HttpHostConnectException) {
Expand All @@ -144,4 +149,9 @@ public void cancelled() {
ApacheHttp2AsyncEntityProducer getEntityProducer() {
return entityProducer;
}

@VisibleForTesting
ApacheHttp2AsyncEntityConsumer getEntityConsumer() {
return entityConsumer;
}
}
45 changes: 28 additions & 17 deletions src/main/java/com/google/firebase/internal/ApacheHttp2Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,27 @@
import java.io.IOException;
import java.io.InputStream;

import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Message;
import org.apache.hc.core5.http.message.StatusLine;

public class ApacheHttp2Response extends LowLevelHttpResponse {
private final SimpleHttpResponse response;
private final Message<HttpResponse, ApacheHttp2Entity> message;
private final HttpResponse response;
private final Header[] allHeaders;
private final EntityDetails entity;
private final byte[] content;

ApacheHttp2Response(SimpleHttpResponse response) {
this.response = response;
allHeaders = response.getHeaders();
ApacheHttp2Response(Message<HttpResponse, ApacheHttp2Entity> message) {
this.message = message;
this.response = message.getHead();
this.allHeaders = response.getHeaders();

ApacheHttp2Entity body = message.getBody();
this.entity = body != null ? body.getEntityDetails() : null;
this.content = body != null ? body.getContent() : null;
}

@Override
Expand All @@ -43,25 +53,25 @@ public int getStatusCode() {

@Override
public InputStream getContent() throws IOException {
return new ByteArrayInputStream(response.getBodyBytes());
return content == null ? null : new ByteArrayInputStream(content);
}

@Override
public String getContentEncoding() {
Header contentEncodingHeader = response.getFirstHeader("Content-Encoding");
return contentEncodingHeader == null ? null : contentEncodingHeader.getValue();
return entity == null ? null : entity.getContentEncoding();
}

@Override
public long getContentLength() {
String bodyText = response.getBodyText();
return bodyText == null ? 0 : bodyText.length();
if (content != null) {
return content.length;
}
return entity == null ? -1 : entity.getContentLength();
}

@Override
public String getContentType() {
ContentType contentType = response.getContentType();
return contentType == null ? null : contentType.toString();
return entity == null ? null : entity.getContentType();
}

@Override
Expand All @@ -71,11 +81,12 @@ public String getReasonPhrase() {

@Override
public String getStatusLine() {
return response.toString();
return new StatusLine(response).toString();
}

public String getHeaderValue(String name) {
return response.getLastHeader(name).getValue();
Header header = response.getLastHeader(name);
return header == null ? null : header.getValue();
}

@Override
Expand All @@ -94,7 +105,7 @@ public String getHeaderName(int index) {
}

@VisibleForTesting
public SimpleHttpResponse getResponse() {
return response;
public Message<HttpResponse, ApacheHttp2Entity> getMessage() {
return message;
}
}
Loading
Loading