Skip to content

Commit 21ebb16

Browse files
author
Stuart Espey
committed
Merge stable into fix-body-writer-exception-handling
2 parents 9ad8a89 + 1e4f51c commit 21ebb16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+262
-93
lines changed

.github/workflows/danger.yml

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
name: Danger
2+
23
on:
34
pull_request:
5+
types: [opened, synchronize, edited]
46
branches:
5-
- stable
7+
- stable
68

79
jobs:
810
Danger:
9-
runs-on: macos-latest
11+
runs-on: ubuntu-18.04
1012
steps:
11-
- uses: actions/checkout@v2
12-
- name: Cache Bundle Dependencies
13-
uses: actions/cache@v1
14-
with:
15-
path: vendor/bundle
16-
key: 1-gems-{{ checksum "Gemfile.lock" }}
17-
restore-keys: 1-gems-
18-
- name: Set Ruby Version
19-
uses: actions/setup-ruby@v1
13+
- uses: actions/checkout@v1
14+
15+
- uses: ruby/setup-ruby@v1
2016
with:
21-
ruby-version: 2.6
22-
- name: Install Ruby Dependencies
17+
ruby-version: 2.6 # Not needed with a .ruby-version file
18+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
19+
20+
- name: Danger
2321
run: |
24-
bundle config path vendor/bundle
25-
bundle check || bundle install
26-
env:
27-
BUNDLE_JOBS: 4
28-
BUNDLE_RETRY: 3
29-
- name: Running Danger
30-
run: bundle exec danger
22+
bundle exec danger --fail-on-errors=true
23+
shell: bash
3124
env:
32-
DANGER_GITHUB_API_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN }}
25+
DANGER_GITHUB_API_TOKEN: ${{ secrets.BOT_ACCESS_TOKEN }}

.github/workflows/macos-tests.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ jobs:
2121
mkdir -p tmp/test-results/
2222
- name: Run Unit Test - macOS
2323
run: |
24-
cd XCode
24+
cd Xcode
2525
set -o pipefail && xcodebuild test -scheme SwifterMac -sdk macosx -destination "arch=x86_64" | xcpretty -c -r html --output $TEST_REPORTS/macOS.html
2626
- name: Run Unit Test - iOS
2727
run: |
28-
cd XCode
28+
cd Xcode
2929
set -o pipefail && xcodebuild test -scheme SwifteriOS -sdk iphonesimulator -destination "name=iPhone 8" | xcpretty -c -r html --output $TEST_REPORTS/iOS.html
3030
- name: Run Unit Test - tvOS
3131
run: |
32-
cd XCode
32+
cd Xcode
3333
set -o pipefail && xcodebuild test -scheme SwiftertvOS -sdk appletvsimulator -destination "name=Apple TV 4K (at 1080p)" | xcpretty -c -r html --output $TEST_REPORTS/tvOS.html
3434
- name: Run Unit Test - Swift Package Manager
3535
run: |
36-
cd XCode
36+
cd Xcode
3737
swift build && swift test
3838
- name: Archive Test results
3939
uses: actions/upload-artifact@v1

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ DerivedData
2727

2828
.build/
2929
Packages/
30-
/XCode/.DS_Store
30+
/Xcode/.DS_Store

.swiftlint.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ disabled_rules:
1616

1717
excluded: # paths to ignore during linting. Takes precedence over `included`.
1818
- LinuxMain.swift
19-
- XCode/Tests/XCTestManifests.swift
19+
- Xcode/Tests/XCTestManifests.swift
2020
- Tests/XCTestManifests.swift
2121
- Package.swift

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ All notable changes to this project will be documented in this file. Changes not
1818
1919
# [Unreleased]
2020

21+
22+
## Added
23+
24+
- Add custom headers into .ok HttpResponse. ([#500](https://github.com/httpswift/swifter/pull/500) by [@yuri-qualtie](https://github.com/yuri-qualtie)
25+
- Add support for using `**` as a catch-all at the end of a route. ([#479](https://github.com/httpswift/swifter/pull/479)) by [@michaelenger](https://github.com/michaelenger)
26+
- Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
27+
- The `shareFile` function now sets `Content-Type` and `Content-Length` headers like `shareFilesFromDirectory`. ([#493](https://github.com/httpswift/swifter/pull/493)) by [@jcrate](https://github.com/jcrate)
28+
- Adds response body to any http response. ([#476](https://github.com/httpswift/swifter/pull/476) by [@mtgto](https://github.com/mtgto)
29+
30+
## Fixed
31+
32+
- Fix misspell `serialise`. ([#473](https://github.com/httpswift/swifter/pull/473)) by [@mtgto](https://github.com/mtgto)
33+
- Fix an issue causing Danger was not working properly. ([#486](https://github.com/httpswift/swifter/pull/486)) by [@Vkt0r](https://github.com/Vkt0r)
34+
- Set Swift version to 5.0 in podspec. ([#475](https://github.com/httpswift/swifter/pull/475)) by [@p-krasnobrovkin-tcs](https://github.com/p-krasnobrovkin-tcs)
35+
- Improved performance of data reading. ([#487](https://github.com/httpswift/swifter/pull/487)) by [@till0xff](https://github.com/till0xff)
36+
37+
## Changed
38+
39+
- Rename `Example` product to `SwifterExample` ([#492](https://github.com/httpswift/swifter/pull/492) by [@fortmarek](https://github.com/fortmarek))
40+
2141
# [1.5.0]
2242

2343
## Added

Dangerfile

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ swiftlint.config_file = '.swiftlint.yml'
2626
swiftlint.lint_files
2727

2828
# Warn when new tests are added but the XCTestManifests wasn't updated to run on Linux
29-
tests_added_or_modified = !git.modified_files.grep(/XCode\/Tests/).empty? || !git.added_files.grep(/XCode\/Tests/).empty?
30-
xc_manifest_updated = !git.modified_files.grep(/XCode\/Tests\/XCTestManifests.swift/).empty?
29+
tests_added_or_modified = !git.modified_files.grep(/Xcode\/Tests/).empty? || !git.added_files.grep(/Xcode\/Tests/).empty?
30+
xc_manifest_updated = !git.modified_files.grep(/Xcode\/Tests\/XCTestManifests.swift/).empty?
3131
if tests_added_or_modified && !xc_manifest_updated
32-
warn("It seems like you've added new tests to the library. If that's the case, please update the [XCTestManifests.swift](https://github.com/httpswift/swifter/blob/stable/XCode/Tests/XCTestManifests.swift) file running in your terminal the command `swift test --generate-linuxmain`.")
32+
warn("It seems like you've added new tests to the library. If that's the case, please update the [XCTestManifests.swift](https://github.com/httpswift/swifter/blob/stable/Xcode/Tests/XCTestManifests.swift) file running in your terminal the command `swift test --generate-linuxmain`.")
3333

3434
# This is a temporary warning to remove the entry for the failed test until we solve the issue in Linux
3535
warn("If you ran the command `swift test --generate-linuxmain` in your terminal, please remove the line `testCase(IOSafetyTests.__allTests__IOSafetyTests),` from `public func __allTests() -> [XCTestCaseEntry]` in the bottom of the file. For more reference see [#366](https://github.com/httpswift/swifter/issues/366).")

Gemfile.lock

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4-
addressable (2.7.0)
4+
addressable (2.8.0)
55
public_suffix (>= 2.0.2, < 5.0)
66
claide (1.0.3)
77
claide-plugins (0.9.2)
@@ -11,7 +11,7 @@ GEM
1111
colored2 (3.1.2)
1212
cork (0.3.0)
1313
colored2 (~> 3.1)
14-
danger (8.0.5)
14+
danger (8.2.3)
1515
claide (~> 1.0)
1616
claide-plugins (>= 0.9.2)
1717
colored2 (~> 3.1)
@@ -23,36 +23,44 @@ GEM
2323
kramdown-parser-gfm (~> 1.0)
2424
no_proxy_fix
2525
octokit (~> 4.7)
26-
terminal-table (~> 1)
27-
danger-swiftlint (0.24.4)
26+
terminal-table (>= 1, < 4)
27+
danger-swiftlint (0.26.0)
2828
danger
2929
rake (> 10)
3030
thor (~> 0.19)
31-
faraday (1.0.1)
31+
faraday (1.4.1)
32+
faraday-excon (~> 1.1)
33+
faraday-net_http (~> 1.0)
34+
faraday-net_http_persistent (~> 1.1)
3235
multipart-post (>= 1.2, < 3)
36+
ruby2_keywords (>= 0.0.4)
37+
faraday-excon (1.1.0)
3338
faraday-http-cache (2.2.0)
3439
faraday (>= 0.8)
35-
git (1.7.0)
40+
faraday-net_http (1.0.1)
41+
faraday-net_http_persistent (1.1.0)
42+
git (1.8.1)
3643
rchardet (~> 1.8)
37-
kramdown (2.3.0)
44+
kramdown (2.3.1)
3845
rexml
3946
kramdown-parser-gfm (1.1.0)
4047
kramdown (~> 2.0)
4148
multipart-post (2.1.1)
4249
nap (1.1.0)
4350
no_proxy_fix (0.1.2)
44-
octokit (4.18.0)
51+
octokit (4.21.0)
4552
faraday (>= 0.9)
4653
sawyer (~> 0.8.0, >= 0.5.3)
4754
open4 (1.3.4)
4855
public_suffix (4.0.6)
49-
rake (13.0.1)
56+
rake (13.0.3)
5057
rchardet (1.8.0)
51-
rexml (3.2.4)
58+
rexml (3.2.5)
59+
ruby2_keywords (0.0.4)
5260
sawyer (0.8.2)
5361
addressable (>= 2.3.5)
5462
faraday (> 0.8, < 2.0)
55-
terminal-table (1.8.0)
63+
terminal-table (3.0.0)
5664
unicode-display_width (~> 1.1, >= 1.1.1)
5765
thor (0.20.3)
5866
unicode-display_width (1.7.0)

Package.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let package = Package(
77

88
products: [
99
.library(name: "Swifter", targets: ["Swifter"]),
10-
.executable(name: "Example", targets: ["Example"])
10+
.executable(name: "SwifterExample", targets: ["SwifterExample"])
1111
],
1212

1313
dependencies: [],
@@ -16,22 +16,23 @@ let package = Package(
1616
.target(
1717
name: "Swifter",
1818
dependencies: [],
19-
path: "XCode/Sources"
19+
path: "Xcode/Sources"
2020
),
2121

2222
.target(
23-
name: "Example",
23+
name: "SwifterExample",
2424
dependencies: [
2525
"Swifter"
2626
],
27-
path: "Example"),
27+
path: "SwifterExample"
28+
),
2829

2930
.testTarget(
3031
name: "SwifterTests",
3132
dependencies: [
3233
"Swifter"
3334
],
34-
path: "XCode/Tests"
35+
path: "Xcode/Tests"
3536
)
3637
]
37-
)
38+
)

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
Tiny http server engine written in [Swift](https://developer.apple.com/swift/) programming language.
1010

1111
### Branches
12-
`* stable` - lands on CocoaPods and others. Supports the latest non-beta XCode and SPM. Stable.
12+
`* stable` - lands on CocoaPods and others. Supports the latest non-beta Xcode and SPM. Stable.
1313

1414
`* master` - stable branch plus experimental web-framework layer.
1515

Swifter.podspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Pod::Spec.new do |s|
1010
s.osx.deployment_target = "10.10"
1111
s.tvos.deployment_target = "9.0"
1212
s.source = { :git => "https://github.com/httpswift/swifter.git", :tag => "1.5.0" }
13-
s.source_files = 'XCode/Sources/*.{swift}'
14-
s.swift_version = '4.2'
13+
s.source_files = 'Xcode/Sources/*.{swift}'
14+
s.swift_version = '5.0'
1515

16-
end
16+
end
File renamed without changes.

XCode/LinuxMain.swift renamed to Xcode/LinuxMain.swift

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import XCTest
2-
32
import SwifterTests
43

54
var tests = [XCTestCaseEntry]()
File renamed without changes.

XCode/Sources/DemoServer.swift renamed to Xcode/Sources/DemoServer.swift

+5-5
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(.htmlBody("You asked for " + $0.path)) }
33+
server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path), ["XXX-Custom-Header": "value"]) }
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(.htmlBody(response))
101+
return HttpResponse.ok(.htmlBody(response), ["XXX-Custom-Header": "value"])
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(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")))
139+
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")), ["XXX-Custom-Header": "value"])
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(.htmlBody(longResponse))
168+
return .ok(.htmlBody(longResponse), ["XXX-Custom-Header": "value"])
169169
}
170170

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

175175
server["/stream"] = { _ in
File renamed without changes.

XCode/Sources/Files.swift renamed to Xcode/Sources/Files.swift

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@ import Foundation
1010
public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
1111
return { _ in
1212
if let file = try? path.openForReading() {
13-
return .raw(200, "OK", [:], { writer in
13+
let mimeType = path.mimeType()
14+
var responseHeader: [String: String] = ["Content-Type": mimeType]
15+
16+
if let attr = try? FileManager.default.attributesOfItem(atPath: path),
17+
let fileSize = attr[FileAttributeKey.size] as? UInt64 {
18+
responseHeader["Content-Length"] = String(fileSize)
19+
}
20+
return .raw(200, "OK", responseHeader, { writer in
1421
try? writer.write(file)
1522
file.close()
1623
})
1724
}
18-
return .notFound
25+
return .notFound()
1926
}
2027
}
2128

2229
public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
2330
return { request in
2431
guard let fileRelativePath = request.params.first else {
25-
return .notFound
32+
return .notFound()
2633
}
2734
if fileRelativePath.value.isEmpty {
2835
for path in defaults {
@@ -50,19 +57,19 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
5057
file.close()
5158
})
5259
}
53-
return .notFound
60+
return .notFound()
5461
}
5562
}
5663

5764
public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
5865
return { request in
5966
guard let (_, value) = request.params.first else {
60-
return HttpResponse.notFound
67+
return .notFound()
6168
}
6269
let filePath = dir + String.pathSeparator + value
6370
do {
6471
guard try filePath.exists() else {
65-
return .notFound
72+
return .notFound()
6673
}
6774
if try filePath.directory() {
6875
var files = try filePath.files()
@@ -85,15 +92,15 @@ public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
8592
}(request)
8693
} else {
8794
guard let file = try? filePath.openForReading() else {
88-
return .notFound
95+
return .notFound()
8996
}
9097
return .raw(200, "OK", [:], { writer in
9198
try? writer.write(file)
9299
file.close()
93100
})
94101
}
95102
} catch {
96-
return HttpResponse.internalServerError
103+
return HttpResponse.internalServerError(.text("Internal Server Error"))
97104
}
98105
}
99106
}

0 commit comments

Comments
 (0)