Skip to content

Commit

Permalink
Merge stable into fix-body-writer-exception-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Stuart Espey committed Nov 8, 2022
2 parents 9ad8a89 + 1e4f51c commit 21ebb16
Show file tree
Hide file tree
Showing 63 changed files with 262 additions and 93 deletions.
35 changes: 14 additions & 21 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,25 @@
name: Danger

on:
pull_request:
types: [opened, synchronize, edited]
branches:
- stable
- stable

jobs:
Danger:
runs-on: macos-latest
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Cache Bundle Dependencies
uses: actions/cache@v1
with:
path: vendor/bundle
key: 1-gems-{{ checksum "Gemfile.lock" }}
restore-keys: 1-gems-
- name: Set Ruby Version
uses: actions/setup-ruby@v1
- uses: actions/checkout@v1

- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- name: Install Ruby Dependencies
ruby-version: 2.6 # Not needed with a .ruby-version file
bundler-cache: true # runs 'bundle install' and caches installed gems automatically

- name: Danger
run: |
bundle config path vendor/bundle
bundle check || bundle install
env:
BUNDLE_JOBS: 4
BUNDLE_RETRY: 3
- name: Running Danger
run: bundle exec danger
bundle exec danger --fail-on-errors=true
shell: bash
env:
DANGER_GITHUB_API_TOKEN: ${{ secrets.DANGER_GITHUB_API_TOKEN }}
DANGER_GITHUB_API_TOKEN: ${{ secrets.BOT_ACCESS_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/macos-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ jobs:
mkdir -p tmp/test-results/
- name: Run Unit Test - macOS
run: |
cd XCode
cd Xcode
set -o pipefail && xcodebuild test -scheme SwifterMac -sdk macosx -destination "arch=x86_64" | xcpretty -c -r html --output $TEST_REPORTS/macOS.html
- name: Run Unit Test - iOS
run: |
cd XCode
cd Xcode
set -o pipefail && xcodebuild test -scheme SwifteriOS -sdk iphonesimulator -destination "name=iPhone 8" | xcpretty -c -r html --output $TEST_REPORTS/iOS.html
- name: Run Unit Test - tvOS
run: |
cd XCode
cd Xcode
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
- name: Run Unit Test - Swift Package Manager
run: |
cd XCode
cd Xcode
swift build && swift test
- name: Archive Test results
uses: actions/upload-artifact@v1
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ DerivedData

.build/
Packages/
/XCode/.DS_Store
/Xcode/.DS_Store
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ disabled_rules:

excluded: # paths to ignore during linting. Takes precedence over `included`.
- LinuxMain.swift
- XCode/Tests/XCTestManifests.swift
- Xcode/Tests/XCTestManifests.swift
- Tests/XCTestManifests.swift
- Package.swift
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ All notable changes to this project will be documented in this file. Changes not
# [Unreleased]


## Added

- Add custom headers into .ok HttpResponse. ([#500](https://github.com/httpswift/swifter/pull/500) by [@yuri-qualtie](https://github.com/yuri-qualtie)
- 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)
- Set `Content-Type` to HttpBody and Text HttpResponse. ([#474](https://github.com/httpswift/swifter/pull/474)) by [@mtgto](https://github.com/mtgto)
- 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)
- Adds response body to any http response. ([#476](https://github.com/httpswift/swifter/pull/476) by [@mtgto](https://github.com/mtgto)

## Fixed

- Fix misspell `serialise`. ([#473](https://github.com/httpswift/swifter/pull/473)) by [@mtgto](https://github.com/mtgto)
- Fix an issue causing Danger was not working properly. ([#486](https://github.com/httpswift/swifter/pull/486)) by [@Vkt0r](https://github.com/Vkt0r)
- 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)
- Improved performance of data reading. ([#487](https://github.com/httpswift/swifter/pull/487)) by [@till0xff](https://github.com/till0xff)

## Changed

- Rename `Example` product to `SwifterExample` ([#492](https://github.com/httpswift/swifter/pull/492) by [@fortmarek](https://github.com/fortmarek))

# [1.5.0]

## Added
Expand Down
6 changes: 3 additions & 3 deletions Dangerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ swiftlint.config_file = '.swiftlint.yml'
swiftlint.lint_files

# Warn when new tests are added but the XCTestManifests wasn't updated to run on Linux
tests_added_or_modified = !git.modified_files.grep(/XCode\/Tests/).empty? || !git.added_files.grep(/XCode\/Tests/).empty?
xc_manifest_updated = !git.modified_files.grep(/XCode\/Tests\/XCTestManifests.swift/).empty?
tests_added_or_modified = !git.modified_files.grep(/Xcode\/Tests/).empty? || !git.added_files.grep(/Xcode\/Tests/).empty?
xc_manifest_updated = !git.modified_files.grep(/Xcode\/Tests\/XCTestManifests.swift/).empty?
if tests_added_or_modified && !xc_manifest_updated
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`.")
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`.")

# This is a temporary warning to remove the entry for the failed test until we solve the issue in Linux
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).")
Expand Down
30 changes: 19 additions & 11 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
addressable (2.8.0)
public_suffix (>= 2.0.2, < 5.0)
claide (1.0.3)
claide-plugins (0.9.2)
Expand All @@ -11,7 +11,7 @@ GEM
colored2 (3.1.2)
cork (0.3.0)
colored2 (~> 3.1)
danger (8.0.5)
danger (8.2.3)
claide (~> 1.0)
claide-plugins (>= 0.9.2)
colored2 (~> 3.1)
Expand All @@ -23,36 +23,44 @@ GEM
kramdown-parser-gfm (~> 1.0)
no_proxy_fix
octokit (~> 4.7)
terminal-table (~> 1)
danger-swiftlint (0.24.4)
terminal-table (>= 1, < 4)
danger-swiftlint (0.26.0)
danger
rake (> 10)
thor (~> 0.19)
faraday (1.0.1)
faraday (1.4.1)
faraday-excon (~> 1.1)
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
multipart-post (>= 1.2, < 3)
ruby2_keywords (>= 0.0.4)
faraday-excon (1.1.0)
faraday-http-cache (2.2.0)
faraday (>= 0.8)
git (1.7.0)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.1.0)
git (1.8.1)
rchardet (~> 1.8)
kramdown (2.3.0)
kramdown (2.3.1)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
multipart-post (2.1.1)
nap (1.1.0)
no_proxy_fix (0.1.2)
octokit (4.18.0)
octokit (4.21.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
open4 (1.3.4)
public_suffix (4.0.6)
rake (13.0.1)
rake (13.0.3)
rchardet (1.8.0)
rexml (3.2.4)
rexml (3.2.5)
ruby2_keywords (0.0.4)
sawyer (0.8.2)
addressable (>= 2.3.5)
faraday (> 0.8, < 2.0)
terminal-table (1.8.0)
terminal-table (3.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thor (0.20.3)
unicode-display_width (1.7.0)
Expand Down
13 changes: 7 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let package = Package(

products: [
.library(name: "Swifter", targets: ["Swifter"]),
.executable(name: "Example", targets: ["Example"])
.executable(name: "SwifterExample", targets: ["SwifterExample"])
],

dependencies: [],
Expand All @@ -16,22 +16,23 @@ let package = Package(
.target(
name: "Swifter",
dependencies: [],
path: "XCode/Sources"
path: "Xcode/Sources"
),

.target(
name: "Example",
name: "SwifterExample",
dependencies: [
"Swifter"
],
path: "Example"),
path: "SwifterExample"
),

.testTarget(
name: "SwifterTests",
dependencies: [
"Swifter"
],
path: "XCode/Tests"
path: "Xcode/Tests"
)
]
)
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Tiny http server engine written in [Swift](https://developer.apple.com/swift/) programming language.

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

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

Expand Down
6 changes: 3 additions & 3 deletions Swifter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Pod::Spec.new do |s|
s.osx.deployment_target = "10.10"
s.tvos.deployment_target = "9.0"
s.source = { :git => "https://github.com/httpswift/swifter.git", :tag => "1.5.0" }
s.source_files = 'XCode/Sources/*.{swift}'
s.swift_version = '4.2'
s.source_files = 'Xcode/Sources/*.{swift}'
s.swift_version = '5.0'

end
end
File renamed without changes.
1 change: 0 additions & 1 deletion XCode/LinuxMain.swift → Xcode/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import XCTest

import SwifterTests

var tests = [XCTestCaseEntry]()
Expand Down
File renamed without changes
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
}
}

server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path)) }
server["/magic"] = { .ok(.htmlBody("You asked for " + $0.path), ["XXX-Custom-Header": "value"]) }

server["/test/:param1/:param2"] = { request in
scopes {
Expand Down Expand Up @@ -98,7 +98,7 @@ public func demoServer(_ publicDir: String) -> HttpServer {
guard let name = multipart.name, let fileName = multipart.fileName else { continue }
response += "Name: \(name) File name: \(fileName) Size: \(multipart.body.count)<br>"
}
return HttpResponse.ok(.htmlBody(response))
return HttpResponse.ok(.htmlBody(response), ["XXX-Custom-Header": "value"])
}

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

server.POST["/login"] = { request in
let formFields = request.parseUrlencodedForm()
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")))
return HttpResponse.ok(.htmlBody(formFields.map({ "\($0.0) = \($0.1)" }).joined(separator: "<br>")), ["XXX-Custom-Header": "value"])
}

server["/demo"] = scopes {
Expand Down Expand Up @@ -165,11 +165,11 @@ public func demoServer(_ publicDir: String) -> HttpServer {
server["/long"] = { _ in
var longResponse = ""
for index in 0..<1000 { longResponse += "(\(index)),->" }
return .ok(.htmlBody(longResponse))
return .ok(.htmlBody(longResponse), ["XXX-Custom-Header": "value"])
}

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

server["/stream"] = { _ in
Expand Down
File renamed without changes.
23 changes: 15 additions & 8 deletions XCode/Sources/Files.swift → Xcode/Sources/Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@ import Foundation
public func shareFile(_ path: String) -> ((HttpRequest) -> HttpResponse) {
return { _ in
if let file = try? path.openForReading() {
return .raw(200, "OK", [:], { writer in
let mimeType = path.mimeType()
var responseHeader: [String: String] = ["Content-Type": mimeType]

if let attr = try? FileManager.default.attributesOfItem(atPath: path),
let fileSize = attr[FileAttributeKey.size] as? UInt64 {
responseHeader["Content-Length"] = String(fileSize)
}
return .raw(200, "OK", responseHeader, { writer in
try? writer.write(file)
file.close()
})
}
return .notFound
return .notFound()
}
}

public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String] = ["index.html", "default.html"]) -> ((HttpRequest) -> HttpResponse) {
return { request in
guard let fileRelativePath = request.params.first else {
return .notFound
return .notFound()
}
if fileRelativePath.value.isEmpty {
for path in defaults {
Expand Down Expand Up @@ -50,19 +57,19 @@ public func shareFilesFromDirectory(_ directoryPath: String, defaults: [String]
file.close()
})
}
return .notFound
return .notFound()
}
}

public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
return { request in
guard let (_, value) = request.params.first else {
return HttpResponse.notFound
return .notFound()
}
let filePath = dir + String.pathSeparator + value
do {
guard try filePath.exists() else {
return .notFound
return .notFound()
}
if try filePath.directory() {
var files = try filePath.files()
Expand All @@ -85,15 +92,15 @@ public func directoryBrowser(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
}(request)
} else {
guard let file = try? filePath.openForReading() else {
return .notFound
return .notFound()
}
return .raw(200, "OK", [:], { writer in
try? writer.write(file)
file.close()
})
}
} catch {
return HttpResponse.internalServerError
return HttpResponse.internalServerError(.text("Internal Server Error"))
}
}
}
Loading

0 comments on commit 21ebb16

Please sign in to comment.