scoop:scoop.http.client:0.1.0
src/resume/request.scoop
Selected production source · src/resume/request.scoop
1
package scoop.http.client3
import scoop.http.*4
import scoop.platform.fs.Path6
internal class ClientResumeRange(7
val record: ClientResumeRecord,8
val offset: Long,9
val resumed: Long10
)12
internal fun validateClientResumeRequest(13
request: HttpRequest,14
destination: Path,15
options: HttpResumeOptions16
) {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.duplex28
)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
)60
}62
internal 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.options77
val decoding = options.decoding ?: config.decoding78
val prepared =79
httpCallOptions {80
this.continueWait = options.continueWait81
this.decoding =82
Some(83
httpDecodingOptions {84
this.mode = HttpContentDecoding.Raw85
this.bufferSize = decoding.bufferSize86
this.maxWindowBytes = decoding.maxWindowBytes87
}88
)89
this.timeouts = options.timeouts90
this.onInformational = options.onInformational91
this.redirects = options.redirects92
this.retries = options.retries93
this.authentication = options.authentication94
this.proxy = options.proxy95
this.useCookies = options.useCookies96
this.cookieContext = options.cookieContext97
this.cacheMode = HttpCacheMode.Bypass98
this.observer = options.observer99
}100
return httpRequest(original.url) {101
this.method = "GET"102
this.fields = fields103
this.body = original.body104
this.options = prepared105
}106
}108
internal suspend fun executeClientResumeRequest(109
download: ClientDownload,110
request: HttpRequest111
): 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 = None118
}119
}120
val response = executeClientSequence(download.client, request, download.sequence)121
download.client.lock.withLock { download.responseCall = Some(response.call) }122
return response123
}