Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions FlyingFox/Sources/HTTPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,14 @@ struct HTTPDecoder {

let method = HTTPMethod(String(comps[0]))
let version = HTTPVersion(String(comps[2]))
let (path, query) = readComponents(from: String(comps[1]))

let target = makeTarget(from: comps[1])
let headers = try await readHeaders(from: bytes)
let body = try await readBody(from: bytes, length: headers[.contentLength])

return HTTPRequest(
method: method,
version: version,
path: path,
query: query,
target: target,
headers: HTTPHeaders(headers),
body: body
)
Expand Down Expand Up @@ -87,7 +85,21 @@ struct HTTPDecoder {
}

func readComponents(from target: String) -> (path: String, query: [HTTPRequest.QueryItem]) {
makeComponents(from: URLComponents(string: target))
makeComponents(from: makeTarget(from: target))
}

func makeTarget(from target: some StringProtocol) -> HTTPRequest.Target {
let comps = target.split(separator: "?", maxSplits: 1, omittingEmptySubsequences: false)
let path = comps.first ?? ""
let query = comps.count > 1 ? comps[1] : ""
return HTTPRequest.Target(
path: String(path),
query: String(query)
)
}

func makeComponents(from target: HTTPRequest.Target) -> (path: String, query: [HTTPRequest.QueryItem]) {
makeComponents(from: URLComponents(string: target.rawValue))
}

func makeComponents(from comps: URLComponents?) -> (path: String, query: [HTTPRequest.QueryItem]) {
Expand Down Expand Up @@ -142,6 +154,10 @@ struct HTTPDecoder {

extension HTTPDecoder {

init() {
self.init(sharedRequestBufferSize: 128, sharedRequestReplaySize: 1024)
}

struct Error: LocalizedError {
var errorDescription: String?

Expand Down
10 changes: 10 additions & 0 deletions FlyingFox/Sources/HTTPEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,14 @@ struct HTTPEncoder {

return data
}

static func makeTarget(path: String, query: [HTTPRequest.QueryItem]) -> HTTPRequest.Target {
var comps = URLComponents()
comps.path = path
comps.queryItems = query.map { .init(name: $0.name, value: $0.value) }
return HTTPRequest.Target(
path: comps.percentEncodedPath,
query: comps.percentEncodedQuery ?? ""
)
}
}
71 changes: 71 additions & 0 deletions FlyingFox/Sources/HTTPRequest+Target.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// HTTPRequest+Target.swift
// FlyingFox
//
// Created by Simon Whitty on 08/11/2025.
// Copyright © 2025 Simon Whitty. All rights reserved.
//
// Distributed under the permissive MIT license
// Get the latest version from here:
//
// https://github.com/swhitty/FlyingFox
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import Foundation

public extension HTTPRequest {

// RFC9112: e.g. /a%2Fb?q=1
struct Target: Sendable, Equatable {

// raw percent encoded path e.g. /fish%20chips
private var _path: String

// raw percent encoded query string e.g. q=fish%26chips&qty=15
private var _query: String

public init(path: String, query: String) {
self._path = path
self._query = query
}

public func path(percentEncoded: Bool = true) -> String {
guard percentEncoded else {
return _path.removingPercentEncoding ?? _path
}
return _path
}

public func query(percentEncoded: Bool = true) -> String {
guard percentEncoded else {
return _query.removingPercentEncoding ?? _query
}
return _query
}

public var rawValue: String {
guard !_query.isEmpty else {
return _path
}
return "\(_path)?\(_query)"
}
}
}
51 changes: 34 additions & 17 deletions FlyingFox/Sources/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import Foundation
public struct HTTPRequest: Sendable {
public var method: HTTPMethod
public var version: HTTPVersion
public var target: Target

public var path: String
public var query: [QueryItem]
public var headers: HTTPHeaders
Expand All @@ -58,15 +60,35 @@ public struct HTTPRequest: Sendable {
set { fatalError("unavailable") }
}

public init(method: HTTPMethod,
version: HTTPVersion,
path: String,
query: [QueryItem],
headers: HTTPHeaders,
body: HTTPBodySequence,
remoteAddress: Address? = nil) {
public init(
method: HTTPMethod,
version: HTTPVersion,
target: Target,
headers: HTTPHeaders,
body: HTTPBodySequence,
remoteAddress: Address? = nil
) {
self.method = method
self.version = version
self.target = target
(self.path, self.query) = HTTPDecoder().makeComponents(from: target)
self.headers = headers
self.bodySequence = body
self.remoteAddress = remoteAddress
}

public init(
method: HTTPMethod,
version: HTTPVersion,
path: String,
query: [QueryItem],
headers: HTTPHeaders,
body: HTTPBodySequence,
remoteAddress: Address? = nil
) {
self.method = method
self.version = version
self.target = HTTPEncoder.makeTarget(path: path, query: query)
self.path = path
self.query = query
self.headers = headers
Expand All @@ -79,9 +101,11 @@ public struct HTTPRequest: Sendable {
path: String,
query: [QueryItem],
headers: [HTTPHeader: String],
body: Data) {
body: Data
) {
self.method = method
self.version = version
self.target = HTTPEncoder.makeTarget(path: path, query: query)
self.path = path
self.query = query
self.headers = HTTPHeaders(headers)
Expand All @@ -90,24 +114,17 @@ public struct HTTPRequest: Sendable {
}
}

@available(*, deprecated, message: "Use ``HTTPHeaders`` instead of [HTTPHeader: String]")
public extension HTTPRequest {

@available(*, unavailable, message: "Use ``HTTPHeaders`` instead of [HTTPHeader: String]")
init(method: HTTPMethod,
version: HTTPVersion,
path: String,
query: [QueryItem],
headers: [HTTPHeader: String],
body: HTTPBodySequence
) {
self.init(
method: method,
version: version,
path: path,
query: query,
headers: HTTPHeaders(headers),
body: body
)
fatalError("unavailable")
}
}

Expand Down
6 changes: 0 additions & 6 deletions FlyingFox/Sources/HTTPRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,3 @@ public extension Array where Element == HTTPRoute.Parameter {
}
}
}

private extension HTTPDecoder {
init() {
self.init(sharedRequestBufferSize: 128, sharedRequestReplaySize: 1024)
}
}
1 change: 1 addition & 0 deletions FlyingFox/Tests/HTTPConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct HTTPConnectionTests {
)

let request = try await connection.requests.first()
print(request)
#expect(
await request == .make(
method: .GET,
Expand Down
7 changes: 2 additions & 5 deletions FlyingFox/Tests/HTTPDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct HTTPDecoderTests {

@Test
func invalidPathDecodes() {
let comps = HTTPDecoder.make().makeComponents(from: nil)
let comps = HTTPDecoder.make().readComponents(from: "")
#expect(comps.path == "")
#expect(comps.query == [])
}
Expand Down Expand Up @@ -243,11 +243,8 @@ struct HTTPDecoderTests {

@Test
func emptyQueryItem_Decodes() {
var urlComps = URLComponents()
urlComps.queryItems = [.init(name: "name", value: nil)]

#expect(
HTTPDecoder.make().makeComponents(from: urlComps).query == [
HTTPDecoder.make().readComponents(from: "/?name=").query == [
.init(name: "name", value: "")
]
)
Expand Down
50 changes: 36 additions & 14 deletions FlyingFox/Tests/HTTPRequest+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,42 @@
import Foundation

extension HTTPRequest {
static func make(method: HTTPMethod = .GET,
version: HTTPVersion = .http11,
path: String = "/",
query: [QueryItem] = [],
headers: HTTPHeaders = [:],
body: Data = Data(),
remoteAddress: Address? = nil) -> Self {
HTTPRequest(method: method,
version: version,
path: path,
query: query,
headers: headers,
body: HTTPBodySequence(data: body),
remoteAddress: remoteAddress)
static func make(
method: HTTPMethod = .GET,
version: HTTPVersion = .http11,
target: String = "/",
headers: HTTPHeaders = [:],
body: Data = Data(),
remoteAddress: Address? = nil
) -> Self {
HTTPRequest(
method: method,
version: version,
target: HTTPDecoder().makeTarget(from: target),
headers: headers,
body: HTTPBodySequence(data: body),
remoteAddress: remoteAddress
)
}

static func make(
method: HTTPMethod = .GET,
version: HTTPVersion = .http11,
path: String = "/",
query: [QueryItem] = [],
headers: HTTPHeaders = [:],
body: Data = Data(),
remoteAddress: Address? = nil
) -> Self {
HTTPRequest(
method: method,
version: version,
path: path,
query: query,
headers: headers,
body: HTTPBodySequence(data: body),
remoteAddress: remoteAddress
)
}

static func make(method: HTTPMethod = .GET, _ url: String, headers: HTTPHeaders = [:]) -> Self {
Expand Down
35 changes: 35 additions & 0 deletions FlyingFox/Tests/HTTPRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,39 @@ struct HTTPRequestTests {
try await request.bodyData == Data([0x05, 0x06])
)
}

@Test
func targetIsCreated() {
// when
let request = HTTPRequest.make(path: "/meal plan/order", query: [
.init(name: "food", value: "fish & chips"),
.init(name: "qty", value: "15")
])

// then
#expect(request.target.rawValue == "/meal%20plan/order?food=fish%20%26%20chips&qty=15")
#expect(request.target.path() == "/meal%20plan/order")
#expect(request.target.path(percentEncoded: false) == "/meal plan/order")
#expect(request.target.query() == "food=fish%20%26%20chips&qty=15")
#expect(request.target.query(percentEncoded: false) == "food=fish & chips&qty=15")
}

@Test
func pathIsCreated() {
// when
let request = HTTPRequest.make(
target: "/meal%20plan/order?food=fish%20%26%20chips&qty=15"
)

// then
#expect(request.path == "/meal plan/order")
#expect(request.query == [
.init(name: "food", value: "fish & chips"),
.init(name: "qty", value: "15")
])
#expect(request.target.path() == "/meal%20plan/order")
#expect(request.target.path(percentEncoded: false) == "/meal plan/order")
#expect(request.target.query() == "food=fish%20%26%20chips&qty=15")
#expect(request.target.query(percentEncoded: false) == "food=fish & chips&qty=15")
}
}
Loading