parsav  Diff

Differences From Artifact [6d6c40fc94]:

To Artifact [e5e590a634]:


     1      1   -- vim: ft=terra
     2      2   local m = {}
     3      3   local util = dofile('common.lua')
     4      4   
            5  +m.method = lib.enum { 'get', 'post', 'head', 'options', 'put', 'delete' }
            6  +
            7  +m.findheader = terralib.externfunction('mg_http_get_header', {&lib.net.mg_http_message, rawstring} -> &lib.mem.ref(int8)) -- unfortunately necessary to access this function, as its return type conflicts with a function name
            8  +
     5      9   struct m.header {
     6     10   	key: rawstring
     7     11   	value: rawstring
     8     12   }
     9     13   struct m.page {
    10     14   	respcode: uint16
    11     15   	body: lib.mem.ptr(int8)
................................................................................
    15     19   local resps = {
    16     20   	[200] = 'OK';
    17     21   	[201] = 'Created';
    18     22   	[301] = 'Moved Permanently';
    19     23   	[302] = 'Found';
    20     24   	[303] = 'See Other';
    21     25   	[307] = 'Temporary Redirect';
    22         -	[404] = 'Not Found';
           26  +	[400] = 'Bad Request';
    23     27   	[401] = 'Unauthorized';
           28  +	[402] = 'Payment Required';
    24     29   	[403] = 'Forbidden';
           30  +	[404] = 'Not Found';
           31  +	[405] = 'Method Not Allowed';
           32  +	[406] = 'Not Acceptable';
    25     33   	[418] = 'I\'m a teapot';
    26     34   	[405] = 'Method Not Allowed';
    27     35   	[500] = 'Internal Server Error';
    28     36   }
    29     37   local resptext = symbol(rawstring)
    30     38   local resplen = symbol(intptr)
    31     39   local respbranches = {}
................................................................................
    36     44   	end
    37     45   end
    38     46   m.codestr = terra(code: uint16)
    39     47   	var [resptext] var [resplen]
    40     48   	switch code do [respbranches] end
    41     49   	return resptext, resplen
    42     50   end
           51  +
           52  +terra m.hier(uri: lib.mem.ptr(int8)): lib.mem.ptr(lib.mem.ref(int8))
           53  +	if uri.ct == 0 then return [lib.mem.ptr(lib.mem.ref(int8))] { ptr = nil, ct = 0 } end
           54  +	var sz = 1
           55  +	var start = 0 if uri.ptr[0] == @'/' then start = 1 end
           56  +	for i = start, uri.ct do if uri.ptr[i] == @'/' then sz = sz + 1 end end
           57  +	var lst = lib.mem.heapa([lib.mem.ref(int8)], sz)
           58  +	if sz == 0 then
           59  +		lst.ptr[0].ptr = uri.ptr
           60  +		lst.ptr[0].ct = uri.ct
           61  +		return lst
           62  +	end
           63  +
           64  +	var idx: intptr = 0
           65  +	var len: intptr = 0
           66  +	lst.ptr[0].ptr = uri.ptr
           67  +	for i = 0, uri.ct do
           68  +		if uri.ptr[i] == @'/' then
           69  +			if len == 0 then
           70  +				lst.ptr[idx].ptr = lst.ptr[idx].ptr + 1
           71  +				goto skip
           72  +			end
           73  +			lst.ptr[idx].ct = len
           74  +			idx = idx + 1
           75  +			lst.ptr[idx].ptr = uri.ptr + i + 1
           76  +			len = 0
           77  +		else
           78  +			len = len + 1
           79  +		end
           80  +	::skip::end
           81  +	lst.ptr[idx].ct = len
           82  +	
           83  +	return lst
           84  +end
           85  +
    43     86   m.page.methods = {
    44     87   	free = terra(self: &m.page)
    45     88   		self.body:free()
    46     89   		self.headers:free()
    47     90   	end;
    48     91   	send = terra(self: &m.page, con: &lib.net.mg_connection)
    49     92   		var code: rawstring