-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
DownloadService.kt
144 lines (123 loc) · 5.17 KB
/
DownloadService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* paperweight is a Gradle plugin for the PaperMC project.
*
* Copyright (c) 2021 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package io.papermc.paperweight
import io.papermc.paperweight.util.convertToPath
import io.papermc.paperweight.util.convertToUrl
import java.net.URL
import java.nio.file.Path
import java.nio.file.attribute.FileTime
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.concurrent.TimeUnit
import kotlin.io.path.*
import org.apache.http.HttpHost
import org.apache.http.HttpStatus
import org.apache.http.client.config.CookieSpecs
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.utils.DateUtils
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.impl.client.HttpClientBuilder
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
abstract class DownloadService : BuildService<BuildServiceParameters.None>, AutoCloseable {
private val httpClient: CloseableHttpClient = HttpClientBuilder.create().let { builder ->
builder.setRetryHandler { _, count, _ -> count < 3 }
builder.useSystemProperties()
builder.build()
}
fun download(source: Any, target: Any) {
val url = source.convertToUrl()
val file = target.convertToPath()
download(url, file)
}
private fun download(source: URL, target: Path) {
target.parent.createDirectories()
val etagDir = target.resolveSibling("etags")
etagDir.createDirectories()
val etagFile = etagDir.resolve(target.name + ".etag")
val etag = if (etagFile.exists()) etagFile.readText() else null
val host = HttpHost(source.host, source.port, source.protocol)
val time = if (target.exists()) target.getLastModifiedTime().toInstant() else Instant.EPOCH
val httpGet = HttpGet(source.file)
// high timeout, reduce chances of weird things going wrong
val timeouts = TimeUnit.MINUTES.toMillis(5).toInt()
httpGet.config = RequestConfig.custom()
.setConnectTimeout(timeouts)
.setConnectionRequestTimeout(timeouts)
.setSocketTimeout(timeouts)
.setCookieSpec(CookieSpecs.STANDARD)
.build()
if (target.exists()) {
if (time != Instant.EPOCH) {
val value = DateTimeFormatter.RFC_1123_DATE_TIME.format(time.atZone(ZoneOffset.UTC))
httpGet.setHeader("If-Modified-Since", value)
}
if (etag != null) {
httpGet.setHeader("If-None-Match", etag)
}
}
httpClient.execute(host, httpGet).use { response ->
val code = response.statusLine.statusCode
if (code !in 200..299 && code != HttpStatus.SC_NOT_MODIFIED) {
val reason = response.statusLine.reasonPhrase
throw PaperweightException("Download failed, HTTP code: $code; URL: $source; Reason: $reason")
}
val lastModified = handleResponse(response, time, target)
saveEtag(response, lastModified, target, etagFile)
}
}
private fun handleResponse(response: CloseableHttpResponse, time: Instant, target: Path): Instant {
val lastModified = with(response.getLastHeader("Last-Modified")) {
if (this == null) {
return@with Instant.EPOCH
}
if (value.isNullOrBlank()) {
return@with Instant.EPOCH
}
return@with DateUtils.parseDate(value).toInstant() ?: Instant.EPOCH
}
if (response.statusLine.statusCode == HttpStatus.SC_NOT_MODIFIED) {
return lastModified
}
val entity = response.entity ?: return lastModified
target.outputStream().use { output ->
entity.content.use { input ->
input.copyTo(output)
}
}
return lastModified
}
private fun saveEtag(response: CloseableHttpResponse, lastModified: Instant, target: Path, etagFile: Path) {
if (lastModified != Instant.EPOCH) {
target.setLastModifiedTime(FileTime.from(lastModified))
}
val header = response.getFirstHeader("ETag") ?: return
val etag = header.value
etagFile.writeText(etag)
}
override fun close() {
httpClient.close()
}
}