From 80e0191048375b4695a4adce1b31993461a426da Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Mon, 28 Sep 2020 12:11:17 +0200 Subject: [PATCH 1/2] Close the connection during the head phase. --- .../KituraNet/HTTP/HTTPRequestHandler.swift | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Sources/KituraNet/HTTP/HTTPRequestHandler.swift b/Sources/KituraNet/HTTP/HTTPRequestHandler.swift index a57f2629..d1819168 100644 --- a/Sources/KituraNet/HTTP/HTTPRequestHandler.swift +++ b/Sources/KituraNet/HTTP/HTTPRequestHandler.swift @@ -79,6 +79,22 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle if errorResponseSent { return } switch request { case .head(let header): + server.connectionCount.add(1) + if let connectionLimit = server.options.connectionLimit, + server.connectionCount.load() > connectionLimit { + // Reaching connection limit: closing now. + do { + if let (httpStatus, response) = server.options.connectionResponseGenerator(connectionLimit,serverRequest?.remoteAddress ?? "") { + serverResponse = HTTPServerResponse(channel: context.channel, handler: self) + errorResponseSent = true + try serverResponse?.end(with: httpStatus, message: response) + } + } catch { + Log.error("Failed to send error response") + } + context.close() + return + } serverRequest = HTTPServerRequest(channel: context.channel, requestHead: header, enableSSL: enableSSLVerification) if let requestSizeLimit = server.options.requestSizeLimit, let contentLength = header.headers["Content-Length"].first, @@ -125,20 +141,6 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle case .end: requestSize = 0 - server.connectionCount.add(1) - if let connectionLimit = server.options.connectionLimit { - if server.connectionCount.load() > connectionLimit { - do { - if let (httpStatus, response) = server.options.connectionResponseGenerator(connectionLimit,serverRequest?.remoteAddress ?? "") { - serverResponse = HTTPServerResponse(channel: context.channel, handler: self) - errorResponseSent = true - try serverResponse?.end(with: httpStatus, message: response) - } - } catch { - Log.error("Failed to send error response") - } - } - } serverResponse = HTTPServerResponse(channel: context.channel, handler: self) //Make sure we use the latest delegate registered with the server DispatchQueue.global().async { From 987b7b3b8edaa5dff47d3055503d1d1acb6eea5c Mon Sep 17 00:00:00 2001 From: Mathieu Barnachon Date: Mon, 28 Sep 2020 13:00:41 +0200 Subject: [PATCH 2/2] Fix result warnings. --- Sources/KituraNet/HTTP/HTTPRequestHandler.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/KituraNet/HTTP/HTTPRequestHandler.swift b/Sources/KituraNet/HTTP/HTTPRequestHandler.swift index d1819168..2cfba67f 100644 --- a/Sources/KituraNet/HTTP/HTTPRequestHandler.swift +++ b/Sources/KituraNet/HTTP/HTTPRequestHandler.swift @@ -79,7 +79,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle if errorResponseSent { return } switch request { case .head(let header): - server.connectionCount.add(1) + _ = server.connectionCount.add(1) if let connectionLimit = server.options.connectionLimit, server.connectionCount.load() > connectionLimit { // Reaching connection limit: closing now. @@ -92,7 +92,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle } catch { Log.error("Failed to send error response") } - context.close() + context.close(promise: nil) return } serverRequest = HTTPServerRequest(channel: context.channel, requestHead: header, enableSSL: enableSSLVerification) @@ -109,7 +109,7 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle } catch { Log.error("Failed to send error response") } - context.close() + context.close(promise: nil) } } serverRequest = HTTPServerRequest(channel: context.channel, requestHead: header, enableSSL: enableSSLVerification) @@ -203,6 +203,6 @@ internal class HTTPRequestHandler: ChannelInboundHandler, RemovableChannelHandle } func channelInactive(context: ChannelHandlerContext) { - server.connectionCount.sub(1) + _ = server.connectionCount.sub(1) } }