Skip to content

Commit 0c9efd1

Browse files
committed
Added client IP to Worker/BasicHTTP and DataHandler log lines.
Reduced log level to INFO. Removed no longer needed debug. Created Makefile
1 parent 22e7788 commit 0c9efd1

File tree

14 files changed

+116
-43
lines changed

14 files changed

+116
-43
lines changed

Makefile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Tool invocations
2+
CC=g++
3+
SRC=src
4+
CFLAGS=-I$(SRC) -std=c++11 -O3 -Wall -fmessage-length=0
5+
6+
ODIR=Release
7+
LIBS=-lpthread
8+
9+
_OBJS = BasicHTTP CmdLine Config DataHandler DataHandler/Exec DataHandler/Static Logger LoggingSingleton Manager Router SignalHandler Worker
10+
OBJ = $(patsubst %,$(ODIR)/%.o,$(_OBJS))
11+
12+
hackttp: $(OBJ) $(ODIR)/server.o
13+
@echo 'Building HackTTP: $@'
14+
@$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
15+
16+
17+
$(ODIR)/DataHandler/%.o: $(SRC)/DataHandler/%.cpp $(SRC)/DataHandler.h $(ODIR)/.empty
18+
@echo 'Building object: $@'
19+
@$(CC) -c -o $@ $< $(CFLAGS)
20+
21+
$(ODIR)/%.o: $(SRC)/%.cpp $(SRC)/%.h $(ODIR)/.empty
22+
@echo 'Building object: $@'
23+
@$(CC) -c -o $@ $< $(CFLAGS)
24+
25+
$(ODIR)/server.o: $(SRC)/server.cpp
26+
@echo 'Building object: $@'
27+
@$(CC) -c -o $@ $< $(CFLAGS)
28+
29+
$(ODIR)/.empty:
30+
@echo 'Creating $(ODIR) directory'
31+
@mkdir -p $(ODIR)/DataHandler
32+
@touch $(ODIR)/.empty
33+
34+
all: hackttp
35+
36+
clean:
37+
@echo 'Removing: hackttp $(ODIR)'
38+
@rm -rf hackttp $(ODIR)
39+
40+
.PHONY: all clean

config

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
port=8000
22
log_path=./log
33
queue_size=10
4-
config_test=Config Loaded, hello from server.cpp!
54
start_full_server=true
6-
current_log_level=DEBUG
5+
current_log_level=INFO
76
worker_count=10
87
;
98

src/BasicHTTP.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* BasicHTTP - implements a simple way to create serializable HTTP reponses that can be returned by worker
55
*/
66

7-
BasicHTTP::BasicHTTP() {
7+
BasicHTTP::BasicHTTP(string client) {
88
this->logger = new Logger("BasicHTTP");
9+
this->logger->set_postfix(client);
910
}
1011

1112
BasicHTTP::~BasicHTTP() {
@@ -51,7 +52,7 @@ BasicHTTP::request BasicHTTP::parse_request(std::string req_str) {
5152
http_ver = req_str.substr(prev_pos, pos-prev_pos);
5253
prev_pos = pos + 2;
5354
}
54-
this->logger->info("Method: " + req.method + ", HTTP version string: " + http_ver + ", URI: " + req.uri);
55+
this->logger->debug("Method: " + req.method + ", HTTP version string: " + http_ver + ", URI: " + req.uri);
5556

5657
// now end with checking if it's actually valid
5758
req.valid = is_valid(req, http_ver);

src/BasicHTTP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class BasicHTTP {
3434
void handle_post_method(BasicHTTP::request * req, std::string req_str);
3535

3636
public:
37-
BasicHTTP();
37+
BasicHTTP(string client);
3838
virtual ~BasicHTTP();
3939
request parse_request(std::string req_str);
4040
response render_headers(int code, DataHandler::resource rsrc);

src/DataHandler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
* this memory should be shared between all workers/static handlers
1212
*/
1313

14-
DataHandler::DataHandler() {
14+
DataHandler::DataHandler(string client) {
1515
this->logger = new Logger("DataHandler");
16+
this->logger->set_postfix(client);
1617
}
1718

1819
DataHandler::~DataHandler() {

src/DataHandler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class DataHandler {
1717
std::string get_working_path();
1818
bool verify_path(std::string path);
1919
public:
20-
DataHandler();
20+
DataHandler(string client);
2121
virtual ~DataHandler();
2222
resource read_resource(std::string path);
2323
resource read_resource(std::string path, std::string cookies);

src/Logger.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// global
1717
std::time_t epochTimestamp;
1818

19-
Logger::Logger(std::string path, std::string name) {
19+
Logger::Logger(string path, string name) {
2020
isLoggingToFileEnabled = true;
2121
std::map<string, int > map;
2222
map.insert(pair<string, int>("QUIET", 0));
@@ -44,13 +44,20 @@ Logger::~Logger() {
4444
close(logFileDescriptor);
4545
}
4646

47-
void Logger::_log(std::string msg, int level) {
47+
void Logger::set_postfix(string postfix) {
48+
addit_postfix = postfix;
49+
}
50+
51+
void Logger::_log(string msg, int level) {
4852
if(this->current_log_level >= level) {
4953
epochTimestamp = std::time(nullptr);
5054
fullDateTimestamp = std::asctime(std::localtime(&epochTimestamp));
5155
fullMessage = "[" + fullDateTimestamp.substr(0, fullDateTimestamp.size() - 1) + "] " +
52-
"[:" + this->class_name + "] " +
53-
msg + "\n";
56+
"[:" + this->class_name + "] ";
57+
if (addit_postfix.length() > 0) {
58+
fullMessage += "[" + addit_postfix + "] ";
59+
}
60+
fullMessage += msg + "\n";
5461
if(!isLoggingToFileEnabled){
5562
fullMessage = "WARNING: Logging to file disabled " + fullMessage;
5663
}

src/Logger.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@ class Logger {
1212
int current_log_level = DEBUG;
1313

1414
private:
15-
std::string class_name;
16-
std::string fullMessage;
17-
std::string fullDateTimestamp;
18-
std::string log_path;
15+
string class_name;
16+
string addit_postfix;
17+
string fullMessage;
18+
string fullDateTimestamp;
19+
string log_path;
1920
int logFileDescriptor;
2021
bool isLoggingToFileEnabled;
2122

2223
public:
2324
Logger() {}
24-
Logger(std::string path, std::string name);
25+
Logger(string path, string name);
2526
// delegation is fun
26-
Logger(std::string name) : Logger(Config::get_str_setting("log_path"), name) {};
27+
Logger(string name) : Logger(Config::get_str_setting("log_path"), name) {};
2728
virtual ~Logger();
29+
void set_postfix(string postfix);
2830

2931
void _log(std::string msg, int level);
3032
void warn(std::string msg) { this->_log(msg, WARNINGS); }

src/Manager.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,22 @@ Manager::~Manager() {
2626

2727
void *worker_runner(void *socket_fd);
2828

29-
void Manager::handle_request(int socket_fd) {
29+
union dCont {
30+
int ival;
31+
const char * strval;
32+
};
33+
34+
void Manager::handle_request(string client, int socket_fd) {
3035
this->logger->debug("Creating new thread for request at socket: " + std::to_string(socket_fd));
3136

3237
// get worker, if available
3338
int worker_ind = this->get_free_worker_index();
3439

3540
// handle the requst in a new thread
36-
int * params = (int*) malloc(2*sizeof(int));
37-
params[0] = worker_ind;
38-
params[1] = socket_fd;
41+
dCont * params = (dCont *) malloc(2*sizeof(int) + 16*sizeof(char));
42+
params[0].ival = worker_ind;
43+
params[1].ival = socket_fd;
44+
params[2].strval = client.c_str();
3945
if (pthread_create(&this->pool[worker_ind], NULL, worker_runner, (void *)params) < 0) {
4046
char * err = std::strerror(errno);
4147
throw Manager::Exception("Error creating thread: " + std::string(err ? err : "unknown error"));
@@ -62,13 +68,15 @@ int Manager::get_free_worker_index() {
6268
}
6369

6470
void *worker_runner(void *arg) {
65-
int *params = (int *) arg;
66-
int worker_ind = params[0];
67-
int socket_fd = params[1];
71+
dCont *params = (dCont *) arg;
72+
int worker_ind = params[0].ival;
73+
int socket_fd = params[1].ival;
74+
string client = string(params[2].strval);
75+
free(params);
6876

6977
// start handling the request
7078
try {
71-
Worker worker(socket_fd);
79+
Worker worker(client, socket_fd);
7280
worker.handle_request();
7381
}
7482
catch (Worker::Exception &e) {

src/Manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Manager {
1313
public:
1414
Manager();
1515
virtual ~Manager();
16-
void handle_request(int socket_fd);
16+
void handle_request(string client, int socket_fd);
1717

1818
class Exception: public BaseException {
1919
public:

0 commit comments

Comments
 (0)