/
WebjarService.scala
278 lines (251 loc) · 9.89 KB
/
WebjarService.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
/*
* Copyright 2014 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
package server
package staticcontent
import cats.data.{Kleisli, OptionT}
import cats.effect.{Blocker, ContextShift, Sync}
import cats.syntax.all._
import java.nio.file.{Path, Paths}
import org.http4s.internal.CollectionCompat.CollectionConverters._
import scala.util.control.NoStackTrace
/** [[org.http4s.server.staticcontent.WebjarServiceBuilder]] builder
*
* @param blocker execution context for blocking I/O
* @param filter To filter which assets from the webjars should be served
* @param cacheStrategy strategy to use for caching purposes.
* @param classLoader optional classloader for extracting the resources
* @param preferGzipped prefer gzip compression format?
*/
class WebjarServiceBuilder[F[_]] private (
blocker: Blocker,
webjarAssetFilter: WebjarServiceBuilder.WebjarAssetFilter,
cacheStrategy: CacheStrategy[F],
classLoader: Option[ClassLoader],
preferGzipped: Boolean) {
import WebjarServiceBuilder.{WebjarAsset, WebjarAssetFilter, serveWebjarAsset}
private def copy(
blocker: Blocker = blocker,
webjarAssetFilter: WebjarAssetFilter = webjarAssetFilter,
cacheStrategy: CacheStrategy[F] = cacheStrategy,
classLoader: Option[ClassLoader] = classLoader,
preferGzipped: Boolean = preferGzipped) =
new WebjarServiceBuilder[F](
blocker,
webjarAssetFilter,
cacheStrategy,
classLoader,
preferGzipped)
def withWebjarAssetFilter(webjarAssetFilter: WebjarAssetFilter): WebjarServiceBuilder[F] =
copy(webjarAssetFilter = webjarAssetFilter)
def withCacheStrategy(cacheStrategy: CacheStrategy[F]): WebjarServiceBuilder[F] =
copy(cacheStrategy = cacheStrategy)
def withClassLoader(classLoader: Option[ClassLoader]): WebjarServiceBuilder[F] =
copy(classLoader = classLoader)
def withBlocker(blocker: Blocker): WebjarServiceBuilder[F] =
copy(blocker = blocker)
def withPreferGzipped(preferGzipped: Boolean): WebjarServiceBuilder[F] =
copy(preferGzipped = preferGzipped)
def toRoutes(implicit F: Sync[F], cs: ContextShift[F]): HttpRoutes[F] = {
object BadTraversal extends Exception with NoStackTrace
val Root = Paths.get("")
Kleisli {
// Intercepts the routes that match webjar asset names
case request if request.method == Method.GET =>
val segments = request.pathInfo.segments.map(_.decoded(plusIsSpace = true))
OptionT
.liftF(F.catchNonFatal {
segments.foldLeft(Root) {
case (_, "" | "." | "..") => throw BadTraversal
case (path, segment) =>
path.resolve(segment)
}
})
.subflatMap(toWebjarAsset)
.filter(webjarAssetFilter)
.flatMap(serveWebjarAsset(blocker, cacheStrategy, classLoader, request, preferGzipped)(_))
.recover { case BadTraversal =>
Response(Status.BadRequest)
}
case _ => OptionT.none
}
}
/** Returns an Option(WebjarAsset) for a Request, or None if it couldn't be mapped
*
* @param p The request path without the prefix
* @return The WebjarAsset, or None if it couldn't be mapped
*/
private def toWebjarAsset(p: Path): Option[WebjarAsset] = {
val count = p.getNameCount
if (count > 2) {
val library = p.getName(0).toString
val version = p.getName(1).toString
val asset = asScalaIterator(p.subpath(2, count).iterator()).mkString("/")
Some(WebjarAsset(library, version, asset))
} else
None
}
/** Creates a scala.Iterator from a java.util.Iterator.
*
* We're not using scala.jdk.CollectionConverters (which was added in 2.13)
* or scala.collection.convert.ImplicitConversion (which was deprecated in 2.13)
* to ease cross-building against multiple Scala versions.
*/
private def asScalaIterator[A](underlying: java.util.Iterator[A]): Iterator[A] =
new Iterator[A] {
override def hasNext: Boolean = underlying.hasNext
override def next(): A = underlying.next()
}
}
object WebjarServiceBuilder {
def apply[F[_]](blocker: Blocker) =
new WebjarServiceBuilder(
blocker = blocker,
webjarAssetFilter = _ => true,
cacheStrategy = NoopCacheStrategy[F],
classLoader = None,
preferGzipped = false)
/** A filter callback for Webjar asset
* It's a function that takes the WebjarAsset and returns whether or not the asset
* should be served to the client.
*/
type WebjarAssetFilter = WebjarAsset => Boolean
/** Contains the information about an asset inside a webjar
*
* @param library The webjar's library name
* @param version The version of the webjar
* @param asset The asset name inside the webjar
*/
final case class WebjarAsset(library: String, version: String, asset: String) {
/** Constructs a full path for an asset inside a webjar asset
*
* @return The full name in the Webjar
*/
private[staticcontent] lazy val pathInJar: String =
s"/META-INF/resources/webjars/$library/$version/$asset"
}
/** Returns an asset that matched the request if it's found in the webjar path
*
* @param webjarAsset The WebjarAsset
* @param config The configuration
* @param request The Request
* @param optional class loader
* @param preferGzipped prefer gzip compression format?
* @return Either the the Asset, if it exist, or Pass
*/
private def serveWebjarAsset[F[_]: Sync: ContextShift](
blocker: Blocker,
cacheStrategy: CacheStrategy[F],
classLoader: Option[ClassLoader],
request: Request[F],
preferGzipped: Boolean)(webjarAsset: WebjarAsset): OptionT[F, Response[F]] =
StaticFile
.fromResource(
webjarAsset.pathInJar,
blocker,
Some(request),
classloader = classLoader,
preferGzipped = preferGzipped)
.semiflatMap(cacheStrategy.cache(request.pathInfo, _))
}
object WebjarService {
/** [[org.http4s.server.staticcontent.WebjarService]] configuration
*
* @param blocker execution context for blocking I/O
* @param filter To filter which assets from the webjars should be served
* @param cacheStrategy strategy to use for caching purposes. Default to no caching.
*/
final case class Config[F[_]](
blocker: Blocker,
filter: WebjarAssetFilter = _ => true,
cacheStrategy: CacheStrategy[F] = NoopCacheStrategy[F])
/** Contains the information about an asset inside a webjar
*
* @param library The webjar's library name
* @param version The version of the webjar
* @param asset The asset name inside the webjar
*/
final case class WebjarAsset(library: String, version: String, asset: String) {
/** Constructs a full path for an asset inside a webjar asset
*
* @return The full name in the Webjar
*/
private[staticcontent] lazy val pathInJar: String =
s"/META-INF/resources/webjars/$library/$version/$asset"
}
/** A filter callback for Webjar asset
* It's a function that takes the WebjarAsset and returns whether or not the asset
* should be served to the client.
*/
type WebjarAssetFilter = WebjarAsset => Boolean
/** Creates a new [[HttpRoutes]] that will filter the webjars
*
* @param config The configuration for this service
* @return The HttpRoutes
*/
@deprecated("use WebjarServiceBuilder", "0.22.0-M1")
def apply[F[_]](config: Config[F])(implicit F: Sync[F], cs: ContextShift[F]): HttpRoutes[F] = {
object BadTraversal extends Exception with NoStackTrace
val Root = Paths.get("")
Kleisli {
// Intercepts the routes that match webjar asset names
case request if request.method == Method.GET && request.pathInfo.nonEmpty =>
val segments = request.pathInfo.segments.map(_.decoded(plusIsSpace = true))
OptionT
.liftF(F.catchNonFatal {
segments.foldLeft(Root) {
case (_, "" | "." | "..") => throw BadTraversal
case (path, segment) =>
path.resolve(segment)
}
})
.subflatMap(toWebjarAsset)
.filter(config.filter)
.flatMap(serveWebjarAsset(config, request)(_))
.recover { case BadTraversal =>
Response(Status.BadRequest)
}
case _ => OptionT.none
}
}
/** Returns an Option(WebjarAsset) for a Request, or None if it couldn't be mapped
*
* @param p The request path without the prefix
* @return The WebjarAsset, or None if it couldn't be mapped
*/
private def toWebjarAsset(p: Path): Option[WebjarAsset] = {
val count = p.getNameCount
if (count > 2) {
val library = p.getName(0).toString
val version = p.getName(1).toString
val asset = p.subpath(2, count).iterator().asScala.mkString("/")
Some(WebjarAsset(library, version, asset))
} else
None
}
/** Returns an asset that matched the request if it's found in the webjar path
*
* @param webjarAsset The WebjarAsset
* @param config The configuration
* @param request The Request
* @return Either the the Asset, if it exist, or Pass
*/
private def serveWebjarAsset[F[_]: Sync: ContextShift](config: Config[F], request: Request[F])(
webjarAsset: WebjarAsset): OptionT[F, Response[F]] =
StaticFile
.fromResource(webjarAsset.pathInJar, config.blocker, Some(request))
.semiflatMap(config.cacheStrategy.cache(request.pathInfo, _))
}