Skip to content

Commit a7bf34c

Browse files
committed
Add Scopes, change statusCode and reasonPhrase to public computed variables
1 parent 1dbff28 commit a7bf34c

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

XCode/Sources/HttpResponse.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -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
}
107107

108-
func reasonPhrase() -> String {
108+
var reasonPhrase: String {
109109
switch self {
110110
case .switchProtocols : return "Switching Protocols"
111111
case .ok : return "OK"
@@ -122,7 +122,7 @@ public enum HttpResponse {
122122
}
123123
}
124124

125-
func headers() -> [String: String] {
125+
public func headers() -> [String: String] {
126126
var headers = ["Server": "Swifter \(HttpServer.VERSION)"]
127127
switch self {
128128
case .switchProtocols(let switchHeaders, _):
@@ -179,5 +179,5 @@ public enum HttpResponse {
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

+1-1
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

+35-20
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)