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
15 changes: 3 additions & 12 deletions Examples/EchoServer/EchoServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,9 @@ struct EchoServer {
}

static func echo<Server: HTTPServer>(server: Server) async throws {
try await server.serve { request, requestContext, requestBodyAndTrailers, responseSender in
// Needed since we are lacking call-once closures
var requestBodyAndTrailers = Optional(requestBodyAndTrailers)
let responseBodyAndTrailers = try await responseSender.send(.init(status: .ok))

try await responseBodyAndTrailers.produceAndConclude { responseBody in
// Needed since we are lacking call-once closures
var responseBody = responseBody
return try await requestBodyAndTrailers.take()!.consumeAndConclude { reader in
try await responseBody.write(reader)
}
}
try await server.serve { request, requestContext, reader, responseSender in
let writer = try await responseSender.send(.init(status: .ok))
try await reader.pipe(into: writer)
}
}
}
2 changes: 1 addition & 1 deletion Examples/ExampleMiddleware/ForwardingMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct ForwardingMiddleware<Input: ~Copyable & ~Escapable>: Middleware {
}

@available(anyAppleOS 26.0, *)
extension Middleware {
extension Middleware where Input: ~Copyable & ~Escapable, NextInput: ~Copyable & ~Escapable {
public func forwarding() -> ForwardingMiddleware<Input> {
ForwardingMiddleware()
}
Expand Down
37 changes: 37 additions & 0 deletions Examples/ExampleMiddleware/HTTPClientMiddlewareInput.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift HTTP API Proposal open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift HTTP API Proposal project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

public import AsyncStreaming
public import HTTPAPIs

/// The input passed through client-side middleware: the request head plus the
/// request body the user wants to send.
///
/// Mirrors ``HTTPServerMiddlewareInput`` on the server side. Wrapping
/// middlewares can substitute a different `Writer` type for `NextInput` so
/// the inner stage sees a wrapped body that intercepts the bytes the user
/// wrote.
@available(anyAppleOS 26.0, *)
public struct HTTPClientMiddlewareInput<Writer: CallerAsyncWriter & ~Copyable & SendableMetatype>: ~Copyable
where Writer.WriteElement == UInt8, Writer.FinalElement == HTTPFields? {
public var request: HTTPRequest
public var body: HTTPClientRequestBody<Writer>?

public init(request: HTTPRequest, body: consuming HTTPClientRequestBody<Writer>?) {
self.request = request
self.body = body
}
}

@available(*, unavailable)
extension HTTPClientMiddlewareInput: Sendable {}
Loading
Loading