/
HTTPServer.swift
447 lines (383 loc) · 16.6 KB
/
HTTPServer.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import NIO
import NIOExtras
import NIOHTTP1
import NIOHTTP2
import NIOHTTPCompression
import NIOSSL
public enum HTTPVersionMajor: Equatable, Hashable {
case one
case two
}
public final class HTTPServer: Server {
/// Engine server config struct.
///
/// let serverConfig = HTTPServerConfig.default(port: 8123)
/// services.register(serverConfig)
///
public struct Configuration {
/// Host name the server will bind to.
public var hostname: String
/// Port the server will bind to.
public var port: Int
/// Listen backlog.
public var backlog: Int
/// When `true`, can prevent errors re-binding to a socket after successive server restarts.
public var reuseAddress: Bool
/// When `true`, OS will attempt to minimize TCP packet delay.
public var tcpNoDelay: Bool
/// Response compression configuration.
public var responseCompression: CompressionConfiguration
/// Supported HTTP compression options.
public struct CompressionConfiguration {
/// Disables compression. This is the default.
public static var disabled: Self {
.init(storage: .disabled)
}
/// Enables compression with default configuration.
public static var enabled: Self {
.enabled(initialByteBufferCapacity: 1024)
}
/// Enables compression with custom configuration.
public static func enabled(
initialByteBufferCapacity: Int
) -> Self {
.init(storage: .enabled(
initialByteBufferCapacity: initialByteBufferCapacity
))
}
enum Storage {
case disabled
case enabled(initialByteBufferCapacity: Int)
}
var storage: Storage
}
/// Request decompression configuration.
public var requestDecompression: DecompressionConfiguration
/// Supported HTTP decompression options.
public struct DecompressionConfiguration {
/// Disables decompression. This is the default option.
public static var disabled: Self {
.init(storage: .disabled)
}
/// Enables decompression with default configuration.
public static var enabled: Self {
.enabled(limit: .ratio(10))
}
/// Enables decompression with custom configuration.
public static func enabled(
limit: NIOHTTPDecompression.DecompressionLimit
) -> Self {
.init(storage: .enabled(limit: limit))
}
enum Storage {
case disabled
case enabled(limit: NIOHTTPDecompression.DecompressionLimit)
}
var storage: Storage
}
/// When `true`, HTTP server will support pipelined requests.
public var supportPipelining: Bool
public var supportVersions: Set<HTTPVersionMajor>
public var tlsConfiguration: TLSConfiguration?
/// If set, this name will be serialized as the `Server` header in outgoing responses.
public var serverName: String?
/// Any uncaught server or responder errors will go here.
public var logger: Logger
public init(
hostname: String = "127.0.0.1",
port: Int = 8080,
backlog: Int = 256,
reuseAddress: Bool = true,
tcpNoDelay: Bool = true,
responseCompression: CompressionConfiguration = .disabled,
requestDecompression: DecompressionConfiguration = .disabled,
supportPipelining: Bool = false,
supportVersions: Set<HTTPVersionMajor>? = nil,
tlsConfiguration: TLSConfiguration? = nil,
serverName: String? = nil,
logger: Logger? = nil
) {
self.hostname = hostname
self.port = port
self.backlog = backlog
self.reuseAddress = reuseAddress
self.tcpNoDelay = tcpNoDelay
self.responseCompression = responseCompression
self.requestDecompression = requestDecompression
self.supportPipelining = supportPipelining
if let supportVersions = supportVersions {
self.supportVersions = supportVersions
} else {
self.supportVersions = tlsConfiguration == nil ? [.one] : [.one, .two]
}
self.tlsConfiguration = tlsConfiguration
self.serverName = serverName
self.logger = logger ?? Logger(label: "codes.vapor.http-server")
}
}
public var onShutdown: EventLoopFuture<Void> {
guard let connection = self.connection else {
fatalError("Server has not started yet")
}
return connection.channel.closeFuture
}
private let responder: Responder
private let configuration: Configuration
private let eventLoopGroup: EventLoopGroup
private var connection: HTTPServerConnection?
private var didShutdown: Bool
private var didStart: Bool
private var application: Application
init(
application: Application,
responder: Responder,
configuration: Configuration,
on eventLoopGroup: EventLoopGroup
) {
self.application = application
self.responder = responder
self.configuration = configuration
self.eventLoopGroup = eventLoopGroup
self.didStart = false
self.didShutdown = false
}
public func start(hostname: String?, port: Int?) throws {
// determine which hostname / port to bind to
var configuration = self.configuration
configuration.hostname = hostname ?? configuration.hostname
configuration.port = port ?? configuration.port
// print starting message
let scheme = configuration.tlsConfiguration == nil ? "http" : "https"
let address = "\(scheme)://\(configuration.hostname):\(configuration.port)"
self.configuration.logger.notice("Server starting on \(address)")
// start the actual HTTPServer
self.connection = try HTTPServerConnection.start(
application: self.application,
responder: self.responder,
configuration: configuration,
on: self.eventLoopGroup
).wait()
self.didStart = true
}
public func shutdown() {
guard let connection = self.connection else {
return
}
self.configuration.logger.debug("Requesting HTTP server shutdown")
do {
try connection.close().wait()
} catch {
self.configuration.logger.error("Could not stop HTTP server: \(error)")
}
self.configuration.logger.debug("HTTP server shutting down")
self.didShutdown = true
}
deinit {
assert(!self.didStart || self.didShutdown, "HTTPServer did not shutdown before deinitializing")
}
}
private final class HTTPServerConnection {
let channel: Channel
let quiesce: ServerQuiescingHelper
static func start(
application: Application,
responder: Responder,
configuration: HTTPServer.Configuration,
on eventLoopGroup: EventLoopGroup
) -> EventLoopFuture<HTTPServerConnection> {
let quiesce = ServerQuiescingHelper(group: eventLoopGroup)
let bootstrap = ServerBootstrap(group: eventLoopGroup)
// Specify backlog and enable SO_REUSEADDR for the server itself
.serverChannelOption(ChannelOptions.backlog, value: Int32(configuration.backlog))
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: configuration.reuseAddress ? SocketOptionValue(1) : SocketOptionValue(0))
// Set handlers that are applied to the Server's channel
.serverChannelInitializer { channel in
channel.pipeline.addHandler(quiesce.makeServerChannelHandler(channel: channel))
}
// Set the handlers that are applied to the accepted Channels
.childChannelInitializer { [weak application] channel in
// add TLS handlers if configured
if var tlsConfiguration = configuration.tlsConfiguration {
// prioritize http/2
if configuration.supportVersions.contains(.two) {
tlsConfiguration.applicationProtocols.append("h2")
}
if configuration.supportVersions.contains(.one) {
tlsConfiguration.applicationProtocols.append("http/1.1")
}
let sslContext: NIOSSLContext
let tlsHandler: NIOSSLServerHandler
do {
sslContext = try NIOSSLContext(configuration: tlsConfiguration)
tlsHandler = try NIOSSLServerHandler(context: sslContext)
} catch {
configuration.logger.error("Could not configure TLS: \(error)")
return channel.close(mode: .all)
}
return channel.pipeline.addHandler(tlsHandler).flatMap { _ in
return channel.configureHTTP2SecureUpgrade(h2ChannelConfigurator: { channel in
return channel.configureHTTP2Pipeline(
mode: .server,
inboundStreamStateInitializer: { (channel, streamID) in
return channel.pipeline.addVaporHTTP2Handlers(
application: application!,
responder: responder,
configuration: configuration,
streamID: streamID
)
}
).map { _ in }
}, http1ChannelConfigurator: { channel in
return channel.pipeline.addVaporHTTP1Handlers(
application: application!,
responder: responder,
configuration: configuration
)
})
}
} else {
guard !configuration.supportVersions.contains(.two) else {
fatalError("Plaintext HTTP/2 (h2c) not yet supported.")
}
return channel.pipeline.addVaporHTTP1Handlers(
application: application!,
responder: responder,
configuration: configuration
)
}
}
// Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels
.childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY), value: configuration.tcpNoDelay ? SocketOptionValue(1) : SocketOptionValue(0))
.childChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: configuration.reuseAddress ? SocketOptionValue(1) : SocketOptionValue(0))
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
return bootstrap.bind(host: configuration.hostname, port: configuration.port).map { channel in
return .init(channel: channel, quiesce: quiesce)
}.flatMapErrorThrowing { error -> HTTPServerConnection in
quiesce.initiateShutdown(promise: nil)
throw error
}
}
init(channel: Channel, quiesce: ServerQuiescingHelper) {
self.channel = channel
self.quiesce = quiesce
}
func close() -> EventLoopFuture<Void> {
let promise = self.channel.eventLoop.makePromise(of: Void.self)
self.channel.eventLoop.scheduleTask(in: .seconds(10)) {
promise.fail(Abort(.internalServerError, reason: "Server stop took too long."))
}
self.quiesce.initiateShutdown(promise: promise)
return promise.futureResult
}
var onClose: EventLoopFuture<Void> {
self.channel.closeFuture
}
deinit {
assert(!self.channel.isActive, "HTTPServerConnection deinitialized without calling shutdown()")
}
}
final class HTTPServerErrorHandler: ChannelInboundHandler {
typealias InboundIn = Never
let logger: Logger
init(logger: Logger) {
self.logger = logger
}
func errorCaught(context: ChannelHandlerContext, error: Error) {
self.logger.debug("Unhandled HTTP server error: \(error)")
context.close(mode: .output, promise: nil)
}
}
extension ChannelPipeline {
func addVaporHTTP2Handlers(
application: Application,
responder: Responder,
configuration: HTTPServer.Configuration,
streamID: HTTP2StreamID
) -> EventLoopFuture<Void> {
// create server pipeline array
var handlers: [ChannelHandler] = []
let http2 = HTTP2ToHTTP1ServerCodec(streamID: streamID)
handlers.append(http2)
// add NIO -> HTTP request decoder
let serverReqDecoder = HTTPServerRequestDecoder(
application: application
)
handlers.append(serverReqDecoder)
// add NIO -> HTTP response encoder
let serverResEncoder = HTTPServerResponseEncoder(
serverHeader: configuration.serverName,
dateCache: .eventLoop(self.eventLoop)
)
handlers.append(serverResEncoder)
// add server request -> response delegate
let handler = HTTPServerHandler(responder: responder)
handlers.append(handler)
return self.addHandlers(handlers).flatMap {
self.addHandler(HTTPServerErrorHandler(logger: configuration.logger))
}
}
func addVaporHTTP1Handlers(
application: Application,
responder: Responder,
configuration: HTTPServer.Configuration
) -> EventLoopFuture<Void> {
// create server pipeline array
var handlers: [RemovableChannelHandler] = []
// configure HTTP/1
// add http parsing and serializing
let httpResEncoder = HTTPResponseEncoder()
let httpReqDecoder = ByteToMessageHandler(HTTPRequestDecoder(
leftOverBytesStrategy: .forwardBytes
))
handlers += [httpResEncoder, httpReqDecoder]
// add pipelining support if configured
if configuration.supportPipelining {
let pipelineHandler = HTTPServerPipelineHandler()
handlers.append(pipelineHandler)
}
// add response compressor if configured
switch configuration.responseCompression.storage {
case .enabled(let initialByteBufferCapacity):
let responseCompressionHandler = HTTPResponseCompressor(
initialByteBufferCapacity: initialByteBufferCapacity
)
handlers.append(responseCompressionHandler)
case .disabled:
break
}
// add request decompressor if configured
switch configuration.requestDecompression.storage {
case .enabled(let limit):
let requestDecompressionHandler = NIOHTTPRequestDecompressor(
limit: limit
)
handlers.append(requestDecompressionHandler)
case .disabled:
break
}
// add NIO -> HTTP response encoder
let serverResEncoder = HTTPServerResponseEncoder(
serverHeader: configuration.serverName,
dateCache: .eventLoop(self.eventLoop)
)
handlers.append(serverResEncoder)
// add NIO -> HTTP request decoder
let serverReqDecoder = HTTPServerRequestDecoder(
application: application
)
handlers.append(serverReqDecoder)
// add server request -> response delegate
let handler = HTTPServerHandler(responder: responder)
// add HTTP upgrade handler
let upgrader = HTTPServerUpgradeHandler(
httpRequestDecoder: httpReqDecoder,
httpHandlers: handlers + [handler]
)
handlers.append(upgrader)
handlers.append(handler)
// wait to add delegate as final step
return self.addHandlers(handlers).flatMap {
self.addHandler(HTTPServerErrorHandler(logger: configuration.logger))
}
}
}