-
Notifications
You must be signed in to change notification settings - Fork 3
/
Request+APIGateway.swift
107 lines (98 loc) · 3.36 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
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2023-2024 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 HTTPTypes
import Hummingbird
import NIOCore
protocol APIRequest {
var path: String { get }
var httpMethod: AWSLambdaEvents.HTTPMethod { get }
var queryString: String { get }
var httpHeaders: [(name: String, value: String)] { get }
var body: String? { get }
var isBase64Encoded: Bool { get }
}
extension HBRequest {
/// Specialization of HBLambda.request where `In` is `APIGateway.Request`
init(context: LambdaContext, from: some APIRequest) throws {
func urlPercentEncoded(_ string: String) -> String {
return string.addingPercentEncoding(withAllowedCharacters: .urlQueryComponentAllowed) ?? string
}
guard let method = HTTPRequest.Method(from.httpMethod.rawValue) else {
throw HBHTTPError(.badRequest)
}
// construct URI with query parameters
var uri = from.path
if from.queryString.count > 0 {
uri += "?\(from.queryString)"
}
// construct headers
var authority: String?
let headers = HTTPFields(headers: from.httpHeaders, authority: &authority)
// 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: .init(
method: method,
scheme: "https",
authority: authority,
path: uri,
headerFields: headers
),
body: body.map { .init(buffer: $0) } ?? .init(buffer: .init())
)
}
}
extension HTTPFields {
/// Initialize HTTPFields from HTTP headers and multivalue headers in an APIGateway request
/// - Parameters:
/// - headers: headers
/// - multiValueHeaders: multi-value headers
/// - authority: reference to authority string
init(headers: [(name: String, value: String)], authority: inout String?) {
self.init()
self.reserveCapacity(headers.count)
var firstHost = true
for (name, value) in headers {
if firstHost, name.lowercased() == "host" {
firstHost = false
authority = value
continue
}
if let fieldName = HTTPField.Name(name) {
self.append(HTTPField(name: fieldName, value: value))
}
}
}
}
extension CharacterSet {
static var urlQueryComponentAllowed: CharacterSet = {
var cs = CharacterSet.urlQueryAllowed
cs.remove(charactersIn: "&=")
return cs
}()
}