scoop:scoop.http.asyncio:0.1.0
src/multipart/source.scoop
Selected production source · src/multipart/source.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.platform.memory.NativeByteBuffer7
/**8
* A sequential multipart reader over a borrowed source by default. Finish or explicitly discard9
* the current part before requesting another.10
*/11
public class MultipartSource private constructor(12
private val reader: MultipartReader13
): AsyncCloseable {14
public constructor(15
source: AsyncByteSource,16
boundary: MultipartBoundary,17
limits: MultipartLimits = multipartLimits(),18
bufferSize: Long = 8192L19
)20
: this(createMultipartReader(source, None, boundary, limits, bufferSize)) {}22
/**23
* Factories that transfer underlying endpoint cleanup responsibility.24
*/25
public companion object {26
/**27
* Creates the adapter and assumes cleanup responsibility for the supplied asynchronous28
* endpoint.29
*/30
public fun <T> takeOwnership(31
source: T,32
boundary: MultipartBoundary,33
limits: MultipartLimits = multipartLimits(),34
bufferSize: Long = 8192L35
): MultipartSource36
where T: AsyncByteSource, T: AsyncCloseable =37
MultipartSource(38
createMultipartReader(39
source,40
Some<AsyncCloseable>(source),41
boundary,42
limits,43
bufferSize44
)45
)46
}48
/**49
* Whether adapter cleanup has completed.50
*/51
public val isClosed: Boolean get() = reader.io.isClosed52
/**53
* Whether the complete protocol boundary or input end has been reached.54
*/55
public val isFinished: Boolean get() = reader.finished56
/**57
* The retained terminal exception, or None before failure.58
*/59
public val failure: Throwable? get() = reader.io.failure60
/**61
* Receives the next part head and body, or None after the final multipart boundary.62
*/63
public suspend fun nextPart(): MultipartPart? = reader.nextPart()64
/**65
* Closes parser/writer state and an endpoint whose ownership was transferred; borrowed66
* endpoints retain their own cleanup responsibility.67
*/68
public override suspend fun close() {69
reader.io.close()70
}71
}73
/**74
* A received multipart header paired with its single-pass body.75
*/76
public class MultipartPart internal constructor(77
/**78
* The parsed part header.79
*/80
public val head: MultipartPartHead,81
/**82
* The body that must be consumed or explicitly discarded before advancing.83
*/84
public val body: MultipartBody85
)87
/**88
* Discarding a multipart body exceeded the caller's explicit byte limit.89
*/90
public class MultipartDiscardLimitException public constructor(91
/**92
* The maximum permitted discarded byte count.93
*/94
public val maxBytes: Long95
)96
: Exception(Some("multipart discard exceeds " + maxBytes.toString() + " bytes"))98
private fun createMultipartReader(99
source: AsyncByteSource,100
owner: AsyncCloseable?,101
boundary: MultipartBoundary,102
limits: MultipartLimits,103
bufferSize: Long104
): MultipartReader {105
val loop = currentEventLoop()106
val job = httpJob(loop, true)107
if (bufferSize <= 0L)108
throw IllegalArgumentException(Some("multipart buffer size must be positive"))109
val decoder = MultipartDecoder(boundary, limits)110
val input =111
try {112
NativeByteBuffer.allocate(bufferSize)113
}114
catch (error: Throwable) {115
throw messageCleanup(Some(error)) { decoder.close() }!!116
}117
try {118
job.ensureActive()119
return MultipartReader(source, owner, loop, MultipartReadState(decoder, input))120
} catch (error: Throwable) {121
var failure = messageCleanup(Some(error)) { input.close() }122
failure = messageCleanup(failure) { decoder.close() }123
throw failure!!124
}125
}