-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.cpp
More file actions
107 lines (87 loc) · 2.55 KB
/
http.cpp
File metadata and controls
107 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "http.h"
#include <unistd.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#include <thread>
#include <fstream>
#include <streambuf>
#include <sstream>
namespace {
const std::string GET_REQUEST = "GET";
void shutdown_connection(int sock) {
shutdown(sock, SHUT_RDWR);
close(sock);
}
// TODO(stash): move to util?
bool FileExists(const std::string& filename) {
std::ifstream ifs(filename);
bool res = ifs.good();
ifs.close();
return res;
}
std::string ReadFile(const std::string& filename) {
std::ifstream ifs(filename);
return std::string(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>());
}
std::string Generate404() {
char outbuf[] =
"HTTP/1.0 404 Not Found\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: 10\r\n"
"Connection: close\r\n"
"\r\n"
"Not found!";
return std::string(outbuf);
}
std::string Generate200(const std::string& filename) {
const std::string content = ReadFile(filename);
std::ostringstream oss;
oss <<
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: " << content.size() << "\r\n"
"Connection: close\r\n"
"\r\n" << content;
return oss.str();
}
}
void RequestHandler(int sock, const std::string& dir) {
std::cout << "Accepted" << std::endl;
char* buf = new char[10000];
// TODO(stash): better read
auto res = read(sock, buf, 10000);
if (-1 == res) {
perror("Read failed");
shutdown_connection(sock);
return;
} else {
std::cout << std::string(buf, res) << std::endl;
}
std::string request(buf, res);
if (request.substr(0, GET_REQUEST.size()) != GET_REQUEST) {
return;
}
auto offset = GET_REQUEST.size() + 1;
auto spacePos = request.find(' ', offset);
auto path = request.substr(offset, spacePos - offset);
auto cgipos = path.find('?');
if (cgipos != std::string::npos) {
path = path.substr(0, cgipos);
}
// TODO(stash): security hole!
auto fullPath = dir + "/" + path;
std::string result;
if (FileExists(fullPath)) {
std::cout << "Exists!" << std::endl;
result = Generate200(fullPath);
} else {
std::cout << "Not found!" << std::endl;
result = Generate404();
}
write(sock, result.c_str(), result.size());
shutdown_connection(sock);
std::cout << "Handled by: " << std::this_thread::get_id() << std::endl;
}