/
Request+APIGateway.swift
82 lines (76 loc) · 2.53 KB
/
Request+APIGateway.swift
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2021 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AWSLambdaEvents
import AWSLambdaRuntime
import ExtrasBase64
import Foundation
import Hummingbird
import NIOHTTP1
protocol APIRequest {
var path: String { get }
var httpMethod: AWSLambdaEvents.HTTPMethod { get }
var queryString: String { get }
var httpHeaders: HTTPHeaders { get }
var body: String? { get }
var isBase64Encoded: Bool { get }
}
extension LambdaContext: HBRequestContext {
public var remoteAddress: SocketAddress? { return nil }
}
extension HBRequest {
/// Specialization of HBLambda.request where `In` is `APIGateway.Request`
init<Request: APIRequest>(context: LambdaContext, application: HBApplication, from: Request) throws {
func urlPercentEncoded(_ string: String) -> String {
return string.addingPercentEncoding(withAllowedCharacters: .urlQueryComponentAllowed) ?? string
}
// construct URI with query parameters
var uri = from.path
if from.queryString.count > 0 {
uri += "?\(from.queryString)"
}
// construct headers
let headers = from.httpHeaders
let head = HTTPRequestHead(
version: .init(major: 2, minor: 0),
method: .init(rawValue: from.httpMethod.rawValue),
uri: uri,
headers: headers
)
// get body
let body: ByteBuffer?
if let apiGatewayBody = from.body {
if from.isBase64Encoded {
let base64Decoded = try apiGatewayBody.base64decoded()
body = context.allocator.buffer(bytes: base64Decoded)
} else {
body = context.allocator.buffer(string: apiGatewayBody)
}
} else {
body = nil
}
self.init(
head: head,
body: .byteBuffer(body),
application: application,
context: context
)
}
}
extension CharacterSet {
static var urlQueryComponentAllowed: CharacterSet = {
var cs = CharacterSet.urlQueryAllowed
cs.remove(charactersIn: "&=")
return cs
}()
}