|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the AsyncHTTPClient open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import NIO |
| 16 | +import NIOHTTP1 |
| 17 | + |
| 18 | +final class HTTP1ProxyConnectHandler: ChannelDuplexHandler, RemovableChannelHandler { |
| 19 | + typealias OutboundIn = Never |
| 20 | + typealias OutboundOut = HTTPClientRequestPart |
| 21 | + typealias InboundIn = HTTPClientResponsePart |
| 22 | + |
| 23 | + enum State { |
| 24 | + case initialized(EventLoopPromise<Void>) |
| 25 | + case connectSent(EventLoopPromise<Void>) |
| 26 | + case headReceived(EventLoopPromise<Void>) |
| 27 | + case failed(Error) |
| 28 | + case completed |
| 29 | + } |
| 30 | + |
| 31 | + private var state: State |
| 32 | + |
| 33 | + let targetHost: String |
| 34 | + let targetPort: Int |
| 35 | + let proxyAuthorization: HTTPClient.Authorization? |
| 36 | + |
| 37 | + init(targetHost: String, |
| 38 | + targetPort: Int, |
| 39 | + proxyAuthorization: HTTPClient.Authorization?, |
| 40 | + connectPromise: EventLoopPromise<Void>) { |
| 41 | + self.targetHost = targetHost |
| 42 | + self.targetPort = targetPort |
| 43 | + self.proxyAuthorization = proxyAuthorization |
| 44 | + |
| 45 | + self.state = .initialized(connectPromise) |
| 46 | + } |
| 47 | + |
| 48 | + func handlerAdded(context: ChannelHandlerContext) { |
| 49 | + precondition(context.channel.isActive, "Expected to be added to an active channel") |
| 50 | + |
| 51 | + self.sendConnect(context: context) |
| 52 | + } |
| 53 | + |
| 54 | + func handlerRemoved(context: ChannelHandlerContext) { |
| 55 | + switch self.state { |
| 56 | + case .failed, .completed: |
| 57 | + break |
| 58 | + case .initialized, .connectSent, .headReceived: |
| 59 | + preconditionFailure("Removing the handler, while connecting seems wrong") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { |
| 64 | + preconditionFailure("We don't support outgoing traffic during HTTP Proxy update.") |
| 65 | + } |
| 66 | + |
| 67 | + func channelRead(context: ChannelHandlerContext, data: NIOAny) { |
| 68 | + switch self.unwrapInboundIn(data) { |
| 69 | + case .head(let head): |
| 70 | + guard case .connectSent(let promise) = self.state else { |
| 71 | + preconditionFailure("HTTPDecoder should throw an error, if we have not send a request") |
| 72 | + } |
| 73 | + |
| 74 | + switch head.status.code { |
| 75 | + case 200..<300: |
| 76 | + // Any 2xx (Successful) response indicates that the sender (and all |
| 77 | + // inbound proxies) will switch to tunnel mode immediately after the |
| 78 | + // blank line that concludes the successful response's header section |
| 79 | + self.state = .headReceived(promise) |
| 80 | + case 407: |
| 81 | + let error = HTTPClientError.proxyAuthenticationRequired |
| 82 | + self.state = .failed(error) |
| 83 | + context.close(promise: nil) |
| 84 | + promise.fail(error) |
| 85 | + default: |
| 86 | + // Any response other than a successful response |
| 87 | + // indicates that the tunnel has not yet been formed and that the |
| 88 | + // connection remains governed by HTTP. |
| 89 | + let error = HTTPClientError.invalidProxyResponse |
| 90 | + self.state = .failed(error) |
| 91 | + context.close(promise: nil) |
| 92 | + promise.fail(error) |
| 93 | + } |
| 94 | + case .body: |
| 95 | + switch self.state { |
| 96 | + case .headReceived(let promise): |
| 97 | + // we don't expect a body |
| 98 | + let error = HTTPClientError.invalidProxyResponse |
| 99 | + self.state = .failed(error) |
| 100 | + context.close(promise: nil) |
| 101 | + promise.fail(error) |
| 102 | + case .failed: |
| 103 | + // ran into an error before... ignore this one |
| 104 | + break |
| 105 | + case .completed, .connectSent, .initialized: |
| 106 | + preconditionFailure("Invalid state") |
| 107 | + } |
| 108 | + |
| 109 | + case .end: |
| 110 | + switch self.state { |
| 111 | + case .headReceived(let promise): |
| 112 | + self.state = .completed |
| 113 | + promise.succeed(()) |
| 114 | + case .failed: |
| 115 | + // ran into an error before... ignore this one |
| 116 | + break |
| 117 | + case .initialized, .connectSent, .completed: |
| 118 | + preconditionFailure("Invalid state") |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + func sendConnect(context: ChannelHandlerContext) { |
| 124 | + guard case .initialized(let promise) = self.state else { |
| 125 | + preconditionFailure("Invalid state") |
| 126 | + } |
| 127 | + |
| 128 | + self.state = .connectSent(promise) |
| 129 | + |
| 130 | + var head = HTTPRequestHead( |
| 131 | + version: .init(major: 1, minor: 1), |
| 132 | + method: .CONNECT, |
| 133 | + uri: "\(self.targetHost):\(self.targetPort)" |
| 134 | + ) |
| 135 | + head.headers.add(name: "proxy-connection", value: "keep-alive") |
| 136 | + if let authorization = self.proxyAuthorization { |
| 137 | + head.headers.add(name: "proxy-authorization", value: authorization.headerValue) |
| 138 | + } |
| 139 | + context.write(self.wrapOutboundOut(.head(head)), promise: nil) |
| 140 | + context.write(self.wrapOutboundOut(.end(nil)), promise: nil) |
| 141 | + context.flush() |
| 142 | + } |
| 143 | +} |
0 commit comments