@@ -39,6 +39,7 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
3939 private var upgradeState : State = State . idle
4040 private var logger : Logger
4141 private var targetHost : String ?
42+ private var targetPort : Int ?
4243 private static let globalRequestID = ManagedAtomic < Int > ( 0 ) // FIXME: should initialize with latest saved ID
4344 public var requestParts : [ HTTPClientRequestPart ] = [ ]
4445 private var waitingContext : ChannelHandlerContext ?
@@ -61,7 +62,11 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
6162 case connectRequested
6263 }
6364
64- public func forwardRequestForProtocol( httpProtocol: HttpProtocol , port: Int , context: ChannelHandlerContext , requestParts: [ HTTPClientRequestPart ] ) {
65+ public func forwardRequestForProtocol( httpProtocol: HttpProtocol , context: ChannelHandlerContext , requestParts: [ HTTPClientRequestPart ] ) {
66+ guard let port = self . targetPort else {
67+ fatalError ( " Port was not passed " )
68+ }
69+
6570 if httpProtocol == . HTTPS {
6671 forwardRequestForHttps ( context: context, port: port, requestParts: requestParts)
6772 } else {
@@ -233,7 +238,7 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
233238 }
234239 }
235240 } else {
236- forwardRequestForProtocol ( httpProtocol: httpProtocol, port : port , context: context, requestParts: requestParts)
241+ forwardRequestForProtocol ( httpProtocol: httpProtocol, context: context, requestParts: requestParts)
237242 }
238243 }
239244 }
@@ -258,8 +263,6 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
258263 switch ( reqPart) {
259264 case . head( var head) :
260265 if head. method == . CONNECT {
261- let pid : Int32 = ProcessInfo . processInfo. processIdentifier
262- print ( " pid: \( pid) " )
263266 upgradeState = . connectRequested
264267 handleConnectRequest ( context: context, head: & head)
265268 } else {
@@ -274,7 +277,8 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
274277 guard let newHost else {
275278 return
276279 }
277- targetHost = newHost
280+ self . targetHost = newHost
281+ self . targetPort = originalURI. port ?? 80
278282
279283 head. headers. replaceOrAdd ( name: " Host " , value: newHost)
280284 head. uri = originalURI. relativePath
@@ -289,10 +293,10 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
289293 private func handleConnectRequest( context: ChannelHandlerContext , head: inout HTTPRequestHead ) {
290294 let uriComponents = head. uri. split ( separator: " : " , maxSplits: 1 , omittingEmptySubsequences: false )
291295 self . targetHost = String ( uriComponents. first!)
296+ self . targetPort = Int ( uriComponents. last!)
292297
293298 guard let targetHost = self . targetHost,
294- let targetPort = uriComponents. last,
295- let targetPortInt = Int ( targetPort) else {
299+ let _ = self . targetPort else {
296300 sendHttpResponse ( ctx: context, status: . badRequest)
297301 context. close ( promise: nil )
298302 return
@@ -382,8 +386,8 @@ final class ProxyHandler: ChannelInboundHandler, RemovableChannelHandler, Equata
382386 let updatedRequestParts = parseRawRequest ( rawRequest: rawRequest)
383387
384388 // Forward the updated request
385- // FIXME: don't hardcode
386- forwardRequestForProtocol ( httpProtocol: . HTTP, port : 80 , context: waitingContext!, requestParts: updatedRequestParts)
389+ // FIXME: don't hardcode used protocol
390+ forwardRequestForProtocol ( httpProtocol: . HTTP, context: waitingContext!, requestParts: updatedRequestParts)
387391 }
388392
389393 private func parseRawRequest( rawRequest: String ) -> [ HTTPClientRequestPart ] {
0 commit comments