Scoop API

scoop:scoop.http.server:0.1.0

src/files/selection.scoop

Selected production source · src/files/selection.scoop

1package scoop.http.server
3import scoop.asyncio.AsyncFile
4import scoop.http.*
5import scoop.time.Instant
7internal fun prepareFileResponse(
8 ctx: HttpContext,
9 source: AsyncFile,
10 options: FileResponseOptions,
11 size: Long,
12 reference: Instant,
13 initial: HttpResponseDraft,
14 minimal: Boolean
15): ResponsePlan {
16 val draft = HttpResponseDraft(initial.status, initial.fields)
17 val method = ctx.request.method
18 val representation =
19 httpResponseAllowsContent(
20 HttpResponseHead(ctx.request.head.version, draft.status),
21 if (method == "HEAD")
22 "GET".toByteArray()
23 else
24 ctx.request.head.method
25 )
26 if (
27 !minimal &&
28 representation &&
29 !acceptsResponseRepresentation(requestAcceptEncoding(ctx), draft.fields)
30 )
31 return prepareResponse(
32 ctx,
33 empty(406U),
34 HttpResponseDraft(406U, unacceptableFields(draft.fields)),
35 minimal,
36 false
37 )
38 val declared = parseHttpContentLength(draft.fields)
39 if (representation && present(declared) && declared!! != size)
40 throw IllegalArgumentException(Some("Content-Length does not match file size"))
41 var offset = 0L
42 var length = size
43 if (draft.status == 200U) {
44 val(tag, modified) = fileResponseValidators(draft.fields, reference)
45 val condition =
46 try {
47 evaluateHttpPreconditions(
48 method,
49 ctx.request.head.fields,
50 true,
51 tag,
52 modified,
53 reference
54 )
55 }
56 catch (error: HttpFieldParseException) {
57 throw HttpRejection(
58 text("invalid request precondition", 400U),
59 Some<Throwable>(error)
60 )
61 }
62 if (condition != HttpPreconditionResult.Proceed) {
63 draft.status =
64 if (condition == HttpPreconditionResult.NotModified)
65 304U
66 else
67 412U
68 removeFileContentFields(draft)
69 return prepareResponse(ctx, empty(draft.status), draft, minimal, false)
70 }
71 if (method == "GET" && size > 0L) {
72 val range =
73 fileRequestRange(
74 ctx.request.head.fields,
75 tag,
76 modified,
77 options.lastModifiedIsStrong,
78 reference
79 )
80 if (present(range)) {
81 val resolved = range!!.resolve(size)
82 if (!resolved.isSatisfiable) {
83 draft.status = 416U
84 removeFileContentFields(draft)
85 draft.set(HttpContentRange.unsatisfied(size).toHttpField())
86 return prepareResponse(ctx, empty(416U), draft, minimal, false)
87 }
88 val selected = resolved.ranges[0L]
89 offset = selected.first;
90 length = selected.length
91 draft.status = 206U
92 draft.set(HttpContentRange(selected, Some(size)).toHttpField())
93 draft.set(field("Content-Length", length.toString()))
94 }
95 }
96 }
97 val start = offset;
98 val count = length
99 val body =
100 HttpBody.Stream(
101 HttpStreamBody(
102 suspend {
103 writer -> copyResponseFile(source, start, count, options.bufferSize, writer)
104 },
105 Some(length)
106 )
107 )
108 return prepareResponse(
109 ctx,
110 httpResult {
111 this.status = draft.status
112 this.fields = draft.fields
113 this.body = body
114 this.allowCompression = false
115 },
116 draft,
117 minimal,
118 false
119 )