Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make HttpServer an open class for customization
Turn `HttpServer` into an open class to allow for slightly more
customization than the `middleware` property allows.

For my particular use case, I'm trying to add some simple logging, which
would be an easy thing to do if I could just put a small wrapper around
`dispatch`.

I only made the `dispatch` method open here, since it seems like an
obviously good customization point that would need inheritance instead
of containment to customize.
  • Loading branch information
Andrew Cobb committed Feb 27, 2020
commit 398b81ed05ea042e6861789859173061ae9ff449
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file. Changes not

## Changed

- Turn `HttpServer` and `HttpServerIO` into open classes to allow for more customization. (#???) by [@cobbal](https://github.com/cobbal)
- Set the version of the HTTP Server based in the project version in the **Info.plist** for macOS, iOS and tvOS platforms. ([#416](https://github.com/httpswift/swifter/pull/416)) by [@Vkt0r](https://github.com/Vkt0r)
- Update `HttpParser` so it percent-encodes the URL components before initializing `URLComponents`. ([#423](https://github.com/httpswift/swifter/pull/423)) by [@nejcvivod](https://github.com/nejcvivod)
- Update `SwifterTestsHttpParser` with a test for parsing bracketed query strings. ([#423](https://github.com/httpswift/swifter/pull/423)) by [@nejcvivod](https://github.com/nejcvivod)
Expand Down
4 changes: 2 additions & 2 deletions XCode/Sources/HttpServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public class HttpServer: HttpServerIO {
open class HttpServer: HttpServerIO {

public static let VERSION: String = {

Expand Down Expand Up @@ -56,7 +56,7 @@ public class HttpServer: HttpServerIO {

public var middleware = [(HttpRequest) -> HttpResponse?]()

override public func dispatch(_ request: HttpRequest) -> ([String: String], (HttpRequest) -> HttpResponse) {
override open func dispatch(_ request: HttpRequest) -> ([String: String], (HttpRequest) -> HttpResponse) {
for layer in middleware {
if let response = layer(request) {
return ([:], { _ in response })
Expand Down
4 changes: 2 additions & 2 deletions XCode/Sources/HttpServerIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public protocol HttpServerIODelegate: class {
func socketConnectionReceived(_ socket: Socket)
}

public class HttpServerIO {
open class HttpServerIO {

public weak var delegate: HttpServerIODelegate?

Expand Down Expand Up @@ -111,7 +111,7 @@ public class HttpServerIO {
self.state = .stopped
}

public func dispatch(_ request: HttpRequest) -> ([String: String], (HttpRequest) -> HttpResponse) {
open func dispatch(_ request: HttpRequest) -> ([String: String], (HttpRequest) -> HttpResponse) {
return ([:], { _ in HttpResponse.notFound })
}

Expand Down