-
Notifications
You must be signed in to change notification settings - Fork 595
/
MethodDirectives.scala
125 lines (110 loc) · 3.09 KB
/
MethodDirectives.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
/*
* Copyright (C) 2009-2023 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.scaladsl.server
package directives
import akka.http.scaladsl.model.{ StatusCodes, HttpMethod }
import akka.http.scaladsl.model.HttpMethods._
/**
* @groupname method Method directives
* @groupprio method 130
*/
trait MethodDirectives {
import BasicDirectives._
import RouteDirectives._
import ParameterDirectives._
import MethodDirectives._
/**
* Rejects all non-DELETE requests.
*
* @group method
*/
def delete: Directive0 = _delete
/**
* Rejects all non-GET requests.
*
* @group method
*/
def get: Directive0 = _get
/**
* Rejects all non-HEAD requests.
*
* @group method
*/
def head: Directive0 = _head
/**
* Rejects all non-OPTIONS requests.
*
* @group method
*/
def options: Directive0 = _options
/**
* Rejects all non-PATCH requests.
*
* @group method
*/
def patch: Directive0 = _patch
/**
* Rejects all non-POST requests.
*
* @group method
*/
def post: Directive0 = _post
/**
* Rejects all non-PUT requests.
*
* @group method
*/
def put: Directive0 = _put
/**
* Extracts the request method.
*
* @group method
*/
def extractMethod: Directive1[HttpMethod] = _extractMethod
/**
* Rejects all requests whose HTTP method does not match the given one.
*
* @group method
*/
//#method
def method(httpMethod: HttpMethod): Directive0 =
extractMethod.flatMap[Unit] {
case `httpMethod` => pass
case _ => reject(MethodRejection(httpMethod))
} & cancelRejections(classOf[MethodRejection])
//#method
/**
* Changes the HTTP method of the request to the value of the specified query string parameter. If the query string
* parameter is not specified this directive has no effect. If the query string is specified as something that is not
* a HTTP method, then this directive completes the request with a `501 Not Implemented` response.
*
* This directive is useful for:
* - Use in combination with JSONP (JSONP only supports GET)
* - Supporting older browsers that lack support for certain HTTP methods. E.g. IE8 does not support PATCH
*
* @group method
*/
def overrideMethodWithParameter(paramName: String): Directive0 =
parameter(paramName.optional) flatMap {
case Some(method) =>
getForKey(method.toUpperCase) match {
case Some(m) => mapRequest(_.withMethod(m))
case _ => complete(StatusCodes.NotImplemented)
}
case None => pass
}
}
object MethodDirectives extends MethodDirectives {
private val _extractMethod: Directive1[HttpMethod] =
BasicDirectives.extract(_.request.method)
// format: OFF
private val _delete : Directive0 = method(DELETE)
private val _get : Directive0 = method(GET)
private val _head : Directive0 = method(HEAD)
private val _options: Directive0 = method(OPTIONS)
private val _patch : Directive0 = method(PATCH)
private val _post : Directive0 = method(POST)
private val _put : Directive0 = method(PUT)
// format: ON
}