-
Notifications
You must be signed in to change notification settings - Fork 786
/
StaticFile.scala
351 lines (313 loc) · 10.8 KB
/
StaticFile.scala
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright 2013 http4s.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.http4s
import cats.ApplicativeThrow
import cats.Functor
import cats.MonadError
import cats.MonadThrow
import cats.Semigroup
import cats.data.NonEmptyList
import cats.data.OptionT
import cats.effect.Sync
import cats.effect.SyncIO
import cats.syntax.all._
import fs2.Stream
import fs2.io._
import fs2.io.file.Files
import fs2.io.file.Path
import org.http4s.Status.NotModified
import org.http4s.headers._
import org.http4s.syntax.header._
import org.typelevel.vault._
import java.io._
import java.net.URL
object StaticFile {
private[this] val logger = Platform.loggerFactory.getLogger
val DefaultBufferSize = 10240
def fromString[F[_]: Files: MonadThrow](
url: String,
req: Option[Request[F]] = None,
): OptionT[F, Response[F]] =
fromPath(Path(url), req)
def fromResource[F[_]: Sync](
name: String,
req: Option[Request[F]] = None,
preferGzipped: Boolean = false,
classloader: Option[ClassLoader] = None,
): OptionT[F, Response[F]] = {
val loader = classloader.getOrElse(getClass.getClassLoader)
val acceptEncodingHeader: Option[`Accept-Encoding`] =
req.flatMap(_.headers.get[`Accept-Encoding`])
val tryGzipped =
preferGzipped && acceptEncodingHeader.exists { acceptEncoding =>
acceptEncoding.satisfiedBy(ContentCoding.gzip) || acceptEncoding.satisfiedBy(
ContentCoding.`x-gzip`
)
}
val normalizedName = name.split("/").filter(_.nonEmpty).mkString("/")
def getResource(name: String) =
OptionT(Sync[F].blocking(Option(loader.getResource(name))))
val gzUrl: OptionT[F, URL] =
if (tryGzipped) getResource(normalizedName + ".gz") else OptionT.none
gzUrl
.flatMap { url =>
fromURL(url, req).map {
_.removeHeader[`Content-Type`]
.putHeaders(
`Content-Encoding`(ContentCoding.gzip),
nameToContentType(normalizedName), // Guess content type from the name without ".gz"
)
}
}
.orElse(
getResource(normalizedName)
.flatMap(fromURL(_, req))
)
}
def fromURL[F[_]](url: URL, req: Option[Request[F]] = None)(implicit
F: Sync[F]
): OptionT[F, Response[F]] = {
val fileUrl = url.getFile()
val file = new File(fileUrl)
OptionT.apply(F.defer {
if (url.getProtocol === "file" && file.isDirectory())
F.pure(none[Response[F]])
else {
val urlConn = url.openConnection
val lastmod = HttpDate.fromEpochSecond(urlConn.getLastModified / 1000).toOption
val ifModifiedSince: Option[`If-Modified-Since`] =
req.flatMap(_.headers.get[`If-Modified-Since`])
val expired = (ifModifiedSince, lastmod).mapN(_.date < _).getOrElse(true)
if (expired) {
val len = urlConn.getContentLengthLong
val headers = Headers(
lastmod.map(`Last-Modified`(_)),
nameToContentType(url.getPath),
if (len >= 0) `Content-Length`.unsafeFromLong(len)
else `Transfer-Encoding`(TransferCoding.chunked.pure[NonEmptyList]),
)
F.blocking(urlConn.getInputStream)
.redeem(
recover = {
case _: FileNotFoundException => None
case other => throw other
},
f = { inputStream =>
Some(
Response(
headers = headers,
body = readInputStream[F](F.pure(inputStream), DefaultBufferSize),
)
)
},
)
} else
F.blocking(urlConn.getInputStream.close())
.handleError(_ => ())
.as(Some(Response(NotModified)))
}
})
}
@deprecated("Use calculateETag", "0.23.5")
def calcETag[F[_]: Files: Functor]: File => F[String] =
f =>
Files[F]
.isRegularFile(Path.fromNioPath(f.toPath()))
.map(isFile =>
if (isFile) s"${f.lastModified().toHexString}-${f.length().toHexString}" else ""
)
def calculateETag[F[_]: Files: ApplicativeThrow]: Path => F[String] =
f =>
Files[F]
.getBasicFileAttributes(f, followLinks = true)
.map(attr =>
if (attr.isRegularFile)
s"${attr.lastModifiedTime.toMillis.toHexString}-${attr.size.toHexString}"
else ""
)
@deprecated("Use fromPath", "0.23.5")
def fromFile[F[_]: Files: MonadThrow](
f: File,
req: Option[Request[F]] = None,
): OptionT[F, Response[F]] =
fromPath(Path.fromNioPath(f.toPath()), DefaultBufferSize, req, calculateETag[F])
def fromPath[F[_]: Files: MonadThrow](
f: Path,
req: Option[Request[F]] = None,
): OptionT[F, Response[F]] =
fromPath(f, DefaultBufferSize, req, calculateETag[F])
@deprecated("Use fromPath", "0.23.5")
def fromFile[F[_]: Files: MonadThrow](
f: File,
req: Option[Request[F]],
etagCalculator: File => F[String],
): OptionT[F, Response[F]] =
fromPath(
Path.fromNioPath(f.toPath()),
DefaultBufferSize,
req,
etagCalculator.compose(_.toNioPath.toFile()),
)
def fromPath[F[_]: Files: MonadThrow](
f: Path,
req: Option[Request[F]],
etagCalculator: Path => F[String],
): OptionT[F, Response[F]] =
fromPath(f, DefaultBufferSize, req, etagCalculator)
@deprecated("Use fromPath", "0.23.5")
def fromFile[F[_]: Files: MonadThrow](
f: File,
buffsize: Int,
req: Option[Request[F]],
etagCalculator: File => F[String],
): OptionT[F, Response[F]] =
fromPath(
Path.fromNioPath(f.toPath()),
0,
f.length(),
buffsize,
req,
etagCalculator.compose(_.toNioPath.toFile()),
)
def fromPath[F[_]: Files: MonadThrow](
f: Path,
buffsize: Int,
req: Option[Request[F]],
etagCalculator: Path => F[String],
): OptionT[F, Response[F]] =
OptionT
.liftF(Files[F].getBasicFileAttributes(f, followLinks = true).map(_.size))
.flatMap { size =>
fromPath(f, 0, size, buffsize, req, etagCalculator)
}
.recoverWith { case _: fs2.io.file.NoSuchFileException =>
OptionT.none
}
@deprecated("Use fromPath", "0.23.5")
def fromFile[F[_]: Files](
f: File,
start: Long,
end: Long,
buffsize: Int,
req: Option[Request[F]],
etagCalculator: File => F[String],
)(implicit
F: MonadError[F, Throwable]
): OptionT[F, Response[F]] =
fromPath(
Path.fromNioPath(f.toPath()),
start,
end,
buffsize,
req,
etagCalculator.compose(_.toNioPath.toFile()),
)
def fromPath[F[_]: Files](
f: Path,
start: Long,
end: Long,
buffsize: Int,
req: Option[Request[F]],
etagCalculator: Path => F[String],
)(implicit
F: MonadError[F, Throwable]
): OptionT[F, Response[F]] =
OptionT(for {
etagCalc <- etagCalculator(f).map(et => ETag(et))
res <- Files[F].isRegularFile(f).flatMap[Option[Response[F]]] { isFile =>
if (isFile) {
if (start >= 0 && end >= start && buffsize > 0) {
Files[F]
.getBasicFileAttributes(f, followLinks = true)
.flatMap { attr =>
val lastModified =
HttpDate.fromEpochSecond(attr.lastModifiedTime.toSeconds).toOption
F.pure(notModified(req, etagCalc, lastModified).orElse {
val (body, contentLength) =
if (attr.size < end) (Stream.empty, 0L)
else (fileToBody[F](f, start, end), end - start)
val contentType = nameToContentType(f.fileName.toString)
val hs =
Headers(
lastModified.map(`Last-Modified`(_)),
`Content-Length`.fromLong(contentLength).toOption,
contentType,
etagCalc,
)
val r = Response(
headers = hs,
body = body,
attributes = Vault.empty.insert(staticPathKey, f),
)
logger.trace(s"Static file generated response: $r").unsafeRunSync()
r.some
})
}
} else {
F.raiseError[Option[Response[F]]](
new IllegalArgumentException(
s"requirement failed: start: $start, end: $end, buffsize: $buffsize"
)
)
}
} else {
F.pure(none[Response[F]])
}
}
} yield res)
private def notModified[F[_]](
req: Option[Request[F]],
etagCalc: ETag,
lastModified: Option[HttpDate],
): Option[Response[F]] = {
implicit val conjunction: Semigroup[Boolean] = new Semigroup[Boolean] {
def combine(x: Boolean, y: Boolean): Boolean = x && y
}
List(etagMatch(req, etagCalc), notModifiedSince(req, lastModified)).combineAll
.filter(identity)
.map(_ => Response[F](NotModified))
}
private def etagMatch[F[_]](req: Option[Request[F]], etagCalc: ETag) =
for {
r <- req
etagHeader <- r.headers.get[`If-None-Match`]
etagMatch = etagHeader.tags.exists(_.exists(_ == etagCalc.tag))
_ = logger.trace(
s"Matches `If-None-Match`: $etagMatch Previous ETag: ${etagHeader.value}, New ETag: $etagCalc"
)
} yield etagMatch
private def notModifiedSince[F[_]](req: Option[Request[F]], lastModified: Option[HttpDate]) =
for {
r <- req
h <- r.headers.get[`If-Modified-Since`]
lm <- lastModified
notModified = h.date >= lm
_ = logger.trace(
s"Matches `If-Modified-Since`: $notModified. Request age: ${h.date}, Modified: $lm"
)
} yield notModified
private def fileToBody[F[_]: Files](f: Path, start: Long, end: Long): EntityBody[F] =
Files[F].readRange(f, DefaultBufferSize, start, end)
private def nameToContentType(name: String): Option[`Content-Type`] =
name.lastIndexOf('.') match {
case -1 => None
case i => MediaType.forExtension(name.substring(i + 1)).map(`Content-Type`(_))
}
private[http4s] val staticPathKey = Key.newKey[SyncIO, Path].unsafeRunSync()
@deprecated("Use staticPathKey", since = "0.23.5")
private[http4s] lazy val staticFileKey: Key[File] =
staticPathKey.imap(_.toNioPath.toFile)(f => Path.fromNioPath(f.toPath))
}