|
| 1 | +public import HTTPTypes |
| 2 | + |
| 3 | +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 4 | +/// A generic HTTP server protocol that can handle incoming HTTP requests. |
| 5 | +public protocol HTTPServerProtocol: Sendable, ~Copyable, ~Escapable { |
| 6 | + // TODO: write down in the proposal why we can't make the serve method generic over the handler (closure-based APIs can't |
| 7 | + // be implemented) |
| 8 | + |
| 9 | + /// The ``HTTPServerRequestHandler`` to use when handling requests. |
| 10 | + associatedtype RequestHandler: HTTPServerRequestHandler |
| 11 | + |
| 12 | + /// Starts an HTTP server with the specified request handler. |
| 13 | + /// |
| 14 | + /// This method creates and runs an HTTP server that processes incoming requests using the provided |
| 15 | + /// ``HTTPServerRequestHandler`` implementation. |
| 16 | + /// |
| 17 | + /// Implementations of this method should handle each connection concurrently using Swift's structured concurrency. |
| 18 | + /// |
| 19 | + /// - Parameters: |
| 20 | + /// - handler: A ``HTTPServerRequestHandler`` implementation that processes incoming HTTP requests. The handler |
| 21 | + /// receives each request along with a body reader and ``HTTPResponseSender``. |
| 22 | + /// |
| 23 | + /// ## Example |
| 24 | + /// |
| 25 | + /// ```swift |
| 26 | + /// struct EchoHandler: HTTPServerRequestHandler { |
| 27 | + /// func handle( |
| 28 | + /// request: HTTPRequest, |
| 29 | + /// requestBodyAndTrailers: consuming HTTPRequestConcludingAsyncReader, |
| 30 | + /// responseSender: consuming HTTPResponseSender<HTTPResponseConcludingAsyncWriter> |
| 31 | + /// ) async throws { |
| 32 | + /// let response = HTTPResponse(status: .ok) |
| 33 | + /// let writer = try await responseSender.send(response) |
| 34 | + /// // Handle request and write response... |
| 35 | + /// } |
| 36 | + /// } |
| 37 | + /// |
| 38 | + /// let server = // create an instance of a type conforming to the `ServerProtocol` |
| 39 | + /// |
| 40 | + /// try await server.serve(handler: EchoHandler()) |
| 41 | + /// ``` |
| 42 | + func serve(handler: RequestHandler) async throws |
| 43 | +} |
| 44 | + |
| 45 | +@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) |
| 46 | +extension HTTPServerProtocol where RequestHandler == HTTPServerClosureRequestHandler<HTTPRequestConcludingAsyncReader, HTTPResponseConcludingAsyncWriter> { |
| 47 | + /// Starts an HTTP server with a closure-based request handler. |
| 48 | + /// |
| 49 | + /// This method provides a convenient way to start an HTTP server using a closure to handle incoming requests. |
| 50 | + /// |
| 51 | + /// - Parameters: |
| 52 | + /// - handler: An async closure that processes HTTP requests. The closure receives: |
| 53 | + /// - `HTTPRequest`: The incoming HTTP request with headers and metadata |
| 54 | + /// - ``HTTPRequestConcludingAsyncReader``: An async reader for consuming the request body and trailers |
| 55 | + /// - ``HTTPResponseSender``: A non-copyable wrapper for a function that accepts an `HTTPResponse` and provides access to an ``HTTPResponseConcludingAsyncWriter`` |
| 56 | + /// |
| 57 | + /// ## Example |
| 58 | + /// |
| 59 | + /// ```swift |
| 60 | + /// try await server.serve { request, bodyReader, sendResponse in |
| 61 | + /// // Process the request |
| 62 | + /// let response = HTTPResponse(status: .ok) |
| 63 | + /// let writer = try await sendResponse(response) |
| 64 | + /// try await writer.produceAndConclude { writer in |
| 65 | + /// try await writer.write("Hello, World!".utf8) |
| 66 | + /// return ((), nil) |
| 67 | + /// } |
| 68 | + /// } |
| 69 | + /// ``` |
| 70 | + public func serve( |
| 71 | + handler: @Sendable @escaping ( |
| 72 | + _ request: HTTPRequest, |
| 73 | + _ requestBodyAndTrailers: consuming HTTPRequestConcludingAsyncReader, |
| 74 | + _ responseSender: consuming HTTPResponseSender<HTTPResponseConcludingAsyncWriter> |
| 75 | + ) async throws -> Void |
| 76 | + ) async throws { |
| 77 | + try await self.serve(handler: HTTPServerClosureRequestHandler(handler: handler)) |
| 78 | + } |
| 79 | +} |
0 commit comments