|
9 | 9 | import Foundation
|
10 | 10 |
|
11 | 11 | /// Typealias for a closure that handles an incoming HTTP request
|
12 |
| -/// - Parameter req: the incoming HTTP request. |
13 |
| -/// - Parameter res: a writer providing functions to create an HTTP reponse to the request. |
14 |
| -/// - Returns HTTPBodyProcessing: a enum that either discards the request data, or provides a closure to process it. |
15 |
| -/// - See: `HTTPRequestHandling` for more information |
16 |
| -public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing |
17 |
| - |
18 |
| -/// Class protocol containing the HTTPRequestHandler that responds to the incoming HTTP requests. |
19 |
| -/// The following is an example of a HTTPRequestHandler that returns the request as a response: |
20 |
| -/// |
| 12 | +/// The following is an example of an echo `HTTPRequestHandler` that returns the request it receives as a response: |
21 | 13 | /// ```swift
|
22 |
| -/// class EchoHandler: HTTPRequestHandling { |
23 |
| -/// func handle(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { |
24 |
| -/// res.writeHeader(status: .ok, headers: [:]) |
25 |
| -/// return .processBody { (chunk, stop) in |
26 |
| -/// switch chunk { |
27 |
| -/// case .chunk(let data, let finishedProcessing): |
28 |
| -/// res.writeBody(data) { _ in |
29 |
| -/// finishedProcessing() |
30 |
| -/// } |
31 |
| -/// case .end: |
32 |
| -/// res.done() |
33 |
| -/// default: |
34 |
| -/// stop = true |
35 |
| -/// res.abort() |
| 14 | +/// func echo(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { |
| 15 | +/// response.writeHeader(status: .ok) |
| 16 | +/// return .processBody { (chunk, stop) in |
| 17 | +/// switch chunk { |
| 18 | +/// case .chunk(let data, let finishedProcessing): |
| 19 | +/// response.writeBody(data) { _ in |
| 20 | +/// finishedProcessing() |
36 | 21 | /// }
|
| 22 | +/// case .end: |
| 23 | +/// response.done() |
| 24 | +/// default: |
| 25 | +/// stop = true |
| 26 | +/// response.abort() |
37 | 27 | /// }
|
38 | 28 | /// }
|
39 | 29 | /// }
|
40 | 30 | /// ```
|
| 31 | +/// This then needs to be registered with the server using `HTTPServer.start(port:handler:)` |
| 32 | +/// - Parameter req: the incoming HTTP request. |
| 33 | +/// - Parameter res: a writer providing functions to create an HTTP reponse to the request. |
| 34 | +/// - Returns HTTPBodyProcessing: a enum that either discards the request data, or provides a closure to process it. |
| 35 | +public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing |
| 36 | + |
| 37 | +/// Class protocol containing a `handle()` function that implements `HTTPRequestHandler` to respond to incoming HTTP requests. |
| 38 | +/// - See: `HTTPRequestHandler` for more information |
41 | 39 | public protocol HTTPRequestHandling: class {
|
42 |
| - /// handle: function called when a new HTTP request is received by the HTTP server. |
| 40 | + /// handle: function that implements `HTTPRequestHandler` and is called when a new HTTP request is received by the HTTP server. |
43 | 41 | /// - Parameter request: the incoming HTTP request.
|
44 | 42 | /// - Parameter response: a writer providing functions to create an HTTP response to the request.
|
45 | 43 | /// - Returns HTTPBodyProcessing: a enum that either discards the request data, or provides a closure to process it.
|
| 44 | + /// - See: `HTTPRequestHandler` for more information |
46 | 45 | func handle(request: HTTPRequest, response: HTTPResponseWriter) -> HTTPBodyProcessing
|
47 | 46 | }
|
48 | 47 |
|
|
0 commit comments