Skip to content

Commit

Permalink
Do not allocate String to check if HTTP message is keep alive (#402)
Browse files Browse the repository at this point in the history
Motivation:

We can check if a HTTP message is using keep alive without any allocation.

Modifications:

Directly act on the internal storage of HTTPHeaders to find out if keep alive is used or not.

Result:

Less allocations
  • Loading branch information
normanmaurer authored and Lukasa committed May 16, 2018
1 parent a6239a9 commit e029ede
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Sources/NIOHTTP1/HTTPTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import NIO
let crlf: StaticString = "\r\n"
let headerSeparator: StaticString = ": "

private let connectionUtf8 = "connection".utf8

// Keep track of keep alive state.
internal enum KeepAliveState {
Expand Down Expand Up @@ -298,15 +297,16 @@ private extension UInt8 {
case .keepAlive:
return true
case .unknown:
guard let connection = self["connection"].first?.lowercased() else {
// HTTP 1.1 use keep-alive by default if not otherwise told.
return version.major == 1 && version.minor == 1
}

if connection == "close" {
return false
for header in self.headers {
if self.buffer.equalCaseInsensitiveASCII(view: "connection".utf8, at: header.name) {
if self.buffer.equalCaseInsensitiveASCII(view: "close".utf8, at: header.value) {
return false
}
return self.buffer.equalCaseInsensitiveASCII(view: "keep-alive".utf8, at: header.value)
}
}
return connection == "keep-alive"
// HTTP 1.1 use keep-alive by default if not otherwise told.
return version.major == 1 && version.minor >= 1
}
}
}
Expand Down Expand Up @@ -415,7 +415,7 @@ public struct HTTPHeaders: CustomStringConvertible {
}

private func isConnectionHeader(_ header: HTTPHeaderIndex) -> Bool {
return self.buffer.equalCaseInsensitiveASCII(view: connectionUtf8, at: header)
return self.buffer.equalCaseInsensitiveASCII(view: "connection".utf8, at: header)
}

/// Add a header name/value pair to the block.
Expand Down

0 comments on commit e029ede

Please sign in to comment.