-
Notifications
You must be signed in to change notification settings - Fork 0
Files
/
TinyCHttpServer.c
550 lines (467 loc) · 21.4 KB
/
TinyCHttpServer.c
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include <windows.h>
#include <winsock.h>
#include <winsock2.h>
#include <winuser.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib") // Winsock Library
int main(int argc, char* argv[]) {
int port;
port = atoi(argv[1]);
if (port < 1 || port > 65535) {
port = 55555;
}
// Initialize WSA variables
WSADATA wsaData;
int wsaerr;
WORD wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
// Check for initialization success
if (wsaerr != 0) {
printf("The Winsock dll not found!\n");
return 0;
} else {
printf("The Winsock dll found\n");
}
SOCKET serverSocket;
serverSocket = INVALID_SOCKET; // Initializing as a inivalid socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
printf("Error at socket\n");
WSACleanup();
return 0;
} else {
printf("Socket is ok\n");
}
// Bind the socket to an IP address and port number
SOCKADDR_IN service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("0.0.0.0"); // Desired IP address
service.sin_port = htons(port); // Port number
int iResult = 0;
iResult = bind(serverSocket, (SOCKADDR*)&service, sizeof(service));
if (iResult == SOCKET_ERROR) {
printf("Bind failed with error %u\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return 1;
} else {
printf("Bind returned success\n");
}
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("Listen function failed with error: %d\n", WSAGetLastError());
}
printf("Listening on http://127.0.0.1:%d\n", port);
// Create a SOCKET for accepting incoming requests.
SOCKET AcceptSocket;
unsigned long long counter = 0;
while (1) {
printf("Waiting for client to connect ...\n");
AcceptSocket = accept(serverSocket, NULL, NULL);
time_t t = time(NULL);
struct tm tm = *localtime(&t);
if (AcceptSocket == INVALID_SOCKET) {
printf("%d-%02d-%02d %02d:%02d:%02d - Accept failed with error: %ld\n\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, WSAGetLastError());
continue;
} else {
printf("%d-%02d-%02d %02d:%02d:%02d - Client connected!\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}
printf("Conn. counter=%llu\n", counter++);
char req[65536];
char request[65536];
DWORD timeout = 1500;
setsockopt(AcceptSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
// timeout = 1000;
setsockopt(AcceptSocket, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout));
// setsockopt(AcceptSocket, SOL_SOCKET, SO_KEEPALIVE,(char*)&timeout, sizeof(timeout));
int bytes_recvd = recv(AcceptSocket, req, 65535, 0);
if (bytes_recvd > 65534 || bytes_recvd < 1) {
printf("Reading error! Socket Closed.\n\n");
closesocket(AcceptSocket);
continue;
}
char uri[65536] = "./serverroot"; // Root web directory
printf("Recieved req=%d bytes.\n", bytes_recvd);
int p = 0;
while (req[p] != '/' && p < bytes_recvd) {
p++;
}
int p2 = 12;
while (req[p] != ' ' && req[p] != '?' && req[p] != '*' && req[p] != ':' && p < bytes_recvd) {
if (req[p] == '%' && req[p + 1] == '2' && req[p + 2] == '0') {
p = p + 2;
uri[p2++] = ' ';
} else {
uri[p2++] = tolower(req[p]);
}
p++;
}
bool IsDirRequested = false;
p = strlen(uri);
if (uri[p - 1] == '/') {
IsDirRequested = true;
sprintf(uri + p, "%s", "index.html");
}
printf("Requested file: %s\n", uri);
char text[65536];
FILE* fp;
fp = fopen(uri, "rb");
char* fname = strrchr(uri, '/');
fname++;
if (fp == NULL) {
fclose(fp);
printf("FILE NOT FOUND!\n");
WIN32_FIND_DATA data;
char buf[65536];
if (IsDirRequested) {
uri[p] = '*';
uri[p + 1] = '\0';
HANDLE hFind = FindFirstFile(uri, &data);
uri[p] = '\0';
p = 0;
if (hFind != INVALID_HANDLE_VALUE) {
// memset(text, 0, sizeof(text));memset(buf, 0, sizeof(buf));memset(request, 0, sizeof(request));
printf("Creating listing of %s ...\n", uri);
printf("Send: HTTP/1.1 200 OK ...\n");
sprintf(request, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nCache-Control: no-cache\r\nServer: TinyCHttpServer\r\nAccept-ranges: bytes\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n");
send(AcceptSocket, request, strlen(request), 0);
sprintf(request, "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link href=\"data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAAD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD///SkxgD0pMYAAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA//8AAPwfAAD8HwAA/B8AAPwfAAD8HwAA/B8AAPwfAAD8HwAA/B8AAPwfAACAAQAAgAEAAIABAACAAQAA//8AAA==\" rel=\"icon\" type=\"image/x-icon\"><style>html {line-break: anywhere;word-wrap: break-word;height: 100%%;padding: 0;margin: 0;}body {text-align: center;max-width: 920px;margin: auto;border-color: #bf4f00;border-style: solid;}b {color:#bf4f00;}</style><title>TinyCHttpServer: Index of %s</title></head><body><br>date time now: %d-%02d-%02d %02d:%02d:%02d\n<br>and random number: %d<p><h1><b>Index of %s</b></h1><p><a href=\"..\">..</a>\n", uri + 12, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, rand(), uri + 12);
sprintf(buf, "\r\n%x\r\n%s\r\n", strlen(request), request);
send(AcceptSocket, buf, strlen(buf), 0);
do {
if (strcmp(data.cFileName, ".") != 0 && strcmp(data.cFileName, "..") != 0) { // memset(text, 0, sizeof(text));memset(buf, 0, sizeof(buf));
sprintf(text, "<p><a href=\"%s\">%s</a> - %d\n", data.cFileName, data.cFileName, data.nFileSizeLow);
sprintf(buf, "%x\r\n%s\r\n", strlen(text), text);
send(AcceptSocket, buf, strlen(buf), 0);
}
} while (FindNextFile(hFind, &data));
FindClose(hFind);
sprintf(text, "<br><br><a href=\"/\">Back to root</a><p><b>TinyCHttpServer</b></body></html>");
sprintf(request, "%x\r\n%s\r\n", strlen(text), text);
send(AcceptSocket, request, strlen(request), 0);
send(AcceptSocket, "0\r\n\r\n", 5, 0);
closesocket(AcceptSocket);
printf("Socket Closed.\n\n");
continue;
}
} else {
uri[p] = '/';
uri[p + 1] = '*';
uri[p + 2] = '\0';
HANDLE hFind = FindFirstFile(uri, &data);
uri[p] = '/';
uri[p + 1] = '\0';
p = 0;
if (hFind != INVALID_HANDLE_VALUE) {
// do{p++;if(p>2){break;}}while(FindNextFile(hFind, &data));
FindClose(hFind);
printf("DIR FOUND!\nSend: HTTP/1.1 301 Moved Permanently ...\n");
// sprintf(request,"<html><head><title>301 Moved Permanently</title></head><body><center><h1>301 Moved Permanently</h1></center><hr><center>TinyCHttpServer</center></body></html>");
sprintf(buf, "HTTP/1.1 301 Moved Permanently\r\nContent-Type: text/html; charset=UTF-8\r\nLocation: %s\r\nCache-Control: no-cache\r\nServer: TinyCHttpServer\r\nAccept-ranges: bytes\r\nContent-Length: 158\r\nConnection: close\r\n\r\n<html><head><title>301 Moved Permanently</title></head><body><center><h1>301 Moved Permanently</h1></center><hr><center>TinyCHttpServer</center></body></html>", uri + 12);
send(AcceptSocket, buf, strlen(buf), 0);
closesocket(AcceptSocket);
printf("Socket Closed.\n\n");
continue;
}
}
sprintf(text, "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link href=\"data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAQAABILAAASCwAAAAAAAAAAAAD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYAAAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD///SkxgD0pMYAAAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA///0pMYA9KTGAAAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//9KTGAPSkxgAAAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD//wAA//8AAP//AAD///SkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA9KTGAPSkxgD0pMYA//8AAPwfAAD8HwAA/B8AAPwfAAD8HwAA/B8AAPwfAAD8HwAA/B8AAPwfAACAAQAAgAEAAIABAACAAQAA//8AAA==\" rel=\"icon\" type=\"image/x-icon\"><style>html {line-break: anywhere;word-wrap: break-word;height: 100%%;padding: 0;margin: 0;}body {text-align: center;max-width: 920px;margin: auto;border-color: #bf4f00;border-style: solid;}b {color:#bf4f00;}</style><title>TinyCHttpServer: 404 Not Found</title></head><body><br>date time now: %d-%02d-%02d %02d:%02d:%02d\n<br>and random number: %d<p><h1><b>404 Page not found</b></h1><br><a href=\"/\">Back to root</a><p><b>TinyCHttpServer</b></body></html>\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, rand());
sprintf(request, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=UTF-8\r\nCache-Control: no-cache\r\nAccept-ranges: bytes\r\nServer: TinyCHttpServer\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", strlen(text), text);
printf("Send: HTTP/1.1 404 Not Found ...\n");
send(AcceptSocket, request, strlen(request), 0);
} else {
char* ext = strrchr(fname, '.');
char mimetype[32] = "application/octet-stream"; // Default mimetype
if (ext != NULL) {
ext++;
if (strcmp(ext, "txt") == 0) {
strcpy(mimetype, "text/plain; charset=UTF-8");
} else {
if (strcmp(ext, "html") == 0 || strcmp(ext, "htm") == 0 || strcmp(ext, "xhtml") == 0) {
strcpy(mimetype, "text/html; charset=UTF-8");
} else {
if (strcmp(ext, "jpeg") == 0 || strcmp(ext, "jpg") == 0 || strcmp(ext, "jfif") == 0) {
strcpy(mimetype, "image/jpg");
} else {
if (strcmp(ext, "css") == 0) {
strcpy(mimetype, "text/css");
} else {
if (strcmp(ext, "js") == 0) {
strcpy(mimetype, "application/javascript");
} else {
if (strcmp(ext, "json") == 0) {
strcpy(mimetype, "application/json");
} else {
if (strcmp(ext, "gif") == 0) {
strcpy(mimetype, "image/gif");
} else {
if (strcmp(ext, "png") == 0) {
strcpy(mimetype, "image/png");
} else {
if (strcmp(ext, "ico") == 0) {
strcpy(mimetype, "image/x-icon");
} else {
if (strcmp(ext, "webm") == 0) {
strcpy(mimetype, "video/webm");
} else {
if (strcmp(ext, "mp4") == 0 || strcmp(ext, "mp4v") == 0 || strcmp(ext, "mpg4") == 0) {
strcpy(mimetype, "video/mp4");
} else {
if (strcmp(ext, "mp3") == 0 || strcmp(ext, "m2a") == 0 || strcmp(ext, "m3a") == 0 || strcmp(ext, "mp2") == 0 || strcmp(ext, "mp2a") == 0 || strcmp(ext, "mpga") == 0) {
strcpy(mimetype, "audio/mpeg");
} else {
if (strcmp(ext, "svg") == 0 || strcmp(ext, "svgz") == 0) {
strcpy(mimetype, "image/svg+xml");
} else {
if (strcmp(ext, "wbmp") == 0) {
strcpy(mimetype, "image/vnd.wap.wbmp");
} else {
if (strcmp(ext, "webp") == 0) {
strcpy(mimetype, "image/webp");
}
}
}
}
}
}
}
}
send(AcceptSocket, request, strlen(request), 0);
fseek(fp, ranges[0][1], SEEK_SET);
sz = to_read;
p = (sz / 65536);
printf("Filesize=%lld bytes\n", sz);
for (p2 = 0; p2 < p; p2++) {
printf("Sending file: %d/%d ...\r", p2 + 1, p);
fread(request, 65536, 1, fp);
if (send(AcceptSocket, request, 65536, 0) == -1) {
break;
}
}
if (p > 0) {
if (p2 >= p) {
printf("\nFile sent successfully!\n");
} else {
printf("\nFile transfer interrupted.\n");
}
} else {
printf("File sent successfully!\n");
}
p = sz - (p * 65536);
fread(request, p, 1, fp);
send(AcceptSocket, request, p, 0);
fclose(fp);
closesocket(AcceptSocket);
printf("Socket Closed.\n\n");
continue;
}
} else {
sprintf(request, "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nCache-Control: no-cache\r\nServer: TinyCHttpServer\r\nAccept-ranges: bytes\r\nContent-Length: %lld\r\nConnection: close\r\n\r\n", mimetype, sz);
printf("Send: HTTP/1.1 200 OK ...\n");
send(AcceptSocket, request, strlen(request), 0);
p = (sz / 65536);
printf("Filesize=%lld bytes\n", sz);
for (p2 = 0; p2 < p; p2++) {
printf("Sending file: %d/%d ...\r", p2 + 1, p);
fread(request, 65536, 1, fp);
if (send(AcceptSocket, request, 65536, 0) == -1) {
break;
}
}
if (p > 0) {
if (p2 >= p) {
printf("\nFile sent successfully!\n");
} else {
printf("\nFile transfer interrupted.\n");
}
} else {
printf("File sent successfully!\n");
}
p = sz - (p * 65536);
fread(request, p, 1, fp);
send(AcceptSocket, request, p, 0);
fclose(fp);
}
}
closesocket(AcceptSocket);
printf("Socket Closed.\n\n");
}
return 0;
}