Dart DocumentationptwebserverptWebServer

ptWebServer class

class ptWebServer {
 ptConfig config;
 
 
 /*
  * Pass in your configuration file for the server instance
  */
 ptWebServer(ptConfig this.config) {
   
 }
 
 /*
  * This will start the server and handle all of the responses
  */
 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);
   });
 }
 
 
 /*
  * This will read a file and send the exact document contents to the requester
  */
 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");
   }
 }
 
 /*
  * Decide how to handle the file
  */
 void HandleFile(ptHost host,HttpRequest req, String path) {
   bool handled = false;
   print(host.extensionHandlers.length);
   for(int i=0; i<host.extensionHandlers.length; i++) {
     if(host.extensionHandlers[i]==null) {
       print("extensionHandler[${i}] is null");
       continue;
     }
     
     if(host.extensionHandlers[i].test(path)) {
       host.extensionHandlers[i].handle(host, req, path);
       handled=true;
       break;
     }
   }
   if(!handled) {
     if(host.Default!=null)
       host.Default.handle(host, req, path);
     else {
       // We don't have our default handler. Pass a server error to the client
       req.response.statusCode=500;
       req.response.close();
     }
   }
 }
 
 
 /*
  * Used if a custom request response is not created in the configuration
  */
 void DefaultRequest(ptHost host, HttpRequest req) {
   // 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;
               HandleFile(host,req,files[i].path);
               break;
             }
           }
           if(indexFound) {
             break;
           }
         }
         
         /*
          * We haven't found an index, and there was no file request. 
          * Create a directory structure and send it.
          */
         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>");
           req.response.close();
         }
       }
     } else {
       HandleFile(host,req,reqPath);
     }
   } else {
     
   }
   
   
 }
 
 
 /*
  * Listens for a specific address and port for 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].IsHostname(headers.host)) {
           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 we haven't gotten a host to match, respond with 400 and close the response socket
        */
       if(!handled) {
         request.response.statusCode = 400;
         request.response.close();
       }
     });
   });
 }
}

Constructors

new ptWebServer(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.

docs inherited from Object
ptWebServer(ptConfig this.config) {
 
}

Properties

ptConfig config #

ptConfig config

Methods

void DefaultRequest(ptHost host, HttpRequest req) #

void DefaultRequest(ptHost host, HttpRequest req) {
 // 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;
             HandleFile(host,req,files[i].path);
             break;
           }
         }
         if(indexFound) {
           break;
         }
       }
       
       /*
        * We haven't found an index, and there was no file request. 
        * Create a directory structure and send it.
        */
       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>");
         req.response.close();
       }
     }
   } else {
     HandleFile(host,req,reqPath);
   }
 } else {
   
 }
 
 
}

void HandleFile(ptHost host, HttpRequest req, String path) #

void HandleFile(ptHost host,HttpRequest req, String path) {
 bool handled = false;
 print(host.extensionHandlers.length);
 for(int i=0; i<host.extensionHandlers.length; i++) {
   if(host.extensionHandlers[i]==null) {
     print("extensionHandler[${i}] is null");
     continue;
   }
   
   if(host.extensionHandlers[i].test(path)) {
     host.extensionHandlers[i].handle(host, req, path);
     handled=true;
     break;
   }
 }
 if(!handled) {
   if(host.Default!=null)
     host.Default.handle(host, req, path);
   else {
     // We don't have our default handler. Pass a server error to the client
     req.response.statusCode=500;
     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) #

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].IsHostname(headers.host)) {
         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 we haven't gotten a host to match, respond with 400 and close the response socket
      */
     if(!handled) {
       request.response.statusCode = 400;
       request.response.close();
     }
   });
 });
}