Skip to content

Commit ccdd880

Browse files
author
lh5053
committed
Loading static files is almost done, for some reason there is a segmentation error thrown when attempting to.
Needs more debugging.
1 parent e853290 commit ccdd880

File tree

3 files changed

+127
-18
lines changed

3 files changed

+127
-18
lines changed

index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example index file

src/Worker.cpp

Lines changed: 109 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
#include <netdb.h>
44
#include <unistd.h>
55
#include <stdlib.h>
6+
#include <stdio.h>
67
#include <cstring>
78
#include <string.h>
9+
#include <fcntl.h>
10+
#include <linux/limits.h>
811

912
#include "Worker.h"
1013
#include "BasicHTTP.h"
1114

15+
#define HTTP_REQUEST_LENGTH 8192
16+
1217
/*
1318
* worker - uses an appropriate handler to serve the request
1419
*/
@@ -23,30 +28,116 @@ Worker::~Worker() {
2328
close(this->socket_fd);
2429
}
2530

31+
//void read_static_file(std::string path, char * data);
32+
std::string get_working_path();
33+
2634
void Worker::handle_request() {
27-
char reply[100], *msg;
35+
char *request = (char *)calloc(HTTP_REQUEST_LENGTH, sizeof(char)); // standard limit of 8kb
2836

2937
this->logger.info("Handling request via socket: " + std::to_string(this->socket_fd));
30-
// Send welcome message
31-
std::string string = "Welcome to HACKttp: " + std::to_string(this->socket_fd) + "\nYour message: ";
32-
msg = (char *) string.c_str();
33-
send(this->socket_fd, msg, strlen(msg), 0);
34-
35-
std::string response;
36-
while(true){
37-
memset(&reply, 0, sizeof reply);
38-
if(recv(this->socket_fd, reply, 100, 0) <= 0)
38+
39+
// first read the request
40+
if(recv(this->socket_fd, request, HTTP_REQUEST_LENGTH, 0) <= 0) {
41+
// TODO replace with proper HTTP response
42+
char * err = std::strerror(errno);
43+
throw WorkerException("Error while reading request: " + std::string(err ? err : "unknown error"));
44+
}
45+
46+
// get the static file contents
47+
std::string req_str = std::string(request);
48+
int space_count = 0;
49+
size_t pos = 0, prev_pos = 0;
50+
while (space_count < 2) {
51+
prev_pos = pos;
52+
if ((pos = req_str.find(" ", prev_pos+1)) != std::string::npos) {
53+
space_count++;
54+
}
55+
else
3956
break;
40-
response.append("HackTTP echo: ");
41-
response.append(reply);
42-
response.append("\nYour message: ");
43-
msg = (char *)response.c_str();
44-
send(this->socket_fd, msg, strlen(msg), 0);
45-
//reply[99] = '\0';
46-
printf("Sent back: %s", response.c_str());
47-
response.clear();
4857
}
58+
59+
char * data;
60+
if (space_count == 2) {
61+
std::string file_path = req_str.substr(prev_pos, pos-prev_pos);
62+
// data = "Contents of file at: " + file_path + "\nUsing request: " + req_str;
63+
if (file_path.compare("/"))
64+
file_path = "/index.txt";
65+
66+
file_path = get_working_path() + file_path;
67+
int fd = open(file_path.c_str(), O_RDONLY);
68+
if (fd < 0) {
69+
// TODO: replace with a proper HTTP CODE
70+
char * err = std::strerror(errno);
71+
throw WorkerException("Error while reading file contents at " + file_path + ": " + std::string(err ? err : "unknown error"));
72+
}
73+
74+
this->logger.debug("Opened file: " + file_path);
75+
76+
long fsize = lseek(fd, 0, SEEK_END);
77+
lseek(fd, 0, SEEK_SET);
78+
79+
this->logger.debug("File size: " + std::to_string(fsize));
80+
data = (char *) malloc(fsize+1);
81+
this->logger.debug("Memory assigned");
82+
read(fd, &data, fsize);
83+
this->logger.debug("Data read");
84+
close(fd);
85+
// add null termination
86+
this->logger.debug("... and null-terminated");
87+
data[fsize] = '\0';
88+
89+
// this->logger.debug("File data: " + std::string(data));
90+
// read_static_file(file_path, data);
91+
}
92+
else {
93+
data = (char*) std::string("Error, could not parse request").c_str();
94+
}
95+
96+
// data.append("Welcome to HACKttp: " + std::to_string(this->socket_fd) + "<br/>Your message: ");
97+
// data.append(file_path);
98+
// send_msg(data);
99+
100+
// now answer
101+
// printf("Sending data: %s\n", data);
102+
if (send(this->socket_fd, data, strlen(data), 0) < 0) {
103+
char * err = std::strerror(errno);
104+
throw WorkerException("Error while reading request: " + std::string(err ? err : "unknown error"));
105+
}
106+
free(data);
107+
49108
this->logger.debug("Reqest handling done");
50109

51110
return;
52111
}
112+
113+
void Worker::send_msg(std::string msg) {
114+
char *msgc = (char *) msg.c_str();
115+
send(this->socket_fd, msgc, strlen(msgc), 0);
116+
return;
117+
}
118+
119+
//void read_static_file(std::string path, char * data) {
120+
// // prepend cwd() to path
121+
// path = get_working_path() + path;
122+
// int fd = open(path.c_str(), O_RDONLY);
123+
// if (fd < 0) {
124+
// // TODO: replace with a proper HTTP CODE
125+
// char * err = std::strerror(errno);
126+
// throw WorkerException("Error while reading file contents at " + path + ": " + std::string(err ? err : "unknown error"));
127+
// }
128+
//
129+
// lseek(fd, 0, SEEK_END);
130+
// long fsize = lseek(fd, 0, SEEK_CUR);
131+
// lseek(fd, 0, SEEK_SET);
132+
//
133+
// data = (char *) malloc(fsize + 1);
134+
// read(fd, &data, fsize);
135+
// close(fd);
136+
//
137+
// return;
138+
//}
139+
140+
std::string get_working_path() {
141+
char temp[PATH_MAX];
142+
return ( getcwd(temp, PATH_MAX) ? std::string( temp ) : std::string("") );
143+
}

src/Worker.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
#ifndef SRC_WORKER_H_
22
#define SRC_WORKER_H_
33

4+
#include <cstring>
45
#include "Logger.h"
56

67
class Worker {
78
private:
89
int socket_fd;
910
Logger logger;
11+
void send_msg(std::string msg);
1012

1113
public:
1214
Worker(int socket_fd);
1315
virtual ~Worker();
1416
void handle_request();
1517
};
1618

19+
using namespace std;
20+
// Possibly we could also add a stack trace here:
21+
// http://stackoverflow.com/questions/353180/how-do-i-find-the-name-of-the-calling-function
22+
class WorkerException: public exception {
23+
std::string reason;
24+
25+
public:
26+
WorkerException(std::string msg = "Unknown exception") {
27+
reason = msg;
28+
}
29+
virtual const char* what() const throw() {
30+
return reason.c_str();
31+
}
32+
};
33+
1734
#endif /* SRC_WORKER_H_ */

0 commit comments

Comments
 (0)