net/httpserver.lua
changeset 2775 72b31799b0cb
parent 2767 473627393d40
child 2877 1edeb8fe7d14
equal deleted inserted replaced
2774:55ec6991c1a8 2775:72b31799b0cb
    20 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
    20 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type;
    21 
    21 
    22 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
    22 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%02x", c:byte()); end)); end
    23 
    23 
    24 local log = require "util.logger".init("httpserver");
    24 local log = require "util.logger".init("httpserver");
    25 
       
    26 -- TODO: Should we read this from /etc/mime.types if it exists? (startup time...?)
       
    27 local mime_map = { html = "text/html", txt = "plain/text; charset=utf-8", js = "text/javascript" };
       
    28 
    25 
    29 local http_servers = {};
    26 local http_servers = {};
    30 
    27 
    31 module "httpserver"
    28 module "httpserver"
    32 
    29 
    66 		-- Response we have is just a string (the body)
    63 		-- Response we have is just a string (the body)
    67 		log("debug", "Sending 200 response to %s", request.id or "<none>");
    64 		log("debug", "Sending 200 response to %s", request.id or "<none>");
    68 		
    65 		
    69 		resp = { "HTTP/1.0 200 OK\r\n" };
    66 		resp = { "HTTP/1.0 200 OK\r\n" };
    70 		t_insert(resp, "Connection: close\r\n");
    67 		t_insert(resp, "Connection: close\r\n");
    71 		t_insert(resp, "Content-Type: ");
    68 		t_insert(resp, "Content-Type: text/html\r\n");
    72 		t_insert(resp, mime_map[request.url.path:match("%.(%w+)")] or "application/octet-stream");
       
    73 		t_insert(resp, "\r\n");
       
    74 		t_insert(resp, "Content-Length: ");
    69 		t_insert(resp, "Content-Length: ");
    75 		t_insert(resp, #response);
    70 		t_insert(resp, #response);
    76 		t_insert(resp, "\r\n\r\n");
    71 		t_insert(resp, "\r\n\r\n");
    77 		
    72 		
    78 		t_insert(resp, response);
    73 		t_insert(resp, response);