ptWebServer
class
class ptWebServer {
ptConfig config;
ptWebServer({String configFile,ptConfig this.config}) {
}
void Run() {
print("Starting ${config.servername}");
Map hosts = new Map();
if(config == null) {
print("Unable to start, please provide a config");
return;
}
for(int i=0; i<config.hosts.length; ++i) {
print("Reading host ${i+1} of ${config.hosts.length}");
String key = "${config.hosts[i].address}:${config.hosts[i].port}";
if(!hosts.containsKey(key)) {
hosts[key]=new List<ptHost>();
}
List<ptHost> list = hosts[key];
list.add(config.hosts[i]);
}
hosts.forEach((String addrport, List<ptHost> hostList) {
String addr = hostList[0].address;
int port = hostList[0].port;
StartListener(addr,port,hostList);
});
}
void SendFile(String path, HttpRequest req) {
File file = new File(path);
if(file.existsSync()) {
print("Sending ${path}");
req.response.write(file.readAsStringSync(encoding: ASCII));
} else {
print("Unable to send ${path}, doesn't exist");
}
}
void DefaultRequest(ptHost host, HttpRequest req) {
//req.response.writeln("path : ${req.uri.path}");
//req.response.writeln("toFilePath : ${req.uri.toFilePath()}");
// Pass the requested file back
if(host.public_path.length > 0) {
Directory dir = new Directory("${host.public_path}${req.uri.path}");
String reqPath = "${host.public_path}${req.uri.path}";
if(reqPath.endsWith("/") && (new Directory(reqPath)).existsSync()) {
Directory path = new Directory(reqPath);
if(host.allow_dir_view) {
bool indexFound = false;
List<FileSystemEntity> files = path.listSync(recursive: false, followLinks: false);
for(int i=0; i<files.length; ++i) {
for(int f=0; f<host.public_index_files.length; ++f) {
if(files[i].path.endsWith(host.public_index_files[f])) {
indexFound = true;
SendFile(files[i].path,req);
break;
}
}
if(indexFound) {
break;
}
}
if(!indexFound) {
print("Generating folder view for ${req.uri.path} : ${reqPath}");
// Render the directory structure
req.response.write("<html><body><h1>Index of ${req.uri.path}</h1><table><tr><td style=\"width: 25%\">Name</td><td style=\"width: 25%\">Size</td><td style=\"width: 25%\">Last Modified</td></tr>");
for(int i=0; i<files.length; ++i) {
req.response.write("<tr><td><a href=\"${files[i].path.substring(files[i].parent.path.length+1)}\">${files[i].path.substring(files[i].parent.path.length+1)}</a></td><td>${files[i].statSync().size}</td><td>${files[i].statSync().modified.toString()}</td></tr>");
}
req.response.write("</table></body></html>");
}
}
} else {
SendFile(reqPath,req);
}
} else {
}
req.response.close();
}
void StartListener(String address,int port, List<ptHost> hosts) {
print("Binding to ${address}:${port}");
HttpServer.bind(address,port).then((server) {
print("Listening to ${address}:${port}");
server.listen((HttpRequest request) {
print("Request from : ${request.connectionInfo.remoteAddress.address}");
HttpHeaders headers = request.headers;
HttpResponse response = request.response;
bool handled = false;
for(int i=0; i<hosts.length; ++i) {
// If there is no hostname, assume universal for this address and port, otherwise check for specific host match
print("Hostname requested : ${headers.host}");
if(hosts[i].hostname.toLowerCase().compareTo(headers.host.toLowerCase())==0) {
print("${hosts[i].name} responding to request");
handled = true;
if(hosts[i].Request!=null) {
print("Using custom response");
hosts[i].Request(request);
} else {
print("Using default response");
DefaultRequest(hosts[i],request);
}
break;
}
}
if(!handled) {
request.response.statusCode = 400;
request.response.reasonPhrase = "Unknown host was request";
request.response.close();
}
/*
headers.forEach((name,List<String> value) {
for(int i=0; i<value.length; ++i)
response.write("${name} ${value[i]}\r\n");
});
*/
response.close();
});
});
}
}
Constructors
new ptWebServer({String configFile, ptConfig config}) #
Creates a new Object instance.
Object instances have no meaningful state, and are only useful
through their identity. An Object instance is equal to itself
only.
ptWebServer({String configFile,ptConfig this.config}) {
}
Methods
void DefaultRequest(ptHost host, HttpRequest req) #
void DefaultRequest(ptHost host, HttpRequest req) {
//req.response.writeln("path : ${req.uri.path}");
//req.response.writeln("toFilePath : ${req.uri.toFilePath()}");
// Pass the requested file back
if(host.public_path.length > 0) {
Directory dir = new Directory("${host.public_path}${req.uri.path}");
String reqPath = "${host.public_path}${req.uri.path}";
if(reqPath.endsWith("/") && (new Directory(reqPath)).existsSync()) {
Directory path = new Directory(reqPath);
if(host.allow_dir_view) {
bool indexFound = false;
List<FileSystemEntity> files = path.listSync(recursive: false, followLinks: false);
for(int i=0; i<files.length; ++i) {
for(int f=0; f<host.public_index_files.length; ++f) {
if(files[i].path.endsWith(host.public_index_files[f])) {
indexFound = true;
SendFile(files[i].path,req);
break;
}
}
if(indexFound) {
break;
}
}
if(!indexFound) {
print("Generating folder view for ${req.uri.path} : ${reqPath}");
// Render the directory structure
req.response.write("<html><body><h1>Index of ${req.uri.path}</h1><table><tr><td style=\"width: 25%\">Name</td><td style=\"width: 25%\">Size</td><td style=\"width: 25%\">Last Modified</td></tr>");
for(int i=0; i<files.length; ++i) {
req.response.write("<tr><td><a href=\"${files[i].path.substring(files[i].parent.path.length+1)}\">${files[i].path.substring(files[i].parent.path.length+1)}</a></td><td>${files[i].statSync().size}</td><td>${files[i].statSync().modified.toString()}</td></tr>");
}
req.response.write("</table></body></html>");
}
}
} else {
SendFile(reqPath,req);
}
} else {
}
req.response.close();
}
void Run() #
void Run() {
print("Starting ${config.servername}");
Map hosts = new Map();
if(config == null) {
print("Unable to start, please provide a config");
return;
}
for(int i=0; i<config.hosts.length; ++i) {
print("Reading host ${i+1} of ${config.hosts.length}");
String key = "${config.hosts[i].address}:${config.hosts[i].port}";
if(!hosts.containsKey(key)) {
hosts[key]=new List<ptHost>();
}
List<ptHost> list = hosts[key];
list.add(config.hosts[i]);
}
hosts.forEach((String addrport, List<ptHost> hostList) {
String addr = hostList[0].address;
int port = hostList[0].port;
StartListener(addr,port,hostList);
});
}
void SendFile(String path, HttpRequest req) {
File file = new File(path);
if(file.existsSync()) {
print("Sending ${path}");
req.response.write(file.readAsStringSync(encoding: ASCII));
} else {
print("Unable to send ${path}, doesn't exist");
}
}
void StartListener(String address, int port, List<ptHost> hosts) #
void StartListener(String address,int port, List<ptHost> hosts) {
print("Binding to ${address}:${port}");
HttpServer.bind(address,port).then((server) {
print("Listening to ${address}:${port}");
server.listen((HttpRequest request) {
print("Request from : ${request.connectionInfo.remoteAddress.address}");
HttpHeaders headers = request.headers;
HttpResponse response = request.response;
bool handled = false;
for(int i=0; i<hosts.length; ++i) {
// If there is no hostname, assume universal for this address and port, otherwise check for specific host match
print("Hostname requested : ${headers.host}");
if(hosts[i].hostname.toLowerCase().compareTo(headers.host.toLowerCase())==0) {
print("${hosts[i].name} responding to request");
handled = true;
if(hosts[i].Request!=null) {
print("Using custom response");
hosts[i].Request(request);
} else {
print("Using default response");
DefaultRequest(hosts[i],request);
}
break;
}
}
if(!handled) {
request.response.statusCode = 400;
request.response.reasonPhrase = "Unknown host was request";
request.response.close();
}
/*
headers.forEach((name,List<String> value) {
for(int i=0; i<value.length; ++i)
response.write("${name} ${value[i]}\r\n");
});
*/
response.close();
});
});
}