Skip to content

Commit 0152210

Browse files
authored
Merge pull request #410 from apocolipse/stable
Added scopes, make headers and status code public
2 parents 56c89ea + 429bcf4 commit 0152210

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All notable changes to this project will be documented in this file. Changes not
2121
## Added
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)
24+
- Added the following to `Scopes`: `manifest`, `ontouchstart`, `dataText`. ([#410](https://github.com/httpswift/swifter/pull/410)) by [@apocolipse](https://github.com/apocolipse)
2425

2526
## Fixed
2627
- An issue causing a crash regarding a thread race condition. ([#399](https://github.com/httpswift/swifter/pull/399)) by [@Vkt0r](https://github.com/Vkt0r)
@@ -35,6 +36,8 @@ All notable changes to this project will be documented in this file. Changes not
3536
- Podspec source_files updated to match source file directory changes. ([#400](https://github.com/httpswift/swifter/pull/400)) by [@welsonpan](https://github.com/welsonpan)
3637
- Refactor: Use Foundation API for Base64 encoding. ([#403](https://github.com/httpswift/swifter/pull/403)) by [@mazyod](https://github.com/mazyod)
3738
- Refactor: Use `URLComponents` for `HttpRequest` path and query parameters parsing [#404](https://github.com/httpswift/swifter/pull/404)) by [@mazyod](https://github.com/mazyod)
39+
- `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+
3841

3942
## Removed
4043
- 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)

XCode/Sources/HttpResponse.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public protocol HttpResponseBodyWriter {
2121
}
2222

2323
public enum HttpResponseBody {
24-
24+
2525
case json(AnyObject)
2626
case html(String)
2727
case text(String)
2828
case data(Data)
2929
case custom(Any, (Any) throws -> String)
30-
30+
3131
func content() -> (Int, ((HttpResponseBodyWriter) throws -> Void)?) {
3232
do {
3333
switch self {
@@ -79,7 +79,7 @@ public enum HttpResponseBody {
7979

8080
// swiftlint:disable cyclomatic_complexity
8181
public enum HttpResponse {
82-
82+
8383
case switchProtocols([String: String], (Socket) -> Void)
8484
case ok(HttpResponseBody), created, accepted
8585
case movedPermanently(String)
@@ -88,7 +88,7 @@ public enum HttpResponse {
8888
case internalServerError
8989
case raw(Int, String, [String:String]?, ((HttpResponseBodyWriter) throws -> Void)? )
9090

91-
func statusCode() -> Int {
91+
public var statusCode: Int {
9292
switch self {
9393
case .switchProtocols : return 101
9494
case .ok : return 200
@@ -101,11 +101,11 @@ public enum HttpResponse {
101101
case .forbidden : return 403
102102
case .notFound : return 404
103103
case .internalServerError : return 500
104-
case .raw(let code, _, _, _) : return code
104+
case .raw(let code, _, _, _) : return code
105105
}
106106
}
107-
108-
func reasonPhrase() -> String {
107+
108+
public var reasonPhrase: String {
109109
switch self {
110110
case .switchProtocols : return "Switching Protocols"
111111
case .ok : return "OK"
@@ -121,8 +121,8 @@ public enum HttpResponse {
121121
case .raw(_, let phrase, _, _) : return phrase
122122
}
123123
}
124-
125-
func headers() -> [String: String] {
124+
125+
public func headers() -> [String: String] {
126126
var headers = ["Server": "Swifter \(HttpServer.VERSION)"]
127127
switch self {
128128
case .switchProtocols(let switchHeaders, _):
@@ -149,7 +149,7 @@ public enum HttpResponse {
149149
}
150150
return headers
151151
}
152-
152+
153153
func content() -> (length: Int, write: ((HttpResponseBodyWriter) throws -> Void)?) {
154154
switch self {
155155
case .ok(let body) : return body.content()
@@ -158,7 +158,7 @@ public enum HttpResponse {
158158
default : return (-1, nil)
159159
}
160160
}
161-
161+
162162
func socketSession() -> ((Socket) -> Void)? {
163163
switch self {
164164
case .switchProtocols(_, let handler) : return handler
@@ -171,13 +171,13 @@ public enum HttpResponse {
171171
Makes it possible to compare handler responses with '==', but
172172
ignores any associated values. This should generally be what
173173
you want. E.g.:
174-
174+
175175
let resp = handler(updatedRequest)
176176
if resp == .NotFound {
177177
print("Client requested not found: \(request.url)")
178178
}
179179
*/
180180

181181
func == (inLeft: HttpResponse, inRight: HttpResponse) -> Bool {
182-
return inLeft.statusCode() == inRight.statusCode()
182+
return inLeft.statusCode == inRight.statusCode
183183
}

XCode/Sources/HttpServerIO.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public class HttpServerIO {
175175

176176
var responseHeader = String()
177177

178-
responseHeader.append("HTTP/1.1 \(response.statusCode()) \(response.reasonPhrase())\r\n")
178+
responseHeader.append("HTTP/1.1 \(response.statusCode) \(response.reasonPhrase)\r\n")
179179

180180
let content = response.content()
181181

XCode/Sources/Scopes.swift

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public var media: String?
6060
public var title: String?
6161
public var scope: String?
6262
public var classs: String?
63+
public var manifest: String?
6364
public var value: String?
6465
public var clear: String?
6566
public var start: String?
@@ -85,6 +86,7 @@ public var prompt: String?
8586
public var onfocus: String?
8687
public var enctype: String?
8788
public var onclick: String?
89+
public var ontouchstart: String?
8890
public var onkeyup: String?
8991
public var profile: String?
9092
public var version: String?
@@ -131,6 +133,7 @@ public var onkeypress: String?
131133
public var ondblclick: String?
132134
public var onmouseout: String?
133135
public var httpEquiv: String?
136+
public var dataText: String?
134137
public var background: String?
135138
public var onmousemove: String?
136139
public var onmouseover: String?
@@ -334,9 +337,9 @@ var scopesBuffer = [UInt64: String]()
334337

335338
// swiftlint:disable cyclomatic_complexity function_body_length
336339
private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closure: Closure) {
337-
340+
338341
// Push the attributes.
339-
342+
340343
let stackid = idd
341344
let stackdir = dir
342345
let stackrel = rel
@@ -377,6 +380,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
377380
let stacktitle = title
378381
let stackscope = scope
379382
let stackclass = classs
383+
let stackmanifest = manifest
380384
let stackvalue = value
381385
let stackclear = clear
382386
let stackstart = start
@@ -402,6 +406,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
402406
let stackonfocus = onfocus
403407
let stackenctype = enctype
404408
let stackonclick = onclick
409+
let stackontouchstart = ontouchstart
405410
let stackonkeyup = onkeyup
406411
let stackprofile = profile
407412
let stackversion = version
@@ -447,6 +452,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
447452
let stackondblclick = ondblclick
448453
let stackonmouseout = onmouseout
449454
let stackhttpEquiv = httpEquiv
455+
let stackdataText = dataText
450456
let stackbackground = background
451457
let stackonmousemove = onmousemove
452458
let stackonmouseover = onmouseover
@@ -459,9 +465,9 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
459465
let stackmarginheight = marginheight
460466
let stackacceptCharset = acceptCharset
461467
let stackinner = inner
462-
468+
463469
// Reset the values before a nested scope evalutation.
464-
470+
465471
idd = nil
466472
dir = nil
467473
rel = nil
@@ -502,6 +508,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
502508
title = nil
503509
scope = nil
504510
classs = nil
511+
manifest = nil
505512
value = nil
506513
clear = nil
507514
start = nil
@@ -527,6 +534,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
527534
onfocus = nil
528535
enctype = nil
529536
onclick = nil
537+
ontouchstart = nil
530538
onkeyup = nil
531539
profile = nil
532540
version = nil
@@ -572,6 +580,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
572580
ondblclick = nil
573581
onmouseout = nil
574582
httpEquiv = nil
583+
dataText = nil
575584
background = nil
576585
onmousemove = nil
577586
onmouseover = nil
@@ -584,25 +593,25 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
584593
marginheight = nil
585594
acceptCharset = nil
586595
inner = nil
587-
596+
588597
scopesBuffer[Process.tid] = (scopesBuffer[Process.tid] ?? "") + "<" + node
589-
598+
590599
// Save the current output before the nested scope evalutation.
591-
600+
592601
var output = scopesBuffer[Process.tid] ?? ""
593-
602+
594603
// Clear the output buffer for the evalutation.
595-
604+
596605
scopesBuffer[Process.tid] = ""
597-
606+
598607
// Evaluate the nested scope.
599-
608+
600609
closure()
601-
610+
602611
// Render attributes set by the evalutation.
603-
612+
604613
var mergedAttributes = [String: String?]()
605-
614+
606615
if let idd = idd { mergedAttributes["id"] = idd }
607616
if let dir = dir { mergedAttributes["dir"] = dir }
608617
if let rel = rel { mergedAttributes["rel"] = rel }
@@ -643,6 +652,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
643652
if let title = title { mergedAttributes["title"] = title }
644653
if let scope = scope { mergedAttributes["scope"] = scope }
645654
if let classs = classs { mergedAttributes["class"] = classs }
655+
if let manifest = manifest { mergedAttributes["manifest"] = manifest }
646656
if let value = value { mergedAttributes["value"] = value }
647657
if let clear = clear { mergedAttributes["clear"] = clear }
648658
if let start = start { mergedAttributes["start"] = start }
@@ -668,6 +678,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
668678
if let onfocus = onfocus { mergedAttributes["onfocus"] = onfocus }
669679
if let enctype = enctype { mergedAttributes["enctype"] = enctype }
670680
if let onclick = onclick { mergedAttributes["onclick"] = onclick }
681+
if let ontouchstart = ontouchstart { mergedAttributes["ontouchstart"] = ontouchstart }
671682
if let onkeyup = onkeyup { mergedAttributes["onkeyup"] = onkeyup }
672683
if let profile = profile { mergedAttributes["profile"] = profile }
673684
if let version = version { mergedAttributes["version"] = version }
@@ -713,6 +724,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
713724
if let ondblclick = ondblclick { mergedAttributes["ondblclick"] = ondblclick }
714725
if let onmouseout = onmouseout { mergedAttributes["onmouseout"] = onmouseout }
715726
if let httpEquiv = httpEquiv { mergedAttributes["http-equiv"] = httpEquiv }
727+
if let dataText = dataText { mergedAttributes["data-text"] = dataText }
716728
if let background = background { mergedAttributes["background"] = background }
717729
if let onmousemove = onmousemove { mergedAttributes["onmousemove"] = onmousemove }
718730
if let onmouseover = onmouseover { mergedAttributes["onmouseover"] = onmouseover }
@@ -724,28 +736,28 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
724736
if let placeholder = placeholder { mergedAttributes["placeholder"] = placeholder }
725737
if let marginheight = marginheight { mergedAttributes["marginheight"] = marginheight }
726738
if let acceptCharset = acceptCharset { mergedAttributes["accept-charset"] = acceptCharset }
727-
739+
728740
for item in attrs.enumerated() {
729741
mergedAttributes.updateValue(item.element.1, forKey: item.element.0)
730742
}
731-
743+
732744
output += mergedAttributes.reduce("") { result, item in
733745
if let value = item.value {
734746
return result + " \(item.key)=\"\(value)\""
735747
} else {
736748
return result
737749
}
738750
}
739-
751+
740752
if let inner = inner {
741753
scopesBuffer[Process.tid] = output + ">" + (inner) + "</" + node + ">"
742754
} else {
743755
let current = scopesBuffer[Process.tid] ?? ""
744756
scopesBuffer[Process.tid] = output + ">" + current + "</" + node + ">"
745757
}
746-
758+
747759
// Pop the attributes.
748-
760+
749761
idd = stackid
750762
dir = stackdir
751763
rel = stackrel
@@ -786,6 +798,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
786798
title = stacktitle
787799
scope = stackscope
788800
classs = stackclass
801+
manifest = stackmanifest
789802
value = stackvalue
790803
clear = stackclear
791804
start = stackstart
@@ -811,6 +824,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
811824
onfocus = stackonfocus
812825
enctype = stackenctype
813826
onclick = stackonclick
827+
ontouchstart = stackontouchstart
814828
onkeyup = stackonkeyup
815829
profile = stackprofile
816830
version = stackversion
@@ -856,6 +870,7 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
856870
ondblclick = stackondblclick
857871
onmouseout = stackonmouseout
858872
httpEquiv = stackhttpEquiv
873+
dataText = stackdataText
859874
background = stackbackground
860875
onmousemove = stackonmousemove
861876
onmouseover = stackonmouseover
@@ -867,6 +882,6 @@ private func evaluate(_ node: String, _ attrs: [String: String?] = [:], _ closur
867882
cellspacing = stackcellspacing
868883
marginheight = stackmarginheight
869884
acceptCharset = stackacceptCharset
870-
885+
871886
inner = stackinner
872887
}

0 commit comments

Comments
 (0)