Scoop API

scoop:scoop.http.server:0.1.0

src/multipart/part.scoop

Selected production source · src/multipart/part.scoop

1package scoop.http.server
3import scoop.asyncio.*
4import scoop.bytes.ByteArrayBuilder
5import scoop.charset.Charset
6import scoop.http.*
7import scoop.http.asyncio.MultipartPart
9/** One ordered form-data part with strict text metadata and a single body consumption path. */
10public class RequestPart internal constructor(part: MultipartPart, owner: RequestMultipart,
11 private val options: RequestMultipartOptions) : AsyncCloseable {
12 private val disposition: HttpContentDisposition = requestPartDisposition(part.head)
13 private val input: RequestPartInput = RequestPartInput(part.body, owner, options.maxPartBytes)
14 private val selected: AtomicBoolean = AtomicBoolean(false)
16 /** The original ordered part fields; Content-Length does not change boundary framing. */
17 public val head: MultipartPartHead = part.head
18 /** The raw, unescaped name parameter, including an explicitly empty name. */
19 public val nameBytes: Array<Byte> = disposition.parameter("name")!!.value
20 /** The raw filename parameter; missing and empty filenames remain distinct. */
21 public val filenameBytes: Array<Byte>? = when (disposition.parameter("filename")) {
22 case None -> None
23 case Some(val value) -> Some(value.value)
24 }
25 /** The name interpreted strictly as UTF-8. */
26 public val name: String get() = inputText(nameBytes, "multipart name")
27 /** The optional filename interpreted strictly as UTF-8, without path processing. */
28 public val filename: String? get() = when (filenameBytes) {
29 case None -> None
30 case Some(val value) -> Some(inputText(value, "multipart filename"))
31 }
32 /** Whether this part reached its boundary through a positive-sized read or discard. */
33 public val isComplete: Boolean get() = input.isComplete
35 private fun select() {
36 if (!selected.compareAndSet(false, true)) throw IllegalStateException(Some("multipart part already has a consumer"))
37 }
39 /** Claims a bounded stream that becomes invalid when iteration advances. */
40 public fun openStream(): AsyncByteSource { select(); return input }
42 /** Collects text within the configured field and part limits. */
43 public suspend fun text(charset: Charset? = None): String = text(options.maxFieldBytes, charset)
45 /** Collects text within the smaller of the explicit, configured field and part limits. */
46 public suspend fun text(maxBytes: Long, charset: Charset? = None): String {
47 if (maxBytes < 0L) throw IllegalArgumentException(Some("negative multipart text limit"))
48 val encoding = requestPartCharset(head, charset)
49 select()
50 val result = ByteArrayBuilder()
51 val limit = if (maxBytes < options.maxFieldBytes) maxBytes else options.maxFieldBytes
52 input.consume(limit, suspend { buffer, count -> result.append(buffer.readBytes(count)); Unit })
53 return requestPartText(result.toByteArray(), encoding)
54 }
56 /** Copies bounded content with backpressure, without flushing or closing the borrowed sink. */
57 public suspend fun copyTo(sink: AsyncByteSink, maxBytes: Long): Long {
58 if (maxBytes < 0L) throw IllegalArgumentException(Some("negative multipart copy limit"))
59 select()
60 return input.consume(maxBytes, suspend { buffer, count -> sink.writeFully(buffer, count) })
61 }
63 /** Explicitly discards remaining body bytes within both limits, even after a partial read. */
64 public suspend fun discard(maxBytes: Long): Long {
65 if (maxBytes < 0L) throw IllegalArgumentException(Some("negative multipart discard limit"))
66 selected.store(true)
67 return input.discard(maxBytes)
68 }
70 /** Closes this view; abandoning an incomplete part terminates the multipart reader. */
71 public override suspend fun close() { input.close() }
73 internal fun retire() { input.retire() }