mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-28 05:14:25 +00:00
Motivation: We don't always require an array of header values to be extracted. Sometimes we just need to be able to iterate through them. For 'regular' header values we incur an allocation for the array, for values in their canonical form we incure at least two allocations. We can do better than this by providing a header value sequence. Modifications: - Add new API to `HPACKHeaders` for getting a sequence of header values which can optionally be in their canonical form. - Update the API of the two existing subscripts to use this new API. For `subscript(canonicalForm:)` we save an allocation by not going via `subscript(name:)`. Result: - Fewer allocations in `subscript(canonicalForm:)` - Header values can be iterated without allocating
85 lines
2.8 KiB
Swift
85 lines
2.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
import NIOHPACK
|
|
|
|
/// An object that verifies that a content-length field on a HTTP request or
|
|
/// response is respected.
|
|
struct ContentLengthVerifier {
|
|
internal var expectedContentLength: Int?
|
|
}
|
|
|
|
extension ContentLengthVerifier {
|
|
/// A chunk of data has been received from the network.
|
|
mutating func receivedDataChunk(length: Int) throws {
|
|
assert(length >= 0, "received data chunks must be positive")
|
|
|
|
// If there was no content-length, don't keep track.
|
|
guard let expectedContentLength = self.expectedContentLength else {
|
|
return
|
|
}
|
|
|
|
let newContentLength = expectedContentLength - length
|
|
if newContentLength < 0 {
|
|
throw NIOHTTP2Errors.contentLengthViolated()
|
|
}
|
|
self.expectedContentLength = newContentLength
|
|
}
|
|
|
|
/// Called when end of stream has been received. Validates that the complete body was received.
|
|
func endOfStream() throws {
|
|
switch self.expectedContentLength {
|
|
case .none, .some(0):
|
|
break
|
|
default:
|
|
throw NIOHTTP2Errors.contentLengthViolated()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ContentLengthVerifier {
|
|
internal init(_ headers: HPACKHeaders) throws {
|
|
let contentLengths = headers.values(forHeader: "content-length", canonicalForm: true)
|
|
var iterator = contentLengths.makeIterator()
|
|
guard let first = iterator.next() else {
|
|
return
|
|
}
|
|
// multiple content-length headers are permitted as long as they agree
|
|
while let next = iterator.next() {
|
|
if next != first {
|
|
throw NIOHTTP2Errors.contentLengthHeadersMismatch()
|
|
}
|
|
}
|
|
|
|
self.expectedContentLength = Int(first, radix: 10)
|
|
|
|
guard let expectedLength = self.expectedContentLength else {
|
|
throw NIOHTTP2Errors.contentLengthHeaderMalformedValue()
|
|
}
|
|
if expectedLength < 0 {
|
|
throw NIOHTTP2Errors.contentLengthHeaderNegative()
|
|
}
|
|
}
|
|
|
|
/// The verifier for use when content length verification is disabled.
|
|
internal static var disabled: ContentLengthVerifier {
|
|
return ContentLengthVerifier(expectedContentLength: nil)
|
|
}
|
|
}
|
|
|
|
extension ContentLengthVerifier: CustomStringConvertible {
|
|
var description: String {
|
|
return "ContentLengthVerifier(length: \(String(describing: self.expectedContentLength)))"
|
|
}
|
|
}
|