/
Response.swift
153 lines (130 loc) · 5.01 KB
/
Response.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import NIO
import NIOHTTP1
import NIOFoundationCompat
/// An HTTP response from a server back to the client.
///
/// let httpRes = HTTPResponse(status: .ok)
///
/// See `HTTPClient` and `HTTPServer`.
public final class Response: CustomStringConvertible {
/// Maximum streaming body size to use for `debugPrint(_:)`.
private let maxDebugStreamingBodySize: Int = 1_000_000
/// The HTTP version that corresponds to this response.
public var version: HTTPVersion
/// The HTTP response status.
public var status: HTTPResponseStatus
/// The header fields for this HTTP response.
/// The `"Content-Length"` and `"Transfer-Encoding"` headers will be set automatically
/// when the `body` property is mutated.
public var headers: HTTPHeaders
/// The `HTTPBody`. Updating this property will also update the associated transport headers.
///
/// httpRes.body = HTTPBody(string: "Hello, world!")
///
/// Also be sure to set this message's `contentType` property to a `MediaType` that correctly
/// represents the `HTTPBody`.
public var body: Body {
didSet { self.headers.updateContentLength(self.body.count) }
}
// If `true`, don't serialize the body.
var forHeadRequest: Bool
internal enum Upgrader {
case webSocket(maxFrameSize: WebSocketMaxFrameSize, onUpgrade: (WebSocket) -> ())
}
internal var upgrader: Upgrader?
public var storage: Storage
/// Get and set `HTTPCookies` for this `HTTPResponse`
/// This accesses the `"Set-Cookie"` header.
public var cookies: HTTPCookies {
get {
return self.headers.setCookie ?? .init()
}
set {
self.headers.setCookie = newValue
}
}
/// See `CustomStringConvertible`
public var description: String {
var desc: [String] = []
desc.append("HTTP/\(self.version.major).\(self.version.minor) \(self.status.code) \(self.status.reasonPhrase)")
desc.append(self.headers.debugDescription)
desc.append(self.body.description)
return desc.joined(separator: "\n")
}
// MARK: Content
private struct _ContentContainer: ContentContainer {
let response: Response
var contentType: HTTPMediaType? {
return self.response.headers.contentType
}
func encode<E>(_ encodable: E, using encoder: ContentEncoder) throws where E : Encodable {
var body = ByteBufferAllocator().buffer(capacity: 0)
try encoder.encode(encodable, to: &body, headers: &self.response.headers)
self.response.body = .init(buffer: body)
}
func decode<D>(_ decodable: D.Type, using decoder: ContentDecoder) throws -> D where D : Decodable {
guard let body = self.response.body.buffer else {
throw Abort(.unprocessableEntity)
}
return try decoder.decode(D.self, from: body, headers: self.response.headers)
}
}
public var content: ContentContainer {
get {
return _ContentContainer(response: self)
}
set {
// ignore since Request is a reference type
}
}
// MARK: Init
/// Creates a new `HTTPResponse`.
///
/// let httpRes = HTTPResponse(status: .ok)
///
/// - parameters:
/// - status: `HTTPResponseStatus` to use. This defaults to `HTTPResponseStatus.ok`
/// - version: `HTTPVersion` of this response, should usually be (and defaults to) 1.1.
/// - headers: `HTTPHeaders` to include with this response.
/// Defaults to empty headers.
/// The `"Content-Length"` and `"Transfer-Encoding"` headers will be set automatically.
/// - body: `HTTPBody` for this response, defaults to an empty body.
/// See `LosslessHTTPBodyRepresentable` for more information.
public convenience init(
status: HTTPResponseStatus = .ok,
version: HTTPVersion = .init(major: 1, minor: 1),
headers: HTTPHeaders = .init(),
body: Body = .empty
) {
self.init(
status: status,
version: version,
headersNoUpdate: headers,
body: body
)
self.headers.updateContentLength(body.count)
}
/// Internal init that creates a new `HTTPResponse` without sanitizing headers.
public init(
status: HTTPResponseStatus,
version: HTTPVersion,
headersNoUpdate headers: HTTPHeaders,
body: Body
) {
self.status = status
self.version = version
self.headers = headers
self.body = body
self.storage = .init()
self.forHeadRequest = false
}
}
extension HTTPHeaders {
mutating func updateContentLength(_ contentLength: Int) {
let count = contentLength.description
self.remove(name: .transferEncoding)
if count != self[.contentLength].first {
self.replaceOrAdd(name: .contentLength, value: count)
}
}
}