mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-23 04:34:24 +00:00
Stacks on #554 — the diff will show that commit too until it merges. ## Motivation `isValidPseudoHeaderValue` rejects CR, LF and NUL, which is narrower than the HTTP/1.1 request line actually needs. `:path` ends up as the request-target in: ``` METHOD SP request-target SP HTTP-version CRLF ``` so `:path = "/a HTTP/1.1"` serializes to `GET /a HTTP/1.1 HTTP/1.1`. Whether a recipient reads the first or the last SP-delimited token as the version decides what the request-target actually is, which is the usual parser-differential setup. RFC 9112 § 3.2 wants SP in a request-target percent-encoded, and bare HTAB and the other CTLs aren't valid there either. None of the HTTP/2 pseudo-headers have a grammar that allows SP or a CTL: `:method` and `:protocol` are tokens, `:scheme` is a URI scheme, `:authority` is host[:port], `:path` is a request-target, `:status` is 3DIGIT. So rejecting them doesn't reject anything well-formed. Same severity caveat as #554: NIOHTTP1's `uriOnlyContainsAllowedCharacters` already rejects SP in a request-target by default, so this is hardening rather than something exploitable on a stock pipeline. ## Modifications `isValidPseudoHeaderValue` goes from `{0x00, 0x0A, 0x0D}` to `<= 0x20 || == 0x7F`, so the whole CTL range plus SP and DEL. Bytes >= 0x80 stay allowed on purpose. They aren't delimiters in a request line or header block, and rejecting them would break anyone sending unencoded UTF-8 in `:path`. ### One behaviour change worth flagging `validateRequestBlock` runs on send as well as receive, so a client passing a URI with a raw space in it will now throw where it didn't before. I'd argue that's the right outcome, since NIOHTTP1's validator already rejects that URI by default and this makes the two layers agree, but it is a behaviour change and it's the bit most likely to need discussion. That's why I split it out of #554 instead of bundling them. ## Result Pseudo-header values containing SP or any control character are rejected. ## Testing Added to `HTTP2FramePayloadToHTTP1CodecCRLFTests.swift`: - `:path` with SP (`/a HTTP/1.1`), HTAB, VT, FF, DEL, SOH - SP in `:authority`, `:method`, `:scheme` - covered at the predicate, request-validation and server-codec levels The existing positive cases in that file still pass untouched — `/`, `/foo/bar`, `/foo?q=1&r=2`, `/foo#fragment`, `*`, `/foo%20bar`, `200` — which is the check that this doesn't reject anything legitimate. Built and ran on Linux, Swift 6.1 aarch64. `swift test -Xswiftc -warnings-as-errors` is clean with no new warnings, the new tests pass, and `swift format lint --strict Sources/ Tests/` is clean. --------- Co-authored-by: George Barnett <gbarnett@apple.com>
922 lines
39 KiB
Swift
922 lines
39 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2026 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 NIOCore
|
|
import NIOHPACK
|
|
import NIOHTTP1
|
|
|
|
// MARK: - Client
|
|
|
|
private struct BaseClientCodec {
|
|
private let protocolString: String
|
|
private let normalizeHTTPHeaders: Bool
|
|
|
|
private var headerStateMachine: HTTP2HeadersStateMachine = HTTP2HeadersStateMachine(mode: .client)
|
|
private var outgoingHTTP1RequestHead: HTTPRequestHead?
|
|
|
|
struct PendingFrameWrite {
|
|
var frame: HTTP2Frame.FramePayload
|
|
var promise: EventLoopPromise<Void>?
|
|
|
|
init(_ frame: HTTP2Frame.FramePayload, _ promise: EventLoopPromise<Void>?) {
|
|
self.frame = frame
|
|
self.promise = promise
|
|
}
|
|
}
|
|
|
|
/// Caches the most recent outbound frame until flush or .end is received.
|
|
/// This allows us to set endStream=true on the final data/headers frame
|
|
/// instead of sending a separate empty data frame just to signal stream closure.
|
|
private var pendingFrameWrite: PendingFrameWrite?
|
|
|
|
/// Initializes a `BaseClientCodec`.
|
|
///
|
|
/// - Parameters:
|
|
/// - httpProtocol: The protocol (usually `"http"` or `"https"` that is used).
|
|
/// - normalizeHTTPHeaders: Whether to automatically normalize the HTTP headers to be suitable for HTTP/2.
|
|
/// The normalization will for example lower-case all header names (as required by the
|
|
/// HTTP/2 spec) and remove headers that are unsuitable for HTTP/2 such as
|
|
/// headers related to HTTP/1's keep-alive behaviour. Unless you are sure that all your
|
|
/// headers conform to the HTTP/2 spec, you should leave this parameter set to `true`.
|
|
fileprivate init(httpProtocol: HTTP2FramePayloadToHTTP1ClientCodec.HTTPProtocol, normalizeHTTPHeaders: Bool) {
|
|
self.normalizeHTTPHeaders = normalizeHTTPHeaders
|
|
|
|
switch httpProtocol {
|
|
case .http:
|
|
self.protocolString = "http"
|
|
case .https:
|
|
self.protocolString = "https"
|
|
}
|
|
}
|
|
|
|
mutating func processInboundData(
|
|
_ data: HTTP2Frame.FramePayload
|
|
) throws -> (first: HTTPClientResponsePart?, second: HTTPClientResponsePart?) {
|
|
switch data {
|
|
case .headers(let headerContent):
|
|
switch try self.headerStateMachine.newHeaders(block: headerContent.headers) {
|
|
case .trailer:
|
|
return (first: .end(HTTPHeaders(regularHeadersFrom: headerContent.headers)), second: nil)
|
|
|
|
case .informationalResponseHead:
|
|
return (first: .head(try HTTPResponseHead(http2HeaderBlock: headerContent.headers)), second: nil)
|
|
|
|
case .finalResponseHead:
|
|
guard let outgoingHTTP1RequestHead = outgoingHTTP1RequestHead else {
|
|
preconditionFailure("Expected not to get a response without having sent a request")
|
|
}
|
|
self.outgoingHTTP1RequestHead = nil
|
|
let respHead = try HTTPResponseHead(
|
|
http2HeaderBlock: headerContent.headers,
|
|
requestHead: outgoingHTTP1RequestHead,
|
|
endStream: headerContent.endStream
|
|
)
|
|
let first = HTTPClientResponsePart.head(respHead)
|
|
var second: HTTPClientResponsePart? = nil
|
|
if headerContent.endStream {
|
|
second = .end(nil)
|
|
}
|
|
return (first: first, second: second)
|
|
|
|
case .requestHead:
|
|
preconditionFailure("A client can not receive request heads")
|
|
}
|
|
case .data(let content):
|
|
guard case .byteBuffer(let b) = content.data else {
|
|
preconditionFailure("Received DATA frame with non-bytebuffer IOData")
|
|
}
|
|
|
|
var first = HTTPClientResponsePart.body(b)
|
|
var second: HTTPClientResponsePart? = nil
|
|
if content.endStream {
|
|
if b.readableBytes == 0 {
|
|
first = .end(nil)
|
|
} else {
|
|
second = .end(nil)
|
|
}
|
|
}
|
|
return (first: first, second: second)
|
|
case .alternativeService, .rstStream, .priority, .windowUpdate, .settings, .pushPromise, .ping, .goAway,
|
|
.origin:
|
|
// These don't have an HTTP/1 equivalent, so let's drop them.
|
|
return (first: nil, second: nil)
|
|
}
|
|
}
|
|
|
|
mutating func processOutboundData(
|
|
_ data: HTTPClientRequestPart,
|
|
promise: EventLoopPromise<Void>?
|
|
) throws -> (first: PendingFrameWrite?, second: PendingFrameWrite?) {
|
|
switch data {
|
|
case .head(let head):
|
|
precondition(self.outgoingHTTP1RequestHead == nil, "Only a single HTTP request allowed per HTTP2 stream")
|
|
let h1Headers = try HTTPHeaders(requestHead: head, protocolString: self.protocolString)
|
|
self.outgoingHTTP1RequestHead = head
|
|
let headerContent = HTTP2Frame.FramePayload.Headers(
|
|
headers: HPACKHeaders(
|
|
httpHeaders: h1Headers,
|
|
normalizeHTTPHeaders: self.normalizeHTTPHeaders
|
|
)
|
|
)
|
|
self.pendingFrameWrite = .init(.headers(headerContent), promise)
|
|
return (nil, nil)
|
|
|
|
case .body(let body):
|
|
let cached = self.pendingFrameWrite
|
|
self.pendingFrameWrite = .init(.data(HTTP2Frame.FramePayload.Data(data: body)), promise)
|
|
return (cached, nil)
|
|
|
|
case .end(let trailers):
|
|
defer { self.pendingFrameWrite = nil }
|
|
|
|
switch (self.pendingFrameWrite?.frame, trailers) {
|
|
case (.none, .none):
|
|
return (.init(.data(.init(data: .byteBuffer(ByteBuffer()), endStream: true)), promise), nil)
|
|
|
|
case (.none, .some(let trailers)):
|
|
return (.init(self.makeH2TrailerFramePayload(trailers), promise), nil)
|
|
|
|
case (.data(var data), .none):
|
|
data.endStream = true
|
|
var pendingPromise = self.pendingFrameWrite!.promise
|
|
pendingPromise.setOrCascade(to: promise)
|
|
return (.init(.data(data), pendingPromise), nil)
|
|
|
|
case (.data(let data), .some(let trailers)):
|
|
let trailerFrame = self.makeH2TrailerFramePayload(trailers)
|
|
return (
|
|
.init(.data(data), self.pendingFrameWrite!.promise),
|
|
.init(trailerFrame, promise)
|
|
)
|
|
|
|
case (.headers(var headers), .none):
|
|
headers.endStream = true
|
|
var pendingPromise = self.pendingFrameWrite!.promise
|
|
pendingPromise.setOrCascade(to: promise)
|
|
return (.init(.headers(headers), pendingPromise), nil)
|
|
|
|
case (.headers(let headers), .some(let trailers)):
|
|
let trailers = self.makeH2TrailerFramePayload(trailers)
|
|
return (
|
|
.init(.headers(headers), self.pendingFrameWrite!.promise),
|
|
.init(trailers, promise)
|
|
)
|
|
|
|
case (.priority, _), (.rstStream, _), (.settings, _), (.pushPromise, _), (.ping, _), (.goAway, _),
|
|
(.windowUpdate, _), (.alternativeService, _), (.origin, _):
|
|
fatalError("Only header and data frames are cached here")
|
|
}
|
|
}
|
|
}
|
|
|
|
mutating func clearCache() -> PendingFrameWrite? {
|
|
defer { self.pendingFrameWrite = nil }
|
|
return self.pendingFrameWrite
|
|
}
|
|
|
|
private func makeH2TrailerFramePayload(_ trailers: HTTPHeaders) -> HTTP2Frame.FramePayload {
|
|
HTTP2Frame.FramePayload.headers(
|
|
.init(
|
|
headers: HPACKHeaders(
|
|
httpHeaders: trailers,
|
|
normalizeHTTPHeaders: self.normalizeHTTPHeaders
|
|
),
|
|
endStream: true
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
/// A simple channel handler that translates HTTP/2 concepts into HTTP/1 data types,
|
|
/// and vice versa, for use on the client side.
|
|
///
|
|
/// This channel handler should be used alongside the ``HTTP2StreamMultiplexer`` to
|
|
/// help provide a HTTP/1.1-like abstraction on top of a HTTP/2 multiplexed
|
|
/// connection.
|
|
@available(*, deprecated, renamed: "HTTP2FramePayloadToHTTP1ClientCodec")
|
|
public final class HTTP2ToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutboundHandler {
|
|
public typealias InboundIn = HTTP2Frame
|
|
public typealias InboundOut = HTTPClientResponsePart
|
|
|
|
public typealias OutboundIn = HTTPClientRequestPart
|
|
public typealias OutboundOut = HTTP2Frame
|
|
|
|
/// The HTTP protocol scheme being used on this connection.
|
|
public typealias HTTPProtocol = HTTP2FramePayloadToHTTP1ClientCodec.HTTPProtocol
|
|
|
|
private let streamID: HTTP2StreamID
|
|
private var baseCodec: BaseClientCodec
|
|
|
|
/// Initializes a ``HTTP2ToHTTP1ClientCodec`` for the given ``HTTP2StreamID``.
|
|
///
|
|
/// - Parameters:
|
|
/// - streamID: The HTTP/2 stream ID this ``HTTP2ToHTTP1ClientCodec`` will be used for
|
|
/// - httpProtocol: The protocol (usually `"http"` or `"https"` that is used).
|
|
/// - normalizeHTTPHeaders: Whether to automatically normalize the HTTP headers to be suitable for HTTP/2.
|
|
/// The normalization will for example lower-case all header names (as required by the
|
|
/// HTTP/2 spec) and remove headers that are unsuitable for HTTP/2 such as
|
|
/// headers related to HTTP/1's keep-alive behaviour. Unless you are sure that all your
|
|
/// headers conform to the HTTP/2 spec, you should leave this parameter set to `true`.
|
|
public init(streamID: HTTP2StreamID, httpProtocol: HTTPProtocol, normalizeHTTPHeaders: Bool) {
|
|
self.streamID = streamID
|
|
self.baseCodec = BaseClientCodec(httpProtocol: httpProtocol, normalizeHTTPHeaders: normalizeHTTPHeaders)
|
|
}
|
|
|
|
/// Initializes a ``HTTP2ToHTTP1ClientCodec`` for the given ``HTTP2StreamID``.
|
|
///
|
|
/// - Parameters:
|
|
/// - streamID: The HTTP/2 stream ID this ``HTTP2ToHTTP1ClientCodec`` will be used for
|
|
/// - httpProtocol: The protocol (usually `"http"` or `"https"` that is used).
|
|
public convenience init(streamID: HTTP2StreamID, httpProtocol: HTTPProtocol) {
|
|
self.init(streamID: streamID, httpProtocol: httpProtocol, normalizeHTTPHeaders: true)
|
|
}
|
|
|
|
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
let frame = self.unwrapInboundIn(data)
|
|
do {
|
|
let (first, second) = try self.baseCodec.processInboundData(frame.payload)
|
|
if let first = first {
|
|
context.fireChannelRead(self.wrapInboundOut(first))
|
|
}
|
|
if let second = second {
|
|
context.fireChannelRead(self.wrapInboundOut(second))
|
|
}
|
|
} catch {
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
|
let responsePart = self.unwrapOutboundIn(data)
|
|
|
|
do {
|
|
let (first, second) = try self.baseCodec.processOutboundData(responsePart, promise: promise)
|
|
if let first = first {
|
|
let part = HTTP2Frame(streamID: self.streamID, payload: first.frame)
|
|
context.write(self.wrapOutboundOut(part), promise: first.promise)
|
|
if let second = second {
|
|
let part = HTTP2Frame(streamID: self.streamID, payload: second.frame)
|
|
context.write(self.wrapOutboundOut(part), promise: second.promise)
|
|
}
|
|
}
|
|
} catch {
|
|
promise?.fail(error)
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
public func flush(context: ChannelHandlerContext) {
|
|
if let pending = self.baseCodec.clearCache() {
|
|
let part = HTTP2Frame(streamID: self.streamID, payload: pending.frame)
|
|
context.write(self.wrapOutboundOut(part), promise: pending.promise)
|
|
}
|
|
context.flush()
|
|
}
|
|
|
|
public func errorCaught(context: ChannelHandlerContext, error: any Error) {
|
|
if let pending = self.baseCodec.clearCache() {
|
|
pending.promise?.fail(error)
|
|
}
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension HTTP2ToHTTP1ClientCodec: Sendable {}
|
|
|
|
/// A simple channel handler that translates HTTP/2 concepts into HTTP/1 data types,
|
|
/// and vice versa, for use on the client side.
|
|
///
|
|
/// This channel handler should be used alongside the ``HTTP2StreamMultiplexer`` to
|
|
/// help provide a HTTP/1.1-like abstraction on top of a HTTP/2 multiplexed
|
|
/// connection.
|
|
///
|
|
/// This handler uses ``HTTP2Frame/FramePayload`` as its HTTP/2 currency type.
|
|
public final class HTTP2FramePayloadToHTTP1ClientCodec: ChannelInboundHandler, ChannelOutboundHandler {
|
|
public typealias InboundIn = HTTP2Frame.FramePayload
|
|
public typealias InboundOut = HTTPClientResponsePart
|
|
|
|
public typealias OutboundIn = HTTPClientRequestPart
|
|
public typealias OutboundOut = HTTP2Frame.FramePayload
|
|
|
|
private var baseCodec: BaseClientCodec
|
|
|
|
/// The HTTP protocol scheme being used on this connection.
|
|
public enum HTTPProtocol: Sendable, Hashable {
|
|
case https
|
|
case http
|
|
}
|
|
|
|
/// Initializes a ``HTTP2FramePayloadToHTTP1ClientCodec``.
|
|
///
|
|
/// - Parameters:
|
|
/// - httpProtocol: The protocol (usually `"http"` or `"https"` that is used).
|
|
/// - normalizeHTTPHeaders: Whether to automatically normalize the HTTP headers to be suitable for HTTP/2.
|
|
/// The normalization will for example lower-case all header names (as required by the
|
|
/// HTTP/2 spec) and remove headers that are unsuitable for HTTP/2 such as
|
|
/// headers related to HTTP/1's keep-alive behaviour. Unless you are sure that all your
|
|
/// headers conform to the HTTP/2 spec, you should leave this parameter set to `true`.
|
|
public init(httpProtocol: HTTPProtocol, normalizeHTTPHeaders: Bool = true) {
|
|
self.baseCodec = BaseClientCodec(httpProtocol: httpProtocol, normalizeHTTPHeaders: normalizeHTTPHeaders)
|
|
}
|
|
|
|
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
let payload = self.unwrapInboundIn(data)
|
|
do {
|
|
let (first, second) = try self.baseCodec.processInboundData(payload)
|
|
if let first = first {
|
|
context.fireChannelRead(self.wrapInboundOut(first))
|
|
}
|
|
if let second = second {
|
|
context.fireChannelRead(self.wrapInboundOut(second))
|
|
}
|
|
} catch {
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
|
let requestPart = self.unwrapOutboundIn(data)
|
|
|
|
do {
|
|
let (first, second) = try self.baseCodec.processOutboundData(
|
|
requestPart,
|
|
promise: promise
|
|
)
|
|
if let first {
|
|
context.write(self.wrapOutboundOut(first.frame), promise: first.promise)
|
|
if let second {
|
|
context.write(self.wrapOutboundOut(second.frame), promise: second.promise)
|
|
}
|
|
}
|
|
} catch {
|
|
promise?.fail(error)
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
public func flush(context: ChannelHandlerContext) {
|
|
if let pending = self.baseCodec.clearCache() {
|
|
context.write(self.wrapOutboundOut(pending.frame), promise: pending.promise)
|
|
}
|
|
context.flush()
|
|
}
|
|
|
|
public func errorCaught(context: ChannelHandlerContext, error: any Error) {
|
|
if let pending = self.baseCodec.clearCache() {
|
|
pending.promise?.fail(error)
|
|
}
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension HTTP2FramePayloadToHTTP1ClientCodec: Sendable {}
|
|
|
|
// MARK: - Server
|
|
|
|
private struct BaseServerCodec {
|
|
private let normalizeHTTPHeaders: Bool
|
|
private var headerStateMachine: HTTP2HeadersStateMachine = HTTP2HeadersStateMachine(mode: .server)
|
|
|
|
init(normalizeHTTPHeaders: Bool) {
|
|
self.normalizeHTTPHeaders = normalizeHTTPHeaders
|
|
}
|
|
|
|
mutating func processInboundData(
|
|
_ data: HTTP2Frame.FramePayload
|
|
) throws -> (first: HTTPServerRequestPart?, second: HTTPServerRequestPart?) {
|
|
switch data {
|
|
case .headers(let headerContent):
|
|
if case .trailer = try self.headerStateMachine.newHeaders(block: headerContent.headers) {
|
|
return (first: .end(HTTPHeaders(regularHeadersFrom: headerContent.headers)), second: nil)
|
|
} else {
|
|
let reqHead = try HTTPRequestHead(
|
|
http2HeaderBlock: headerContent.headers,
|
|
isEndStream: headerContent.endStream
|
|
)
|
|
|
|
let first = HTTPServerRequestPart.head(reqHead)
|
|
var second: HTTPServerRequestPart? = nil
|
|
if headerContent.endStream {
|
|
second = .end(nil)
|
|
}
|
|
return (first: first, second: second)
|
|
}
|
|
case .data(let dataContent):
|
|
guard case .byteBuffer(let b) = dataContent.data else {
|
|
preconditionFailure("Received non-byteBuffer IOData from network")
|
|
}
|
|
var first = HTTPServerRequestPart.body(b)
|
|
var second: HTTPServerRequestPart? = nil
|
|
if dataContent.endStream {
|
|
if b.readableBytes == 0 {
|
|
first = .end(nil)
|
|
} else {
|
|
second = .end(nil)
|
|
}
|
|
}
|
|
return (first: first, second: second)
|
|
default:
|
|
// Any other frame type is ignored.
|
|
return (first: nil, second: nil)
|
|
}
|
|
}
|
|
|
|
mutating func processOutboundData(
|
|
_ data: HTTPServerResponsePart,
|
|
allocator: ByteBufferAllocator
|
|
) -> HTTP2Frame.FramePayload {
|
|
switch data {
|
|
case .head(let head):
|
|
let h1 = HTTPHeaders(responseHead: head)
|
|
let payload = HTTP2Frame.FramePayload.Headers(
|
|
headers: HPACKHeaders(
|
|
httpHeaders: h1,
|
|
normalizeHTTPHeaders: self.normalizeHTTPHeaders
|
|
)
|
|
)
|
|
return .headers(payload)
|
|
case .body(let body):
|
|
let payload = HTTP2Frame.FramePayload.Data(data: body)
|
|
return .data(payload)
|
|
case .end(let trailers):
|
|
if let trailers = trailers {
|
|
return .headers(
|
|
.init(
|
|
headers: HPACKHeaders(
|
|
httpHeaders: trailers,
|
|
normalizeHTTPHeaders: self.normalizeHTTPHeaders
|
|
),
|
|
endStream: true
|
|
)
|
|
)
|
|
} else {
|
|
return .data(.init(data: .byteBuffer(allocator.buffer(capacity: 0)), endStream: true))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A simple channel handler that translates HTTP/2 concepts into HTTP/1 data types,
|
|
/// and vice versa, for use on the server side.
|
|
///
|
|
/// This channel handler should be used alongside the ``HTTP2StreamMultiplexer`` to
|
|
/// help provide a HTTP/1.1-like abstraction on top of a HTTP/2 multiplexed
|
|
/// connection.
|
|
@available(*, deprecated, renamed: "HTTP2FramePayloadToHTTP1ServerCodec")
|
|
public final class HTTP2ToHTTP1ServerCodec: ChannelInboundHandler, ChannelOutboundHandler {
|
|
public typealias InboundIn = HTTP2Frame
|
|
public typealias InboundOut = HTTPServerRequestPart
|
|
|
|
public typealias OutboundIn = HTTPServerResponsePart
|
|
public typealias OutboundOut = HTTP2Frame
|
|
|
|
private let streamID: HTTP2StreamID
|
|
private var baseCodec: BaseServerCodec
|
|
|
|
/// Initializes a ``HTTP2ToHTTP1ServerCodec`` for the given ``HTTP2StreamID``.
|
|
///
|
|
/// - Parameters:
|
|
/// - streamID: The HTTP/2 stream ID this ``HTTP2ToHTTP1ServerCodec`` will be used for
|
|
/// - normalizeHTTPHeaders: Whether to automatically normalize the HTTP headers to be suitable for HTTP/2.
|
|
/// The normalization will for example lower-case all header names (as required by the
|
|
/// HTTP/2 spec) and remove headers that are unsuitable for HTTP/2 such as
|
|
/// headers related to HTTP/1's keep-alive behaviour. Unless you are sure that all your
|
|
/// headers conform to the HTTP/2 spec, you should leave this parameter set to `true`.
|
|
public init(streamID: HTTP2StreamID, normalizeHTTPHeaders: Bool) {
|
|
self.streamID = streamID
|
|
self.baseCodec = BaseServerCodec(normalizeHTTPHeaders: normalizeHTTPHeaders)
|
|
}
|
|
|
|
public convenience init(streamID: HTTP2StreamID) {
|
|
self.init(streamID: streamID, normalizeHTTPHeaders: true)
|
|
}
|
|
|
|
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
let frame = self.unwrapInboundIn(data)
|
|
|
|
do {
|
|
let (first, second) = try self.baseCodec.processInboundData(frame.payload)
|
|
if let first = first {
|
|
context.fireChannelRead(self.wrapInboundOut(first))
|
|
}
|
|
if let second = second {
|
|
context.fireChannelRead(self.wrapInboundOut(second))
|
|
}
|
|
} catch {
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
|
let responsePart = self.unwrapOutboundIn(data)
|
|
let transformedPayload = self.baseCodec.processOutboundData(responsePart, allocator: context.channel.allocator)
|
|
let part = HTTP2Frame(streamID: self.streamID, payload: transformedPayload)
|
|
context.write(self.wrapOutboundOut(part), promise: promise)
|
|
}
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension HTTP2ToHTTP1ServerCodec: Sendable {}
|
|
|
|
/// A simple channel handler that translates HTTP/2 concepts into HTTP/1 data types,
|
|
/// and vice versa, for use on the server side.
|
|
///
|
|
/// This channel handler should be used alongside the ``HTTP2StreamMultiplexer`` to
|
|
/// help provide a HTTP/1.1-like abstraction on top of a HTTP/2 multiplexed
|
|
/// connection.
|
|
///
|
|
/// This handler uses ``HTTP2Frame/FramePayload`` as its HTTP/2 currency type.
|
|
public final class HTTP2FramePayloadToHTTP1ServerCodec: ChannelInboundHandler, ChannelOutboundHandler {
|
|
public typealias InboundIn = HTTP2Frame.FramePayload
|
|
public typealias InboundOut = HTTPServerRequestPart
|
|
|
|
public typealias OutboundIn = HTTPServerResponsePart
|
|
public typealias OutboundOut = HTTP2Frame.FramePayload
|
|
|
|
private var baseCodec: BaseServerCodec
|
|
|
|
/// Initializes a ``HTTP2FramePayloadToHTTP1ServerCodec``.
|
|
///
|
|
/// - Parameters:
|
|
/// - normalizeHTTPHeaders: Whether to automatically normalize the HTTP headers to be suitable for HTTP/2.
|
|
/// The normalization will for example lower-case all header names (as required by the
|
|
/// HTTP/2 spec) and remove headers that are unsuitable for HTTP/2 such as
|
|
/// headers related to HTTP/1's keep-alive behaviour. Unless you are sure that all your
|
|
/// headers conform to the HTTP/2 spec, you should leave this parameter set to `true`.
|
|
public init(normalizeHTTPHeaders: Bool = true) {
|
|
self.baseCodec = BaseServerCodec(normalizeHTTPHeaders: normalizeHTTPHeaders)
|
|
}
|
|
|
|
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
|
let payload = self.unwrapInboundIn(data)
|
|
|
|
do {
|
|
let (first, second) = try self.baseCodec.processInboundData(payload)
|
|
if let first = first {
|
|
context.fireChannelRead(self.wrapInboundOut(first))
|
|
}
|
|
if let second = second {
|
|
context.fireChannelRead(self.wrapInboundOut(second))
|
|
}
|
|
} catch {
|
|
context.fireErrorCaught(error)
|
|
}
|
|
}
|
|
|
|
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
|
|
let responsePart = self.unwrapOutboundIn(data)
|
|
let transformedPayload = self.baseCodec.processOutboundData(responsePart, allocator: context.channel.allocator)
|
|
context.write(self.wrapOutboundOut(transformedPayload), promise: promise)
|
|
}
|
|
}
|
|
|
|
@available(*, unavailable)
|
|
extension HTTP2FramePayloadToHTTP1ServerCodec: Sendable {}
|
|
|
|
extension HTTPMethod {
|
|
/// Create a `HTTPMethod` from the string representation of that method.
|
|
fileprivate init(methodString: String) {
|
|
switch methodString {
|
|
case "GET":
|
|
self = .GET
|
|
case "PUT":
|
|
self = .PUT
|
|
case "ACL":
|
|
self = .ACL
|
|
case "HEAD":
|
|
self = .HEAD
|
|
case "POST":
|
|
self = .POST
|
|
case "COPY":
|
|
self = .COPY
|
|
case "LOCK":
|
|
self = .LOCK
|
|
case "MOVE":
|
|
self = .MOVE
|
|
case "BIND":
|
|
self = .BIND
|
|
case "LINK":
|
|
self = .LINK
|
|
case "PATCH":
|
|
self = .PATCH
|
|
case "TRACE":
|
|
self = .TRACE
|
|
case "MKCOL":
|
|
self = .MKCOL
|
|
case "MERGE":
|
|
self = .MERGE
|
|
case "PURGE":
|
|
self = .PURGE
|
|
case "NOTIFY":
|
|
self = .NOTIFY
|
|
case "SEARCH":
|
|
self = .SEARCH
|
|
case "UNLOCK":
|
|
self = .UNLOCK
|
|
case "REBIND":
|
|
self = .REBIND
|
|
case "UNBIND":
|
|
self = .UNBIND
|
|
case "REPORT":
|
|
self = .REPORT
|
|
case "DELETE":
|
|
self = .DELETE
|
|
case "UNLINK":
|
|
self = .UNLINK
|
|
case "CONNECT":
|
|
self = .CONNECT
|
|
case "MSEARCH":
|
|
self = .MSEARCH
|
|
case "OPTIONS":
|
|
self = .OPTIONS
|
|
case "PROPFIND":
|
|
self = .PROPFIND
|
|
case "CHECKOUT":
|
|
self = .CHECKOUT
|
|
case "PROPPATCH":
|
|
self = .PROPPATCH
|
|
case "SUBSCRIBE":
|
|
self = .SUBSCRIBE
|
|
case "MKCALENDAR":
|
|
self = .MKCALENDAR
|
|
case "MKACTIVITY":
|
|
self = .MKACTIVITY
|
|
case "UNSUBSCRIBE":
|
|
self = .UNSUBSCRIBE
|
|
default:
|
|
self = .RAW(value: methodString)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK:- Methods for creating `HTTPRequestHead`/`HTTPResponseHead` objects from
|
|
// header blocks generated by the HTTP/2 layer.
|
|
extension HTTPRequestHead {
|
|
/// Create a `HTTPRequestHead` from the header block.
|
|
init(http2HeaderBlock headers: HPACKHeaders, isEndStream: Bool) throws {
|
|
// A request head should have only up to four psuedo-headers.
|
|
let method = HTTPMethod(methodString: try headers.peekPseudoHeader(name: ":method"))
|
|
let version = HTTPVersion(major: 2, minor: 0)
|
|
let uri = try headers.peekPseudoHeader(name: ":path")
|
|
|
|
// Here we peek :scheme just to confirm it's there: we want the throw effect, but we don't care about the value.
|
|
_ = try headers.peekPseudoHeader(name: ":scheme")
|
|
|
|
let authority = try headers.peekPseudoHeader(name: ":authority")
|
|
|
|
// We do a manual implementation of HTTPHeaders(regularHeadersFrom:) here because we may
|
|
// need to add an extra Host: and Transfer-Encoding: header here, and that can cause copies
|
|
// if we're unlucky. We need headers.count - 2 spaces: we remove :method, :path, :scheme,
|
|
// and :authority, but we may add in Host and Transfer-Encoding
|
|
var rawHeaders: [(String, String)] = []
|
|
rawHeaders.reserveCapacity(headers.count - 2)
|
|
if !headers.contains(name: "host") {
|
|
rawHeaders.append(("host", authority))
|
|
}
|
|
switch method {
|
|
case .GET, .HEAD, .DELETE, .CONNECT, .TRACE:
|
|
// A user agent SHOULD NOT send a Content-Length header field when the request
|
|
// message does not contain a payload body and the method semantics do not
|
|
// anticipate such a body.
|
|
//
|
|
// There is an interesting case here, in which the header frame does not have the
|
|
// endStream flag set, because the HTTP1 to HTTP2 translation on the client side, emits
|
|
// header frame and endStream in two frames. What do we want to do in those cases?
|
|
//
|
|
// The payload has no defined semantics as defined in RFC7231, but we are not sure, if
|
|
// we will receive a payload eventually. In most cases, there won't be a payload and
|
|
// therefore we should not add transfer-encoding headers. If there is a content-length
|
|
// header, we should keep it.
|
|
break
|
|
|
|
default:
|
|
if !headers.contains(name: "content-length") {
|
|
if isEndStream {
|
|
rawHeaders.append(("content-length", "0"))
|
|
} else {
|
|
rawHeaders.append(("transfer-encoding", "chunked"))
|
|
}
|
|
}
|
|
}
|
|
rawHeaders.appendRegularHeaders(from: headers)
|
|
|
|
self.init(version: version, method: method, uri: uri, headers: HTTPHeaders(rawHeaders))
|
|
}
|
|
}
|
|
|
|
extension HTTPResponseHead {
|
|
/// Create a `HTTPResponseHead` from the header block. Use this for informational responses.
|
|
init(http2HeaderBlock headers: HPACKHeaders) throws {
|
|
// A response head should have only one psuedo-header. We strip it off.
|
|
let statusHeader = try headers.peekPseudoHeader(name: ":status")
|
|
guard let integerStatus = Int(statusHeader, radix: 10) else {
|
|
throw NIOHTTP2Errors.invalidStatusValue(statusHeader)
|
|
}
|
|
let status = HTTPResponseStatus(statusCode: integerStatus)
|
|
self.init(version: .init(major: 2, minor: 0), status: status, headers: HTTPHeaders(regularHeadersFrom: headers))
|
|
}
|
|
|
|
/// Create a `HTTPResponseHead` from the header block. Use this for non-informational responses.
|
|
init(http2HeaderBlock headers: HPACKHeaders, requestHead: HTTPRequestHead, endStream: Bool) throws {
|
|
try self.init(http2HeaderBlock: headers)
|
|
|
|
// NOTE: We don't need to create the header array here ourselves. The HTTP/1 response
|
|
// headers, will not contain a :status field, but may contain a "Transfer-Encoding"
|
|
// field. For this reason the default allocation works great for us.
|
|
if self.shouldAddContentLengthOrTransferEncodingHeaderToResponse(
|
|
hpackHeaders: headers,
|
|
requestHead: requestHead
|
|
) {
|
|
if endStream {
|
|
self.headers.add(name: "content-length", value: "0")
|
|
} else {
|
|
self.headers.add(name: "transfer-encoding", value: "chunked")
|
|
}
|
|
}
|
|
}
|
|
|
|
private func shouldAddContentLengthOrTransferEncodingHeaderToResponse(
|
|
hpackHeaders: HPACKHeaders,
|
|
requestHead: HTTPRequestHead
|
|
) -> Bool {
|
|
let statusCode = self.status.code
|
|
return !(100..<200).contains(statusCode)
|
|
&& statusCode != 204
|
|
&& statusCode != 304
|
|
&& requestHead.method != .HEAD
|
|
&& requestHead.method != .CONNECT
|
|
&& !hpackHeaders.contains(name: "content-length") // compare on h2 headers - for speed
|
|
}
|
|
}
|
|
|
|
extension HPACKHeaders {
|
|
/// Grabs a pseudo-header from a header block. Does not remove it.
|
|
///
|
|
/// - parameter:
|
|
/// - name: The header name to find.
|
|
/// - Returns: The value for this pseudo-header.
|
|
/// - throws: If there is no such header, or multiple.
|
|
internal func peekPseudoHeader(name: String) throws -> String {
|
|
// This could be done with .lazy.filter.map but that generates way more ARC traffic.
|
|
var headerValue: String? = nil
|
|
|
|
for (fieldName, fieldValue, _) in self {
|
|
if name == fieldName {
|
|
guard headerValue == nil else {
|
|
throw NIOHTTP2Errors.duplicatePseudoHeader(name)
|
|
}
|
|
headerValue = fieldValue
|
|
}
|
|
}
|
|
|
|
if let headerValue = headerValue {
|
|
if !Self.isValidPseudoHeaderValue(headerValue) {
|
|
throw NIOHTTP2Errors.invalidPseudoHeaderValue(name: name, value: headerValue)
|
|
}
|
|
return headerValue
|
|
} else {
|
|
throw NIOHTTP2Errors.missingPseudoHeader(name)
|
|
}
|
|
}
|
|
|
|
static func isValidPseudoHeaderValue(_ value: String) -> Bool {
|
|
// Pseudo-header values must not contain CR, LF, or NUL bytes. These characters could
|
|
// enable HTTP/2-to-HTTP/1.1 request smuggling when the value is placed into an HTTP/1.1
|
|
// message (e.g. :path becomes the request-target).
|
|
//
|
|
// We reject the entire CTL range (0x00-0x1F), SP (0x20), and DEL (0x7F), rather than
|
|
// only CR/LF/NUL, for two reasons:
|
|
//
|
|
// 1. `:path` becomes the HTTP/1.1 request-target, which is serialized into the request
|
|
// line `METHOD SP request-target SP HTTP-version CRLF`. A bare SP inside `:path`
|
|
// therefore produces an ambiguous request line (e.g. `GET /a HTTP/1.1 HTTP/1.1`),
|
|
// which HTTP/1.1 parsers may split differently depending on whether they take the
|
|
// first or last SP-delimited token as the version. RFC 9112 § 3.2 requires any SP
|
|
// in a request-target to be percent-encoded. Bare HTAB and the other CTLs are
|
|
// likewise not permitted in a request-target.
|
|
//
|
|
// 2. No pseudo-header defined for HTTP/2 has a grammar admitting SP or a CTL:
|
|
// `:method` and `:protocol` are tokens (RFC 9110 § 5.6.2), `:scheme` is a URI scheme
|
|
// (RFC 3986 § 3.1), `:authority` is host[:port], `:path` is a request-target, and
|
|
// `:status` is 3DIGIT. Rejecting these bytes is therefore conformant and does not
|
|
// reject any well-formed value.
|
|
//
|
|
// Bytes >= 0x80 are deliberately still permitted: they are not delimiters in an
|
|
// HTTP/1.1 request line or header block, and rejecting them would break peers that
|
|
// send unencoded UTF-8 in `:path`.
|
|
!value.utf8.contains(where: { $0 <= 0x20 || $0 == 0x7F })
|
|
}
|
|
|
|
/// Whether this is a valid value for a regular (non-pseudo) HTTP/2 header field.
|
|
static func isValidFieldValue(_ value: String) -> Bool {
|
|
// RFC 9113 § 8.2.1 states:
|
|
//
|
|
// > A field value MUST NOT contain the zero value (ASCII NUL, 0x00), line feed
|
|
// > (ASCII LF, 0x0a), or carriage return (ASCII CR, 0x0d) at any position.
|
|
//
|
|
// and requires that a request or response containing such a value be treated as
|
|
// malformed. These bytes are smuggling-relevant for the same reason they are in a
|
|
// pseudo-header: when an HTTP/2-to-HTTP/1.1 translator serializes a field as
|
|
// `name: value CRLF`, an embedded CR or LF terminates the field early and injects
|
|
// attacker-controlled header lines — or an entire second request — into the
|
|
// downstream HTTP/1.1 message.
|
|
//
|
|
// Unlike pseudo-header values, SP, HTAB, and obs-text (0x80-0xFF) are permitted here,
|
|
// because RFC 9110 § 5.5 admits all of them inside a field value.
|
|
//
|
|
// Note: RFC 9113 § 8.2.1 additionally forbids a field value from *starting or ending*
|
|
// with SP or HTAB. That constraint is not enforced here, as it does not enable
|
|
// smuggling and rejecting it risks breaking peers that emit sloppy-but-harmless
|
|
// padding.
|
|
!value.utf8.contains(where: { $0 == 0x00 || $0 == 0x0A || $0 == 0x0D })
|
|
}
|
|
}
|
|
|
|
extension HTTPHeaders {
|
|
fileprivate init(requestHead: HTTPRequestHead, protocolString: String) throws {
|
|
// To avoid too much allocation we create an array first, and then initialize the HTTPHeaders from it.
|
|
// We want to ensure this array is large enough so we only have to allocate once. We will need an
|
|
// array that is the same as the number of headers in requestHead.headers + 3: we're adding :path,
|
|
// :method, and :scheme, and transforming Host to :authority.
|
|
var newHeaders: [(String, String)] = []
|
|
newHeaders.reserveCapacity(requestHead.headers.count + 3)
|
|
|
|
// TODO(cory): This is potentially wrong if the URI contains more than just a path.
|
|
newHeaders.append((":path", requestHead.uri))
|
|
newHeaders.append((":method", requestHead.method.rawValue))
|
|
newHeaders.append((":scheme", protocolString))
|
|
|
|
// We store a place for the :authority header, even though we don't know what it is. We'll find it later and
|
|
// change it when we do. This avoids us needing to potentially search this header block twice.
|
|
var authorityHeader: String? = nil
|
|
newHeaders.append((":authority", ""))
|
|
|
|
// Now fill in the others, except for any Host header we might find, which will become an :authority header.
|
|
for header in requestHead.headers {
|
|
if header.name.lowercased() == "host" {
|
|
if authorityHeader != nil {
|
|
throw NIOHTTP2Errors.duplicateHostHeader()
|
|
}
|
|
|
|
authorityHeader = header.value
|
|
} else {
|
|
newHeaders.append((header.name, header.value))
|
|
}
|
|
}
|
|
|
|
// Now we go back and fill in the authority header.
|
|
guard let actualAuthorityHeader = authorityHeader else {
|
|
throw NIOHTTP2Errors.missingHostHeader()
|
|
}
|
|
newHeaders[3].1 = actualAuthorityHeader
|
|
|
|
self.init(newHeaders)
|
|
}
|
|
|
|
fileprivate init(responseHead: HTTPResponseHead) {
|
|
// To avoid too much allocation we create an array first, and then initialize the HTTPHeaders from it.
|
|
// This array will need to be the size of the response headers + 1, for the :status field.
|
|
var newHeaders: [(String, String)] = []
|
|
newHeaders.reserveCapacity(responseHead.headers.count + 1)
|
|
newHeaders.append((":status", String(responseHead.status.code)))
|
|
for responseHeader in responseHead.headers {
|
|
newHeaders.append((responseHeader.name, responseHeader.value))
|
|
}
|
|
|
|
self.init(newHeaders)
|
|
}
|
|
|
|
internal init(regularHeadersFrom oldHeaders: HPACKHeaders) {
|
|
// We need to create an array to write the header fields into.
|
|
var newHeaders: [(String, String)] = []
|
|
newHeaders.reserveCapacity(oldHeaders.count)
|
|
newHeaders.appendRegularHeaders(from: oldHeaders)
|
|
self.init(newHeaders)
|
|
}
|
|
}
|
|
|
|
extension Array where Element == (String, String) {
|
|
mutating func appendRegularHeaders(from headers: HPACKHeaders) {
|
|
for (name, value, _) in headers {
|
|
if name.first == ":" {
|
|
continue
|
|
}
|
|
|
|
self.append((name, value))
|
|
}
|
|
}
|
|
}
|