-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP.cpp
More file actions
147 lines (130 loc) · 4.25 KB
/
HTTP.cpp
File metadata and controls
147 lines (130 loc) · 4.25 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "HTTP.h"
#define _t(a) std::cout<<a<<std::endl;
HTTPRequest::HTTPRequest(char* message, char* workingdir)
{
//_t(0);
char* mov = req;
while(*message){
*mov = (char)toupper((int)*message);
mov++;
message++;
}
*mov = 0;
//_t(1);
char delim[] = "\r\n";
char delim2[] = " :";
char* p = strtok(req, delim);
while(p){
lines.emplace_back();
lines.at(lines.size() - 1).push_back(p);
p = strtok(0, delim);
}
//_t(2);
for(auto& ln : lines){
char* str = ln[0];
ln.pop_back();
char* q = strtok(str, delim2);
while(q){
//_t(20);
ln.push_back(q);
//_t(21);
q = strtok(0, delim2);
}
}
//_t(3);
res = "";
if(lines.size() && lines[0].size() > 1){
for(unsigned int i = 1; i < lines.size() - 1; i++){
if(!strcmp(lines[i][0], "CONNECTION")){
if(!strcasecmp(lines[i][1], "KEEP-ALIVE")){
keep_alive = true;
}
break;
}
}
res += "HTTP/1.0 ";
if(strcmp(lines.at(0).at(0), "GET") == 0){
char filepath[100];
strcpy(filepath, workingdir);
int len = strlen(workingdir);
if(filepath[len-1] != '/'){
filepath[len++] = '/';
}
strcpy(&filepath[len], lines.at(0).at(1) + 1);
if(!*(lines.at(0).at(1) + 1)){
res += "301 Moved Permanantly\r\nLocation:/index.html\r\n";
if(keep_alive){
res += "Connection: keep-alive\r\n";
res += "Keep-Alive: timeout=10, max=20\r\n";
res += "Content-length: ";
res += "0";
res += "\r\n";
}
res += "\r\n";
}else{
std::fstream file(filepath, std::ios::in | std::ios::binary);
std::string content = "";
if(file.is_open()){
while(!file.eof()){
std::string temp = "";
std::getline(file, temp);
content += temp;
if(!file.eof()){
content += '\n';
}
}
//content += "\r\n";
res += "200 OK\r\n";
if(keep_alive){
res+= "Connection: keep-alive\r\n";
res+= "Keep-Alive: timeout=10, max=20\r\n";
res += "Content-length: ";
res += std::to_string(content.size());
res += "\r\n";
}
res += "\r\n";
res += content;
}else{
res += "404 Not Found\r\n";
content += "<!DOCTYPE html>\r\n";
content += "<html>\r\n";
content += "<body>\r\n";
content += "\r\n";
content += "<h1>ERROR: 404</h1>\r\n";
content += "\r\n";
content += "<p>FILE NOT FOUND</p>\r\n";
content += "\r\n";
content += "</body>\r\n";
content += "</html>\r\n";
if(keep_alive){
res += "Connection: keep-alive\r\n";
res += "Keep-Alive: timeout=10, max=20\r\n";
res += "Content-length: ";
res += std::to_string(content.size());
res += "\r\n";
}
res += "\r\n";
res += content;
}
}
}else{
res += "400 Bad Request\r\n";
if(keep_alive){
res+= "Connection: keep-alive\r\n";
res+= "Keep-Alive: timeout=10, max=20\r\n";
res += "Content-length: ";
res += "0";
res += "\r\n";
}
res += "\r\n";
}
}
}
const std::string& HTTPRequest::get_response()
{
return res;
}
HTTPRequest::~HTTPRequest()
{
//dtor
}