/
Response+Body.swift
189 lines (167 loc) · 7.12 KB
/
Response+Body.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
extension Response {
struct BodyStream {
let count: Int
let callback: (BodyStreamWriter) -> ()
}
/// Represents a `Response`'s body.
///
/// let body = Response.Body(string: "Hello, world!")
///
/// This can contain any data (streaming or static) and should match the message's `"Content-Type"` header.
public struct Body: CustomStringConvertible, ExpressibleByStringLiteral {
/// The internal HTTP body storage enum. This is an implementation detail.
internal enum Storage {
/// Cases
case none
case buffer(ByteBuffer)
case data(Data)
case dispatchData(DispatchData)
case staticString(StaticString)
case string(String)
case stream(BodyStream)
}
/// An empty `Response.Body`.
public static let empty: Body = .init()
public var string: String? {
switch self.storage {
case .buffer(var buffer): return buffer.readString(length: buffer.readableBytes)
case .data(let data): return String(decoding: data, as: UTF8.self)
case .dispatchData(let dispatchData): return String(decoding: dispatchData, as: UTF8.self)
case .staticString(let staticString): return staticString.description
case .string(let string): return string
default: return nil
}
}
/// The size of the HTTP body's data.
/// `-1` is a chunked stream.
public var count: Int {
switch self.storage {
case .data(let data): return data.count
case .dispatchData(let data): return data.count
case .staticString(let staticString): return staticString.utf8CodeUnitCount
case .string(let string): return string.utf8.count
case .buffer(let buffer): return buffer.readableBytes
case .none: return 0
case .stream(let stream): return stream.count
}
}
/// Returns static data if not streaming.
public var data: Data? {
switch self.storage {
case .buffer(var buffer): return buffer.readData(length: buffer.readableBytes)
case .data(let data): return data
case .dispatchData(let dispatchData): return Data(dispatchData)
case .staticString(let staticString): return Data(bytes: staticString.utf8Start, count: staticString.utf8CodeUnitCount)
case .string(let string): return Data(string.utf8)
case .none: return nil
case .stream: return nil
}
}
public var buffer: ByteBuffer? {
switch self.storage {
case .buffer(let buffer): return buffer
case .data(let data):
var buffer = ByteBufferAllocator().buffer(capacity: data.count)
buffer.writeBytes(data)
return buffer
case .dispatchData(let dispatchData):
var buffer = ByteBufferAllocator().buffer(capacity: dispatchData.count)
buffer.writeDispatchData(dispatchData)
return buffer
case .staticString(let staticString):
var buffer = ByteBufferAllocator().buffer(capacity: staticString.utf8CodeUnitCount)
buffer.writeStaticString(staticString)
return buffer
case .string(let string):
var buffer = ByteBufferAllocator().buffer(capacity: string.count)
buffer.writeString(string)
return buffer
case .none: return nil
case .stream: return nil
}
}
public func collect(on eventLoop: EventLoop) -> EventLoopFuture<ByteBuffer?> {
switch self.storage {
case .stream(let stream):
let collector = ResponseBodyCollector(eventLoop: eventLoop)
stream.callback(collector)
return collector.promise.futureResult
.map { $0 }
default:
return eventLoop.makeSucceededFuture(self.buffer)
}
}
/// See `CustomDebugStringConvertible`.
public var description: String {
switch storage {
case .none: return "<no body>"
case .buffer(let buffer): return buffer.getString(at: 0, length: buffer.readableBytes) ?? "n/a"
case .data(let data): return String(data: data, encoding: .ascii) ?? "n/a"
case .dispatchData(let data): return String(data: Data(data), encoding: .ascii) ?? "n/a"
case .staticString(let string): return string.description
case .string(let string): return string
case .stream: return "<stream>"
}
}
internal var storage: Storage
/// Creates an empty body. Useful for `GET` requests where HTTP bodies are forbidden.
public init() {
self.storage = .none
}
/// Create a new body wrapping `Data`.
public init(data: Data) {
storage = .data(data)
}
/// Create a new body wrapping `DispatchData`.
public init(dispatchData: DispatchData) {
storage = .dispatchData(dispatchData)
}
/// Create a new body from the UTF8 representation of a `StaticString`.
public init(staticString: StaticString) {
storage = .staticString(staticString)
}
/// Create a new body from the UTF8 representation of a `String`.
public init(string: String) {
self.storage = .string(string)
}
/// Create a new body from a Swift NIO `ByteBuffer`.
public init(buffer: ByteBuffer) {
self.storage = .buffer(buffer)
}
public init(stream: @escaping (BodyStreamWriter) -> (), count: Int) {
self.storage = .stream(.init(count: count, callback: stream))
}
public init(stream: @escaping (BodyStreamWriter) -> ()) {
self.init(stream: stream, count: -1)
}
/// `ExpressibleByStringLiteral` conformance.
public init(stringLiteral value: String) {
self.storage = .string(value)
}
/// Internal init.
internal init(storage: Storage) {
self.storage = storage
}
}
}
private final class ResponseBodyCollector: BodyStreamWriter {
var buffer: ByteBuffer
var eventLoop: EventLoop
var promise: EventLoopPromise<ByteBuffer>
init(eventLoop: EventLoop) {
self.buffer = ByteBufferAllocator().buffer(capacity: 0)
self.eventLoop = eventLoop
self.promise = self.eventLoop.makePromise(of: ByteBuffer.self)
}
func write(_ result: BodyStreamResult, promise: EventLoopPromise<Void>?) {
switch result {
case .buffer(var buffer):
self.buffer.writeBuffer(&buffer)
case .error(let error):
self.promise.fail(error)
case .end:
self.promise.succeed(self.buffer)
}
promise?.succeed(())
}
}