-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.nim
180 lines (136 loc) · 3.75 KB
/
http.nim
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import std/[tables,strutils,uri]
import cps
import bio, types
type
Headers* = ref object
headers*: Table[string, seq[string]]
Request* = ref object
meth*: string
uri*: Uri
keepAlive*: bool
contentLength*: int
headers*: Headers
Response* = ref object
headers*: Headers
statusCode*: int
reason*: string
contentLength*: int
keepAlive*: bool
body*: string
StatusCode = distinct int
ResponseWriter* = ref object
bio: Bio
proc statusCodeStr(sc: int): string =
case sc
of 200: "200 Ok"
of 201: "201 Created"
of 403: "403 Forbidden"
of 404: "404 Not Found"
else: $sc
# Chunked encoded writer
proc newResponseWriter*(bio: Bio): ResponseWriter =
ResponseWriter(bio: bio)
proc write*(rw: ResponseWriter, s: string) {.cps:C.} =
discard rw.bio.write tohex(s.len)
discard rw.bio.write("\r\n")
discard rw.bio.write(s)
discard rw.bio.write("\r\n")
#
# Headers
#
proc canon(s: string): string =
let l = s.len
result = newStringOfCap(l)
var upper = true
for c in s:
result &= (if upper: c.toUpperAscii else: c.toLowerAscii)
upper = c == '-'
proc add*(headers: Headers, key: string, val: string) =
if key notin headers.headers:
headers.headers[key] = @[]
headers.headers[key].add val
proc read*(bio: Bio, headers: Headers) {.cps:C.} =
while true:
let line = bio.readLine()
if line.len() == 0:
break
let ps = line.split(": ", 2)
if ps.len == 2:
headers.add(ps[0].toLower, ps[1])
proc `$`*(headers: Headers): string =
for k, vs in headers.headers:
result.add canon(k) & ": " & vs.join(",") & "\r\n"
result.add "\r\n"
proc get*(headers: Headers, key: string): string =
let key = key.toLower
if key in headers.headers:
return headers.headers[key][0]
#
# Request
#
proc newRequest*(): Request {.cps:C.} =
Request(
headers: Headers()
)
proc newRequest*(meth: string, url: string): Request =
result = Request(
meth: meth,
headers: http.Headers(),
)
parseUri(url, result.uri)
proc `$`*(req: Request): string =
var path = req.uri.path
if path == "": path = "/"
if req.uri.query.len > 0:
path.add("?" & req.uri.query)
result.add req.meth & " " & path & " HTTP/1.1\r\n"
result.add("Host: " & req.uri.hostname & "\r\n")
if req.contentLength > 0:
result.add("Content-Length: " & $req.contentLength & "\r\n")
result.add $req.headers
proc read*(bio: Bio, req: Request) {.cps:C.} =
let line = bio.readLine()
if line == "":
bio.close()
return
let ps = splitWhitespace(line, 3)
let (meth, target, version) = (ps[0], ps[1], ps[2])
req.meth = meth
bio.read(req.headers)
req.keepAlive = req.headers.get("Connection") == "Keep-Alive"
let host = req.headers.get("Host")
try:
req.contentLength = parseInt(req.headers.get("Content-Length"))
except:
discard
parseUri("http://" & host & target, req.uri)
proc write*(bio: Bio, req: Request) {.cps:C.} =
discard bio.write($req)
#
# Response
#
proc newResponse*(): Response =
result = Response(
headers: http.Headers(),
contentLength: -1,
)
proc `$`*(rsp: Response): string =
result.add "HTTP/1.0 " & statusCodeStr(rsp.statusCode) & "\r\n"
result.add "Content-Type: text/plain\r\n"
if rsp.contentLength > 0:
result.add "Content-Length: " & $rsp.contentLength & "\r\n"
if rsp.keepAlive:
result.add "Connection: Keep-Alive\r\n"
result.add $rsp.headers
proc read*(bio: Bio, rsp: Response) {.cps:C.}=
let line = bio.readLine()
let ps = splitWhitespace(line, 3)
rsp.statusCode = parseInt(ps[1])
rsp.reason = ps[2]
bio.read(rsp.headers)
try:
rsp.contentLength = parseInt(rsp.headers.get("Content-Length"))
except:
discard
proc write*(bio: Bio, rsp: Response) {.cps:C.} =
discard bio.write $rsp