-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyhttpd-windows.cpp
283 lines (241 loc) · 6.13 KB
/
tinyhttpd-windows.cpp
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
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Windows.h>
#include <iphlpapi.h>
#include <io.h>
#define ISspace(x) isspace((int)(x))
#pragma comment(lib, "Ws2_32.lib")
#define SERVER_STRING "Server: jdbhttpd-windows/0.1.0\r\n"
void accept_request(SOCKET);
void cat(SOCKET, FILE *);
void error_die(const char *);
int get_line(SOCKET, char *, int);
void headers(SOCKET, const char *);
void not_found(SOCKET);
void server_file(SOCKET, const char *);
SOCKET startup(const char *);
void unimplemented(SOCKET);
void accept_request(SOCKET client_socket) {
SOCKET client = client_socket;
int numchars = 0;
size_t i, j;
char buf[1024];
char method[255];
char url[255];
char path[512];
int iResult;
numchars = get_line(client, buf, sizeof(buf));
i = 0;
j = 0;
while (!ISspace(buf[i]) && (i < sizeof(method) - 1)) {
method[i] = buf[i];
i++;
}
j = i;
method[i] = '\0';
if (_stricmp(method, "get") && _stricmp(method, "post")) {
unimplemented(client);
return;
}
i = 0;
while (ISspace(buf[j]) && (j < numchars)) {
j++;
}
while (!ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < numchars)) {
url[i] = buf[j];
i++;
j++;
}
url[i] = '\0';
sprintf_s(path, "htdocs%s", url);
if (path[strlen(path) - 1] == '/') {
strcat_s(path, "index.html");
}
if (!_access(path, 0)) {
server_file(client,path);
} else {
not_found(client);
}
iResult = shutdown(client, SD_SEND);
if (iResult == SOCKET_ERROR) {
closesocket(client);
WSACleanup();
error_die("shutdown failed with error\n");
}
closesocket(client);
}
void cat(SOCKET client, FILE *resource) {
char buf[1024];
while (!feof(resource)) {
fgets(buf, sizeof(buf), resource);
send(client, buf, strlen(buf), 0);
}
}
void error_die(const char *sc)
{
perror(sc);
exit(1);
}
int get_line(SOCKET sock, char *buf, int size) {
int i = 0;
char c = '\0';
int n;
while ((i < size - 1) && (c != '\n')) {
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';
}
}
buf[i] = c;
i++;
}
else
c = '\n';
}
buf[i] = '\0';
return i;
}
void headers(SOCKET client, const char *filename) {
char buf[1024];
strcpy_s(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, SERVER_STRING);
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);
}
void not_found(SOCKET client) {
char buf[1024];
strcpy_s(buf, "HTTP/1.0 404 NOT FOUND\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, SERVER_STRING);
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><TITLE>Not Found</TITLE>\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "<BODY><P>The server could not fulfill\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "your request because the resource specified\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "is unavailable or nonexistent.\r\n");
send(client, buf, strlen(buf), 0);
strcpy_s(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
}
void server_file(SOCKET client, const char *filename) {
FILE *resource = NULL;
int numchars = 1;
char buf[1024];
buf[0] = 'A';
buf[1] = '\0';
while ((numchars > 0) && strcmp("\n", buf)) {
numchars = get_line(client, buf, sizeof(buf));
}
fopen_s(&resource,filename, "r");
if (resource == NULL) {
not_found(client);
} else {
headers(client, filename);
cat(client, resource);
}
fclose(resource);
}
SOCKET startup(char *port) {
SOCKET httpd = INVALID_SOCKET;
//initialize winsock
WSADATA wsaData;
int iResult;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
error_die("WSAStartup failed\n");
}
struct addrinfo *result = NULL, *ptr = NULL, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
iResult = getaddrinfo(NULL, port, &hints, &result);
if (iResult != 0) {
WSACleanup();
error_die("getaddrinfo failed:\n");
}
httpd = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (httpd == INVALID_SOCKET) {
freeaddrinfo(result);
WSACleanup();
error_die("Error at socket()\n");
}
iResult = bind(httpd, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
freeaddrinfo(result);
closesocket(httpd);
WSACleanup();
error_die("bind failed with error\n");
}
if (listen(httpd, SOMAXCONN) == SOCKET_ERROR) {
closesocket(httpd);
WSACleanup();
error_die("Listen failed with error\n");
}
return httpd;
}
void unimplemented(SOCKET 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_STRING);
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);
}
int main(void) {
SOCKET server_sock = INVALID_SOCKET;
char port[] = "4000";
SOCKET client_sock = INVALID_SOCKET;
server_sock = startup(port);
printf("httpd running on port %s\n", port);
while (true) {
client_sock = accept(server_sock, NULL, NULL);
if (client_sock == INVALID_SOCKET) {
printf("accept failed:%d\n", WSAGetLastError());
closesocket(server_sock);
WSACleanup();
return 1;
}
accept_request(client_sock);
}
closesocket(server_sock);
WSACleanup();
return 0;
}