-
Notifications
You must be signed in to change notification settings - Fork 1
/
Netz.io
89 lines (77 loc) · 2.33 KB
/
Netz.io
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Netz := Object clone do(
NotImplementedError := Exception clone
NetzError := Exception clone
BaseRequest := Object clone do(
environment ::= nil
body ::= nil
requestMethod := method(
environment at("REQUEST_METHOD") asUppercase
)
contentType := method(
environment at("CONTENT_TYPE")
)
isGet := method(requestMethod == "GET")
isPost := method(requestMethod == "POST")
isPut := method(requestMethod == "PUT")
isDelete := method(requestMethod == "DELETE")
path := method(
environment at("PATH_INFO")
)
write := method(stuff,
NotImplementedError raise("Request misses a `write` implementation!")
)
writeln := method(line,
self write(line)
self write("\r\n")
self
)
)
SocketRequest := BaseRequest clone do(
socket ::= nil
write := method(line,
if(line isNil not,
socket write(line)
)
)
)
/*doc Netz decodeUrlParam(url)
decode an url parameter as described in rfc 1738
*/
decodeUrlParam := method(url,
sign := "%" at(0)
i := 0
decoded := Sequence clone asMutable
while(i < url size,
if(url at(i) == sign,
# decode sequence following
seq := url exSlice(i + 1, i + 3)
decoded append(("0x" .. seq) asNumber)
i = i + 3
,
# nothing special following
decoded append(url at(i))
i = i + 1
)
)
decoded
)
/*doc Netz escapeHtmlSpecialChars(string[, quotes])
convert <, > and & to <, > and &. If *quotes*
is true, " is additionally converted to ".
Make sure that you call this for already url-decoded
strings (call decodeUrlParam before) for more safety.
*/
escapeHtmlSpecialChars := method(string, quotes,
if(string isMutable not,
string = string asMutable
)
string println
string replaceSeq("&", "&")
string replaceSeq("<", "<")
string replaceSeq(">", ">")
if(quotes,
string replaceSeq("\"", """)
)
string
)
)