|
| 1 | +package io.sentry.okhttp |
| 2 | + |
| 3 | +import java.io.IOException |
| 4 | +import java.util.concurrent.atomic.AtomicBoolean |
| 5 | +import okhttp3.MediaType |
| 6 | +import okhttp3.ResponseBody |
| 7 | +import okio.Buffer |
| 8 | +import okio.BufferedSource |
| 9 | +import okio.ForwardingSource |
| 10 | +import okio.Source |
| 11 | +import okio.buffer |
| 12 | + |
| 13 | +/** |
| 14 | + * A [ResponseBody] that copies the bytes the application reads into a capped buffer. |
| 15 | + * |
| 16 | + * @param delegate the body to capture from. |
| 17 | + * @param maxBytes the maximum number of bytes to retain; capture stops once it is reached. |
| 18 | + * @param onCaptured invoked at most once, as soon as the capture can no longer grow: the cap is |
| 19 | + * reached, the stream ends, a read fails, or the body is closed. Runs synchronously on the thread |
| 20 | + * that read or closed the body, so it must not block. A body abandoned without any of those never |
| 21 | + * reaches it. `isComplete` is false when the bytes are only a prefix of the body. |
| 22 | + */ |
| 23 | +internal class CapturedResponseBody( |
| 24 | + private val delegate: ResponseBody, |
| 25 | + private val maxBytes: Long, |
| 26 | + private val onCaptured: (bytes: ByteArray, isComplete: Boolean) -> Unit, |
| 27 | +) : ResponseBody() { |
| 28 | + |
| 29 | + private val captured = Buffer() |
| 30 | + private val capturedOut = captured.outputStream() |
| 31 | + |
| 32 | + private val reported = AtomicBoolean(false) |
| 33 | + @Volatile private var bytesRead = 0L |
| 34 | + @Volatile private var endOfStream = false |
| 35 | + |
| 36 | + private val capturingSourceLazy = lazy { CapturingSource(delegate.source()).buffer() } |
| 37 | + private val capturingSource: BufferedSource by capturingSourceLazy |
| 38 | + |
| 39 | + override fun contentType(): MediaType? = delegate.contentType() |
| 40 | + |
| 41 | + override fun contentLength(): Long = delegate.contentLength() |
| 42 | + |
| 43 | + override fun source(): BufferedSource = capturingSource |
| 44 | + |
| 45 | + override fun close() { |
| 46 | + // Closing the delegate notifies listeners, which may serialize the breadcrumb this capture |
| 47 | + // belongs to, so report first. |
| 48 | + delegate.use { |
| 49 | + try { |
| 50 | + reportCaptured() |
| 51 | + } finally { |
| 52 | + // the application may still hold the source, and must not read buffered bytes from it |
| 53 | + if (capturingSourceLazy.isInitialized()) { |
| 54 | + capturingSource.close() |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private inner class CapturingSource(source: Source) : ForwardingSource(source) { |
| 61 | + override fun read(sink: Buffer, byteCount: Long): Long { |
| 62 | + val sinkBefore = sink.size |
| 63 | + val read = |
| 64 | + try { |
| 65 | + super.read(sink, byteCount) |
| 66 | + } catch (e: IOException) { |
| 67 | + // What arrived is the evidence for this very failure, and a caller handling it is not |
| 68 | + // obliged to close the body. |
| 69 | + reportCaptured() |
| 70 | + throw e |
| 71 | + } |
| 72 | + |
| 73 | + if (read > 0L) { |
| 74 | + bytesRead += read |
| 75 | + if (!reported.get()) { |
| 76 | + val capFull = |
| 77 | + synchronized(captured) { |
| 78 | + // checked again under the lock, so nothing is copied into a drained capture |
| 79 | + if (reported.get()) { |
| 80 | + return@synchronized false |
| 81 | + } |
| 82 | + // the application may not have taken everything in the sink yet, hence the offset |
| 83 | + val toTake = minOf(maxBytes - captured.size, sink.size - sinkBefore) |
| 84 | + if (toTake > 0L) { |
| 85 | + sink.copyTo(capturedOut, sinkBefore, toTake) |
| 86 | + } |
| 87 | + captured.size >= maxBytes |
| 88 | + } |
| 89 | + if (capFull) { |
| 90 | + reportCaptured() |
| 91 | + } |
| 92 | + } |
| 93 | + } else if (read == -1L) { |
| 94 | + endOfStream = true |
| 95 | + reportCaptured() |
| 96 | + } |
| 97 | + return read |
| 98 | + } |
| 99 | + |
| 100 | + override fun close() { |
| 101 | + try { |
| 102 | + reportCaptured() |
| 103 | + } finally { |
| 104 | + super.close() |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private fun reportCaptured() { |
| 110 | + if (reported.compareAndSet(false, true)) { |
| 111 | + val length = delegate.contentLength() |
| 112 | + val isComplete = endOfStream || (length >= 0L && bytesRead >= length) |
| 113 | + onCaptured(synchronized(captured) { captured.readByteArray() }, isComplete) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments