-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
307 lines (249 loc) · 7.13 KB
/
Source.cpp
File metadata and controls
307 lines (249 loc) · 7.13 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// This is the original version using C (not C++) to create a server.
// With socket, thread, it can perform static webpages well. But without cgi, it's still not available to answer information input, so dynamic webpage(x).
// The original model is tinyhttp (https://tinyhttpd.sourceforge.net/): a lite httpserver written by James David in 1999.
// To study and acquire a FULL understanding, I rewrite it using C++ as the next version.
//
// 23.12.26
// Daivd John
//
// Include headers
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<WinSock2.h>
#pragma comment(lib, "WS2_32.lib")
// Standard Output Define
#define PRINTF(str) printf("[%s - %d]"#str": %s\n",__func__, __LINE__, str);
// Error Function
void error_die(const char* str) {
perror(str);
exit(1);
}
// Startup Function
int startup(int* port) {
//startup
WSADATA data;
INT32 ret = WSAStartup(MAKEWORD(1, 1), &data);
if (ret != 0) { error_die("WSAStartup"); }
// create socket
INT32 server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_socket == -1) { error_die("socket"); }
// set socket option
INT32 opt = 1;
ret = setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
if (ret == -1) { error_die("setsocketopt"); }
// config server network
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(*port);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// bind socket
if (bind(server_socket, (const sockaddr*)&server_addr, sizeof(server_addr)) < 0) { error_die("bind"); }
// dynamic port
INT32 nameLen = sizeof(server_addr);
if (*port == 0) {
if (getsockname(server_socket, (struct sockaddr*)&server_addr, &nameLen) < 0) { error_die("getsockname"); }
*port = server_addr.sin_port;
}
// listen port
if (listen(server_socket, 5) < 0) { error_die("listen"); }
return server_socket;
}
// Read File Line by Line
int read_line(int sock, char* buff, int size) {
char c = 0;
int i = 0;
while (i < size - 1 && c != '\n') {
int n = recv(sock, &c, 1, 0);
if (n > 0) {
if (c == '\r') {
n = recv(sock, &c, 1, MSG_PEEK);
if (n > 0 && c == '\n') { recv(sock, &c, 1, 0); }
else { c = '\n'; }
}
buff[i++] = c;
}
else {
c = '\n';
}
}
buff[i] = 0;
return i;
}
// unsupported method == not "GET"
void unsupported_method(int client) {
char buf[1024];
strcpy_s(buf, "HTTP/1.0 501 Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "Server: tinyhttp / 0.1\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "</TITLE></HEAD>\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "<BODY><P>HTTP request method not supported.\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
}
void not_found(int client) {
char buff[1024];
strcpy_s(buff, "HTTP/1.1 404 NOT FOUND\r\n");
send(client, buff, strlen(buff), 0);
strcpy_s(buff, "Server: tinyhttp/0.1\r\n");
send(client, buff, strlen(buff), 0);
strcpy_s(buff, "Content-Type:text/html\r\n");
send(client, buff, strlen(buff), 0);
strcpy_s(buff, "\r\n");
send(client, buff, strlen(buff), 0);
strcpy_s(buff,
"<HTML> \
<TITLE>NOT FOUND </TITLE> \
<BODY><H2> The resource is unavailable </H2></BODY> \
</HTML>"
);
send(client, buff, strlen(buff), 0);
}
const char* gethead_type(const char* fileName) {
const char* ret = "text/html";
const char* p = strrchr(fileName, '.');
if (!p) return ret;
p++;
if (!strcmp(p, "css")) ret = "text/css";
else if (!strcmp(p, "jpg")) ret = "image/jpg";
else if (!strcmp(p, "png")) ret = "image/png";
else if (!strcmp(p, "js")) ret = "application/x-javascript";
return ret;
}
void headers(int client, const char* ret) {
// send headers
char buff[1024];
strcpy_s(buff, "HTTP/1.1 200 OK\r\n");
send(client, buff, strlen(buff), 0);
strcpy_s(buff, "Server: tinyhttp/0.1\r\n");
send(client, buff, strlen(buff), 0);
char buf[1024];
sprintf_s(buf, "Content-Type:%s\r\n", ret);
//strcpy_s(buff, "Content-Type:application/octet-stream\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buff, "Content-Disposition: inline\r\n");
send(client, buff, strlen(buff), 0);
strcpy_s(buff, "\r\n");
send(client, buff, strlen(buff), 0);
}
void cat(int client, FILE* resource) {
// send file
char buff[4096];
int count = 0;
while (1) {
int ret = fread(buff, sizeof(char), sizeof(buff), resource);
if (ret <= 0) {
break;
}
count += ret;
int sent = send(client, buff, ret, 0);
if (sent == -1) {
error_die("send");
}
else {
printf("sent %d bytes \n", sent);
}
}
printf("一共发送%d字节\n", count);
}
void server_file(int client, const char* fileName) {
// read headers and clean up
char buff[1024];
int number_char = 1;
while (number_char > 0 && strcmp(buff, "\n")) {
read_line(client, buff, sizeof(buff));
}
// read and send file
FILE* resource;
if (fopen_s(&resource, fileName, "rb") != 0) {
not_found(client);
}
else {
headers(client, gethead_type(fileName));
cat(client, resource);
printf("资源发送完毕!\n");
}
fclose(resource);
}
//thread
DWORD WINAPI accept_request(LPVOID arg) {
char buff[1024]; // all request data
int client = (SOCKET)arg;
int number_char = read_line(client, buff, sizeof(buff));
PRINTF(buff);
// resolve data method
char method[255];
int i = 0, j = 0;
while (!isspace(buff[j])) {
method[i++] = buff[j++];
}
method[i] = 0;
PRINTF(method);
if (_stricmp(method, "GET") && _stricmp(method, "POST")) {
// return wrong
unsupported_method(client);
return 0;
}
// resolve data url
char url[255];
i = 0;
while (isspace(buff[j]) && j < sizeof(buff)) { //jump ' '
j++;
}
while (!isspace(buff[j]) && i < sizeof(url) - 1 && j < sizeof(buff)) {
url[i++] = buff[j++];
}
url[i] = 0;
PRINTF(url);
// connect path and url
char path[512] = "";
sprintf_s(path, "htm%s", url);
if (path[strlen(path) - 1] == '/') {
strcat_s(path, "index.html");
}
struct stat status;
// if not found, clear up the memory and print out not found
if(stat(path,&status) == -1){
while (number_char>0 && strcmp(buff,"\n")) {
read_line(client, buff, sizeof(buff));
}
not_found(client);
}
// if found, connect index.html
else {
if ((status.st_mode & S_IFMT) == S_IFDIR) {
strcat_s(path, "/index.html");
}
server_file(client, path);
}
closesocket(client);
return 0;
}
int main1() {
// starting http server
int port = 80;
int server_sock = startup(&port);
printf("http服务已经启动,正在监听%d端口", port);
// connect client
struct sockaddr_in client_addr;
int client_addr_len = sizeof(client_addr);
while (1) {
int client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &client_addr_len);
if (client_sock == -1) { error_die("accpet"); }
DWORD threadId = 0;
CreateThread(0, 0, accept_request, (void*)client_sock, 0, &threadId);
}
closesocket(server_sock);
return 0;
}