forked from swift-server/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP1Connection.swift
118 lines (97 loc) · 3.49 KB
/
HTTP1Connection.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
//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Logging
import NIO
import NIOHTTP1
import NIOHTTPCompression
protocol HTTP1ConnectionDelegate {
func http1ConnectionReleased(_: HTTP1Connection)
func http1ConnectionClosed(_: HTTP1Connection)
}
final class HTTP1Connection {
let channel: Channel
/// the connection's delegate, that will be informed about connection close and connection release
/// (ready to run next request).
let delegate: HTTP1ConnectionDelegate
enum State {
case active
case closed
}
private var state: State = .active
let id: HTTPConnectionPool.Connection.ID
init(channel: Channel,
connectionID: HTTPConnectionPool.Connection.ID,
configuration: HTTPClient.Configuration,
delegate: HTTP1ConnectionDelegate,
logger: Logger) throws {
channel.eventLoop.assertInEventLoop()
// let's add the channel handlers needed for h1
self.channel = channel
self.id = connectionID
self.delegate = delegate
// all properties are set here. Therefore the connection is fully initialized. If we
// run into an error, here we need to do the state handling ourselfes.
do {
let sync = channel.pipeline.syncOperations
try sync.addHTTPClientHandlers()
if case .enabled(let limit) = configuration.decompression {
let decompressHandler = NIOHTTPResponseDecompressor(limit: limit)
try sync.addHandler(decompressHandler)
}
let channelHandler = HTTP1ClientChannelHandler(
connection: self,
eventLoop: channel.eventLoop,
logger: logger
)
try sync.addHandler(channelHandler)
// with this we create an intended retain cycle...
self.channel.closeFuture.whenComplete { _ in
self.state = .closed
self.delegate.http1ConnectionClosed(self)
}
} catch {
self.state = .closed
throw error
}
}
deinit {
guard case .closed = self.state else {
preconditionFailure("Connection must be closed, before we can deinit it")
}
}
func execute(request: HTTPExecutableRequest) {
if self.channel.eventLoop.inEventLoop {
self.execute0(request: request)
} else {
self.channel.eventLoop.execute {
self.execute0(request: request)
}
}
}
func cancel() {
self.channel.triggerUserOutboundEvent(HTTPConnectionEvent.cancelRequest, promise: nil)
}
func close() -> EventLoopFuture<Void> {
return self.channel.close()
}
func taskCompleted() {
self.delegate.http1ConnectionReleased(self)
}
private func execute0(request: HTTPExecutableRequest) {
guard self.channel.isActive else {
return request.fail(ChannelError.ioOnClosedChannel)
}
self.channel.write(request, promise: nil)
}
}