scoop:scoop.http:0.1.0
src/multipart/model.scoop
Selected production source · src/multipart/model.scoop
1
package scoop.http3
/**4
* Local limits for part headers, field counts, part counts, boundary padding, and emitted5
* data-chunk size. Counts may be zero; chunk size must be positive.6
*/7
public class MultipartLimits internal constructor(8
/**9
* Maximum bytes in a part's header block.10
*/11
public val maxHeaderBytes: Long = 65536L,12
/**13
* Maximum fields in one part.14
*/15
public val maxFields: Long = 100L,16
/**17
* Maximum parts in the message.18
*/19
public val maxParts: Long = 1024L,20
/**21
* Maximum permitted padding bytes on a boundary line.22
*/23
public val maxBoundaryPaddingBytes: Long = 1024L,24
/**25
* Maximum size of an emitted body-data chunk; must be positive.26
*/27
public val dataChunkBytes: Long = 8192L28
) {29
init {30
if (31
maxHeaderBytes <32
0L ||33
maxFields <34
0L ||35
maxParts <36
0L ||37
maxBoundaryPaddingBytes <38
0L ||39
dataChunkBytes <=40
0L41
)42
throw IllegalArgumentException(Some("invalid multipart limits"))43
}44
}46
/**47
* The multipart parsing limit that was exceeded.48
*/49
public enum MultipartLimitKind {50
/**51
* A part header block exceeded its byte limit.52
*/53
HeaderBytes,54
/**55
* A part contained too many header fields.56
*/57
Fields,58
/**59
* The message contained too many parts.60
*/61
Parts,62
/**63
* Padding on a boundary line exceeded its byte limit.64
*/65
BoundaryPaddingBytes66
}68
/**69
* Multipart input exceeded a configured local limit.70
*/71
public class MultipartLimitException public constructor(72
/**73
* The configured limit category that was exceeded.74
*/75
public val kind: MultipartLimitKind,76
/**77
* The configured maximum for that category.78
*/79
public val limit: Long80
)81
: Exception(Some("multipart input exceeds its configured limit of " + limit.toString()))83
/**84
* Malformed multipart syntax at a zero-based stream byte offset.85
*/86
public class MultipartParseException public constructor(87
/**88
* The zero-based input byte position, when available.89
*/90
public val byteOffset: Long,91
/**92
* The protocol failure explanation.93
*/94
public val detail: String95
)96
: Exception(Some("multipart at byte " + byteOffset.toString() + ": " + detail))98
/**99
* Ordered multipart part, data, and completion events.100
*/101
public enum MultipartEvent {102
/**103
* A complete part header begins the next part.104
*/105
PartStarted(MultipartPartHead),106
/**107
* Owned body bytes from the current part, excluding boundary syntax.108
*/109
Data(Array<Byte>),110
/**111
* The current part body has ended.112
*/113
PartEnded,114
/**115
* The multipart message reached its validated end.116
*/117
End118
}120
internal class MultipartGuard {121
private val busy: AtomicBoolean = AtomicBoolean(false)122
fun <T> withUse(block: () -> T): T {123
if (!busy.compareAndSet(false, true))124
throw IllegalStateException(Some("multipart codec is already in use"))125
try {126
return block()127
} finally {128
busy.store(false)129
}130
}131
}133
internal fun multipartAdd(left: Long, right: Long): Long {134
if (right > Long.MAX_VALUE - left)135
throw ArithmeticException()136
return left + right137
}