Skip to content

Commit 9a24975

Browse files
committed
Added configuration file to specify primary vhost/post/diskpath
1 parent e1bb33a commit 9a24975

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

bin/server.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Default vhost
2+
vhost=192.168.1.5
3+
port=8080
4+
diskpath=./htdocs

src/HTTPMessage.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ void HTTPMessage::addHeader(std::string key, int value) {
295295
*/
296296
std::string HTTPMessage::getHeaderValue(std::string key) {
297297
// Lookup in map
298-
std::map<std::string, std::string>::const_iterator it;
299-
it = headers->find(key);
298+
auto it = headers->find(key);
300299

301300
// Key wasn't found, return a blank value
302301
if (it == headers->end())

src/HTTPServer.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,32 @@
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
*/
254265
Client* 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
/**

src/HTTPServer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ class HTTPServer {
8686
bool canRun;
8787

8888
public:
89-
HTTPServer();
89+
HTTPServer(std::string vhost, int port, std::string diskpath);
9090
~HTTPServer();
9191

92-
bool start(int port);
92+
bool start();
9393
void stop();
9494

9595
// Main event loop

src/Resource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Resource::Resource(std::string loc, bool dir) {
2828

2929
Resource::~Resource() {
3030
if(data != NULL) {
31-
delete data;
31+
delete [] data;
3232
data = NULL;
3333
}
3434
}

src/ResourceHost.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434

3535
// Valid files to serve as an index of a directory
3636
const static char* const validIndexes[] = {
37-
"index.html", // 0
38-
"index.htm", // 1
39-
//"index.php", // 2
37+
"index.html",
38+
"index.htm"
4039
};
4140

4241
class ResourceHost {

src/main.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
#include <iostream>
2020
#include <string>
21+
#include <unordered_map>
22+
#include <fstream>
2123
#include <signal.h>
2224

2325
#include "HTTPServer.h"
26+
#include "ResourceHost.h"
2427

2528
static HTTPServer* svr;
2629

@@ -36,6 +39,37 @@ void handleTermSig(int snum) {
3639

3740
int main (int argc, const char * argv[])
3841
{
42+
// Parse config file
43+
std::map<std::string, std::string> config;
44+
std::fstream cfile;
45+
std::string line, key, val;
46+
int epos;
47+
cfile.open("server.config");
48+
if (!cfile.is_open()) {
49+
printf("Unable to open server.config file in working directory\n");
50+
return -1;
51+
}
52+
while (getline(cfile, line)) {
53+
// Skip lines beginning with a #
54+
if (line.rfind("#", 0) == 0)
55+
continue;
56+
57+
epos = line.find("=");
58+
key = line.substr(0, epos);
59+
val = line.substr(epos+1, line.length());
60+
config.insert(std::pair<std::string, std::string> (key, val));
61+
}
62+
cfile.close();
63+
64+
// Validate at least vhost, port, and diskpath are present
65+
auto it_vhost = config.find("vhost");
66+
auto it_port = config.find("port");
67+
auto it_path = config.find("diskpath");
68+
if (it_vhost == config.end() || it_port == config.end() || it_path == config.end()) {
69+
printf("vhost, port, and diskpath must be supplied in the config, at a minimum\n");
70+
return -1;
71+
}
72+
3973
// Ignore SIGPIPE "Broken pipe" signals when socket connections are broken.
4074
signal(SIGPIPE, handleSigPipe);
4175

@@ -45,8 +79,8 @@ int main (int argc, const char * argv[])
4579
signal(SIGTERM, &handleTermSig);
4680

4781
// Instance and start the server
48-
svr = new HTTPServer();
49-
svr->start(8080);
82+
svr = new HTTPServer(config["vhost"], atoi(config["port"].c_str()), config["diskpath"]);
83+
svr->start();
5084

5185
// Run main event loop
5286
svr->process();

0 commit comments

Comments
 (0)