From c16473969469ae80e5f3d6ea6b6fe6c529268490 Mon Sep 17 00:00:00 2001 From: Guoye Zhang Date: Sun, 11 Jun 2023 23:41:22 -0700 Subject: [PATCH] Import code --- CODE_OF_CONDUCT.md | 5 + CONTRIBUTING.md | 85 +++++ CONTRIBUTORS.txt | 18 + LICENSE.txt | 202 +++++++++++ NOTICE.txt | 34 ++ Package.swift | 29 ++ README.md | 25 ++ Sources/HTTPTypes/HTTPField.swift | 192 ++++++++++ Sources/HTTPTypes/HTTPFieldName.swift | 242 +++++++++++++ Sources/HTTPTypes/HTTPFields.swift | 330 ++++++++++++++++++ Sources/HTTPTypes/HTTPRequest.swift | 300 ++++++++++++++++ Sources/HTTPTypes/HTTPResponse.swift | 315 +++++++++++++++++ Sources/HTTPTypes/ISOLatin1String.swift | 80 +++++ .../HTTPTypesFoundation/HTTPRequest+URL.swift | 137 ++++++++ .../HTTPTypes+ISOLatin1.swift | 53 +++ .../URLRequest+HTTPTypes.swift | 64 ++++ .../URLResponse+HTTPTypes.swift | 57 +++ .../URLSession+HTTPTypes.swift | 128 +++++++ .../HTTPTypesFoundationTests.swift | 112 ++++++ Tests/HTTPTypesTests/HTTPTypesTests.swift | 75 ++++ scripts/generate_contributors_list.sh | 39 +++ 21 files changed, 2522 insertions(+) create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md create mode 100644 CONTRIBUTORS.txt create mode 100644 LICENSE.txt create mode 100644 NOTICE.txt create mode 100644 Package.swift create mode 100644 README.md create mode 100644 Sources/HTTPTypes/HTTPField.swift create mode 100644 Sources/HTTPTypes/HTTPFieldName.swift create mode 100644 Sources/HTTPTypes/HTTPFields.swift create mode 100644 Sources/HTTPTypes/HTTPRequest.swift create mode 100644 Sources/HTTPTypes/HTTPResponse.swift create mode 100644 Sources/HTTPTypes/ISOLatin1String.swift create mode 100644 Sources/HTTPTypesFoundation/HTTPRequest+URL.swift create mode 100644 Sources/HTTPTypesFoundation/HTTPTypes+ISOLatin1.swift create mode 100644 Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift create mode 100644 Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift create mode 100644 Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift create mode 100644 Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift create mode 100644 Tests/HTTPTypesTests/HTTPTypesTests.swift create mode 100755 scripts/generate_contributors_list.sh diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..76501d7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,5 @@ +# Code of Conduct + +The code of conduct for this project can be found at https://swift.org/code-of-conduct. + + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..991e20d --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,85 @@ +## Legal + +By submitting a pull request, you represent that you have the right to license +your contribution to Apple and the community, and agree by submitting the patch +that your contributions are licensed under the Apache 2.0 license (see +`LICENSE.txt`). + +## How to submit a bug report + +Please ensure to specify the following: + +* Swift HTTP Types commit hash +* Contextual information (e.g. what you were trying to achieve with Swift HTTP Types) +* Simplest possible steps to reproduce + * More complex the steps are, lower the priority will be. + * A pull request with failing test case is preferred, but it's just fine to paste the test case into the issue description. +* Anything that might be relevant in your opinion, such as: + * Swift version or the output of `swift --version` + * OS version and the output of `uname -a` + * Network configuration + +### Example + +``` +Commit hash: b17a8a9f0f814c01a56977680cb68d8a779c951f + +Context: +While testing my application that uses with Swift HTTP Types, I noticed that ... + +Steps to reproduce: +1. ... +2. ... +3. ... +4. ... + +$ swift --version +Swift version 5.9-dev (LLVM a93e6b3cd9a083f, Swift 6cdd8f7eed07e21) +Target: aarch64-unknown-linux-gnu + +Operating system: Ubuntu Linux 22.04 aarch64 + +$ uname -a +Linux beefy.machine 5.15.110 #2 SMP Mon May 1 18:48:38 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux + +My system has IPv4 disabled. +``` + +## Writing a Patch + +A good patch is: + +1. Concise, and contains as few changes as needed to achieve the end result. +2. Tested, ensuring that any tests provided failed before the patch and pass after it. +3. Documented, adding API documentation as needed to cover new functions and properties. +4. Accompanied by a great commit message, using our commit message template. + +### Run `./scripts/soundness.sh` + +The scripts directory contains a [soundness.sh script](https://github.com/apple/swift-http-types/blob/main/scripts/soundness.sh) +that enforces additional checks, like license headers and formatting style. + +Please make sure to `./scripts/soundness.sh` before pushing a change upstream, otherwise it is likely the PR validation will fail +on minor changes such as a missing `self.` or similar formatting issues. + +> The script also executes the above mentioned `generate_linux_tests.rb`. + +For frequent contributors, we recommend adding the script as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), which you can do via executing the following command in the project root directory: + +```sh +cat << EOF > .git/hooks/pre-push +#!/bin/sh + +if [ -f "scripts/soundness.sh" ]; then + scripts/soundness.sh +fi +EOF +``` + +Which makes the script execute, and only allow the `git push` to complete if the check has passed. + +In the case of formatting issues, you can then `git add` the formatting changes, and attempt the push again. + +## How to contribute your work + +Please open a pull request at https://github.com/apple/swift-http-types. Make sure the CI passes, and then wait for code review. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt new file mode 100644 index 0000000..712576d --- /dev/null +++ b/CONTRIBUTORS.txt @@ -0,0 +1,18 @@ +For the purpose of tracking copyright, this is the list of individuals and +organizations who have contributed source code to the Swift HTTP Types. + +For employees of an organization/company where the copyright of work done +by employees of that company is held by the company itself, only the company +needs to be listed here. + +## COPYRIGHT HOLDERS + +- Apple Inc. (all contributors with '@apple.com') + +### Contributors + +- Guoye Zhang + +**Updating this list** + +Please do not edit this file manually. It is generated using `./scripts/generate_contributors_list.sh`. If a name is misspelled or appearing multiple times: add an entry in `./.mailmap` diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/NOTICE.txt b/NOTICE.txt new file mode 100644 index 0000000..19f2140 --- /dev/null +++ b/NOTICE.txt @@ -0,0 +1,34 @@ + + The Swift HTTP Types Project + ==================================== + +Please visit the Swift HTTP Types web site for more information: + + * https://github.com/apple/swift-http-types + +Copyright 2023 The Swift HTTP Types Project + +The Swift HTTP Types Project licenses this file to you under the Apache License, +version 2.0 (the "License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at: + + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +License for the specific language governing permissions and limitations +under the License. + +Also, please refer to each LICENSE..txt file, which is located in +the 'license' directory of the distribution file, for the license terms of the +components that this product depends on. + +------------------------------------------------------------------------------- + +This product contains a derivation of various scripts from SwiftNIO. + + * LICENSE (Apache License 2.0): + * https://www.apache.org/licenses/LICENSE-2.0 + * HOMEPAGE: + * https://github.com/apple/swift-nio diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..6486a9d --- /dev/null +++ b/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version: 5.9 + +import PackageDescription + +let package = Package( + name: "http-types", + products: [ + .library(name: "HTTPTypes", targets: ["HTTPTypes"]), + .library(name: "HTTPTypesFoundation", targets: ["HTTPTypesFoundation"]), + ], + targets: [ + .target(name: "HTTPTypes"), + .target( + name: "HTTPTypesFoundation", + dependencies: [ + "HTTPTypes", + ]), + .testTarget( + name: "HTTPTypesTests", + dependencies: [ + "HTTPTypes", + ]), + .testTarget( + name: "HTTPTypesFoundationTests", + dependencies: [ + "HTTPTypesFoundation", + ]), + ] +) diff --git a/README.md b/README.md new file mode 100644 index 0000000..c94e7d5 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# Swift HTTP Types + +Swift HTTP Types are version-independent HTTP currency types designed for both clients and servers. They provide a common set of representations for HTTP requests and responses, focusing on modern HTTP features. + +## Getting Started + +Add the following dependency clause to your Package.swift: + +``` +dependencies: [ + .package(url: "https://github.com/apple/swift-http-types.git", from: "0.1.0") +] +``` + +The `HTTPTypes` library exposes the core HTTP currency types, including `HTTPRequest`, `HTTPResponse`, and `HTTPFields`. + +The `HTTPTypesFoundation` library provides conveniences for using new HTTP types with Foundation, including bidirectional convertors between the new types and Foundation URL types, and URLSession convenience methods with the new types. + +The `HTTPTypesNIO`, `HTTPTypesNIOHTTP1`, and `HTTPTypesNIOHTTP2` libraries provide channel handlers for translating the version-specific NIO HTTP types with the new HTTP types. They can be found in [`swift-nio-extras`](https://github.com/apple/swift-nio-extras). + +## Developing HTTP Types + +For the most part, HTTP Types development is as straightforward as any other SwiftPM project. With that said, we do have a few processes that are worth understanding before you contribute. For details, please see `CONTRIBUTING.md` in this repository. + +Please note that all work on HTTP Types is covered by the [Swift HTTP Types Code of Conduct](https://github.com/apple/swift-http-types/blob/main/CODE_OF_CONDUCT.md). diff --git a/Sources/HTTPTypes/HTTPField.swift b/Sources/HTTPTypes/HTTPField.swift new file mode 100644 index 0000000..4869b49 --- /dev/null +++ b/Sources/HTTPTypes/HTTPField.swift @@ -0,0 +1,192 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +/// HTTP field is a name-value pair with additional metadata. +/// +/// HTTP field name is a case-insensitive but case-preserving ASCII string; HTTP field value is a +/// collection of bytes. +public struct HTTPField: Sendable, Hashable { + /// The strategy for whether the field is indexed in the HPACK or QPACK dynamic table. + public enum DynamicTableIndexingStrategy: UInt8, Sendable, Hashable { + /// Default strategy. + case automatic + /// Always put this field in the dynamic table if possible. + case prefer + /// Don't put this field in the dynamic table. + case avoid + /// Don't put this field in the dynamic table, and set a flag to disallow intermediaries to + /// index this field. + case disallow + } + + /// Create an HTTP field from a name and a value. + /// - Parameters: + /// - name: The HTTP field name. + /// - value: The HTTP field value is initialized from the UTF-8 encoded bytes of the string. + /// Invalid bytes are converted into whitespace characters. + public init(name: Name, value: some StringProtocol) { + self.name = name + self.rawValue = Self.legalizeValue(ISOLatin1String(value)) + } + + /// Create an HTTP field from a name and a value. + /// - Parameters: + /// - name: The HTTP field name. + /// - value: The HTTP field value. Invalid bytes are converted into whitespace characters. + public init(name: Name, value: some Collection) { + self.name = name + self.rawValue = Self.legalizeValue(ISOLatin1String(value)) + } + + init(name: Name, uncheckedValue: ISOLatin1String) { + self.name = name + self.rawValue = uncheckedValue + } + + /// The HTTP field name. + public var name: Name + + /// The HTTP field value as a UTF-8 string. + /// + /// When setting the value, invalid bytes (defined in RFC 9110) are converted into whitespace characters. + /// + /// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-values + /// + /// If the field is not UTF-8 encoded, `withUnsafeValueBytes` can be used to access the + /// underlying bytes of the field value. + public var value: String { + get { + rawValue.string + } + set { + rawValue = Self.legalizeValue(ISOLatin1String(newValue)) + } + } + + /// Runs `body` over the raw HTTP field value bytes as a contiguous buffer. + /// + /// This function is useful if the field is not UTF-8 encoded and the default `value` view + /// cannot be used. + /// + /// Note that it is unsafe to escape the buffer pointer beyond the duration of this call. + /// + /// - Parameter body: The closure to be invoked with the buffer. + /// - Returns: Result of the `body` closure. + public func withUnsafeValueBytes(_ body: (UnsafeBufferPointer) throws -> Result) rethrows -> Result { + try rawValue.withUnsafeBytes(body) + } + + /// The strategy for whether the field is indexed in the HPACK or QPACK dynamic table. + public var indexingStrategy: DynamicTableIndexingStrategy = .automatic + + var rawValue: ISOLatin1String + + private static func _isValidValue(_ bytes: some Sequence) -> Bool { + var iterator = bytes.makeIterator() + guard var byte = iterator.next() else { + // Empty string is allowed. + return true + } + if byte == 0x09 || byte == 0x20 { + // First character cannot be a space or a tab. + return false + } + while true { + switch byte { + case 0x09, 0x20: + break + case 0x21...0x7E, 0x80...0xFF: + break + default: + return false + } + if let next = iterator.next() { + byte = next + } else { + break + } + } + if byte == 0x09 || byte == 0x20 { + // Last character cannot be a space or a tab. + return false + } + return true + } + + static func legalizeValue(_ value: ISOLatin1String) -> ISOLatin1String { + if Self._isValidValue(value._storage.utf8) { + return value + } else { + let bytes = value._storage.utf8.lazy.map { byte -> UInt8 in + switch byte { + case 0x09, 0x20: + return byte + case 0x21...0x7E, 0x80...0xFF: + return byte + default: + return 0x20 + } + } + let trimmed = bytes.reversed().drop { $0 == 0x09 || $0 == 0x20 }.reversed().drop { $0 == 0x09 || $0 == 0x20 } + return ISOLatin1String(unchecked: String(decoding: trimmed, as: UTF8.self)) + } + } + + /// Whether the string is valid for an HTTP field value based on RFC 9110. + /// + /// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-values + /// + /// - Parameter value: The string to validate. + /// - Returns: Whether the string is valid. + public static func isValidValue(_ value: some StringProtocol) -> Bool { + _isValidValue(value.utf8) + } + + /// Whether the byte collection is valid for an HTTP field value based on RFC 9110. + /// + /// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-values + /// + /// - Parameter value: The byte collection to validate. + /// - Returns: Whether the byte collection is valid. + public static func isValidValue(_ value: some Collection) -> Bool { + _isValidValue(value) + } +} + +extension HTTPField: CustomStringConvertible { + public var description: String { + "\(name): \(value)" + } +} + +extension HTTPField: CustomPlaygroundDisplayConvertible { + public var playgroundDescription: Any { + description + } +} + +extension HTTPField { + static func isValidToken(_ token: String) -> Bool { + !token.isEmpty && token.utf8.allSatisfy { + switch $0 { + case 0x21, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2A, 0x2B, 0x2D, 0x2E, 0x5E, 0x5F, 0x60, 0x7C, 0x7E: + return true + case 0x30...0x39, 0x41...0x5A, 0x61...0x7A: // DIGHT, ALPHA + return true + default: + return false + } + } + } +} diff --git a/Sources/HTTPTypes/HTTPFieldName.swift b/Sources/HTTPTypes/HTTPFieldName.swift new file mode 100644 index 0000000..1c40ab1 --- /dev/null +++ b/Sources/HTTPTypes/HTTPFieldName.swift @@ -0,0 +1,242 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +extension HTTPField { + /// HTTP field name is a case-insensitive but case-preserving ASCII string with an allowed + /// character set defined in RFC 9110. + /// + /// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-names + /// + /// Common HTTP field names are provided as convenience static functions, and for custom field + /// names, it is recommended to extend the `HTTPField.Name` struct with new static members. + /// + /// ``` + /// extension HTTPField.Name { + /// static let example = Self("X-Example")! + /// } + /// ``` + public struct Name: Sendable { + /// The original name of the HTTP field received or supplied to the initializer. + public let rawName: String + + /// The lowercased canonical name of the HTTP field used for hashing and comparison. + public let canonicalName: String + + /// Create an HTTP field name from a string. Returns nil if the name contains invalid + /// characters defined in RFC 9110. + /// + /// https://www.rfc-editor.org/rfc/rfc9110.html#name-field-names + /// + /// - Parameter name: The name of the HTTP field. It can be accessed from the `rawName` + /// property. + public init?(_ name: String) { + guard HTTPField.isValidToken(name) else { + return nil + } + rawName = name + canonicalName = name.lowercased() + } + + private init(rawName: String, canonicalName: String) { + self.rawName = rawName + self.canonicalName = canonicalName + } + } +} + +extension HTTPField.Name: Hashable { + public func hash(into hasher: inout Hasher) { + hasher.combine(canonicalName) + } + + public static func ==(lhs: HTTPField.Name, rhs: HTTPField.Name) -> Bool { + lhs.canonicalName == rhs.canonicalName + } +} + +extension HTTPField.Name: LosslessStringConvertible { + public var description: String { + rawName + } +} + +extension HTTPField.Name: CustomPlaygroundDisplayConvertible { + public var playgroundDescription: Any { + description + } +} + +extension HTTPField.Name { + static var method: Self { .init(rawName: ":method", canonicalName: ":method") } + static var scheme: Self { .init(rawName: ":scheme", canonicalName: ":scheme") } + static var authority: Self { .init(rawName: ":authority", canonicalName: ":authority") } + static var path: Self { .init(rawName: ":path", canonicalName: ":path") } + static var `protocol`: Self { .init(rawName: ":protocol", canonicalName: ":protocol") } + static var status: Self { .init(rawName: ":status", canonicalName: ":status") } + /// Accept + public static var accept: Self { .init(rawName: "Accept", canonicalName: "accept") } + /// Accept-Encoding + public static var acceptEncoding: Self { .init(rawName: "Accept-Encoding", canonicalName: "accept-encoding") } + /// Accept-Language + public static var acceptLanguage: Self { .init(rawName: "Accept-Language", canonicalName: "accept-language") } + /// Accept-Ranges + public static var acceptRanges: Self { .init(rawName: "Accept-Ranges", canonicalName: "accept-ranges") } + /// Access-Control-Allow-Credentials + public static var accessControlAllowCredentials: Self { .init(rawName: "Access-Control-Allow-Credentials", canonicalName: "access-control-allow-credentials") } + /// Access-Control-Allow-Headers + public static var accessControlAllowHeaders: Self { .init(rawName: "Access-Control-Allow-Headers", canonicalName: "access-control-allow-headers") } + /// Access-Control-Allow-Methods + public static var accessControlAllowMethods: Self { .init(rawName: "Access-Control-Allow-Methods", canonicalName: "access-control-allow-methods") } + /// Access-Control-Allow-Origin + public static var accessControlAllowOrigin: Self { .init(rawName: "Access-Control-Allow-Origin", canonicalName: "access-control-allow-origin") } + /// Access-Control-Expose-Headers + public static var accessControlExposeHeaders: Self { .init(rawName: "Access-Control-Expose-Headers", canonicalName: "access-control-expose-headers") } + /// Access-Control-Max-Age + public static var accessControlMaxAge: Self { .init(rawName: "Access-Control-Max-Age", canonicalName: "access-control-max-age") } + /// Access-Control-Request-Headers + public static var accessControlRequestHeaders: Self { .init(rawName: "Access-Control-Request-Headers", canonicalName: "access-control-request-headers") } + /// Access-Control-Request-Method + public static var accessControlRequestMethod: Self { .init(rawName: "Access-Control-Request-Method", canonicalName: "access-control-request-method") } + /// Age + public static var age: Self { .init(rawName: "Age", canonicalName: "age") } + /// Alt-Svc + public static var altSvc: Self { .init(rawName: "Alt-Svc", canonicalName: "alt-svc") } + /// Authentication-Info + public static var authenticationInfo: Self { .init(rawName: "Authentication-Info", canonicalName: "authentication-info") } + /// Authorization + public static var authorization: Self { .init(rawName: "Authorization", canonicalName: "authorization") } + /// Cache-Control + public static var cacheControl: Self { .init(rawName: "Cache-Control", canonicalName: "cache-control") } + /// Connection + public static var connection: Self { .init(rawName: "Connection", canonicalName: "connection") } + /// Content-Disposition + public static var contentDisposition: Self { .init(rawName: "Content-Disposition", canonicalName: "content-disposition") } + /// Content-Encoding + public static var contentEncoding: Self { .init(rawName: "Content-Encoding", canonicalName: "content-encoding") } + /// Content-Length + public static var contentLength: Self { .init(rawName: "Content-Length", canonicalName: "content-length") } + /// Content-Security-Policy + public static var contentSecurityPolicy: Self { .init(rawName: "Content-Security-Policy", canonicalName: "content-security-policy") } + /// Content-Type + public static var contentType: Self { .init(rawName: "Content-Type", canonicalName: "content-type") } + /// Cookie + public static var cookie: Self { .init(rawName: "Cookie", canonicalName: "cookie") } + /// Date + public static var date: Self { .init(rawName: "Date", canonicalName: "date") } + /// Early-Data + public static var earlyData: Self { .init(rawName: "Early-Data", canonicalName: "early-data") } + /// ETag + public static var eTag: Self { .init(rawName: "ETag", canonicalName: "etag") } + /// Expect + public static var expect: Self { .init(rawName: "Expect", canonicalName: "expect") } + /// Expires + public static var expires: Self { .init(rawName: "Expires", canonicalName: "expires") } + @available(*, unavailable, message: "Please use HTTPRequest.authority instead") + /// Host + public static var host: Self { .init(rawName: "Host", canonicalName: "host") } + /// If-Modified-Since + public static var ifModifiedSince: Self { .init(rawName: "If-Modified-Since", canonicalName: "if-modified-since") } + /// If-None-Match + public static var ifNoneMatch: Self { .init(rawName: "If-None-Match", canonicalName: "if-none-match") } + /// If-Range + public static var ifRange: Self { .init(rawName: "If-Range", canonicalName: "if-range") } + /// Keep-Alive + public static var keepAlive: Self { .init(rawName: "Keep-Alive", canonicalName: "keep-alive") } + /// Last-Modified + public static var lastModified: Self { .init(rawName: "Last-Modified", canonicalName: "last-modified") } + /// Location + public static var location: Self { .init(rawName: "Location", canonicalName: "location") } + /// Origin + public static var origin: Self { .init(rawName: "Origin", canonicalName: "origin") } + /// Priority + public static var priority: Self { .init(rawName: "Priority", canonicalName: "priority") } + /// Proxy-Authenticate + public static var proxyAuthenticate: Self { .init(rawName: "Proxy-Authenticate", canonicalName: "proxy-authenticate") } + /// Proxy-Authentication-Info + public static var proxyAuthenticationInfo: Self { .init(rawName: "Proxy-Authentication-Info", canonicalName: "proxy-authentication-info") } + /// Proxy-Authorization + public static var proxyAuthorization: Self { .init(rawName: "Proxy-Authorization", canonicalName: "proxy-authorization") } + /// Proxy-Connection + public static var proxyConnection: Self { .init(rawName: "Proxy-Connection", canonicalName: "proxy-connection") } + /// Range + public static var range: Self { .init(rawName: "Range", canonicalName: "range") } + /// Referer + public static var referer: Self { .init(rawName: "Referer", canonicalName: "referer") } + /// Sec-WebSocket-Accept + public static var secWebSocketAccept: Self { .init(rawName: "Sec-WebSocket-Accept", canonicalName: "sec-websocket-accept") } + /// Sec-WebSocket-Extensions + public static var secWebSocketExtensions: Self { .init(rawName: "Sec-WebSocket-Extensions", canonicalName: "sec-websocket-extensions") } + /// Sec-WebSocket-Key + public static var secWebSocketKey: Self { .init(rawName: "Sec-WebSocket-Key", canonicalName: "sec-websocket-key") } + /// Sec-WebSocket-Protocol + public static var secWebSocketProtocol: Self { .init(rawName: "Sec-WebSocket-Protocol", canonicalName: "sec-websocket-protocol") } + /// Sec-WebSocket-Version + public static var secWebSocketVersion: Self { .init(rawName: "Sec-WebSocket-Version", canonicalName: "sec-websocket-version") } + /// Server + public static var server: Self { .init(rawName: "Server", canonicalName: "server") } + /// Set-Cookie + public static var setCookie: Self { .init(rawName: "Set-Cookie", canonicalName: "set-cookie") } + /// Strict-Transport-Security + public static var strictTransportSecurity: Self { .init(rawName: "Strict-Transport-Security", canonicalName: "strict-transport-security") } + /// Trailer + public static var trailer: Self { .init(rawName: "Trailer", canonicalName: "trailer") } + /// Transfer-Encoding + public static var transferEncoding: Self { .init(rawName: "Transfer-Encoding", canonicalName: "transfer-encoding") } + /// Upgrade + public static var upgrade: Self { .init(rawName: "Upgrade", canonicalName: "upgrade") } + /// Upgrade-Insecure-Requests + public static var upgradeInsecureRequests: Self { .init(rawName: "Upgrade-Insecure-Requests", canonicalName: "upgrade-insecure-requests") } + /// User-Agent + public static var userAgent: Self { .init(rawName: "User-Agent", canonicalName: "user-agent") } + /// Vary + public static var vary: Self { .init(rawName: "Vary", canonicalName: "vary") } + /// Via + public static var via: Self { .init(rawName: "Via", canonicalName: "via") } + /// WWW-Authenticate + public static var wwwAuthenticate: Self { .init(rawName: "WWW-Authenticate", canonicalName: "www-authenticate") } + /// X-Content-Type-Options + public static var xContentTypeOptions: Self { .init(rawName: "X-Content-Type-Options", canonicalName: "x-content-type-options") } + // Deprecated + /// P3P + static var p3P: Self { .init(rawName: "P3P", canonicalName: "p3p") } + /// Pragma + static var pragma: Self { .init(rawName: "Pragma", canonicalName: "pragma") } + /// Timing-Allow-Origin + static var timingAllowOrigin: Self { .init(rawName: "Timing-Allow-Origin", canonicalName: "timing-allow-origin") } + /// X-Frame-Options + static var xFrameOptions: Self { .init(rawName: "X-Frame-Options", canonicalName: "x-frame-options") } + /// X-XSS-Protection + static var xXSSProtection: Self { .init(rawName: "X-XSS-Protection", canonicalName: "x-xss-protection") } + // Internal + /// Datagram-Flow-Id + static var datagramFlowId: Self { .init(rawName: "Datagram-Flow-Id", canonicalName: "datagram-flow-id") } + /// Capsule-Protocol + static var capsuleProtocol: Self { .init(rawName: "Capsule-Protocol", canonicalName: "capsule-protocol") } + /// Server-Connection-Id + static var serverConnectionId: Self { .init(rawName: "Server-Connection-Id", canonicalName: "server-connection-id") } + /// Client-Connection-Id + static var clientConnectionId: Self { .init(rawName: "Client-Connection-Id", canonicalName: "client-connection-id") } + /// Sec-CH-Background + static var secCHBackground: Self { .init(rawName: "Sec-CH-Background", canonicalName: "sec-ch-background") } + /// Sec-CH-Geohash + static var secCHGeohash: Self { .init(rawName: "Sec-CH-Geohash", canonicalName: "sec-ch-geohash") } + /// Client-Geohash + static var clientGeohash: Self { .init(rawName: "Client-Geohash", canonicalName: "client-geohash") } + /// Proxy-Status + static var proxyStatus: Self { .init(rawName: "Proxy-Status", canonicalName: "proxy-status") } + /// Proxy-QUIC-Forwarding + static var proxyQUICForwarding: Self { .init(rawName: "Proxy-QUIC-Forwarding", canonicalName: "proxy-quic-forwarding") } + /// Proxy-Config-Epoch + static var proxyConfigEpoch: Self { .init(rawName: "Proxy-Config-Epoch", canonicalName: "proxy-config-epoch") } +} diff --git a/Sources/HTTPTypes/HTTPFields.swift b/Sources/HTTPTypes/HTTPFields.swift new file mode 100644 index 0000000..2cdf860 --- /dev/null +++ b/Sources/HTTPTypes/HTTPFields.swift @@ -0,0 +1,330 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +#if canImport(os.lock) +@_implementationOnly import os.lock +#else +@_implementationOnly import Glibc +#endif + +/// A collection of HTTP fields. It is used in `HTTPRequest` and `HTTPResponse`, and also used for +/// HTTP trailer fields. +/// +/// HTTP fields are an ordered list of name-value pairs. Each field is represented as an instance +/// of `HTTPField` struct. `HTTPFields` also offers conveniences to look up fields by their names. +/// +/// `HTTPFields` adheres to modern HTTP semantics. In particular, the "Cookie" request header field +/// is split into separate header fields by default. +public struct HTTPFields: Sendable, Hashable { + private final class _Storage: @unchecked Sendable, Hashable { + var fields: [(HTTPField, UInt16)] = [] + var index: [String: UInt16]? = [:] +#if canImport(os.lock) + let lock = UnsafeMutablePointer.allocate(capacity: 1) +#else + let lock = UnsafeMutablePointer.allocate(capacity: 1) +#endif + + init() { +#if canImport(os.lock) + lock.initialize(to: os_unfair_lock()) +#else + let err = pthread_mutex_init(lock, nil) + precondition(err == 0, "pthread_mutex_init failed with error \(err)") +#endif + } + + deinit { +#if !canImport(os.lock) + let err = pthread_mutex_destroy(lock) + precondition(err == 0, "pthread_mutex_destroy failed with error \(err)") +#endif + lock.deallocate() + } + + var ensureIndex: [String: UInt16] { +#if canImport(os.lock) + os_unfair_lock_lock(lock) + defer { os_unfair_lock_unlock(lock) } +#else + let err = pthread_mutex_lock(lock) + precondition(err == 0, "pthread_mutex_lock failed with error \(err)") + defer { + let err = pthread_mutex_unlock(lock) + precondition(err == 0, "pthread_mutex_unlock failed with error \(err)") + } +#endif + if let index = index { + return index + } + var newIndex = [String: UInt16]() + for i in fields.indices.reversed() { + let name = fields[i].0.name.canonicalName + fields[i].1 = newIndex[name] ?? .max + newIndex[name] = UInt16(i) + } + index = newIndex + return newIndex + } + + func copy() -> _Storage { + let newStorage = _Storage() + newStorage.fields = fields +#if canImport(os.lock) + os_unfair_lock_lock(lock) +#else + do { + let err = pthread_mutex_lock(lock) + precondition(err == 0, "pthread_mutex_lock failed with error \(err)") + } +#endif + newStorage.index = index +#if canImport(os.lock) + os_unfair_lock_unlock(lock) +#else + do { + let err = pthread_mutex_unlock(lock) + precondition(err == 0, "pthread_mutex_unlock failed with error \(err)") + } +#endif + return newStorage + } + + func hash(into hasher: inout Hasher) { + for (field, _) in fields { + hasher.combine(field) + } + } + + static func == (lhs: _Storage, rhs: _Storage) -> Bool { + lhs.fields.lazy.map(\.0) == rhs.fields.lazy.map(\.0) + } + + func append(field: HTTPField) { + let name = field.name.canonicalName + if var index = index?[name] { + while true { + let next = fields[Int(index)].1 + if next == .max { break } + index = next + } + fields[Int(index)].1 = UInt16(fields.count) + } else { + index?[name] = UInt16(fields.count) + } + fields.append((field, .max)) + } + } + + private var _storage = _Storage() + + /// Create an empty list of HTTP fields + public init() { + } + + /// Access the field value string by name. + /// + /// If multiple fields with the same name exist, they are concatenated with commas (or + /// semicolons in the case of the "Cookie" header field). + /// + /// Example: + /// ```swift + /// // Set a header field in the request. + /// request.headerFields[.accept] = "*/*" + /// + /// // Access a header field value from the response. + /// let contentTypeValue = response.headerFields[.contentType] + /// ``` + /// + /// When setting a "Cookie" header field value, it is split into multiple "Cookie" fields by + /// semicolon. + public subscript(name: HTTPField.Name) -> String? { + get { + let values = self[raw: name] + if !values.isEmpty { + let separator = name == .cookie ? "; " : ", " + return values.joined(separator: separator) + } else { + return nil + } + } + set { + if let newValue { + if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *), + name == .cookie { + self[fields: name] = newValue.split(separator: "; ", omittingEmptySubsequences: false).map { + HTTPField(name: name, value: $0) + } + } else { + self[raw: name] = [newValue] + } + } else { + self[raw: name] = [] + } + } + } + + /// Access the field values by name as an array of strings. The order of fields is preserved. + public subscript(raw name: HTTPField.Name) -> [String] { + get { + self[fields: name].map(\.value) + } + set { + self[fields: name] = newValue.map { HTTPField(name: name, value: $0) } + } + } + + /// Access the fields by name as an array. The order of fields is preserved. + public subscript(fields name: HTTPField.Name) -> [HTTPField] { + get { + var fields = [HTTPField]() + var index = _storage.ensureIndex[name.canonicalName] ?? .max + while index != .max { + let (field, next) = _storage.fields[Int(index)] + fields.append(field) + index = next + } + return fields + } + set { + if !isKnownUniquelyReferenced(&_storage) { + _storage = _storage.copy() + } + var existingIndex = _storage.ensureIndex[name.canonicalName] ?? .max + var newFieldIterator = newValue.makeIterator() + var toDelete = [Int]() + while existingIndex != .max { + if let field = newFieldIterator.next() { + _storage.fields[Int(existingIndex)].0 = field + } else { + toDelete.append(Int(existingIndex)) + } + existingIndex = _storage.fields[Int(existingIndex)].1 + } + if !toDelete.isEmpty { + _storage.fields.remove(at: toDelete) + _storage.index = nil + } + while let field = newFieldIterator.next() { + _storage.append(field: field) + } + } + } + + /// Whether one or more field with this name exists in the fields. + /// - Parameter name: The field name. + /// - Returns: Whether a field exists. + public func contains(_ name: HTTPField.Name) -> Bool { + _storage.ensureIndex[name.canonicalName] != nil + } +} + +extension HTTPFields: ExpressibleByDictionaryLiteral { + public init(dictionaryLiteral elements: (HTTPField.Name, String)...) { + for (name, value) in elements { + _storage.append(field: HTTPField(name: name, value: value)) + } + } +} + +extension HTTPFields: RangeReplaceableCollection, RandomAccessCollection, MutableCollection { + public typealias Element = HTTPField + public typealias Index = Int + + public var startIndex: Int { + _storage.fields.startIndex + } + + public var endIndex: Int { + _storage.fields.endIndex + } + + public var isEmpty: Bool { + _storage.fields.isEmpty + } + + public subscript(position: Int) -> HTTPField { + get { + guard position >= self.startIndex && position < self.endIndex else { + preconditionFailure("getter position: \(position) out of range in HTTPFields") + } + return _storage.fields[position].0 + } + set { + guard position >= self.startIndex && position < self.endIndex else { + preconditionFailure("setter position: \(position) out of range in HTTPFields") + } + if _storage.fields[position].0 == newValue { + return + } + if !isKnownUniquelyReferenced(&_storage) { + _storage = _storage.copy() + } + if newValue.name != _storage.fields[position].0.name { + _storage.index = nil + } + _storage.fields[position].0 = newValue + } + } + + public mutating func replaceSubrange(_ subrange: Range, with newElements: C) where C: Collection, Element == C.Element { + if !isKnownUniquelyReferenced(&_storage) { + _storage = _storage.copy() + } + if subrange.startIndex == count { + for field in newElements { + _storage.append(field: field) + } + } else { + _storage.index = nil + _storage.fields.replaceSubrange(subrange, with: newElements.lazy.map { ($0, 0) }) + } + } + + public mutating func reserveCapacity(_ n: Int) { + if !isKnownUniquelyReferenced(&_storage) { + _storage = _storage.copy() + } + _storage.index?.reserveCapacity(n) + _storage.fields.reserveCapacity(n) + } +} + +extension HTTPFields: CustomDebugStringConvertible { + public var debugDescription: String { + _storage.fields.description + } +} + +extension Array { + // `removalIndices` must be ordered. + mutating func remove(at removalIndices: S) where S.Element == Index { + var offset = 0 + var iterator = removalIndices.makeIterator() + var nextToRemoveOptional = iterator.next() + for i in indices { + while let nextToRemove = nextToRemoveOptional, index(i, offsetBy: offset) == nextToRemove { + offset += 1 + nextToRemoveOptional = iterator.next() + } + let toKeep = index(i, offsetBy: offset) + if toKeep < endIndex { + swapAt(i, toKeep) + } else { + break + } + } + removeLast(offset) + } +} diff --git a/Sources/HTTPTypes/HTTPRequest.swift b/Sources/HTTPTypes/HTTPRequest.swift new file mode 100644 index 0000000..39841a3 --- /dev/null +++ b/Sources/HTTPTypes/HTTPRequest.swift @@ -0,0 +1,300 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +/// HTTP request message consists of pseudo header fields and header fields. +/// +/// Currently supported pseudo header fields are ":method", ":scheme", ":authority", ":path", and +/// ":protocol". Conveniences are provided to set these pseudo header fields through a URL and +/// strings. +/// +/// In a legacy HTTP/1 context, the ":scheme" is ignored and the ":authority" is translated into +/// the "Host" header. +public struct HTTPRequest: Sendable, Hashable { + /// HTTP request method + public struct Method: Hashable, RawRepresentable, LosslessStringConvertible { + /// The string value of the request. + public let rawValue: String + + /// Create a request method from a string. Returns nil if the string contains invalid + /// characters defined in RFC 9110. + /// + /// https://www.rfc-editor.org/rfc/rfc9110.html#name-overview + /// + /// - Parameter method: The method string. It can be accessed from the `rawValue` property. + public init?(_ method: String) { + guard HTTPField.isValidToken(method) else { + return nil + } + rawValue = method + } + + public init?(rawValue: String) { + self.init(rawValue) + } + + private init(unchecked: String) { + rawValue = unchecked + } + + public var description: String { + rawValue + } + } + + /// The HTTP request method. + public var method: Method { + get { + Method(methodField.rawValue._storage)! + } + set { + methodField.rawValue = ISOLatin1String(unchecked: newValue.rawValue) + } + } + + /// The value of the ":scheme" pseudo header field. + /// + /// The scheme is ignored in a legacy HTTP/1 context. + public var scheme: String? { + get { + schemeField?.value + } + set { + if let newValue { + if var field = schemeField { + field.value = newValue + schemeField = field + } else { + var field = HTTPField(name: .scheme, value: newValue) + field.indexingStrategy = .prefer + schemeField = field + } + } else { + schemeField = nil + } + } + } + + /// The value of the ":authority" pseudo header field. + /// + /// The authority is translated into the "Host" header in a legacy HTTP/1 context. + public var authority: String? { + get { + authorityField?.value + } + set { + if let newValue { + if var field = authorityField { + field.value = newValue + authorityField = field + } else { + var field = HTTPField(name: .authority, value: newValue) + field.indexingStrategy = .prefer + authorityField = field + } + } else { + authorityField = nil + } + } + } + + /// The value of the ":path" pseudo header field. + public var path: String? { + get { + pathField?.value + } + set { + if let newValue { + if var field = pathField { + field.value = newValue + pathField = field + } else { + pathField = HTTPField(name: .path, value: newValue) + } + } else { + pathField = nil + } + } + } + + /// The value of the ":protocol" pseudo header field. + public var extendedConnectProtocol: String? { + get { + extendedConnectProtocolField?.value + } + set { + if let newValue { + if var field = extendedConnectProtocolField { + field.value = newValue + extendedConnectProtocolField = field + } else { + var field = HTTPField(name: .protocol, value: newValue) + field.indexingStrategy = .prefer + extendedConnectProtocolField = field + } + } else { + extendedConnectProtocolField = nil + } + } + } + + /// The underlying ":method" pseudo header field. + public var methodField: HTTPField { + willSet { + precondition(newValue.name == .method, "Cannot change pseudo-header field name") + precondition(HTTPField.isValidToken(newValue.rawValue._storage), "Invalid character in method field") + } + } + + /// The underlying ":scheme" pseudo header field. + public var schemeField: HTTPField? { + willSet { + if let name = newValue?.name { + precondition(name == .scheme, "Cannot change pseudo-header field name") + } + } + } + + /// The underlying ":authority" pseudo header field. + public var authorityField: HTTPField? { + willSet { + if let name = newValue?.name { + precondition(name == .authority, "Cannot change pseudo-header field name") + } + } + } + + /// The underlying ":path" pseudo header field. + public var pathField: HTTPField? { + willSet { + if let name = newValue?.name { + precondition(name == .path, "Cannot change pseudo-header field name") + } + } + } + + /// The underlying ":protocol" pseudo header field. + public var extendedConnectProtocolField: HTTPField? { + willSet { + if let name = newValue?.name { + precondition(name == .protocol, "Cannot change pseudo-header field name") + } + } + } + + /// The request header fields. + public var headerFields: HTTPFields + + /// Create an HTTP request with values of pseudo header fields and header fields. + /// - Parameters: + /// - method: The request method. + /// - scheme: The value of the ":scheme" pseudo header field. + /// - authority: The value of the ":authority" pseudo header field. + /// - path: The value of the ":path" pseudo header field. + /// - headerFields: The request header fields. + public init(method: Method, scheme: String?, authority: String?, path: String?, headerFields: HTTPFields = [:]) { + methodField = HTTPField(name: .method, uncheckedValue: ISOLatin1String(unchecked: method.rawValue)) + methodField.indexingStrategy = .prefer + schemeField = scheme.map { + var field = HTTPField(name: .scheme, value: $0) + field.indexingStrategy = .prefer + return field + } + authorityField = authority.map { + var field = HTTPField(name: .authority, value: $0) + field.indexingStrategy = .prefer + return field + } + pathField = path.map { HTTPField(name: .path, value: $0) } + self.headerFields = headerFields + } +} + +extension HTTPRequest: CustomDebugStringConvertible { + public var debugDescription: String { + "(\(methodField.rawValue._storage)) \((schemeField?.value).map { "\($0)://" } ?? "")\(authorityField?.value ?? "")\(pathField?.value ?? "")" + } +} + +extension HTTPRequest.Method { + /// GET + public static let get = HTTPRequest.Method(unchecked: "GET") + /// PUT + public static let put = HTTPRequest.Method(unchecked: "PUT") + /// ACL + public static let acl = HTTPRequest.Method(unchecked: "ACL") + /// HEAD + public static let head = HTTPRequest.Method(unchecked: "HEAD") + /// POST + public static let post = HTTPRequest.Method(unchecked: "POST") + /// COPY + public static let copy = HTTPRequest.Method(unchecked: "COPY") + /// LOCK + public static let lock = HTTPRequest.Method(unchecked: "LOCK") + /// MOVE + public static let move = HTTPRequest.Method(unchecked: "MOVE") + /// BIND + public static let bind = HTTPRequest.Method(unchecked: "BIND") + /// LINK + public static let link = HTTPRequest.Method(unchecked: "LINK") + /// PATCH + public static let patch = HTTPRequest.Method(unchecked: "PATCH") + /// TRACE + public static let trace = HTTPRequest.Method(unchecked: "TRACE") + /// MKCOL + public static let mkcol = HTTPRequest.Method(unchecked: "MKCOL") + /// MERGE + public static let merge = HTTPRequest.Method(unchecked: "MERGE") + /// PURGE + public static let purge = HTTPRequest.Method(unchecked: "PURGE") + /// NOTIFY + public static let notify = HTTPRequest.Method(unchecked: "NOTIFY") + /// SEARCH + public static let search = HTTPRequest.Method(unchecked: "SEARCH") + /// UNLOCK + public static let unlock = HTTPRequest.Method(unchecked: "UNLOCK") + /// REBIND + public static let rebind = HTTPRequest.Method(unchecked: "REBIND") + /// UNBIND + public static let unbind = HTTPRequest.Method(unchecked: "UNBIND") + /// REPORT + public static let report = HTTPRequest.Method(unchecked: "REPORT") + /// DELETE + public static let delete = HTTPRequest.Method(unchecked: "DELETE") + /// UNLINK + public static let unlink = HTTPRequest.Method(unchecked: "UNLINK") + /// CONNECT + public static let connect = HTTPRequest.Method(unchecked: "CONNECT") + /// MSEARCH + public static let msearch = HTTPRequest.Method(unchecked: "MSEARCH") + /// OPTIONS + public static let options = HTTPRequest.Method(unchecked: "OPTIONS") + /// PROPFIND + public static let propfind = HTTPRequest.Method(unchecked: "PROPFIND") + /// CHECKOUT + public static let checkout = HTTPRequest.Method(unchecked: "CHECKOUT") + /// PROPPATCH + public static let proppatch = HTTPRequest.Method(unchecked: "PROPPATCH") + /// SUBSCRIBE + public static let subscribe = HTTPRequest.Method(unchecked: "SUBSCRIBE") + /// MKCALENDAR + public static let mkcalendar = HTTPRequest.Method(unchecked: "MKCALENDAR") + /// MKACTIVITY + public static let mkactivity = HTTPRequest.Method(unchecked: "MKACTIVITY") + /// UNSUBSCRIBE + public static let unsubscribe = HTTPRequest.Method(unchecked: "UNSUBSCRIBE") + /// SOURCE + public static let source = HTTPRequest.Method(unchecked: "SOURCE") + /// CONNECT-UDP + static let connectUDP = HTTPRequest.Method(unchecked: "CONNECT-UDP") +} diff --git a/Sources/HTTPTypes/HTTPResponse.swift b/Sources/HTTPTypes/HTTPResponse.swift new file mode 100644 index 0000000..6d2c479 --- /dev/null +++ b/Sources/HTTPTypes/HTTPResponse.swift @@ -0,0 +1,315 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +/// HTTP request message consists of the ":status" pseudo header field, an optional reason phrase, +/// and header fields. +/// +/// Conveniences are provided to access the status code and its reason phrase. +public struct HTTPResponse: Sendable, Hashable { + /// Status consists of a 3-digit status code and a reason phrase. The reason phrase is ignored + /// by modern HTTP versions. + public struct Status: Hashable, ExpressibleByIntegerLiteral, CustomStringConvertible { + /// The 3-digit status code. + public let code: Int + /// The optional reason phrase (ISOLatin1 encoded). + public let reasonPhrase: String? + + /// Create a custom status from a code and a reason phrase. + /// - Parameters: + /// - code: The status code. + /// - reasonPhrase: The optional reason phrase. Invalid characters are converted into + /// whitespace characters. + public init(code: Int, reasonPhrase: String? = nil) { + precondition((0...999).contains(code), "Invalid status code") + self.code = code + self.reasonPhrase = reasonPhrase.map(Self.legalizingReasonPhrase) + } + + fileprivate init(uncheckedCode: Int, reasonPhrase: String?) { + self.code = uncheckedCode + self.reasonPhrase = reasonPhrase + } + + /// Create a custom status from an integer literal. + /// - Parameter value: The status code. + public init(integerLiteral value: Int) { + precondition((0...999).contains(value), "Invalid status code") + code = value + reasonPhrase = nil + } + + /// The status code is informational (1xx) and the response is not final. + public var isInformational: Bool { + (100...199).contains(code) + } + + /// The status code is successful (2xx). + public var isSuccessful: Bool { + (200...299).contains(code) + } + + /// The status code is a redirection (3xx). + public var isRedirection: Bool { + (300...399).contains(code) + } + + /// The status code is a client error (4xx). + public var isClientError: Bool { + (400...499).contains(code) + } + + /// The status code is a server error (5xx). + public var isServerError: Bool { + (500...599).contains(code) + } + + public func hash(into hasher: inout Hasher) { + hasher.combine(code) + } + + public static func ==(lhs: Status, rhs: Status) -> Bool { + lhs.code == rhs.code + } + + public var description: String { + "\(code)" + } + + var fieldValue: String { + String([ + Character(Unicode.Scalar(UInt8(code / 100) + 48)), + Character(Unicode.Scalar(UInt8((code / 10) % 10) + 48)), + Character(Unicode.Scalar(UInt8(code % 10) + 48)) + ]) + } + + static func isValidStatus(_ status: String) -> Bool { + status.count == 3 && status.utf8.allSatisfy { (0x30...0x39).contains($0) } + } + + static func isValidReasonPhrase(_ reasonPhrase: String) -> Bool { + reasonPhrase.utf8.allSatisfy { + switch $0 { + case 0x09, 0x20: + return true + case 0x21...0x7E, 0x80...0xFF: + return true + default: + return false + } + } + } + + static func legalizingReasonPhrase(_ reasonPhrase: String) -> String { + if Self.isValidReasonPhrase(reasonPhrase) { + return reasonPhrase + } else { + let scalars = reasonPhrase.unicodeScalars.lazy.map { scala -> UnicodeScalar in + switch scala.value { + case 0x09, 0x20: + return scala + case 0x21...0x7E, 0x80...0xFF: + return scala + default: + return " " + } + } + var string = "" + string.unicodeScalars.append(contentsOf: scalars) + return string + } + } + } + + /// The status of the response. + public var status: Status { + get { + Status(uncheckedCode: Int(statusField.rawValue._storage)!, reasonPhrase: reasonPhrase) + } + set { + statusField.rawValue = ISOLatin1String(unchecked: newValue.fieldValue) + reasonPhrase = newValue.reasonPhrase + } + } + + /// The underlying ":status" pseudo header field. + public var statusField: HTTPField { + willSet { + precondition(newValue.name == .status, "Cannot change pseudo-header field name") + precondition(Status.isValidStatus(newValue.rawValue._storage), "Invalid status code") + } + } + + private var reasonPhrase: String? + + /// The response header fields. + public var headerFields: HTTPFields + + /// Create an HTTP response with a status and header fields. + /// - Parameters: + /// - status: The status code and an optional reason phrase. + /// - headerFields: The response header fields. + public init(status: Status, headerFields: HTTPFields = [:]) { + statusField = HTTPField(name: .status, uncheckedValue: ISOLatin1String(unchecked: status.fieldValue)) + reasonPhrase = status.reasonPhrase + self.headerFields = headerFields + } + + public func hash(into hasher: inout Hasher) { + hasher.combine(statusField) + hasher.combine(headerFields) + } + + public static func ==(lhs: HTTPResponse, rhs: HTTPResponse) -> Bool { + lhs.statusField == rhs.statusField && lhs.headerFields == lhs.headerFields + } +} + +extension HTTPResponse: CustomDebugStringConvertible { + public var debugDescription: String { + "\(status)" + } +} + +extension HTTPResponse.Status { + // MARK: 1xx + /// 100 Continue + public static var `continue`: Self { .init(uncheckedCode: 100, reasonPhrase: "Continue") } + /// 101 Switching Protocols + public static var switchingProtocols: Self { .init(uncheckedCode: 101, reasonPhrase: "Switching Protocols") } + /// 102 Processing + public static var processing: Self { .init(uncheckedCode: 102, reasonPhrase: "Processing") } + /// 103 Early Hints + public static var earlyHints: Self { .init(uncheckedCode: 103, reasonPhrase: "Early Hints") } + + // MARK: 2xx + /// 200 OK + public static var ok: Self { .init(uncheckedCode: 200, reasonPhrase: "OK") } + /// 201 Created + public static var created: Self { .init(uncheckedCode: 201, reasonPhrase: "Created") } + /// 202 Accepted + public static var accepted: Self { .init(uncheckedCode: 202, reasonPhrase: "Accepted") } + /// 203 Non-Authoritative Information + public static var nonAuthoritativeInformation: Self { .init(uncheckedCode: 203, reasonPhrase: "Non-Authoritative Information") } + /// 204 No Content + public static var noContent: Self { .init(uncheckedCode: 204, reasonPhrase: "No Content") } + /// 205 Reset Content + public static var resetContent: Self { .init(uncheckedCode: 205, reasonPhrase: "Reset Content") } + /// 206 Partial Content + public static var partialContent: Self { .init(uncheckedCode: 206, reasonPhrase: "Partial Content") } + /// 207 Multi-Status + public static var multiStatus: Self { .init(uncheckedCode: 207, reasonPhrase: "Multi-Status") } + /// 208 Already Reported + public static var alreadyReported: Self { .init(uncheckedCode: 208, reasonPhrase: "Already Reported") } + /// 226 IM Used + public static var imUsed: Self { .init(uncheckedCode: 226, reasonPhrase: "IM Used") } + + // MARK: 3xx + /// 300 Multiple Choices + public static var multipleChoices: Self { .init(uncheckedCode: 300, reasonPhrase: "Multiple Choices") } + /// 301 Moved Permanently + public static var movedPermanently: Self { .init(uncheckedCode: 301, reasonPhrase: "Moved Permanently") } + /// 302 Found + public static var found: Self { .init(uncheckedCode: 302, reasonPhrase: "Found") } + /// 303 See Other + public static var seeOther: Self { .init(uncheckedCode: 303, reasonPhrase: "See Other") } + /// 304 Not Modified + public static var notModified: Self { .init(uncheckedCode: 304, reasonPhrase: "Not Modified") } + /// 305 Use Proxy + public static var useProxy: Self { .init(uncheckedCode: 305, reasonPhrase: "Use Proxy") } + /// 307 Temporary Redirect + public static var temporaryRedirect: Self { .init(uncheckedCode: 307, reasonPhrase: "Temporary Redirect") } + /// 308 Permanent Redirect + public static var permanentRedirect: Self { .init(uncheckedCode: 308, reasonPhrase: "Permanent Redirect") } + + // MARK: 4xx + /// 400 Bad Request + public static var badRequest: Self { .init(uncheckedCode: 400, reasonPhrase: "Bad Request") } + /// 401 Unauthorized + public static var unauthorized: Self { .init(uncheckedCode: 401, reasonPhrase: "Unauthorized") } + /// 402 Payment Required + public static var paymentRequired: Self { .init(uncheckedCode: 402, reasonPhrase: "Payment Required") } + /// 403 Forbidden + public static var forbidden: Self { .init(uncheckedCode: 403, reasonPhrase: "Forbidden") } + /// 404 Not Found + public static var notFound: Self { .init(uncheckedCode: 404, reasonPhrase: "Not Found") } + /// 405 Method Not Allowed + public static var methodNotAllowed: Self { .init(uncheckedCode: 405, reasonPhrase: "Method Not Allowed") } + /// 406 Not Acceptable + public static var notAcceptable: Self { .init(uncheckedCode: 406, reasonPhrase: "Not Acceptable") } + /// 407 Proxy Authentication Required + public static var proxyAuthenticationRequired: Self { .init(uncheckedCode: 407, reasonPhrase: "Proxy Authentication Required") } + /// 408 Request Timeout + public static var requestTimeout: Self { .init(uncheckedCode: 408, reasonPhrase: "Request Timeout") } + /// 409 Conflict + public static var conflict: Self { .init(uncheckedCode: 409, reasonPhrase: "Conflict") } + /// 410 Gone + public static var gone: Self { .init(uncheckedCode: 410, reasonPhrase: "Gone") } + /// 411 Length Required + public static var lengthRequired: Self { .init(uncheckedCode: 411, reasonPhrase: "Length Required") } + /// 412 Precondition Failed + public static var preconditionFailed: Self { .init(uncheckedCode: 412, reasonPhrase: "Precondition Failed") } + /// 413 Payload Too Large + public static var payloadTooLarge: Self { .init(uncheckedCode: 413, reasonPhrase: "Payload Too Large") } + /// 414 URI Too Long + public static var uriTooLong: Self { .init(uncheckedCode: 414, reasonPhrase: "URI Too Long") } + /// 415 Unsupported Media Type + public static var unsupportedMediaType: Self { .init(uncheckedCode: 415, reasonPhrase: "Unsupported Media Type") } + /// 416 Range Not Satisfiable + public static var rangeNotSatisfiable: Self { .init(uncheckedCode: 416, reasonPhrase: "Range Not Satisfiable") } + /// 417 Expectation Failed + public static var expectationFailed: Self { .init(uncheckedCode: 417, reasonPhrase: "Expectation Failed") } + /// 421 Misdirected Request + public static var misdirectedRequest: Self { .init(uncheckedCode: 421, reasonPhrase: "Misdirected Request") } + /// 422 Unprocessable Entity + public static var unprocessableEntity: Self { .init(uncheckedCode: 422, reasonPhrase: "Unprocessable Entity") } + /// 423 Locked + public static var locked: Self { .init(uncheckedCode: 423, reasonPhrase: "Locked") } + /// 424 Failed Dependency + public static var failedDependency: Self { .init(uncheckedCode: 424, reasonPhrase: "Failed Dependency") } + /// 426 Upgrade Required + public static var upgradeRequired: Self { .init(uncheckedCode: 426, reasonPhrase: "Upgrade Required") } + /// 428 Precondition Required + public static var preconditionRequired: Self { .init(uncheckedCode: 428, reasonPhrase: "Precondition Required") } + /// 429 Too Many Requests + public static var tooManyRequests: Self { .init(uncheckedCode: 429, reasonPhrase: "Too Many Requests") } + /// 431 Request Header Fields Too Large + public static var requestHeaderFieldsTooLarge: Self { .init(uncheckedCode: 431, reasonPhrase: "Request Header Fields Too Large") } + /// 451 Unavailable For Legal Reasons + public static var unavailableForLegalReasons: Self { .init(uncheckedCode: 451, reasonPhrase: "Unavailable For Legal Reasons") } + + // MARK: 5xx + /// 500 Internal Server Error + public static var internalServerError: Self { .init(uncheckedCode: 500, reasonPhrase: "Internal Server Error") } + /// 501 Not Implemented + public static var notImplemented: Self { .init(uncheckedCode: 501, reasonPhrase: "Not Implemented") } + /// 502 Bad Gateway + public static var badGateway: Self { .init(uncheckedCode: 502, reasonPhrase: "Bad Gateway") } + /// 503 Service Unavailable + public static var serviceUnavailable: Self { .init(uncheckedCode: 503, reasonPhrase: "Service Unavailable") } + /// 504 Gateway Timeout + public static var gatewayTimeout: Self { .init(uncheckedCode: 504, reasonPhrase: "Gateway Timeout") } + /// 505 HTTP Version Not Supported + public static var httpVersionNotSupported: Self { .init(uncheckedCode: 505, reasonPhrase: "HTTP Version Not Supported") } + /// 506 Variant Also Negotiates + public static var variantAlsoNegotiates: Self { .init(uncheckedCode: 506, reasonPhrase: "Variant Also Negotiates") } + /// 507 Insufficient Storage + public static var insufficientStorage: Self { .init(uncheckedCode: 507, reasonPhrase: "Insufficient Storage") } + /// 508 Loop Detected + public static var loopDetected: Self { .init(uncheckedCode: 508, reasonPhrase: "Loop Detected") } + /// 510 Not Extended + public static var notExtended: Self { .init(uncheckedCode: 510, reasonPhrase: "Not Extended") } + /// 511 Network Authentication Required + public static var networkAuthenticationRequired: Self { .init(uncheckedCode: 511, reasonPhrase: "Network Authentication Required") } +} diff --git a/Sources/HTTPTypes/ISOLatin1String.swift b/Sources/HTTPTypes/ISOLatin1String.swift new file mode 100644 index 0000000..b92fefd --- /dev/null +++ b/Sources/HTTPTypes/ISOLatin1String.swift @@ -0,0 +1,80 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +extension StringProtocol { + var isASCII: Bool { + utf8.allSatisfy { $0 & 0x80 == 0 } + } +} + +struct ISOLatin1String: Sendable, Hashable { + let _storage: String + + private static func transcodeSlowPath(from bytes: some Collection) -> String { + let scalars = bytes.lazy.map { UnicodeScalar(UInt32($0))! } + var string = "" + string.unicodeScalars.append(contentsOf: scalars) + return string + } + + private func withISOLatin1BytesSlowPath(_ body: (UnsafeBufferPointer) throws -> Result) rethrows -> Result { + try withUnsafeTemporaryAllocation(of: UInt8.self, capacity: _storage.unicodeScalars.count) { buffer in + for (index, scalar) in _storage.unicodeScalars.enumerated() { + assert(scalar.value <= UInt8.max) + buffer.initializeElement(at: index, to: UInt8(truncatingIfNeeded: scalar.value)) + } + return try body(UnsafeBufferPointer(buffer)) + } + } + + init(_ string: some StringProtocol) { + if string.isASCII { + _storage = String(string) + } else { + _storage = Self.transcodeSlowPath(from: string.utf8) + } + } + + init(_ bytes: some Collection) { + let ascii = bytes.allSatisfy { $0 & 0x80 == 0 } + if ascii { + _storage = String(decoding: bytes, as: UTF8.self) + } else { + _storage = Self.transcodeSlowPath(from: bytes) + } + } + + init(unchecked: String) { + _storage = unchecked + } + + var string: String { + if _storage.isASCII { + return _storage + } else { + return withISOLatin1BytesSlowPath { + String(decoding: $0, as: UTF8.self) + } + } + } + + func withUnsafeBytes(_ body: (UnsafeBufferPointer) throws -> Result) rethrows -> Result { + if _storage.isASCII { + var string = _storage + return try string.withUTF8(body) + } else { + return try withISOLatin1BytesSlowPath(body) + } + } +} diff --git a/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift new file mode 100644 index 0000000..cfb1c05 --- /dev/null +++ b/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift @@ -0,0 +1,137 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import HTTPTypes +import Foundation +@_implementationOnly import CoreFoundation + +public extension HTTPRequest { + /// The URL of the request synthesized from the scheme, authority, and path pseudo header + /// fields. + var url: URL? { + get { + if let schemeField, + let authorityField, + let pathField { + return schemeField.withUnsafeValueBytes { scheme in + authorityField.withUnsafeValueBytes { authority in + pathField.withUnsafeValueBytes { path in + URL(scheme: scheme, authority: authority, path: path) + } + } + } + } else { + return nil + } + } + set { + if let newValue { + let (scheme, authority, path) = newValue.httpRequestComponents + self.scheme = String(decoding: scheme, as: UTF8.self) + self.authority = authority.map { String(decoding: $0, as: UTF8.self) } + self.path = String(decoding: path, as: UTF8.self) + } else { + schemeField = nil + authorityField = nil + pathField = nil + } + } + } + + /// Create an HTTP request with a method, a URL, and header fields. + /// - Parameters: + /// - method: The request method, defaults to GET. + /// - url: The URL to populate the scheme, authority, and path pseudo header fields. + /// - headerFields: The request header fields. + init(method: Method = .get, url: URL, headerFields: HTTPFields = [:]) { + let (scheme, authority, path) = url.httpRequestComponents + let schemeString = String(decoding: scheme, as: UTF8.self) + let authorityString = authority.map { String(decoding: $0, as: UTF8.self) } + let pathString = String(decoding: path, as: UTF8.self) + + self.init(method: method, scheme: schemeString, authority: authorityString, path: pathString, headerFields: headerFields) + } +} + +private extension URL { + init?(scheme: some Collection, authority: some Collection, path: some Collection) { + var buffer = [UInt8]() + buffer.reserveCapacity(scheme.count + 3 + authority.count + path.count) + buffer.append(contentsOf: scheme) + buffer.append(contentsOf: "://".utf8) + buffer.append(contentsOf: authority) + buffer.append(contentsOf: path) + + if let url = buffer.withUnsafeBytes({ buffer in + CFURLCreateAbsoluteURLWithBytes(kCFAllocatorDefault, buffer.baseAddress, buffer.count, CFStringBuiltInEncodings.ASCII.rawValue, nil, false).map { unsafeBitCast($0, to: NSURL.self) as URL } + }) { + self = url + } else { + return nil + } + } + + var httpRequestComponents: (scheme: [UInt8], authority: [UInt8]?, path: [UInt8]) { + // CFURL parser based on byte ranges does not unnecessarily percent-encode WHATWG URL + let url = unsafeBitCast(self as NSURL, to: CFURL.self) + let length = CFURLGetBytes(url, nil, 0) + return withUnsafeTemporaryAllocation(of: UInt8.self, capacity: length) { buffer in + CFURLGetBytes(url, buffer.baseAddress, buffer.count) + + func unionRange(_ a: CFRange, _ b: CFRange) -> CFRange { + if a.location == kCFNotFound { return b } + if b.location == kCFNotFound { return a } + return CFRange(location: a.location, length: b.location + b.length - a.location ) + } + + func bufferSlice(_ range: CFRange) -> some Collection { + buffer[range.location.. UInt8.max { + buffer.initializeElement(at: index, to: 0x20) + } else { + buffer.initializeElement(at: index, to: UInt8(truncatingIfNeeded: scalar.value)) + } + } + return HTTPField(name: name, value: buffer) + } + } + } + + var isoLatin1Value: String { + if value.isASCII { + return value + } else { + return withUnsafeValueBytes { buffer in + let scalars = buffer.lazy.map { UnicodeScalar(UInt32($0))! } + var string = "" + string.unicodeScalars.append(contentsOf: scalars) + return string + } + } + } +} diff --git a/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift new file mode 100644 index 0000000..188eb4e --- /dev/null +++ b/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import Foundation +import HTTPTypes +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +extension URLRequest { + /// Create a `URLRequest` from an `HTTPRequest`. + /// - Parameter httpRequest: The HTTP request to convert from. + public init?(httpRequest: HTTPRequest) { + guard let url = httpRequest.url else { + return nil + } + var request = URLRequest(url: url) + request.httpMethod = httpRequest.method.rawValue + var combinedFields = [HTTPField.Name: String](minimumCapacity: httpRequest.headerFields.count) + for field in httpRequest.headerFields { + if let existingValue = combinedFields[field.name] { + let separator = field.name == .cookie ? "; " : ", " + combinedFields[field.name] = "\(existingValue)\(separator)\(field.isoLatin1Value)" + } else { + combinedFields[field.name] = field.isoLatin1Value + } + } + var headerFields = [String: String](minimumCapacity: combinedFields.count) + for (name, value) in combinedFields { + headerFields[name.rawName] = value + } + request.allHTTPHeaderFields = headerFields + self = request + } + + /// Convert the `URLRequest` into an `HTTPRequest`. + public var httpRequest: HTTPRequest? { + guard let method = HTTPRequest.Method(httpMethod ?? "GET"), + let url else { + return nil + } + var request = HTTPRequest(method: method, url: url) + if let allHTTPHeaderFields { + request.headerFields.reserveCapacity(allHTTPHeaderFields.count) + for (name, value) in allHTTPHeaderFields { + if let name = HTTPField.Name(name) { + request.headerFields.append(HTTPField(name: name, isoLatin1Value: value)) + } + } + } + return request + } +} diff --git a/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift new file mode 100644 index 0000000..2258038 --- /dev/null +++ b/Sources/HTTPTypesFoundation/URLResponse+HTTPTypes.swift @@ -0,0 +1,57 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import Foundation +import HTTPTypes +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +extension HTTPURLResponse { + /// Create an `HTTPURLResponse` from an `HTTPResponse`. + /// - Parameter httpResponse: The HTTP response to convert from. + /// - Parameter url: The URL of the response. + public convenience init?(httpResponse: HTTPResponse, url: URL) { + var combinedFields = [HTTPField.Name: String](minimumCapacity: httpResponse.headerFields.count) + for field in httpResponse.headerFields { + if let existingValue = combinedFields[field.name] { + combinedFields[field.name] = "\(existingValue), \(field.isoLatin1Value)" + } else { + combinedFields[field.name] = field.isoLatin1Value + } + } + var headerFields = [String: String](minimumCapacity: combinedFields.count) + for (name, value) in combinedFields { + headerFields[name.rawName] = value + } + self.init(url: url, statusCode: httpResponse.status.code, httpVersion: "HTTP/1.1", headerFields: headerFields) + } + + /// Convert the `HTTPURLResponse` into an `HTTPResponse`. + public var httpResponse: HTTPResponse? { + guard (0...999).contains(statusCode) else { + return nil + } + var response = HTTPResponse(status: .init(code: statusCode)) + if let fields = allHeaderFields as? [String: String] { + response.headerFields.reserveCapacity(fields.count) + for (name, value) in fields { + if let name = HTTPField.Name(name) { + response.headerFields.append(HTTPField(name: name, isoLatin1Value: value)) + } + } + } + return response + } +} diff --git a/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift b/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift new file mode 100644 index 0000000..c29cd36 --- /dev/null +++ b/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift @@ -0,0 +1,128 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import Foundation +import HTTPTypes +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +extension URLSessionTask { + /// The original HTTP request this task was created with. + public var originalHTTPRequest: HTTPRequest? { + originalRequest?.httpRequest + } + + /// The current HTTP request -- may differ from the `originalHTTPRequest` due to HTTP redirection. + public var currentHTTPRequest: HTTPRequest? { + currentRequest?.httpRequest + } + + /// The HTTP response received from the server. + public var httpResponse: HTTPResponse? { + (response as? HTTPURLResponse)?.httpResponse + } +} + +private enum HTTPTypeConversionError: Error { + case failedToConvertHTTPRequestToURLRequest + case failedToConvertURLResponseToHTTPResponse +} + +#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) +extension URLSession { + /// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally. + /// + /// - Parameter request: The `HTTPRequest` for which to load data. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Data and response. + public func data(for request: HTTPRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, HTTPResponse) { + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await data(for: urlRequest, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) + } + + /// Convenience method to upload data using an `HTTPRequest`; creates and resumes a `URLSessionUploadTask` internally. + /// + /// - Parameter request: The `HTTPRequest` for which to upload data. + /// - Parameter fileURL: File to upload. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Data and response. + public func upload(for request: HTTPRequest, fromFile fileURL: URL, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, HTTPResponse) { + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await upload(for: urlRequest, fromFile: fileURL, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) + } + + /// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally. + /// + /// - Parameter request: The `HTTPRequest` for which to upload data. + /// - Parameter bodyData: Data to upload. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Data and response. + public func upload(for request: HTTPRequest, from bodyData: Data, delegate: URLSessionTaskDelegate? = nil) async throws -> (Data, HTTPResponse) { + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await upload(for: urlRequest, from: bodyData, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) + } + + /// Convenience method to download using an `HTTPRequest`; creates and resumes a `URLSessionDownloadTask` internally. + /// + /// - Parameter request: The `HTTPRequest` for which to download. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Downloaded file URL and response. The file will not be removed automatically. + public func download(for request: HTTPRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (URL, HTTPResponse) { + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (location, urlResponse) = try await download(for: urlRequest, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (location, response) + } + + /// Returns a byte stream that conforms to AsyncSequence protocol. + /// + /// - Parameter request: The `HTTPRequest` for which to load data. + /// - Parameter delegate: Task-specific delegate. + /// - Returns: Data stream and response. + public func bytes(for request: HTTPRequest, delegate: URLSessionTaskDelegate? = nil) async throws -> (AsyncBytes, HTTPResponse) { + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await bytes(for: urlRequest, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) + } +} +#endif diff --git a/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift new file mode 100644 index 0000000..3c17e9a --- /dev/null +++ b/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift @@ -0,0 +1,112 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import XCTest +import HTTPTypes +import HTTPTypesFoundation +#if canImport(FoundationNetworking) +import FoundationNetworking +#endif + +final class HTTPTypesFoundationTests: XCTestCase { + func testRequestURLParsing() { + let request1 = HTTPRequest(url: URL(string: "h://a")!) + XCTAssertEqual(request1.schemeField?.value, "h") + XCTAssertEqual(request1.authorityField?.value, "a") + XCTAssertEqual(request1.pathField?.value, "/") + XCTAssertEqual(request1.url?.absoluteString, "h://a/") + + let request2 = HTTPRequest(url: URL(string: "h://a:4?")!) + XCTAssertEqual(request2.schemeField?.value, "h") + XCTAssertEqual(request2.authorityField?.value, "a:4") + XCTAssertEqual(request2.pathField?.value, "/?") + XCTAssertEqual(request2.url?.absoluteString, "h://a:4/?") + + let request3 = HTTPRequest(url: URL(string: "h://a/")!) + XCTAssertEqual(request3.schemeField?.value, "h") + XCTAssertEqual(request3.authorityField?.value, "a") + XCTAssertEqual(request3.pathField?.value, "/") + XCTAssertEqual(request3.url?.absoluteString, "h://a/") + + let request4 = HTTPRequest(url: URL(string: "h://a/p?q#1")!) + XCTAssertEqual(request4.schemeField?.value, "h") + XCTAssertEqual(request4.authorityField?.value, "a") + XCTAssertEqual(request4.pathField?.value, "/p?q") + XCTAssertEqual(request4.url?.absoluteString, "h://a/p?q") + + let request5 = HTTPRequest(url: URL(string: "data:,Hello%2C%20World%21")!) + XCTAssertEqual(request5.schemeField?.value, "data") + XCTAssertNil(request5.authorityField) + XCTAssertEqual(request5.pathField?.value, "/") + XCTAssertNil(request5.url) + } + + func testRequestToFoundation() throws { + let request = HTTPRequest( + method: .get, scheme: "https", authority: "www.example.com", path: "/", + headerFields: [ + .accept: "*/*", + .acceptEncoding: "gzip", + .acceptEncoding: "br", + .cookie: "a=b", + .cookie: "c=d", + ] + ) + + let urlRequest = try XCTUnwrap(URLRequest(httpRequest: request)) + XCTAssertEqual(urlRequest.url, URL(string: "https://www.example.com/")!) + XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "aCcEpT"), "*/*") + XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "Accept-Encoding"), "gzip, br") + XCTAssertEqual(urlRequest.value(forHTTPHeaderField: "cookie"), "a=b; c=d") + } + + func testRequestFromFoundation() throws { + var urlRequest = URLRequest(url: URL(string: "https://www.example.com/")!) + urlRequest.httpMethod = "POST" + urlRequest.setValue("Bar", forHTTPHeaderField: "X-Foo") + + let request = try XCTUnwrap(urlRequest.httpRequest) + XCTAssertEqual(request.method, .post) + XCTAssertEqual(request.scheme, "https") + XCTAssertEqual(request.authority, "www.example.com") + XCTAssertEqual(request.path, "/") + XCTAssertEqual(request.headerFields[.init("x-foo")!], "Bar") + } + + func testResponseToFoundation() throws { + let response = HTTPResponse( + status: .ok, + headerFields: [ + .server: "HTTPServer/1.0", + ] + ) + + let urlResponse = try XCTUnwrap(HTTPURLResponse(httpResponse: response, url: URL(string: "https://www.example.com/")!)) + XCTAssertEqual(urlResponse.statusCode, 200) + XCTAssertEqual(urlResponse.value(forHTTPHeaderField: "Server"), "HTTPServer/1.0") + } + + func testResponseFromFoundation() throws { + let urlResponse = HTTPURLResponse( + url: URL(string: "https://www.example.com/")!, statusCode: 204, httpVersion: nil, + headerFields: [ + "X-Emoji": "😀" + ] + )! + + let response = try XCTUnwrap(urlResponse.httpResponse) + XCTAssertEqual(response.status, .noContent) + XCTAssertEqual(response.headerFields[.init("X-EMOJI")!], "😀") + } +} diff --git a/Tests/HTTPTypesTests/HTTPTypesTests.swift b/Tests/HTTPTypesTests/HTTPTypesTests.swift new file mode 100644 index 0000000..1acba48 --- /dev/null +++ b/Tests/HTTPTypesTests/HTTPTypesTests.swift @@ -0,0 +1,75 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift HTTP Types open source project +// +// Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import XCTest +import HTTPTypes + +extension HTTPField.Name { + static let acceptEncodingLower = HTTPField.Name("accept-encoding")! + static let acceptEncodingMixed = HTTPField.Name("aCcEpT-eNcOdInG")! + static let acceptEncodingUpper = HTTPField.Name("ACCEPT-ENCODING")! + static let acceptLanguageUpper = HTTPField.Name("ACCEPT-LANGUAGE")! +} + +final class HTTPTypesTests: XCTestCase { + func testFields() { + var fields = HTTPFields() + fields[.acceptEncoding] = "gzip" + fields.append(HTTPField(name: .acceptEncodingLower, value: "br")) + fields.insert(HTTPField(name: .acceptEncodingMixed, value: "deflate"), at: 1) + + XCTAssertEqual(fields[.acceptEncoding], "gzip, deflate, br") + XCTAssertEqual(fields[raw: .acceptEncodingUpper].count, 3) + } + + func testFieldValue() { + XCTAssertEqual(HTTPField(name: .accept, value: " \n 😀 \t ").value, "😀") + XCTAssertEqual(HTTPField(name: .accept, value: " a 😀 \t\n b \t \r ").value, "a 😀 \t b") + XCTAssertEqual(HTTPField(name: .accept, value: "").value, "") + XCTAssertFalse(HTTPField.isValidValue(" ")) + } + + func testRequest() { + var request1 = HTTPRequest(method: .get, scheme: "https", authority: "www.example.com", path: "/") + request1.headerFields = [ + .acceptLanguage: "en" + ] + var request2 = HTTPRequest(method: HTTPRequest.Method("GET")!, scheme: "https", authority: "www.example.com", path: "/") + request2.headerFields.append(HTTPField(name: .acceptLanguageUpper, value: "en")) + + XCTAssertEqual(request2.method, .get) + XCTAssertEqual(request1, request2) + } + + func testResponse() { + var response1 = HTTPResponse(status: 200) + response1.headerFields = [ + .server: "HTTPServer/1.0", + .contentLength: "0" + ] + + var response2 = response1 + response2.status = .movedPermanently + response2.headerFields.append(HTTPField(name: .location, value: "https://www.example.com/new")) + + XCTAssertEqual(response1.status, .ok) + XCTAssertTrue(response1.status.isSuccessful) + XCTAssertEqual(response1.headerFields.count, 2) + + XCTAssertEqual(response2.status, 301) + XCTAssertTrue(response2.status.isRedirection) + XCTAssertEqual(response2.headerFields.count, 3) + XCTAssertEqual(response2.headerFields[.server], "HTTPServer/1.0") + } +} diff --git a/scripts/generate_contributors_list.sh b/scripts/generate_contributors_list.sh new file mode 100755 index 0000000..7509c31 --- /dev/null +++ b/scripts/generate_contributors_list.sh @@ -0,0 +1,39 @@ +#!/bin/sh +##===----------------------------------------------------------------------===## +## +## This source file is part of the Swift HTTP Types open source project +## +## Copyright (c) 2023 Apple Inc. and the Swift HTTP Types project authors +## Licensed under Apache License v2.0 +## +## See LICENSE.txt for license information +## See CONTRIBUTORS.txt for the list of Swift HTTP Types project authors +## +## SPDX-License-Identifier: Apache-2.0 +## +##===----------------------------------------------------------------------===## + +set -eu +here="$( cd "$( dirname "$0" )" && pwd )" +contributors=$( cd "$here"/.. && git shortlog -es | cut -f2 | sed 's/^/- /' ) + +cat > "$here/../CONTRIBUTORS.txt" <<- EOF + For the purpose of tracking copyright, this is the list of individuals and + organizations who have contributed source code to the Swift HTTP Types. + + For employees of an organization/company where the copyright of work done + by employees of that company is held by the company itself, only the company + needs to be listed here. + + ## COPYRIGHT HOLDERS + + - Apple Inc. (all contributors with '@apple.com') + + ### Contributors + + $contributors + + **Updating this list** + + Please do not edit this file manually. It is generated using \`./scripts/generate_contributors_list.sh\`. If a name is misspelled or appearing multiple times: add an entry in \`./.mailmap\` +EOF