@@ -2,6 +2,7 @@ package com.github.mduesterhoeft.router
2
2
3
3
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
4
4
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
5
+ import java.net.URI
5
6
import java.util.Base64
6
7
7
8
fun APIGatewayProxyRequestEvent.acceptHeader () = getHeaderCaseInsensitive(" accept" )
@@ -13,6 +14,48 @@ fun APIGatewayProxyRequestEvent.getHeaderCaseInsensitive(httpHeader: String): St
13
14
fun APIGatewayProxyResponseEvent.getHeaderCaseInsensitive (httpHeader : String ): String? =
14
15
getCaseInsensitive(httpHeader, headers)
15
16
17
+ fun GET () = APIGatewayProxyRequestEvent ().withHttpMethod(" get" ).withHeaders(mutableMapOf ())
18
+ fun GET (path : String ) = GET ().withPath(path)
19
+ fun POST () = APIGatewayProxyRequestEvent ().withHttpMethod(" post" ).withHeaders(mutableMapOf ())
20
+ fun POST (path : String ) = POST ().withPath(path)
21
+ fun PUT () = APIGatewayProxyRequestEvent ().withHttpMethod(" put" ).withHeaders(mutableMapOf ())
22
+ fun PUT (path : String ) = PUT ().withPath(path)
23
+ fun PATCH () = APIGatewayProxyRequestEvent ().withHttpMethod(" patch" ).withHeaders(mutableMapOf ())
24
+ fun PATCH (path : String ) = PATCH ().withPath(path)
25
+ fun DELETE () = APIGatewayProxyRequestEvent ().withHttpMethod(" delete" ).withHeaders(mutableMapOf ())
26
+ fun DELETE (path : String ) = DELETE ().withPath(path)
27
+
28
+ /* *
29
+ * Get a URI that can be used as location header for responses.
30
+ * The host is taken from the Host header.
31
+ * The protocol is taken from the x-forwarded-proto.
32
+ * The port is taken from the x-forwarded-port header. Standard ports are omitted.
33
+ */
34
+ fun APIGatewayProxyRequestEvent.location (path : String ): URI {
35
+ val host = getHeaderCaseInsensitive(" host" )? : " localhost"
36
+ val proto = getHeaderCaseInsensitive(" x-forwarded-proto" )? : " http"
37
+ val portPart = getHeaderCaseInsensitive(" x-forwarded-port" )
38
+ ?.let {
39
+ when {
40
+ proto == " https" && it == " 443" -> null
41
+ proto == " http" && it == " 80" -> null
42
+ else -> " :$it "
43
+ }
44
+ }? : " "
45
+ return URI (" $proto ://$host$portPart /${path.removePrefix(" /" )} " )
46
+ }
47
+
48
+ fun APIGatewayProxyRequestEvent.withHeader (name : String , value : String ) =
49
+ this .also { if (headers == null ) headers = mutableMapOf () }.also { headers[name] = value }
50
+
51
+ fun APIGatewayProxyResponseEvent.withHeader (name : String , value : String ) =
52
+ this .also { if (headers == null ) headers = mutableMapOf () }.also { headers[name] = value }
53
+
54
+ fun APIGatewayProxyResponseEvent.withLocationHeader (request : APIGatewayProxyRequestEvent , path : String ) =
55
+ this .also { if (headers == null ) headers = mutableMapOf () }.also { headers[" location" ] = request.location(path).toString() }
56
+
57
+ fun APIGatewayProxyResponseEvent.location () = getHeaderCaseInsensitive(" location" )
58
+
16
59
private fun getCaseInsensitive (key : String , map : Map <String , String >): String? =
17
60
map.entries
18
61
.firstOrNull { it.key.toLowerCase() == key.toLowerCase() }
0 commit comments