-
Notifications
You must be signed in to change notification settings - Fork 0
/
javel.bah
209 lines (166 loc) · 5.14 KB
/
javel.bah
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#import "iostream.bah"
#import "http.bah"
#import "json.bah"
#import "path.bah"
#import "time.bah"
// #include "javelWasm.bah"
struct javel_security {
timeout: int = 0
//in progress
test(req http_request) bool {
return true
}
}
struct javel_route {
path: cpstring = ""
base: cpstring = ""
handle: function(javel_route*, http_request, http_response) = null
error: function(javel_route*, int, http_request, http_response) = null
events: function(javel_route*, cpstring, ptr, http_request, http_response) = null
security: javel_security
use(req http_request, resp http_response) {
if this.security.timeout > 0 {
if this.security.test(req) == false {
if this.error != null {
this.error(400, req, resp)
} else {
resp.write("Error: 400 accessing '"+req.path+"' and unconfigured javel error for route '"+this.path+"'.")
}
return
}
} else {
this.handle(req, resp)
}
}
match(path cpstring) int {
if path == this.path {
return strlen(this.path)
}
ps = string(path)
tps = string(this.path)
if tps.hasSuffix("/") && ps.hasPrefix(this.path) {
return strlen(this.path)
}
return 0
}
}
__javel_global_ptr = null
#define __javel_handler(s http_server*, connfd int)
struct javel {
s: http_server*
routes: []javel_route
requestsMaxSize: int = -1
port: int
launch() {
__javel_global_ptr = this
this.s = new http_server{
port: this.port
}
this.s.handle = __javel_handler
this.s.listenAndServe()
}
}
#define __javel_internal_route(req http_request, resp http_response)
__javel_handler(s http_server*, connfd int) {
jav javel* = __javel_global_ptr
if jav == null {
return
}
req http_request
if jav.requestsMaxSize != -1 {
req = http_requestSizeLimited(connfd, jav.requestsMaxSize)
} else {
req = http_request(connfd)
}
resp = http_response(connfd)
bestRoute = javel_route{}
bestScore = 0
if strHasPrefix(req.path, "/__javel/") {
__javel_internal_route(req, resp)
return
}
i=0; for i < len(jav.routes), i++ {
route = jav.routes[i]
score = route.match(req.path)
if score > bestScore {
bestScore = score
bestRoute = route
}
}
if bestRoute.handle != null {
bestRoute.use(req, resp)
} else {
resp.write("Error: unconfigured javel route.")
resp.send()
}
}
javel_parsePath(path cpstring, resp http_response) cpstring {
p = string(path)
if p.hasSuffix("/") {
p.append("index.html")
} else if p.count(".") == 0 {
p.append(".html")
}
if p.hasSuffix(".html") {
resp.setHeader("content-type", "text/html")
} else if p.hasSuffix(".css") {
resp.setHeader("content-type", "text/css")
} else if p.hasSuffix(".js") {
resp.setHeader("content-type", "text/javascript")
} else if p.hasSuffix(".json") {
resp.setHeader("content-type", "application/json")
} else if p.hasSuffix(".png") {
resp.setHeader("content-type", "image/png")
} else if p.hasSuffix(".jpeg") {
resp.setHeader("content-type", "image/jpeg")
} else if p.hasSuffix(".mov") {
resp.setHeader("content-type", "video/mov")
} else if p.hasSuffix(".mp4") {
resp.setHeader("content-type", "video/mp4")
} else if p.hasSuffix(".wasm") {
resp.setHeader("content-type", "application/wasm")
}
return p.str()
}
// #include "jsRuntime.bah"
#include "defaultHandles.bah"
#include "wrappers.bah"
__javel_internal_route(req http_request, resp http_response) {
if strHasPrefix(req.path, "/__javel/load/") {
evts = new javel_eventStream{
response: resp
}
jav javel* = __javel_global_ptr
if jav == null {
return
}
bestRoute = javel_route{}
bestScore = 0
p = string(req.path)
p.trimLeft(strlen("/__javel/load"))
i=0; for i < len(jav.routes), i++ {
route = jav.routes[i]
score = route.match(p.str())
if score > bestScore {
bestScore = score
bestRoute = route
}
}
if bestRoute.handle == javel_serveHTML {
if bestRoute.handle == javel_serveFile || bestRoute.handle == javel_serveHTML {
fp = bestRoute.base + "/" + javel_parsePath(p.str(), resp)
lm = getLastModified(fp)
for 1 == 1 {
if lm != getLastModified(fp) {
evts.send("reload")
lm = getLastModified(fp)
}
}
} else {
if bestRoute.events != null {
bestRoute.events(p.str(), evts, req, resp)
}
}
}
}
}