Skip to content
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

Uploads should be read only once even when logging #4125

Merged
merged 7 commits into from
May 23, 2022

Conversation

BoD
Copy link
Contributor

@BoD BoD commented May 19, 2022

In this PR, 2 commits:

  1. In LoggingInterceptor, fully read the request's body and redispatch downstream + add an option to not log the request's body
  2. add an isOneShot field to HttpBody, and use it with with the OkHttp engine. This makes using OkHttp's log interceptor not break Uploads

Pretty sure 2. is a breaking change, but I still wanted to see where it takes us. (I can think of a hack to still pass the option to OkHttp without adding the field to the interface)

See also #4054

@netlify
Copy link

netlify bot commented May 19, 2022

Deploy Preview for apollo-android-docs canceled.

Name Link
🔨 Latest commit 97477dd
🔍 Latest deploy log https://app.netlify.com/sites/apollo-android-docs/deploys/628b477e0921c800088ec1fe

* This would be the case when reading the source of data consumes it, for instance when uploading files.
*/
val isOneShot: Boolean
get() = false
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like this too much. That one more API that's useful to a small portion of the usecases but accounts for 25% of the interface. It also introduce default methods, which is something I'm never sure how backward compatible it is.

Instead, I'd much rather fail early in DefaultUpload if the body has been transmitted already.

* be fully loaded into memory, which may cause OutOfMemoryErrors.
*/
class LoggingInterceptor(
private val logRequestBody: Boolean,
Copy link
Contributor

Choose a reason for hiding this comment

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

Make this a level, like in OkHttp?

@@ -51,6 +52,10 @@ actual class DefaultHttpEngine constructor(

override fun contentLength() = body.contentLength

// This prevents OkHttp from reading the body several times (e.g. when using its logging interceptor)
// which could consume files when using Uploads
override fun isOneShot() = body is UploadsHttpBody
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A bit hacky...

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

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

One minor comment. LGTM otherwise. Sorry for the delay reviewing this!

@BoD BoD merged commit 699a36f into main May 23, 2022
@BoD BoD deleted the logging-interceptor-dont-consume-request-body branch May 23, 2022 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants