forked from labcoder/simple-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
283 lines (245 loc) · 8.49 KB
/
server.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
276
277
278
279
280
281
282
283
/*
* project: miniweb
* author: Oscar Sanchez (oms1005@gmail.com)
* HTTP Server
* WORKS ON BROWSERS TOO!
* Inspired by IBM's nweb
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
* 2016.05.07 cxd4
*
* setpgrp() documentation says to use setpgid() instead.
* Maybe this is why gcc -ansi -Wall complains about an implicit name.
*/
#include <unistd.h>
#include <assert.h>
#define BUFSIZE 8096
#define ERROR 42
#define SORRY 43
#define LOG 44
struct {
const char* ext;
const char* filetype;
} extensions[] = {
{ "gif", "image/gif" },
{ "jpg", "image/jpeg" },
{ "jpeg", "image/jpeg" },
{ "png", "image/png" },
{ "zip", "image/zip" },
{ "gz", "image/gz" },
{ "tar", "image/tar" },
{ "htm", "text/html" },
{ "html", "text/html" },
{ "php", "image/php" },
{ "cgi", "text/cgi" },
{ "asp", "text/asp" },
{ "jsp", "image/jsp" },
{ "xml", "text/xml" },
{ "js", "text/js" },
{ "css", "test/css" },
{ 0, 0 }
};
static const char* err_IDs[] = {
"(none)",
"EPERM", "ENOENT", "ESRCH", "EINTR", "EIO", "ENXIO", "E2BIG", "ENOEXEC",
"EBADF", "ECHILD", "EAGAIN", "ENOMEM", "EACCES", "EFAULT", "ENOTBLK",
"EBUSY", "EEXIST", "EXDEV", "ENODEV", "ENOTDIR", "EISDIR", "EINVAL",
"ENFILE", "EMFILE", "ENOTTY", "ETXTBSY", "EFBIG", "ENOSPC", "ESPIPE",
"EROFS", "EMLINK", "EPIPE", "EDOM", "ERANGE",
"EDEADLK", "ENAMETOOLONG", "ENOLCK", "ENOSYS", "ENOTEMPTY", "ELOOP", "???",
"ENOMSG", "EIDRM", "ECHRNG", "EL2NSYNC", "EL3HLT", "EL3RST", "ELNRNG",
"EUNATCH", "ENOCSI", "EL2HLT", "EBADE", "EBADR", "EXFULL", "ENOANO",
"EBADRQC", "EBADSLT", "???",
"EBFONT", "ENOSTR", "ENODATA", "ETIME", "ENOSR", "ENONET", "ENOPKG",
"EREMOTE", "ENOLINK", "EADV", "ESRMNT", "ECOMM", "EPROTO", "EMULTIHOP",
"EDOTDOT", "EBADMSG", "EOVERFLOW", "ENOTUNIQ", "EBADFD", "EREMCHG",
"ELIBACC", "ELIBBAD", "ELIBSCN", "ELIBMAX", "ELIBEXEC", "EILSEQ",
"ERESTART", "ESTRPIPE", "EUSERS", "ENOTSOCK", "EDESTADDRREQ", "EMSGSIZE",
"EPROTOTYPE", "ENOPROTOOPT", "EPROTONOSUPPORT", "ESOCKTNOSUPPORT",
"EOPNOTSUPP", "EPFNOSUPPORT", "EAFNOSUPPORT", "EADDRINUSE", "EADDRNOTAVAIL",
"ENETDOWN", "ENETUNREACH", "ENETRESET", "ECONNABORTED", "ECONNRESET",
"ENOBUFS", "EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS", "ETIMEDOUT",
"ECONNREFUSED", "EHOSTDOWN", "EHOSTUNREACH", "EALREADY", "EINPROGRESS",
"ESTALE", "EUCLEAN", "ENOTNAM", "ENAVAIL", "EISNAM", "EREMOTEIO", "EDQUOT",
"ENOMEDIUM", "EMEDIUMTYPE", "ECANCELED", "ENOKEY", "EKEYEXPIRED",
"EKEYREVOKED", "EKEYREJECTED",
"EOWNERDEAD", "ENOTRECOVERABLE",
};
void my_log(int type, char *s1, char *s2, int num)
{
int fd;
char logbuffer[BUFSIZE * 2];
const int known_errors = sizeof(err_IDs) / sizeof(const char *);
switch (type) {
case ERROR:
assert(errno >= 0 && errno < known_errors);
sprintf(
&logbuffer[0],
"ERROR: %s:%s error %s exiting pid %d",
s1, s2, err_IDs[errno], getpid()
);
break;
case SORRY:
sprintf(logbuffer, "<HTML><BODY><H1>Web Server Sorry: %s %s</H1></BODY></HTML>\r\n", s1, s2);
write(num, logbuffer, strlen(logbuffer));
sprintf(logbuffer, "SORRY: %s:%s", s1, s2);
break;
case LOG:
sprintf(logbuffer, "INFO : %s:%s:%d", s1, s2, num);
break;
}
if ((fd = open("server.log", O_CREAT | O_WRONLY | O_APPEND, 0644)) >= 0) {
write(fd, logbuffer, strlen(logbuffer));
write(fd, "\n", 1);
close(fd);
}
if (type == ERROR || type == SORRY)
exit(3);
}
void web(int fd, int hit)
{
static char buffer[BUFSIZE + 1];
const char * fstr;
int j, file_fd, buflen, len;
long i, ret;
ret = read(fd, buffer, BUFSIZE);
if (ret == 0 || ret == -1) {
my_log(SORRY, "failed to read browser request", "", fd);
}
if (ret > 0 && ret < BUFSIZE)
buffer[ret]=0;
else
buffer[0]=0;
for (i = 0; i < ret; i++)
if (buffer[i] == '\r' || buffer[i] == '\n')
buffer[i] = '*';
my_log(LOG, "request", buffer, hit);
if (strncmp(buffer, "GET ", 4) && strncmp(buffer, "get ", 4))
my_log(SORRY, "Only simple GET operation supported", buffer, fd);
for (i = 4; i < BUFSIZE; i++) {
if (buffer[i] == ' ') {
buffer[i] = 0;
break;
}
}
for (j = 0; j < i - 1; j++)
if (buffer[j] == '.' && buffer[j + 1] == '.')
my_log(SORRY, "Parent directory (..) path names not supported", buffer, fd);
if (!strncmp(&buffer[0], "GET /\0", 6) || !strncmp(&buffer[0], "get /\0", 6))
strcpy(buffer, "GET /index.html");
buflen = strlen(buffer);
fstr = NULL;
for (i = 0; extensions[i].ext != 0; i++) {
len = strlen(extensions[i].ext);
if (!strncmp(&buffer[buflen - len], extensions[i].ext, len)) {
fstr = extensions[i].filetype;
break;
}
}
if (fstr == NULL)
my_log(SORRY, "file extension type not supported", buffer, fd);
if ((file_fd = open(&buffer[5], O_RDONLY)) == -1)
my_log(SORRY, "failed to open file", &buffer[5], fd);
my_log(LOG, "SEND", &buffer[5], hit);
sprintf(buffer, "HTTP/1.0 200 OK\r\nContent-Type: %s\r\n\r\n", fstr);
write(fd, buffer, strlen(buffer));
ret = read(file_fd, buffer, BUFSIZE);
while (ret > 0) {
write(fd, buffer, ret);
}
#ifdef LINUX
sleep(1);
#endif
exit(1);
}
static const char* baddirs[] = {
"/", "/etc", "/bin", "/lib", "/tmp", "/usr", "/dev", "/sbin"
};
int main(int argc, char **argv)
{
static struct sockaddr_in cli_addr;
static struct sockaddr_in serv_addr;
long string_as_number;
unsigned short port;
int status;
int pid, listenfd, hit;
register int i;
if (argc < 3 || argc > 3 || !strcmp(argv[1], "-?")) {
printf(
"usage: server [port] [server directory] &"
"\tExample: server 80 ./ &\n\n"
"\tOnly Supports: "
);
for (i = 0; extensions[i].ext != 0; i++) {
putchar(' ');
fputs(extensions[i].ext, stdout);
}
putchar('\n');
exit(0);
}
for (i = 0; i < 8; i++) {
if (strncmp(argv[2], baddirs[i], strlen(baddirs[i])) == 0) {
printf("ERROR: Bad top directory %s, see server -?\n", argv[2]);
exit(3);
}
}
if (chdir(argv[2]) == -1) {
printf("ERROR: Can't Change to directory %s\n", argv[2]);
exit(4);
}
if (fork() != 0)
return 0;
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
for (i = 0; i < 32; i++)
close(i);
status = setpgrp();
if (status != 0)
fprintf(stderr, "Process group set failed with error %i.\n", status);
my_log(LOG, "http server starting", argv[1], getpid());
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0)
my_log(ERROR, "system call", "socket", 0);
string_as_number = strtol(argv[1], NULL, 0);
if (string_as_number < 0 || string_as_number > 60000) {
fprintf(stderr, "Invalid port number: %li\n", string_as_number);
return 1;
}
port = (unsigned short)string_as_number;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
status = bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
if (status != 0)
my_log(ERROR, "system call", "bind", 0);
if (listen(listenfd, 64) < 0)
my_log(ERROR, "system call", "listen", 0);
for (hit = 1; ; hit++) {
socklen_t length;
int socketfd;
length = sizeof(cli_addr);
socketfd = accept(listenfd, (struct sockaddr *)&cli_addr, &length);
if (socketfd < 0)
my_log(ERROR, "system call", "accept", 0);
if ((pid = fork()) < 0) {
my_log(ERROR, "system call", "fork", 0);
} else {
if (pid == 0) {
close(listenfd);
web(socketfd, hit);
} else {
close(socketfd);
}
}
}
}