2121/* *
2222 * Server Constructor
2323 * Initialize state and server variables
24+ *
25+ * @param vhost Name of the primary Host the HTTP server will respond to
26+ * @param port Port the vhost listens on
27+ * @param diskpath Path to the folder the vhost serves up
2428 */
25- HTTPServer::HTTPServer () {
29+ HTTPServer::HTTPServer (std::string vhost, int port, std::string diskpath ) {
2630 canRun = false ;
2731 listenSocket = INVALID_SOCKET;
32+ listenPort = port;
33+
34+ // TODO: Eventually we should allow the config to specify multiple vhosts with their own diskpaths
35+ printf (" Primary vhost: %s, port: %i, disk path: %s\n " , vhost.c_str (), port, diskpath.c_str ());
2836
2937 // Create a resource host serving the base path ./htdocs on disk
30- ResourceHost* resHost = new ResourceHost (" ./htdocs " );
38+ ResourceHost* resHost = new ResourceHost (diskpath );
3139 hostList.push_back (resHost);
3240
33- // Setup the resource host serving htdocs to provide for the following vhosts:
34- vhosts.insert (std::pair<std::string, ResourceHost*>(" localhost:8080" , resHost));
35- vhosts.insert (std::pair<std::string, ResourceHost*>(" 127.0.0.1:8080" , resHost));
36- vhosts.insert (std::pair<std::string, ResourceHost*>(" 192.168.1.59:8080" , resHost));
41+ // Setup the resource host serving htdocs to provide for the following vhosts
42+ // Use the primary vhost to also serve up localhost/127.0.0.1 (which is why we only added one ResourceHost to hostList above)
43+ char tmpstr[32 ];
44+ sprintf (tmpstr, " localhost:%i" , listenPort);
45+ vhosts.insert (std::pair<std::string, ResourceHost*>(std::string (tmpstr).c_str (), resHost));
46+ sprintf (tmpstr, " 127.0.0.1:%i" , listenPort);
47+ vhosts.insert (std::pair<std::string, ResourceHost*>(std::string (tmpstr).c_str (), resHost));
48+ sprintf (tmpstr, " %s:%i" , vhost.c_str (), listenPort);
49+ vhosts.insert (std::pair<std::string, ResourceHost*>(std::string (tmpstr).c_str (), resHost));
3750}
3851
3952/* *
@@ -54,12 +67,10 @@ HTTPServer::~HTTPServer() {
5467 * Start Server
5568 * Initialize the Server Socket by requesting a socket handle, binding, and going into a listening state
5669 *
57- * @param port Port to listen on
5870 * @return True if initialization succeeded. False if otherwise
5971 */
60- bool HTTPServer::start (int port) {
61- canRun = false ;
62- listenPort = port;
72+ bool HTTPServer::start () {
73+ canRun = false ;
6374
6475 // Create a handle for the listening socket, TCP
6576 listenSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -252,8 +263,7 @@ void HTTPServer::acceptConnection() {
252263 * @return Pointer to Client object if found. NULL otherwise
253264 */
254265Client* HTTPServer::getClient (int clfd) {
255- std::unordered_map<int , Client*>::const_iterator it;
256- it = clientMap.find (clfd);
266+ auto it = clientMap.find (clfd);
257267
258268 // Client wasn't found
259269 if (it == clientMap.end ())
@@ -555,7 +565,7 @@ void HTTPServer::handleTrace(Client* cl, HTTPRequest *req) {
555565 sendResponse (cl, resp, true );
556566
557567 delete resp;
558- delete buf;
568+ delete[] buf;
559569}
560570
561571/* *
0 commit comments