1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
...
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
...
878
879
880
881
882
883
884
885
886
887
888
889
890
891
|
-- vim: ft=terra
local util = lib.util
local secmode = lib.enum { 'public', 'private', 'lockdown', 'isolate' }
local pstring = lib.mem.ptr(int8)
local mimetypes = {
{'html', 'text/html'};
{'json', 'application/json'};
{'json', 'application/activity+json'};
{'json', 'application/ld+json'};
{'mkdown', 'text/markdown'};
{'text', 'text/plain'};
{'ansi', 'text/x-ansi'};
}
local struct srv
local struct cfgcache {
secret: pstring
pol_sec: secmode.t
pol_reg: bool
pol_autoherald: bool
................................................................................
terra lib.store.post:publish(s: &srv)
self:comp()
self.posted = lib.osclock.time(nil)
self.discovered = self.posted
self.chgcount = 0
self.edited = 0
self.id = s:post_create(self)
return self.id
end
local struct convo {
srv: &srv
con: &lib.net.mg_connection
msg: &lib.net.mg_http_message
aid: uint64 -- 0 if logged out
aid_issue: lib.store.timepoint
who: &lib.store.actor -- who we're logged in as, if aid ~= 0
peer: lib.store.inet
reqtype: lib.http.mime.t -- negotiated content type
method: lib.http.method.t
live_last: lib.store.timepoint
uploads: lib.mem.vec(lib.http.upload)
body: lib.str.t
-- cache
ui_hue: uint16
navbar: lib.mem.ptr(int8)
actorcache: lib.mem.cache(lib.mem.ptr(lib.store.actor),32) -- naive cache to avoid unnecessary queries
-- private
varbuf: lib.mem.ptr(int8)
vbofs: &int8
}
struct convo.page {
title: pstring
body: pstring
class: pstring
cache: bool
}
local usrdefs = {
str = {
['acl-follow' ] = {cfgfld = 'usrdef_pol_follow', fallback = 'local'};
['acl-follow-req'] = {cfgfld = 'usrdef_pol_follow_req', fallback = 'all'};
};
}
terra convo:matchmime(mime: lib.http.mime.t): bool
return self.reqtype == [lib.http.mime.none]
or self.reqtype == mime
end
terra convo:usercfg_str(uid: uint64, setting: pstring): pstring
var set = self.srv:actor_conf_str_get(&self.srv.pool, uid, setting)
if not set then
[(function()
local q = quote return pstring.null() end
for key, dfl in pairs(usrdefs.str) do
local rv
if dfl.cfgfld then
rv = quote
var cf = self.srv.cfg.[dfl.cfgfld]
in terralib.select(not cf, pstring([dfl.fallback]), cf) end
elseif dfl.lit then rv = dfl.lit end
q = quote
if setting:cmp([key]) then return [rv] else [q] end
end
end
return q
end)()]
else return set end
end
-- this is unfortunately necessary to work around a terra bug
-- it can't seem to handle forward-declarations of structs in C
local getpeer
do local struct strucheader {
next: &lib.net.mg_connection
mgr: &lib.net.mg_mgr
................................................................................
peer: lib.net.mg_addr
}
terra getpeer(con: &lib.net.mg_connection)
return [&strucheader](con).peer
end
end
terra convo:uid2actor_live(uid: uint64)
var actor = self.srv:actor_fetch_uid(uid)
if actor:ref() then
if self.aid ~= 0 and self.who.id ~= uid then
actor(0).relationship = self.srv:actor_rel_calc(self.who.id, uid)
else -- defensive branch
actor(0).relationship = lib.store.relationship {
agent = 0, patient = uid;
rel = [lib.store.relation.null],
recip = [lib.store.relation.null],
}
end
end
return actor
end
terra convo:uid2actor(uid: uint64)
var actor: &lib.store.actor = nil
for j = 0, self.actorcache.top do
if uid == self.actorcache(j).ptr.id then
actor = self.actorcache(j).ptr
break
end
end
if actor == nil then
actor = self.actorcache:insert(self:uid2actor_live(uid)).ptr
end
return actor
end
terra convo:rawpage(code: uint16, pg: convo.page, hdrs: lib.mem.ptr(lib.http.header))
var doc = data.view.docskel {
instance = self.srv.cfg.instance;
title = pg.title;
body = pg.body;
class = pg.class;
navlinks = self.navbar;
attr = '';
}
var attrbuf: int8[32]
if self.aid ~= 0 and self.ui_hue ~= 323 then
var hdecbuf: int8[21]
var hdec = lib.math.decstr(self.ui_hue, &hdecbuf[20])
lib.str.cpy(&attrbuf[0], ' style="--hue:')
lib.str.cpy(&attrbuf[14], hdec)
var len = &hdecbuf[20] - hdec
lib.str.cpy(&attrbuf[14] + len, '"')
doc.attr = &attrbuf[0]
end
if self.method == [lib.http.method.head]
then doc:head(self.con,code,hdrs)
else doc:send(self.con,code,hdrs)
end
end
terra convo:statpage(code: uint16, pg: convo.page)
var hdrs = array(
lib.http.header { key = 'Content-Type', value = 'text/html; charset=UTF-8' },
lib.http.header { key = 'Cache-Control', value = 'no-store' }
)
self:rawpage(code,pg, [lib.mem.ptr(lib.http.header)] {
ptr = &hdrs[0];
ct = [hdrs.type.N] - lib.trn(pg.cache,1,0);
})
end
terra convo:livepage(pg: convo.page, lastup: lib.store.timepoint)
var nbuf: int8[21]
var hdrs = array(
lib.http.header { key = 'Content-Type', value = 'text/html; charset=UTF-8' },
lib.http.header { key = 'Cache-Control', value = 'no-store' },
lib.http.header {
key = 'X-Live-Newest-Artifact';
value = lib.math.decstr(lastup, &nbuf[20]);
},
lib.http.header { key = 'Content-Length', value = '0' }
)
if self.live_last ~= 0 and self.live_last == lastup then
lib.net.mg_printf(self.con, 'HTTP/1.1 %s', lib.http.codestr(200))
for i = 0, [hdrs.type.N] do
lib.net.mg_printf(self.con, '%s: %s\r\n', hdrs[i].key, hdrs[i].value)
end
lib.net.mg_printf(self.con, '\r\n')
else
self:rawpage(200, pg, [lib.mem.ptr(lib.http.header)] {
ptr = &hdrs[0], ct = 3
})
end
end
terra convo:stdpage(pg: convo.page) self:statpage(200, pg) end
terra convo:bytestream_trusted(lockdown: bool, mime: pstring, data: lib.mem.ptr(uint8))
var lockhdr = "Content-Security-Policy: sandbox; default-src 'none'; form-action 'none'; navigate-to 'none';\r\n"
if not lockdown then lockhdr = "" end
lib.net.mg_printf(self.con, "HTTP/1.1 200 OK\r\nContent-Type: %.*s\r\nContent-Length: %llu\r\n%sX-Content-Options: nosniff\r\n\r\n", mime.ct, mime.ptr, data.ct + 2, lockhdr)
lib.net.mg_send(self.con, data.ptr, data.ct)
lib.net.mg_send(self.con, '\r\n', 2)
end
terra convo:json(data: pstring)
self:bytestream_trusted(false, 'application/activity+json; charset=utf-8', data:blob())
end
terra convo:bytestream(mime: pstring, data: lib.mem.ptr(uint8))
-- TODO this is not a satisfactory solution; it's a bandaid on a gaping
-- chest wound. ultimately we need to compile a whitelist of safe mime
-- types as part of mimelib, but that is no small task. for now, this
-- will keep the patient from immediately bleeding out
if mime:cmp('text/html') or
mime:cmp('text/xml') or
mime:cmp('application/xhtml+xml') or
mime:cmp('application/vnd.wap.xhtml+xml')
then -- danger will robinson
mime = 'text/plain'
elseif mime:cmp('application/x-shockwave-flash') then
mime = 'application/octet-stream'
end
self:bytestream_trusted(true, mime, data)
end
terra convo:reroute_cookie(dest: rawstring, cookie: rawstring)
var hdrs = array(
lib.http.header { key = 'Content-Type', value = 'text/html; charset=UTF-8' },
lib.http.header { key = 'Location', value = dest },
lib.http.header { key = 'Set-Cookie', value = cookie }
)
var body = data.view.docskel {
instance = self.srv.cfg.instance.ptr;
title = 'rerouting';
body = 'you are being redirected';
class = 'error';
navlinks = '';
attr = '';
}
body:send(self.con, 303, [lib.mem.ptr(lib.http.header)] {
ptr = &hdrs[0], ct = [hdrs.type.N] - lib.trn(cookie == nil,1,0)
})
end
terra convo:reroute(dest: rawstring) self:reroute_cookie(dest,nil) end
terra convo:installkey(dest: rawstring, aid: uint64)
var sesskey: int8[lib.session.maxlen + #lib.session.cookiename + #"=; Path=/" + 1]
do var p = &sesskey[0]
p = lib.str.ncpy(p, [lib.session.cookiename .. '='], [#lib.session.cookiename + 1])
p = p + lib.session.cookie_gen(self.srv.cfg.secret, aid, lib.osclock.time(nil), p)
lib.dbg('sending cookie ',{&sesskey[0],15})
p = lib.str.ncpy(p, '; Path=/', 9)
end
self:reroute_cookie(dest, &sesskey[0])
end
terra convo:stra(sz: intptr) -- convenience function
var s: lib.str.acc
s:pool(&self.srv.pool,sz)
return s
end
convo.methods.qstr = macro(function(self, ...) -- convenience string builder
local exp = {...}
return `lib.str.acc{}:pcompose(&self.srv.pool, [exp]):finalize()
end)
terra convo:complain(code: uint16, title: rawstring, msg: rawstring)
if msg == nil then msg = "i'm sorry, dave. i can't let you do that" end
if self:matchmime(lib.http.mime.html) then
var body = [convo.page] {
title = self:qstr('error :: ', title);
body = self:qstr('<div class="message"><img class="icon" src="/s/warn.svg"><h1>',title,'</h1><p>',msg,'</p></div>');
class = 'error';
cache = false;
}
self:statpage(code, body)
else
var pg = lib.http.page { respcode = code, body = pstring.null() }
var ctt = lib.http.mime.none
if self:matchmime(lib.http.mime.json) then ctt = lib.http.mime.json
pg.body = ([lib.tpl.mk'{"_parsav_error":@$ekind, "_parsav_error_desc":@$edesc}']
{ekind = title, edesc = msg}):poolstr(&self.srv.pool)
elseif self:matchmime(lib.http.mime.text) then ctt = lib.http.mime.text
pg.body = self:qstr('error: ',title,'\n',msg)
elseif self:matchmime(lib.http.mime.mkdown) then ctt = lib.http.mime.mkdown
pg.body = self:qstr('# error :: ',title,'\n\n',msg)
elseif self:matchmime(lib.http.mime.ansi) then ctt = lib.http.mime.ansi
pg.body = self:qstr('\27[1;31merror :: ',title,'\27[m\n',msg)
end
var cthdr = lib.http.header { 'Content-Type', 'text/plain' }
if ctt == lib.http.mime.none then
pg.headers.ct = 0
else
pg.headers = lib.typeof(pg.headers) { &cthdr, 1 }
switch ctt do
case [ctt.type](lib.http.mime.json) then
cthdr.value = 'application/json'
end
escape
for i,v in ipairs(mimetypes) do local key,mime = v[1],v[2]
if key ~= 'json' then
emit quote case [ctt.type](lib.http.mime.[key]) then cthdr.value = [mime] end end
end
end
end
end
end
pg:send(self.con)
end
end
terra convo:fail(code: uint16)
switch code do
escape
local stderrors = {
{400, 'bad request', "the action you have attempted on this resource is not meaningful"};
{401, 'unauthorized', "this resource is not available at your clearance level"};
{403, 'forbidden', "we can neither confirm nor deny the existence of this resource"};
{404, 'resource not found', "that resource is not extant on or known to this server"};
{405, 'method not allowed', "the method you have attempted on this resource is not meaningful"};
{406, 'not acceptable', "none of the suggested content types are a viable representation of this resource"};
{500, 'internal server error', "parsav did a fucksy wucksy"};
}
for i,v in ipairs(stderrors) do
emit quote case uint16([v[1]]) then
self:complain([v])
end end
end
end
else self:complain(500,'unknown error','an unrecognized error was thrown. this is a bug')
end
end
terra convo:confirm(title: pstring, msg: pstring, cancel: pstring)
var conf = data.view.confirm {
title = title;
query = msg;
cancel = cancel;
}
var ti: lib.str.acc ti:pcompose(&self.srv.pool,'confirm :: ', title)
var body = conf:poolstr(&self.srv.pool) -- defer body:free()
var cf = [convo.page] {
title = ti:finalize();
class = 'query';
body = body; cache = false;
}
self:stdpage(cf)
--cf.title:free()
end
convo.methods.assertpow = macro(function(self, pow)
return quote
var ok = true
if self.aid == 0 or self.who.rights.powers.[pow:asvalue()]() == false then
ok = false
self:complain(403,'insufficient privileges',['you lack the <strong>'..pow:asvalue()..'</strong> power and cannot perform this action'])
end
in ok end
end)
local pstr2mg, mg2pstr
do -- aaaaaaaaaaaaaaaaaaaaaaaa
mgstr = lib.util.find(lib.net.mg_http_message.entries, function(v)
if v.field == 'body' or v[1] == 'body' then return v.type end
end)
terra pstr2mg(p: pstring): mgstr
return mgstr { ptr = p.ptr, len = p.ct }
end
terra mg2pstr(m: mgstr): pstring
return pstring { ptr = m.ptr, ct = m.len }
end
end
-- CALL ONLY ONCE PER VAR
terra convo:postv_next(name: pstring, start: &pstring)
if self.varbuf.ptr == nil then
self.varbuf = self.srv.pool:alloc(int8, self.msg.body.len + self.msg.query.len)
self.vbofs = self.varbuf.ptr
end
var conv = pstr2mg(@start)
var o = lib.net.mg_http_get_var(
&conv,
name.ptr, self.vbofs,
self.varbuf.ct - (self.vbofs - self.varbuf.ptr)
)
if o > 0 then
start:advance(name.ct + o + 2)
var r = self.vbofs
self.vbofs = self.vbofs + o + 1
@(self.vbofs - 1) = 0
var norm = lib.str.normalize([lib.mem.ptr(int8)]{ptr = r, ct = o})
return norm.ptr, norm.ct
else return nil, 0 end
end
terra convo:postv(name: pstring)
var start = mg2pstr(self.msg.body)
return self:postv_next(name, &start)
end
terra convo:ppostv(name: pstring)
var s,l = self:postv(name)
return pstring { ptr = s, ct = l }
end
do
local struct postiter { co: &convo where: pstring name: pstring }
terra convo:eachpostv(name: pstring)
return postiter { co = self, where = mg2pstr(self.msg.body), name = name }
end
postiter.metamethods.__for = function(self, body)
return quote
while true do
var str, len = self.co:postv_next(self.name, &self.where)
if str == nil then break end
[ body(`pstring {str, len}) ]
end
end
end
end
terra convo:getv(name: rawstring)
if self.varbuf.ptr == nil then
self.varbuf = self.srv.pool:alloc(int8, self.msg.query.len + self.msg.body.len)
self.vbofs = self.varbuf.ptr
end
var o = lib.net.mg_http_get_var(&self.msg.query, name, self.vbofs, self.varbuf.ct - (self.vbofs - self.varbuf.ptr))
if o > 0 then
var r = self.vbofs
self.vbofs = self.vbofs + o + 1
@(self.vbofs - 1) = 0
var norm = lib.str.normalize([lib.mem.ptr(int8)]{ptr = r, ct = o})
return norm.ptr, norm.ct
else return nil, 0 end
end
terra convo:pgetv(name: rawstring)
var s,l = self:getv(name)
return pstring { ptr = s, ct = l }
end
local route = {} -- these are defined in route.t, as they need access to renderers
terra route.dispatch_http :: {&convo, lib.mem.ptr(int8)} -> {}
local mimevar = symbol(lib.mem.ref(int8))
local mimeneg = `lib.http.mime.none
for i, t in ipairs(mimetypes) do
local name, mime = t[1], t[2]
mimeneg = quote
var ret: lib.http.mime.t
if lib.str.ncmp(mimevar.ptr, mime, lib.math.biggest(mimevar.ct, [#mime])) == 0 then
ret = [lib.http.mime[name]]
else ret = [mimeneg] end
in ret end
end
local handle = {
http = terra(con: &lib.net.mg_connection, event_kind: int, event: &opaque, userdata: &opaque)
var server = [&srv](userdata)
var mgpeer = getpeer(con)
-- var pbuf: int8[128]
-- the peer property is currently broken and there is precious
................................................................................
co.body.ptr = msg.body.ptr co.body.ct = msg.body.len
-- first, check for an accept header. if it's there, we need to
-- iterate over the values and pick the highest-priority one
do var acc = lib.http.findheader(msg, 'Accept')
-- TODO handle q-value
if acc ~= nil and acc.ptr ~= nil then
var [mimevar] = [lib.mem.ref(int8)] { ptr = acc.ptr }
var i = 0 while i < acc.ct do
if acc.ptr[i] == @',' or acc.ptr[i] == @';' then
mimevar.ct = (acc.ptr+i) - mimevar.ptr
var t = [mimeneg]
if t ~= lib.http.mime.none then
co.reqtype = t
goto foundtype
end
if acc.ptr[i] == @';' then -- fast-forward over q
for j=i+1,acc.ct do i=j
if acc.ptr[j] == @',' then break end
end
................................................................................
mimevar.ptr = acc.ptr + i + 1
end
i=i+1
end
if co.reqtype == lib.http.mime.none then
mimevar.ct = acc.ct - (mimevar.ptr - acc.ptr)
co.reqtype = [mimeneg]
if co.reqtype == lib.http.mime.none then
co.reqtype = lib.http.mime.html
end
end
else co.reqtype = lib.http.mime.html end
::foundtype::end
-- we need to check if there's any cookies sent with the request,
-- and if so, whether they contain any credentials. this will be
-- used to set the auth parameters in the http conversation
var cookies_p = lib.http.findheader(msg, 'Cookie')
if cookies_p ~= nil and cookies_p.ptr ~= nil then
................................................................................
end
bsr:free()
upmap:free()
end
end
end
route.dispatch_http(&co, uri)
::fail::
if co.uploads.run > 0 then
for i=0,co.uploads.sz do
co.uploads(i).filename:free()
co.uploads(i).field:free()
|
<
<
<
<
<
<
<
<
<
>
>
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
>
|
|
|
|
|
|
|
>
>
|
1
2
3
4
5
6
7
8
9
10
11
...
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
...
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
-- vim: ft=terra
local util = lib.util
local secmode = lib.enum { 'public', 'private', 'lockdown', 'isolate' }
local pstring = lib.mem.ptr(int8)
local struct srv
local struct cfgcache {
secret: pstring
pol_sec: secmode.t
pol_reg: bool
pol_autoherald: bool
................................................................................
terra lib.store.post:publish(s: &srv)
self:comp()
self.posted = lib.osclock.time(nil)
self.discovered = self.posted
self.chgcount = 0
self.edited = 0
self.uri = nil -- only for foreign posts
self.convoheaduri = nil -- ditto
self.id = s:post_create(self)
return self.id
end
local convo = terralib.loadfile 'convo.t'(srv)
-- this is unfortunately necessary to work around a terra bug
-- it can't seem to handle forward-declarations of structs in C
local getpeer
do local struct strucheader {
next: &lib.net.mg_connection
mgr: &lib.net.mg_mgr
................................................................................
peer: lib.net.mg_addr
}
terra getpeer(con: &lib.net.mg_connection)
return [&strucheader](con).peer
end
end
local route = {} -- these are defined in route.t, as they need access to renderers
terra route.dispatch_http :: {&convo, lib.mem.ptr(int8)} -> {}
local handle = {
http = terra(con: &lib.net.mg_connection, event_kind: int, event: &opaque, userdata: &opaque)
var server = [&srv](userdata)
var mgpeer = getpeer(con)
-- var pbuf: int8[128]
-- the peer property is currently broken and there is precious
................................................................................
co.body.ptr = msg.body.ptr co.body.ct = msg.body.len
-- first, check for an accept header. if it's there, we need to
-- iterate over the values and pick the highest-priority one
do var acc = lib.http.findheader(msg, 'Accept')
-- TODO handle q-value
if acc ~= nil and acc.ptr ~= nil then
var mimevar = [pstring] { ptr = acc.ptr }
lib.dbg('accept header is ', {acc.ptr,acc.ct})
var i = 0 while i < acc.ct do
if acc.ptr[i] == @',' or acc.ptr[i] == @';' then
mimevar.ct = (acc.ptr+i) - mimevar.ptr
var mk = lib.mime.lookup(mimevar)
if mk ~= nil and mk.output ~= lib.http.mime.none then
co.reqtype = mk.output
goto foundtype
end
if acc.ptr[i] == @';' then -- fast-forward over q
for j=i+1,acc.ct do i=j
if acc.ptr[j] == @',' then break end
end
................................................................................
mimevar.ptr = acc.ptr + i + 1
end
i=i+1
end
if co.reqtype == lib.http.mime.none then
mimevar.ct = acc.ct - (mimevar.ptr - acc.ptr)
var mk = lib.mime.lookup(mimevar)
if mk ~= nil and mk.output ~= lib.http.mime.none then
co.reqtype = mk.output
end
end
end
::foundtype::end
-- we need to check if there's any cookies sent with the request,
-- and if so, whether they contain any credentials. this will be
-- used to set the auth parameters in the http conversation
var cookies_p = lib.http.findheader(msg, 'Cookie')
if cookies_p ~= nil and cookies_p.ptr ~= nil then
................................................................................
end
bsr:free()
upmap:free()
end
end
end
var mtt = lib.http.mime._str(co.reqtype)
lib.dbg('routing with negotiated type of ', {mtt.ptr,mtt.ct})
route.dispatch_http(&co, uri)
::fail::
if co.uploads.run > 0 then
for i=0,co.uploads.sz do
co.uploads(i).filename:free()
co.uploads(i).field:free()
|