-
Notifications
You must be signed in to change notification settings - Fork 301
fix: Reimplement HTTP/2 response entity consumption using ApacheHttp2AsyncEntityConsumer and ApacheHttp2Entity #1181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
| } |
| 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; | ||||||
| } | ||||||
|
|
||||||
| public byte[] getContent() { | ||||||
| return content; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure
ApacheHttp2Entityis immutable, the constructor should create a defensive copy of thecontentbyte array. This prevents the internal state of the object from being modified by external code that might hold a reference to the original array.