-
Notifications
You must be signed in to change notification settings - Fork 787
/
UrlFormLifter.scala
70 lines (63 loc) · 2.36 KB
/
UrlFormLifter.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
/*
* 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 middleware
import cats.data.Kleisli
import cats.effect._
import cats.syntax.all._
import cats.~>
/** [[Middleware]] for lifting application/x-www-form-urlencoded bodies into the
* request query params.
*
* The params are merged into the existing paras _after_ the existing query params. This
* means that if the query already contains the pair "foo" -> Some("bar"), parameters on
* the body must be acessed through `multiParams`.
*/
object UrlFormLifter {
def apply[F[_]: Sync, G[_]: Concurrent](f: G ~> F)(
http: Kleisli[F, Request[G], Response[G]],
strictDecode: Boolean = false,
): Kleisli[F, Request[G], Response[G]] =
Kleisli { req =>
def addUrlForm(form: UrlForm): F[Response[G]] = {
val flatForm = form.values.toVector.flatMap { case (k, vs) =>
vs.toVector.map(v => (k, Some(v)))
}
val params = req.uri.query.toVector ++ flatForm: Vector[(String, Option[String])]
val newQuery = Query(params: _*)
val newRequest = req
.withUri(req.uri.copy(query = newQuery))
.withAttributes(req.attributes)
.withEmptyBody
http(newRequest)
}
req.headers.get[headers.`Content-Type`] match {
case Some(headers.`Content-Type`(MediaType.application.`x-www-form-urlencoded`, _))
if checkRequest(req) =>
for {
decoded <- f(UrlForm.entityDecoder[G].decode(req, strictDecode).value)
resp <- decoded.fold(
mf => f(mf.toHttpResponse[G](req.httpVersion).pure[G]),
addUrlForm,
)
} yield resp
case _ => http(req)
}
}
private def checkRequest[F[_]](req: Request[F]): Boolean =
req.method == Method.POST || req.method == Method.PUT
}