This is probably the simplest Web-server in the world. It is not the most efficient because it runs only single-threaded and does not keep track of clients and closes the connection after every thread. Public domain by TomD.
/*
* Minimal webserver
*/
module mwebserver;
private import tango.io.device.FileConduit;
private import tango.io.Path;
private import tango.net.ServerSocket,
tango.net.SocketConduit;
private import tango.text.Util;
private import tango.text.convert.Sprint;
version(Windows){
const char[] DOCROOT="c:/DATA/tdemmer/";
}
version(linux){
const char[] DOCROOT="/home/tomd/";
}
const int SRVPORT = 80;
bool
sendFile(in char[] name, OutputStream dst){
char[] fullName = DOCROOT ~ name;
char[] mtype;
// Check if file exists
auto p = tango.io.Path.parse(fullName);
if ( !exists( fullName )){
return false;
}
// Shorter as an AA?
switch(p.suffix){
case ".htm":
case ".html":
mtype = "text/html;charset=utf-8";
//mtype = "application/xhtml+xml;charset=utf-8";
break;
case ".xhtml":
mtype = "application/xhtml+xml;charset=utf-8";
break;
case ".jpg":
mtype = "image/jpeg";
break;
case ".gif":
mtype = "image/gif";
break;
case ".svg":
mtype = "image/svg+xml";
break;
case ".png":
mtype = "image/png";
break;
case ".css":
mtype = "text/css";
break;
case ".js":
mtype = "application/x-javascript";
break;
default:
mtype = "application/octet-stream";
return false;
}
int fsize = fileSize(fullName);
// print header
auto sprint = new Sprint!(char);
dst.write(sprint("HTTP/1.1 200 OK\r\nServer: miniserver/0.1\r\n"
"Connection: close\r\nContent-Length: {}\r\n"
"Content-Type: {}\r\n\r\n", fsize, mtype));
// write file
auto fc = new FileConduit(fullName);
dst.copy(fc);
fc.close();
return true;
}
void
main(){
auto server = new ServerSocket (new InternetAddress(SRVPORT));
// wait for requests
while(1){
auto request = server.accept;
char[1024] req;
auto len = request.input.read(req);
if (req[0..3] == "GET"){
auto blnk = locate(req[4..len], ' ', 0);
auto rf = req[4..blnk+4];
if(!sendFile(rf,request.output)){
request.output.write("404: File not found\n");
}
}else{
// write a response
request.output.write ("server replies:"~req[0..len]);
}
request.close;
}
}












