Skip to content
This repository was archived by the owner on Nov 23, 2021. It is now read-only.

Commit 811940b

Browse files
authored
Update Jazzy docs with handler example and other minor improvements (#59)
1 parent 3aa8b03 commit 811940b

File tree

4 files changed

+29
-30
lines changed

4 files changed

+29
-30
lines changed

.jazzy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ custom_categories:
2020
children:
2121
- HTTPServer
2222
- HTTPServing
23-
- HTTPRequestHandling
2423
- HTTPRequestHandler
24+
- HTTPRequestHandling
2525

2626
- name: HTTP Headers
2727
children:

Sources/HTTP/HTTPCommon.swift

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,39 @@
99
import Foundation
1010

1111
/// 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:
2113
/// ```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()
3621
/// }
22+
/// case .end:
23+
/// response.done()
24+
/// default:
25+
/// stop = true
26+
/// response.abort()
3727
/// }
3828
/// }
3929
/// }
4030
/// ```
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
4139
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.
4341
/// - Parameter request: the incoming HTTP request.
4442
/// - Parameter response: a writer providing functions to create an HTTP response to the request.
4543
/// - Returns HTTPBodyProcessing: a enum that either discards the request data, or provides a closure to process it.
44+
/// - See: `HTTPRequestHandler` for more information
4645
func handle(request: HTTPRequest, response: HTTPResponseWriter) -> HTTPBodyProcessing
4746
}
4847

Sources/HTTP/HTTPRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public struct HTTPRequest {
2121
public var headers: HTTPHeaders
2222
}
2323

24-
/// Method that takes a chunk of request body and is expected to write to the ResponseWriter
24+
/// Method that takes a chunk of request body and is expected to write to the `HTTPResponseWriter`
2525
/// - Parameter HTTPBodyChunk: `HTTPBodyChunk` representing some or all of the incoming request body
26-
/// - Parameter Bool: Bool that can be set to true in order to prevent further processing
26+
/// - Parameter Bool: A boolean flag that can be set to true in order to prevent further processing
2727
public typealias HTTPBodyHandler = (HTTPBodyChunk, inout Bool) -> Void
2828

2929
/// Indicates whether the body is going to be processed or ignored

Sources/HTTP/HTTPServer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public protocol HTTPServing : class {
2828
public class HTTPServer: HTTPServing {
2929
private let server = PoCSocketSimpleServer()
3030

31-
/// Create an instance of the server. This needs to be followed with a call to `start()`
31+
/// Create an instance of the server. This needs to be followed with a call to `start(port:handler:)`
3232
public init() {
3333
}
3434

35-
/// Start the HTTP server on the given `port`, using `handler` to process incoming requests
35+
/// Start the HTTP server on the given `port` number, using a `HTTPRequestHandler` to process incoming requests.
3636
public func start(port: Int = 0, handler: @escaping HTTPRequestHandler) throws {
3737
try server.start(port: port, handler: handler)
3838
}
@@ -42,7 +42,7 @@ public class HTTPServer: HTTPServing {
4242
server.stop()
4343
}
4444

45-
/// The port the server is listening on
45+
/// The port number the server is listening on
4646
public var port: Int {
4747
return server.port
4848
}

0 commit comments

Comments
 (0)