forked from square/okhttp
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix poor buffering case for MultipartReader (square#8665)
* Demonstrate poor buffering case * Fix for repeated reads of small byteCount from large part (cherry picked from commit 3cc87c3)
- Loading branch information
Showing
4 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
Submodule hpack-test-case
updated
510 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
okhttp/src/main/kotlin/okhttp3/internal/FixedLengthSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* 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 okhttp3.internal | ||
|
||
import kotlin.jvm.JvmOverloads | ||
import okio.Buffer | ||
import okio.ForwardingSource | ||
import okio.Source | ||
|
||
/** | ||
* Return a new [Source] whose [read function][Source.read] returns -1 after [byteCount] | ||
* bytes have been read. | ||
* | ||
* @param onReadExhausted Callback invoked once when the end of bytes has been reached. It receives | ||
* `true` if the end of bytes was because the underlying stream did not contain enough bytes and | ||
* `false` if [byteCount] bytes were successfully read. | ||
*/ | ||
@JvmOverloads | ||
internal fun Source.limit( | ||
byteCount: Long, | ||
onReadExhausted: (eof: Boolean) -> Unit = {}, | ||
): Source { | ||
require(byteCount >= 0) { "byteCount < 0: $byteCount" } | ||
return FixedLengthSource(this, byteCount, onReadExhausted, truncate = true) | ||
} | ||
|
||
internal class FixedLengthSource( | ||
delegate: Source, | ||
private var bytesRemaining: Long, | ||
onReadExhausted: (eof: Boolean) -> Unit, | ||
private val truncate: Boolean, | ||
) : ForwardingSource(delegate) { | ||
/** `null` once invoked. */ | ||
private var onReadExhausted: ((eof: Boolean) -> Unit)? = onReadExhausted | ||
|
||
override fun read( | ||
sink: Buffer, | ||
byteCount: Long, | ||
): Long { | ||
val requestBytes = | ||
if (truncate) { | ||
if (bytesRemaining == 0L) { | ||
// If the limit was 0 we want to wait until the first call to this function before | ||
// triggering the callback. | ||
onReadExhausted?.invoke(false) | ||
onReadExhausted = null | ||
return -1L | ||
} | ||
minOf(bytesRemaining, byteCount) | ||
} else { | ||
byteCount | ||
} | ||
val readBytes = super.read(sink, requestBytes) | ||
if (readBytes == -1L) { | ||
onReadExhausted!!(true) | ||
onReadExhausted = null | ||
return -1L | ||
} | ||
bytesRemaining -= readBytes | ||
if (bytesRemaining == 0L) { | ||
onReadExhausted!!(false) | ||
onReadExhausted = null | ||
} | ||
return readBytes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters