Scoop API

scoop:scoop.http.client:0.1.0

src/body/part.scoop

Selected production source · src/body/part.scoop

1package scoop.http.client
3import scoop.collections.emptyList
4import scoop.http.*
6/**
7 * A multipart part combining a body plan with derived validated fields. HTTP trailers are not
8 * permitted inside individual parts.
9 */
10public class RequestBodyPart internal constructor(
11 /**
12 * The part's body plan.
13 */
14 public val body: RequestBody,
15 fields: List<HttpField> = emptyList<HttpField>()
16) {
17 /**
18 * Validated part fields and the body's declared length.
19 */
20 public val head: MultipartPartHead =
21 MultipartPartHead(requestBodyFields(body, fields), body.contentLength)
22 init {
23 if (body.trailerNames.size != 0L)
24 throw IllegalArgumentException(Some("multipart parts cannot carry HTTP trailers"))
25 }
28internal fun requestBodyFields(body: RequestBody, fields: List<HttpField>): Array<HttpField> {
29 val result = ArrayList<HttpField>()
30 for (field in fields)
31 result.add(field)
32 if (result.findAll("content-type".toByteArray()).size == 0L) {
33 when (body.contentType) {
34 case Some(val type) -> result.add(type.toHttpField());
35 case None -> {}
36 }
37 }
38 if (body.contentEncodings.size != 0L) {
39 if (result.findAll("content-encoding".toByteArray()).size != 0L)
40 throw IllegalArgumentException(
41 Some("request body already supplies its Content-Encoding")
42 )
43 val coding = StringBuilder()
44 var first = true
45 for (encoding in body.contentEncodings) {
46 if (!first)
47 coding.append(", ")
48 first = false
49 coding.append(encoding)
50 }
51 result.add(HttpField("Content-Encoding".toByteArray(), coding.toString().toByteArray()))
52 }
53 if (body.trailerNames.size != 0L) {
54 if (result.findAll("trailer".toByteArray()).size != 0L)
55 throw IllegalArgumentException(
56 Some("request body already supplies its Trailer declaration")
57 )
58 result.add(body.trailerDeclaration.toHttpField())
59 }
60 return result.toArray()