Scoop API

scoop:scoop.http.asyncio:0.1.0

src/multipart/source.scoop

Selected production source · src/multipart/source.scoop

1package scoop.http.asyncio
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.platform.memory.NativeByteBuffer
7/**
8 * A sequential multipart reader over a borrowed source by default. Finish or explicitly discard
9 * the current part before requesting another.
10 */
11public class MultipartSource private constructor(
12 private val reader: MultipartReader
13): AsyncCloseable {
14 public constructor(
15 source: AsyncByteSource,
16 boundary: MultipartBoundary,
17 limits: MultipartLimits = multipartLimits(),
18 bufferSize: Long = 8192L
19 )
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 asynchronous
28 * endpoint.
29 */
30 public fun <T> takeOwnership(
31 source: T,
32 boundary: MultipartBoundary,
33 limits: MultipartLimits = multipartLimits(),
34 bufferSize: Long = 8192L
35 ): MultipartSource
36 where T: AsyncByteSource, T: AsyncCloseable =
37 MultipartSource(
38 createMultipartReader(
39 source,
40 Some<AsyncCloseable>(source),
41 boundary,
42 limits,
43 bufferSize
44 )
45 )
46 }
48 /**
49 * Whether adapter cleanup has completed.
50 */
51 public val isClosed: Boolean get() = reader.io.isClosed
52 /**
53 * Whether the complete protocol boundary or input end has been reached.
54 */
55 public val isFinished: Boolean get() = reader.finished
56 /**
57 * The retained terminal exception, or None before failure.
58 */
59 public val failure: Throwable? get() = reader.io.failure
60 /**
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; borrowed
66 * endpoints retain their own cleanup responsibility.
67 */
68 public override suspend fun close() {
69 reader.io.close()
70 }
73/**
74 * A received multipart header paired with its single-pass body.
75 */
76public 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: MultipartBody
87/**
88 * Discarding a multipart body exceeded the caller's explicit byte limit.
89 */
90public class MultipartDiscardLimitException public constructor(
91 /**
92 * The maximum permitted discarded byte count.
93 */
94 public val maxBytes: Long
96: Exception(Some("multipart discard exceeds " + maxBytes.toString() + " bytes"))
98private fun createMultipartReader(
99 source: AsyncByteSource,
100 owner: AsyncCloseable?,
101 boundary: MultipartBoundary,
102 limits: MultipartLimits,
103 bufferSize: Long
104): 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 }