mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-24 04:38:00 +00:00
Motivation: Without knowing where an error was thrown it can be unnecessarily difficult to work out the reasons an application ended up in an error state. Modifications: - Add a 'file' string to each of the 'NIOHTTP2Errors' denoting the file and line where an error was created - Add static methods to create errors to 'NIOHTTP2Errors' defaulting file and line, this is unfortunately necessary as defaulting file and line in the `init` makes the defaulted and non-defaulted errors indistinguishable, and the compiler will pick the one with fewest defaulted values. - Add equatable conformance manually so as to ignore 'file' - Add backing storage to errors which no longer be stored in an existential container - Add 'CustomStringConvertible' conformance to the errors using backing storage so that the 'file' is actually visible when converting the error - Deprecate the old `init`s - Fix all deprecation warnings Result: - NIOHTTP2Errors carry a 'file' with them indicating where they were created
66 lines
2.2 KiB
Swift
66 lines
2.2 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 {
|
|
private 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) {
|
|
self.expectedContentLength = headers[canonicalForm: "content-length"].first.flatMap { Int($0, radix: 10) }
|
|
}
|
|
|
|
/// 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)))"
|
|
}
|
|
}
|