Skip to content

Commit e531e9d

Browse files
authored
Merge pull request #412 from hibento/stable
Changes HttpResponse html(String) behaviour
2 parents 0152210 + 896b4a4 commit e531e9d

File tree

8 files changed

+31
-19
lines changed

8 files changed

+31
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All notable changes to this project will be documented in this file. Changes not
2222
- A new `CHANGELOG.md` to keep track of changes in the project. ([#385](https://github.com/httpswift/swifter/pull/385)) by [@Vkt0r](https://github.com/Vkt0r)
2323
- Added [Danger](https://danger.systems/ruby/) and Swiftlint to the project. ([#398](https://github.com/httpswift/swifter/pull/398)) by [@Vkt0r](https://github.com/Vkt0r)
2424
- Added the following to `Scopes`: `manifest`, `ontouchstart`, `dataText`. ([#410](https://github.com/httpswift/swifter/pull/410)) by [@apocolipse](https://github.com/apocolipse)
25+
- Added `htmlBody(String)` to `HttpResonse` as a compability case for the changed `html(String)` case.
2526

2627
## Fixed
2728
- An issue causing a crash regarding a thread race condition. ([#399](https://github.com/httpswift/swifter/pull/399)) by [@Vkt0r](https://github.com/Vkt0r)
@@ -37,7 +38,8 @@ All notable changes to this project will be documented in this file. Changes not
3738
- Refactor: Use Foundation API for Base64 encoding. ([#403](https://github.com/httpswift/swifter/pull/403)) by [@mazyod](https://github.com/mazyod)
3839
- Refactor: Use `URLComponents` for `HttpRequest` path and query parameters parsing [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
3940
- `HttpResponse` functions `statusCode()` and `reasonPhrase` changed to computed variables instead of functions, and made public (No impact on existing usage as it was previously internal). ([#410](https://github.com/httpswift/swifter/pull/410)) by [@apocolipse](https://github.com/apocolipse)
40-
41+
- `HttpResponse`: `html` requires now a complete html-string, not only the body-part.
42+
- Include the `CHANGELOG.md` and `README.md` in the Xcode-Project for easy access / changes.
4143

4244
## Removed
4345
- Dropped macOS 10.9 support [#404](https://github.com/httpswift/swifter/pull/404)), [#408](https://github.com/httpswift/swifter/pull/408)) by [@mazyod](https://github.com/mazyod), [@Vkt0r](https://github.com/Vkt0r)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Tiny http server engine written in [Swift](https://developer.apple.com/swift/) p
1919
### How to start?
2020
```swift
2121
let server = HttpServer()
22-
server["/hello"] = { .ok(.html("You asked for \($0)")) }
22+
server["/hello"] = { .ok(.htmlBody("You asked for \($0)")) }
2323
server.start()
2424
```
2525

XCode/Sources/DemoServer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
3030
}
3131
}
3232

33-
server["/magic"] = { .ok(.html("You asked for " + $0.path)) }
33+
server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path)) }
3434

3535
server["/test/:param1/:param2"] = { request in
3636
scopes {
@@ -98,7 +98,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
9898
guard let name = multipart.name, let fileName = multipart.fileName else { continue }
9999
response += "Name: \(name) File name: \(fileName) Size: \(multipart.body.count)<br>"
100100
}
101-
return HttpResponse.ok(.html(response))
101+
return HttpResponse.ok(.htmlBody(response))
102102
}
103103

104104
server.GET["/login"] = scopes {
@@ -136,7 +136,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
136136

137137
server.POST["/login"] = { request in
138138
let formFields = request.parseUrlencodedForm()
139-
return HttpResponse.ok(.html(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")))
139+
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")))
140140
}
141141

142142
server["/demo"] = scopes {
@@ -165,11 +165,11 @@ public func demoServer(_ publicDir: String) -> HttpServer {
165165
server["/long"] = { _ in
166166
var longResponse = ""
167167
for index in 0..<1000 { longResponse += "(\(index)),->" }
168-
return .ok(.html(longResponse))
168+
return .ok(.htmlBody(longResponse))
169169
}
170170

171171
server["/wildcard/*/test/*/:param"] = { request in
172-
return .ok(.html(request.path))
172+
return .ok(.htmlBody(request.path))
173173
}
174174

175175
server["/stream"] = { _ in

XCode/Sources/HttpResponse.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public enum HttpResponseBody {
2424

2525
case json(AnyObject)
2626
case html(String)
27+
case htmlBody(String)
2728
case text(String)
2829
case data(Data)
2930
case custom(Any, (Any) throws -> String)
@@ -51,7 +52,12 @@ public enum HttpResponseBody {
5152
return (data.count, {
5253
try $0.write(data)
5354
})
54-
case .html(let body):
55+
case .html(let html):
56+
let data = [UInt8](html.utf8)
57+
return (data.count, {
58+
try $0.write(data)
59+
})
60+
case .htmlBody(let body):
5561
let serialised = "<html><meta charset=\"UTF-8\"><body>\(body)</body></html>"
5662
let data = [UInt8](serialised.utf8)
5763
return (data.count, {

XCode/Swifter.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@
170170
269B47A11D3AAAE20042D137 /* Swifter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swifter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
171171
269B47A41D3AAC4F0042D137 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
172172
269B47A51D3AAC4F0042D137 /* SwiftertvOS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftertvOS.h; sourceTree = "<group>"; };
173+
540CA839228F275B00A3AF9B /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
174+
540CA83A228F275B00A3AF9B /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
173175
6A0D4511204E9988000A0726 /* MimeTypesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MimeTypesTests.swift; sourceTree = "<group>"; };
174176
6AE2FF702048011A00302EC4 /* MimeTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MimeTypes.swift; sourceTree = "<group>"; };
175177
7AE893E71C05127900A29F63 /* Swifter.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Swifter.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -354,6 +356,8 @@
354356
7C839B6519422CFF003A6950 = {
355357
isa = PBXGroup;
356358
children = (
359+
540CA83A228F275B00A3AF9B /* CHANGELOG.md */,
360+
540CA839228F275B00A3AF9B /* README.md */,
357361
7B912F4B220507DB0062C360 /* LinuxMain.swift */,
358362
7C76B6E91D2C44F30030FC98 /* Sources */,
359363
7CA4815619A2EF2B0030B30D /* Resources */,

XCode/SwifterSampleOSX/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Swifter
1010
do {
1111
let server = demoServer(try String.File.currentWorkingDirectory())
1212
server["/testAfterBaseRoute"] = { request in
13-
return .ok(.html("ok !"))
13+
return .ok(.htmlBody("ok !"))
1414
}
1515

1616
if #available(OSXApplicationExtension 10.10, *) {

XCode/Tests/ServerThreadingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ServerThreadingTests: XCTestCase {
3232
let queue = DispatchQueue(label: "com.swifter.threading")
3333
let hostURL: URL
3434

35-
server.GET[path] = { .ok(.html("You asked for " + $0.path)) }
35+
server.GET[path] = { .ok(.htmlBody("You asked for " + $0.path)) }
3636

3737
do {
3838

@@ -70,7 +70,7 @@ class ServerThreadingTests: XCTestCase {
7070
func testShouldHandleTheSameRequestConcurrently() {
7171

7272
let path = "/a/:b/c"
73-
server.GET[path] = { .ok(.html("You asked for " + $0.path)) }
73+
server.GET[path] = { .ok(.htmlBody("You asked for " + $0.path)) }
7474

7575
var requestExpectation: XCTestExpectation? = expectation(description: "Should handle the request concurrently")
7676

XCode/Tests/SwifterTestsHttpRouter.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SwifterTestsHttpRouter: XCTestCase {
2626
func testHttpRouterSlashRoot() {
2727

2828
router.register(nil, path: "/", handler: { _ in
29-
return .ok(.html("OK"))
29+
return .ok(.htmlBody("OK"))
3030
})
3131

3232
XCTAssertNotNil(router.route(nil, path: "/"))
@@ -35,7 +35,7 @@ class SwifterTestsHttpRouter: XCTestCase {
3535
func testHttpRouterSimplePathSegments() {
3636

3737
router.register(nil, path: "/a/b/c/d", handler: { _ in
38-
return .ok(.html("OK"))
38+
return .ok(.htmlBody("OK"))
3939
})
4040

4141
XCTAssertNil(router.route(nil, path: "/"))
@@ -48,7 +48,7 @@ class SwifterTestsHttpRouter: XCTestCase {
4848
func testHttpRouterSinglePathSegmentWildcard() {
4949

5050
router.register(nil, path: "/a/*/c/d", handler: { _ in
51-
return .ok(.html("OK"))
51+
return .ok(.htmlBody("OK"))
5252
})
5353

5454
XCTAssertNil(router.route(nil, path: "/"))
@@ -62,7 +62,7 @@ class SwifterTestsHttpRouter: XCTestCase {
6262
func testHttpRouterVariables() {
6363

6464
router.register(nil, path: "/a/:arg1/:arg2/b/c/d/:arg3", handler: { _ in
65-
return .ok(.html("OK"))
65+
return .ok(.htmlBody("OK"))
6666
})
6767

6868
XCTAssertNil(router.route(nil, path: "/"))
@@ -76,7 +76,7 @@ class SwifterTestsHttpRouter: XCTestCase {
7676
func testHttpRouterMultiplePathSegmentWildcards() {
7777

7878
router.register(nil, path: "/a/**/e/f/g", handler: { _ in
79-
return .ok(.html("OK"))
79+
return .ok(.htmlBody("OK"))
8080
})
8181

8282
XCTAssertNil(router.route(nil, path: "/"))
@@ -88,11 +88,11 @@ class SwifterTestsHttpRouter: XCTestCase {
8888
func testHttpRouterEmptyTail() {
8989

9090
router.register(nil, path: "/a/b/", handler: { _ in
91-
return .ok(.html("OK"))
91+
return .ok(.htmlBody("OK"))
9292
})
9393

9494
router.register(nil, path: "/a/b/:var", handler: { _ in
95-
return .ok(.html("OK"))
95+
return .ok(.htmlBody("OK"))
9696
})
9797

9898
XCTAssertNil(router.route(nil, path: "/"))
@@ -108,7 +108,7 @@ class SwifterTestsHttpRouter: XCTestCase {
108108
func testHttpRouterPercentEncodedPathSegments() {
109109

110110
router.register(nil, path: "/a/<>/^", handler: { _ in
111-
return .ok(.html("OK"))
111+
return .ok(.htmlBody("OK"))
112112
})
113113

114114
XCTAssertNil(router.route(nil, path: "/"))

0 commit comments

Comments
 (0)