scoop:scoop.http.client:0.1.0
src/resume/transfer.scoop
Selected production source · src/resume/transfer.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.collections.emptyList5
import scoop.http.*6
import scoop.platform.fs.*7
import scoop.platform.memory.NativeByteBuffer9
internal enum ClientResumeStep {10
Complete(HttpDownloadResult), Continue(ClientResumeRange), Restart11
}13
internal suspend fun prepareClientResume(14
download: ClientDownload,15
original: HttpRequest,16
destination: Path,17
options: HttpDownloadOptions,18
resume: HttpResumeOptions19
): HttpDownloadResult {20
val file =21
download.client.loop.openFile(resume.partialFile, OpenOptions(write = true, create = true))22
val output = ClientDownloadFile(resume.partialFile, file, false)23
download.output = Some(output)24
download.resumeMetadata = Some(resume.metadataFile)25
return output.useAsync suspend {26
owned ->27
var range = selectClientResumeRange(original, owned.file.size(), resume)28
while (true) {29
download.requireActive()30
val selected = range31
val response =32
executeClientResumeRequest(33
download,34
clientResumeRequest(download.client.config, original, selected)35
)36
val step =37
response.useAsync suspend {38
value -> consumeClientResume(39
download,40
original,41
destination,42
owned.file,43
value,44
selected,45
options,46
resume47
)48
}49
when (step) {50
case Complete(val result) -> {51
download.requireActive()52
if (options.syncOnCommit)53
owned.file.syncAll()54
download.requireActive()55
return@useAsync result56
}57
case Continue(val next) -> range = Some(next)58
case Restart -> range = None59
}60
}61
}62
}64
private suspend fun consumeClientResume(65
download: ClientDownload,66
original: HttpRequest,67
destination: Path,68
file: AsyncFile,69
response: HttpResponse,70
range: ClientResumeRange?,71
options: HttpDownloadOptions,72
resume: HttpResumeOptions73
): ClientResumeStep {74
if (response.status == 200U) {75
if (clientPresent(range) && resume.mismatch == HttpResumeMismatch.Fail)76
throw HttpResumeException(77
HttpResumeFailureReason.RepresentationChanged,78
"server returned a full response to a range request",79
Some(response.head)80
)81
val record = clientResumeRecord(original, response)82
val bytes = encodeClientResumeRecord(record, resume.maxMetadataBytes)83
file.truncate(0L);84
file.seek(0L)85
saveClientResumeRecord(bytes, resume, options.syncOnCommit)86
val count =87
copyClientResumeSegment(download, response, file, options, 0L, record.length, 0L, None)88
if (!clientPresent(record.length))89
saveClientResumeRecord(90
encodeClientResumeRecord(record.withLength(count), resume.maxMetadataBytes),91
resume,92
options.syncOnCommit93
)94
return ClientResumeStep.Complete(95
HttpDownloadResult(destination, response.url, response.head, response.trailers!!, count)96
)97
}98
if (response.status == 206U) {99
val selected =100
range ?:101
throw HttpResumeException(102
HttpResumeFailureReason.UnexpectedStatus,103
"server returned a partial representation without a range request",104
Some(response.head)105
)106
val part =107
try {108
clientResumePartialRange(selected, response)109
}110
catch (error: HttpResumeException) {111
if (resume.mismatch == HttpResumeMismatch.Restart)112
return ClientResumeStep.Restart113
throw error114
}115
val total = part.completeLength!!116
val record = selected.record.withLength(total)117
val bytes = encodeClientResumeRecord(record, resume.maxMetadataBytes)118
file.seek(selected.offset)119
saveClientResumeRecord(bytes, resume, options.syncOnCommit)120
val count =121
copyClientResumeSegment(122
download,123
response,124
file,125
options,126
selected.offset,127
Some(total),128
selected.resumed,129
Some(part.range!!.length)130
)131
if (count == total)132
return ClientResumeStep.Complete(133
HttpDownloadResult(134
destination,135
response.url,136
response.head,137
response.trailers!!,138
count,139
selected.resumed140
)141
)142
return ClientResumeStep.Continue(ClientResumeRange(record, count, selected.resumed))143
}144
if (response.status == 416U && clientPresent(range)) {145
if (!clientResumeAlreadyComplete(range!!, response)) {146
if (resume.mismatch == HttpResumeMismatch.Restart)147
return ClientResumeStep.Restart148
throw HttpResumeException(149
HttpResumeFailureReason.InvalidRange,150
"range failure does not prove a complete file",151
Some(response.head)152
)153
}154
NativeByteBuffer.allocate(options.bufferSize).use suspend {155
buffer -> while (true) {156
buffer.clear();157
if (response.body.readInto(buffer, buffer.capacity) == 0L)158
break159
}160
}161
reportClientDownloadProgress(162
download,163
response,164
options,165
range!!.offset,166
Some(range!!.offset),167
range!!.resumed168
)169
return ClientResumeStep.Complete(170
HttpDownloadResult(171
destination,172
response.url,173
response.head,174
response.trailers ?: emptyList<HttpField>().toArray(),175
range!!.offset,176
range!!.resumed177
)178
)179
}180
response.requireSuccess()181
throw HttpResumeException(182
HttpResumeFailureReason.UnexpectedStatus,183
"resume needs a complete or validated partial representation",184
Some(response.head)185
)186
}188
private suspend fun copyClientResumeSegment(189
download: ClientDownload,190
response: HttpResponse,191
file: AsyncFile,192
options: HttpDownloadOptions,193
offset: Long,194
total: Long?,195
resumed: Long,196
expected: Long?197
): Long {198
val integrity = clientResumeIntegrity(response.call)199
try {200
return copyClientDownload(201
download,202
response,203
file,204
options,205
offset,206
total,207
resumed,208
expected209
)210
}211
catch (error: Throwable) {212
if (integrity && !response.body.isComplete) {213
try {214
nonCancellableScope<Unit> {215
file.truncate(offset);216
file.seek(offset)217
}218
}219
catch (cleanup: Throwable) {220
throw ResourceUseException(error, cleanup)221
}222
}223
throw error224
}225
}227
private fun clientResumeIntegrity(call: ClientCall): Boolean {228
val origin = call.authentication.digestUse229
val proxy = clientForwardProxyDigest(call)230
return (clientPresent(origin) && origin!!.nonce.challenge.qop == Some("auth-int")) ||231
(clientPresent(proxy) && proxy!!.nonce.challenge.qop == Some("auth-int"))232
}