-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.h
More file actions
179 lines (153 loc) · 4.99 KB
/
Copy pathhttpserver.h
File metadata and controls
179 lines (153 loc) · 4.99 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class HttpServer : protected Server
{
std::string ok = "HTTP/1.1 200 OK\r\n";
std::string content = "Content-Type: ";
std::string content_ct = "Content-Length: ";
std::string data = "\n\n";
long int content_length = 0;
char *requested_page = "index.html", *client_address, *http_version;
std::string MIME_TYPE;
public:
HttpServer()
{
}
protected:
void read_file(const char *filename)
{
if (std::string(filename) == "/")
{
filename = "/index.html";
}
std::string openfile(std::string("www") + filename); //index file location set
openfile.c_str();
std::string line;
std::ifstream html_page(openfile);
if (!html_page.is_open())
{
html_page.close();
//perror("\n\nCant open the index file\n");
this->data = data.append("<html><body>Not found</body></html>"); //todo :not found page
this->content_length = data.length() - 2;
}
else
{
while (!html_page.eof())
{
getline(html_page, line);
this->content_length += line.length();
data = data.append(line);
}
html_page.close();
}
//std::cout<<data;
}
void set_headers()
{
bool dot_is_found = false;
MIME_TYPE = std::string(requested_page);
size_t req_size = MIME_TYPE.find_last_of("/.");
MIME_TYPE = MIME_TYPE.substr(req_size + 1);
std::cout << "---MIME:" << MIME_TYPE << "------\n";
if ((MIME_TYPE == "html") || MIME_TYPE == "")
{
MIME_TYPE = "text/html";
this->content = "Content-Type: text/html; charset=UFT-8\r\n";
}
else if (MIME_TYPE == "css")
{
MIME_TYPE == "Content-Type: text/css\r\n";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "ico")
{
MIME_TYPE = "Content-Type: image/x-icon\r\n";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "svg")
{
MIME_TYPE = "Keep-Alive: timeout=5, max=98\r\nConnection: Keep-Alive\r\nContent-Type: image/svg+xml\r\n ";
this->content = MIME_TYPE;
}
else if ((MIME_TYPE == "jpg") || (MIME_TYPE == "jpeg") || (MIME_TYPE == "jiff"))
{
MIME_TYPE = "Content-Type: image/jpeg\r\n\r";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "png")
{
MIME_TYPE = "Content-Type: image/png\r\n\r";
this->content = MIME_TYPE;
this->content_length = 0;
}
else if (MIME_TYPE == "js")
{
MIME_TYPE = "Content-Type: text/javascript\r\n\r";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "ttf")
{
MIME_TYPE = "Content-Type: font/ttf\r\n\r";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "woff")
{
MIME_TYPE = "Content-Type: font/woff\r\n\r";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "woff2")
{
MIME_TYPE = "Content-Type: font/woff2\r\n\r";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == "pdf")
{
MIME_TYPE = "Content-Type: application/pdf\r\n\r";
this->content = MIME_TYPE;
}
else if (MIME_TYPE == ".tif" || MIME_TYPE == ".tiff")
{
MIME_TYPE = "Content-Type: image/tiff\r\n\r";
this->content = MIME_TYPE;
}
else
{
MIME_TYPE = "text/plain\r\n";
this->content = MIME_TYPE;
}
}
public:
void send_HTTP()
{
this->decode_client_header();
this->set_headers();
//std::cout<<"ok";
this->read_file(requested_page);
std::string msg_string = this->ok + this->content + this->content_ct + std::to_string(this->content_length) + "" + this->data; //concatenate all http headers
std::cout << msg_string << std::endl;
this->send_str(msg_string);
close(newsockfd);
close(sockfd);
//decode_client_header();
}
void decode_client_header()
{
int ct = 0;
char *saveptr;
char *parse_dat = this->buffer;
std::vector<char *> stack_headers;
char *http_headers = strtok_r(parse_dat, " ", &saveptr);
while (http_headers != NULL)
{
//printf("%s\n",http_headers);
http_headers = strtok_r(NULL, " ", &saveptr);
stack_headers.push_back(http_headers);
if (stack_headers.size() >= 3)
break;
}
this->requested_page = stack_headers[0]; //reemove first "/" from get request
this->client_address = stack_headers[2];
this->http_version = stack_headers[1];
printf("%s\n %s\n %s\n ", requested_page, client_address, http_version);
stack_headers.clear();
}
};