Scoop API

scoop:scoop.http.client:0.1.0

src/resume/request.scoop

Selected production source · src/resume/request.scoop

1package scoop.http.client
3import scoop.http.*
4import scoop.platform.fs.Path
6internal class ClientResumeRange(
7 val record: ClientResumeRecord,
8 val offset: Long,
9 val resumed: Long
12internal fun validateClientResumeRequest(
13 request: HttpRequest,
14 destination: Path,
15 options: HttpResumeOptions
16) {
17 if (
18 request.method !=
19 "GET" ||
20 request.contentLength !=
21 Some(0L) ||
22 request.body.contentLength !=
23 Some(0L) ||
24 !request.body.isReplayable ||
25 request.body.trailerNames.size !=
26 0L ||
27 request.options.duplex
28 )
29 throw IllegalArgumentException(Some("resume requires a replayable empty GET request"))
30 for (
31 name in [
32 "Range",
33 "If-Match",
34 "If-None-Match",
35 "If-Modified-Since",
36 "If-Unmodified-Since",
37 "If-Range"
38 ]
39 )
40 if (clientCacheHas(request.fields, name))
41 throw IllegalArgumentException(Some("resume generates conditional range fields"))
42 if (
43 request.options.cacheMode ==
44 HttpCacheMode.OnlyIfCached ||
45 HttpCacheControl.parseFields(request.fields).contains("only-if-cached")
46 )
47 throw IllegalArgumentException(Some("resume requires network validation"))
48 val target = destination.normalizeLexically()
49 if (
50 target ==
51 options.partialFile.normalizeLexically() ||
52 target ==
53 options.metadataFile.normalizeLexically() ||
54 !clientPresent(options.partialFile.fileName) ||
55 !clientPresent(options.metadataFile.fileName)
56 )
57 throw IllegalArgumentException(
58 Some("resume needs distinct data, metadata and destination paths")
59 )
62internal fun clientResumeRequest(
63 config: HttpClientConfig,
64 original: HttpRequest,
65 range: ClientResumeRange?
66): HttpRequest {
67 val fields = ArrayList<HttpField>()
68 for (field in original.suppliedFields)
69 fields.add(field)
70 if (!clientCacheHas(original.fields, "Accept-Encoding"))
71 fields.add(HttpField("Accept-Encoding".toByteArray(), "identity".toByteArray()))
72 if (clientPresent(range)) {
73 fields.add(HttpRange([HttpByteRange.From(range!!.offset)]).toHttpField())
74 fields.add(HttpField("If-Range".toByteArray(), range!!.record.etag!!.toByteArray()))
75 }
76 val options = original.options
77 val decoding = options.decoding ?: config.decoding
78 val prepared =
79 httpCallOptions {
80 this.continueWait = options.continueWait
81 this.decoding =
82 Some(
83 httpDecodingOptions {
84 this.mode = HttpContentDecoding.Raw
85 this.bufferSize = decoding.bufferSize
86 this.maxWindowBytes = decoding.maxWindowBytes
87 }
88 )
89 this.timeouts = options.timeouts
90 this.onInformational = options.onInformational
91 this.redirects = options.redirects
92 this.retries = options.retries
93 this.authentication = options.authentication
94 this.proxy = options.proxy
95 this.useCookies = options.useCookies
96 this.cookieContext = options.cookieContext
97 this.cacheMode = HttpCacheMode.Bypass
98 this.observer = options.observer
99 }
100 return httpRequest(original.url) {
101 this.method = "GET"
102 this.fields = fields
103 this.body = original.body
104 this.options = prepared
105 }
108internal suspend fun executeClientResumeRequest(
109 download: ClientDownload,
110 request: HttpRequest
111): HttpResponse {
112 val previous = download.client.lock.withLock { download.responseCall }
113 if (clientPresent(previous)) {
114 val next = continueClientSequence(download.client, request, download.sequence, previous!!)
115 download.client.lock.withLock {
116 download.sequence = next;
117 download.responseCall = None
118 }
119 }
120 val response = executeClientSequence(download.client, request, download.sequence)
121 download.client.lock.withLock { download.responseCall = Some(response.call) }
122 return response