-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanoHttp.c
673 lines (590 loc) · 18.9 KB
/
nanoHttp.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//
// webserver.c
//
// Very simple and minimalistic http server
//
#include "nanoHttp.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <dirent.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <time.h>
#include <fcntl.h>
#if _NANOHTTP_THREADING == 1
#include <pthread.h>
#include <signal.h>
#endif
/* ==== GLOBAL SETTINGS ===================================================== */
#define VERSION "0.4"
#define SERVER "nanoHttp/1.1"
#define PROTOCOL "HTTP/1.0"
#define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT"
// Default port
static int port = DEFAULT_PORT;
// Working directory
static char working_dir[STR_BUF];
// Server socket
static int sock;
// Verbosity flag
static bool verbose = false;
/** Approximate number of total sent bytes */
static volatile long bytesSent = 0L;
/** Approximate number of total received bytes */
static volatile long bytesReceived = 0L;
#if _NANOHTTP_THREADING == 1
typedef struct {
int fd;
int thread;
struct sockaddr clientaddr;
socklen_t clientaddr_size;
} thread_args;
volatile pthread_t threads[THREAD_COUNT];
#endif
static ssize_t strwrite(int fd, char* str) {
size_t len = strlen(str);
if(fd < 0 || len <= 0) return 0;
ssize_t ret = write(fd, str, len);
if(ret > 0) bytesSent += (long)ret;
return ret;
}
static char* removeDoubleSlash(char* buf) {
const size_t len = strlen(buf)-1;
for(size_t i = 0; i < len;i++) {
if(buf[i] == '/' && buf[i+1] == '/') {
// Shift
for(size_t j = i;j<len;j++)
buf[j] = buf[j+1];
}
}
return buf;
}
static size_t fdgets(int fd, char* buf, size_t size) {
size_t i = 0;
char c;
while(i < size) {
if (read(fd, &c, 1) <= 0) break;
if(c == EOF) break;
buf[i++] = c;
if (c == '\n') break;
}
if(i > 0) bytesReceived += (long)i;
return i;
}
static char *get_mime_type(char *name) {
char *ext = strrchr(name, '.');
if (!ext) return NULL;
else if (strcmp(ext, ".html") == 0 || strcmp(ext, ".htm") == 0) return "text/html";
else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".jpeg") == 0) return "image/jpeg";
else if (strcmp(ext, ".gif") == 0) return "image/gif";
else if (strcmp(ext, ".png") == 0) return "image/png";
else if (strcmp(ext, ".bmp") == 0) return "image/bmp";
else if (strcmp(ext, ".css") == 0) return "text/css";
else if (strcmp(ext, ".js") == 0) return "application/javascript";
else if (strcmp(ext, ".au") == 0) return "audio/basic";
else if (strcmp(ext, ".wav") == 0) return "audio/wav";
else if (strcmp(ext, ".avi") == 0) return "video/x-msvideo";
else if (strcmp(ext, ".mp3") == 0) return "audio/mpeg3";
else if (strcmp(ext, ".mpeg") == 0 || strcmp(ext, ".mpg") == 0) return "video/mpeg";
else if (strcmp(ext, ".txt") == 0) return "text/plain";
else if (strcmp(ext, ".xml") == 0) return "text/xml";
else if (strcmp(ext, ".c") == 0) return "text/plain";
else if (strcmp(ext, ".c++") == 0) return "text/plain";
else if (strcmp(ext, ".cpp") == 0) return "text/plain";
else if (strcmp(ext, ".cc") == 0) return "text/plain";
else if (strcmp(ext, ".h") == 0) return "text/plain";
else if (strcmp(ext, ".hpp") == 0) return "text/plain";
else if (strcmp(ext, ".h++") == 0) return "text/plain";
else if (strcmp(ext, ".py") == 0) return "text/plain";
else if (strcmp(ext, ".java") == 0) return "text/x-java-source";
else if (strcmp(ext, ".class") == 0) return "application/x-java-class";
else if (strcmp(ext, ".csv") == 0) return "text/plain";
else if (strcmp(ext, ".log") == 0) return "text/plain";
else if (strcmp(ext, ".zip") == 0) return "application/zip";
else if (strcmp(ext, ".bin") == 0) return "application/octet-stream";
else if (strcmp(ext, ".bz") == 0) return "application/x-bzip";
else if (strcmp(ext, ".bz2") == 0) return "application/x-bzip2";
else if (strcmp(ext, ".crt") == 0) return "application/x-x509-user-cert";
else if (strcmp(ext, ".dvi") == 0) return "application/x-dvi";
else if (strcmp(ext, ".f77") == 0) return "text/x-fortran";
else if (strcmp(ext, ".f90") == 0) return "text/x-fortran";
else if (strcmp(ext, ".f") == 0) return "text/x-fortran";
else if (strcmp(ext, ".gz") == 0 || strcmp(ext, ".gzip") == 0) return "application/x-gzip";
else if (strcmp(ext, ".hdf") == 0) return "application/x-hdf";
else if (strcmp(ext, ".hdf5") == 0) return "application/octet-stream";
else if (strcmp(ext, ".mid") == 0) return "audio/midi";
else if (strcmp(ext, ".mov") == 0) return "video/quicktime";
else if (strcmp(ext, ".o") == 0) return "application/octet-stream";
else if (strcmp(ext, ".pps") == 0 || strcmp(ext, ".ppt") == 0 || strcmp(ext, ".ppz") == 0)
return "application/powerpoint";
else if (strcmp(ext, ".ps") == 0) return "application/postscript";
else if (strcmp(ext, ".pyc") == 0) return "application/x-bytecode.python";
else if (strcmp(ext, ".rtf") == 0) return "text/richtext";
else if (strcmp(ext, ".sh") == 0) return "application/x-sh";
else if (strcmp(ext, ".tar") == 0) return "application/x-tar";
else if (strcmp(ext, ".tex") == 0) return "application/x-tex";
else if (strcmp(ext, ".tgz") == 0) return "application/x-compressed";
else if (strcmp(ext, ".xz") == 0) return "application/x-xz";
return NULL;
}
void send_headers(int fd, int status, char *title, char *extra, char *mime, int length, time_t date) {
time_t now;
char timebuf[128];
char strbuf[16*1024];
sprintf(strbuf, "%s %d %s\r\n", PROTOCOL, status, title);
strwrite(fd, strbuf);
sprintf(strbuf, "Server: %s\r\n", SERVER);
strwrite(fd, strbuf);
now = time(NULL);
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now));
sprintf(strbuf, "Date: %s\r\n", timebuf);
strwrite(fd, strbuf);
if (extra) {
sprintf(strbuf, "%s\r\n", extra);
strwrite(fd, strbuf);
}
if (mime) {
sprintf(strbuf, "Content-Type: %s\r\n", mime);
strwrite(fd, strbuf);
}
if (length >= 0) {
sprintf(strbuf, "Content-Length: %d\r\n", length);
strwrite(fd, strbuf);
}
if (date != -1) {
strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&date));
sprintf(strbuf, "Last-Modified: %s\r\n", timebuf);
strwrite(fd, strbuf);
}
strwrite(fd, "Connection: close\r\n");
strwrite(fd, "\r\n");
}
void send_error(int fd, int status, char *title, char *extra, char *text) {
send_headers(fd, status, title, extra, "text/html", -1, -1);
FILE* f = fdopen(fd, "a+");
if(f!=NULL) {
fprintf(f, "<HTML><HEAD><TITLE>%d %s</TITLE></HEAD>\r\n", status, title);
fprintf(f, "<BODY><H4>%d %s</H4>\r\n", status, title);
fprintf(f, "%s\r\n", text);
fprintf(f, "</BODY></HTML>\r\n");
}
}
int send_file(int fd, char *path, struct stat *statbuf) {
char data[READ_BUF];
int n;
int file_fd = open(path, O_RDONLY);
if (file_fd < 0) {
switch(errno) {
case EACCES:
send_error(fd, 403, "Forbidden", NULL, "Access denied.");
return -403;
case ENOENT:
send_error(fd, 404, "Not found", NULL, "404 - Not found");
return -404;
// Linux specific errors
case ENFILE:
case ENOMEM:
send_error(fd, 500, "Server error", NULL, "Server error.");
return -500;
default:
// Default error
send_error(fd, 500, "Server error", NULL, "Server error.");
return -500;
}
} else {
size_t length = S_ISREG(statbuf->st_mode) ? statbuf->st_size : -1;
size_t sent = 0;
send_headers(fd, 200, "OK", NULL, get_mime_type(path), length, statbuf->st_mtime);
// printf("Sending file (%ld bytes) ... \n", length);
while ((n = read(file_fd, data, sizeof(char) * READ_BUF)) > 0) {
size_t b_sent = write(fd, data, sizeof(char) * n);
if((int)b_sent < 0) {
if(errno == 0)
fprintf(stderr, "Error writing to socket: Unknown error\n");
else
fprintf(stderr, "Error writing to socket: %s \n", strerror(errno));
break;
} else if((int)b_sent == 0)
break;
else
sent += b_sent;
}
close(file_fd);
if(sent == length) {
return 0;
} else {
printf("Incomplete sent (%ld of %ld bytes)\n", sent, length);
return -1;
}
}
// This should normally never occur
return -2;
}
/** Process a request on the given file descriptor */
int process(int fd, struct sockaddr clientaddr, socklen_t clientaddr_size) {
char buf[STR_BUF];
char *method;
char path[STR_BUF];
char *w_path;
char *protocol;
struct stat statbuf;
char pathbuf[STR_BUF];
int len;
if (fdgets(fd, buf, sizeof(buf)) <= 0) return -1;
removeDoubleSlash(buf);
// Write source address and request path to log
printf("[");
#if 0
// Not yet working
if(clientaddr_size == sizeof(struct in_addr)) {
char str[INET_ADDRSTRLEN];
struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&clientaddr;
struct in_addr ipAddr = pV4Addr->sin_addr;
inet_ntop( AF_INET, &ipAddr, str, INET_ADDRSTRLEN );
printf("%s", str);
} else if(clientaddr_size == sizeof(struct sockaddr_in6)) {
char str[INET6_ADDRSTRLEN];
struct sockaddr_in6* pV6Addr = (struct sockaddr_in6*)&clientaddr;
struct in6_addr ipAddr = pV6Addr->sin6_addr;
inet_ntop( AF_INET6, &ipAddr, str, INET6_ADDRSTRLEN );
printf("%s", str);
} else {
printf("UNKNOWN");
}
#endif
if(clientaddr_size >= sizeof(struct sockaddr_in)) {
struct sockaddr_in* pV4Addr = (struct sockaddr_in*)&clientaddr;
printf("%s:%d", inet_ntoa(pV4Addr->sin_addr), (int) ntohs(pV4Addr->sin_port));
} else
printf("UNKNOWN");
printf("]: %s", buf);
method = strtok(buf, " ");
w_path = strtok(NULL, " ");
protocol = strtok(NULL, "\r");
if (!method || !w_path || !protocol) return -1;
if(w_path[0] == '/' && strlen(w_path)>1)
snprintf(path, sizeof(path), "%s%s", working_dir, w_path+1);
else
snprintf(path, sizeof(path), "%s%s", working_dir, w_path);
//fseek(f, 0, SEEK_CUR); // Force change of stream direction
if (strcasecmp(method, "GET") != 0) {
send_error(fd, 501, "Not supported", NULL, "Method is not supported.");
fprintf(stderr, "(501) Method '%s' not supported \n", method);
return -2;
} else if (stat(path, &statbuf) < 0) {
send_error(fd, 404, "Not Found", NULL, "File not found.");
fprintf(stderr, "(404) File '%s' not found \n", path);
return -404;
} else if (S_ISDIR(statbuf.st_mode)) {
len = strlen(path);
if (len == 0 || path[len - 1] != '/') {
snprintf(pathbuf, sizeof(pathbuf), "Location: %s/", path);
send_error(fd, 302, "Found", pathbuf, "Directories must end with a slash.");
} else {
snprintf(pathbuf, sizeof(pathbuf), "%sindex.html", path);
if (stat(pathbuf, &statbuf) >= 0) {
int res = send_file(fd, pathbuf, &statbuf);
if(res != 0) return res;
} else {
// Remove double slash at end, if so
size_t slash = (len-1);
while(slash > 0 && path[slash] == '/') {
path[slash+1] = '\0';
slash--;
}
char strbuf[STR_BUF];
DIR *dir;
struct dirent *de;
send_headers(fd, 200, "OK", NULL, "text/html", -1, statbuf.st_mtime);
sprintf(strbuf, "<HTML><HEAD><TITLE>Index of %s</TITLE></HEAD>\r\n<BODY>", path);
strwrite(fd, strbuf);
sprintf(strbuf, "<H4>Index of %s</H4>\r\n<PRE>\n", path);
strwrite(fd, strbuf);
sprintf(strbuf, "Name Last Modified Size\r\n");
strwrite(fd, strbuf);
sprintf(strbuf, "<HR>\r\n");
strwrite(fd, strbuf);
if (len > 1) {
sprintf(strbuf, "<A HREF=\"..\">..</A>\r\n");
strwrite(fd, strbuf);
}
dir = opendir(path);
while ((de = readdir(dir)) != NULL) {
char timebuf[32];
struct tm *tm;
strcpy(pathbuf, path);
strcat(pathbuf, de->d_name);
stat(pathbuf, &statbuf);
tm = gmtime(&statbuf.st_mtime);
strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tm);
sprintf(strbuf, "<A HREF=\"%s%s\">", de->d_name, S_ISDIR(statbuf.st_mode) ? "/" : "");
strwrite(fd, strbuf);
sprintf(strbuf, "%s%s", de->d_name, S_ISDIR(statbuf.st_mode) ? "/</A>" : "</A> ");
strwrite(fd, strbuf);
if (strlen(de->d_name) < 32) {
sprintf(strbuf, "%*s", 32 - (int)strlen(de->d_name), "");
strwrite(fd, strbuf);
}
if (S_ISDIR(statbuf.st_mode)) {
sprintf(strbuf, "%s\r\n", timebuf);
strwrite(fd, strbuf);
} else {
sprintf(strbuf, "%s %10d\r\n", timebuf, (int)statbuf.st_size);
strwrite(fd, strbuf);
}
}
closedir(dir);
sprintf(strbuf, "</PRE>\r\n<HR>\r\n<ADDRESS>%s</ADDRESS>\r\n</BODY></HTML>\r\n", SERVER);
strwrite(fd, strbuf);
}
}
} else {
send_file(fd, path, &statbuf);
}
return 0;
}
#if _NANOHTTP_THREADING == 1
/* Process a socket */
void process_thread(thread_args *args) {
int fd, thread;
int res;
fd = args->fd;
thread = args->thread;
free(args);
threads[thread] = pthread_self();
res = process(fd, args->clientaddr, args->clientaddr_size);
shutdown(fd, SHUT_RDWR);
close(fd);
threads[thread] = 0;
if(res < 0) {
if(res == -404 || res == -502) {
// Ignore since the error message already took place
} else
printf("Returned status %d \n", res);
}
}
#endif
static void exit_program(const int status) {
printf("Total bytes sent: %ld (approx)\n", bytesSent);
printf("Total bytes received: %ld (approx)\n", bytesReceived);
printf("Bye\n");
exit(status);
}
static void sig_handler(int signo) {
switch(signo) {
case SIGINT:
fprintf(stderr, "SIGINT received. Exiting\n");
exit_program(EXIT_FAILURE);
return;
case SIGTERM:
fprintf(stderr, "SIGTERM received. Exiting\n");
exit_program(EXIT_FAILURE);
return;
case SIGUSR1:
// Print stats
printf("Total bytes sent: %ld (approx)", bytesSent);
printf("Total bytes received: %ld (approx)", bytesReceived);
break;
case SIGUSR2:
fprintf(stderr, "SIGUSR2 received. Exiting\n");
exit(EXIT_FAILURE);
break;
case SIGALRM:
fprintf(stderr, "SIGALRM received. Exiting\n");
exit(EXIT_FAILURE);
break;
#if 0 // Old debug output
case SIGSEGV:
fprintf(stderr, "Segmentation fault\n");
exit(EXIT_FAILURE);
break;
#endif
}
}
void cleanup(void) {
if(sock > 0) close(sock);
sock = 0;
#if _NANOHTTP_THREADING == 1
for(int i=0;i<THREAD_COUNT;i++) {
pthread_t pid = threads[i];
if(pid <= 0) continue;
else
pthread_kill(pid, SIGINT);
}
#endif
}
int main(int argc, char *argv[]) {
struct sockaddr_in sin;
bool daemon = false;
if(getcwd(working_dir, sizeof(working_dir)) == NULL) {
fprintf(stderr, "WARN: error getting working directory\n");
strcpy(working_dir, "/var/www");
}
// Parse program arguments
for(int i=1;i<argc;i++) {
char* arg = argv[i];
bool is_last = i >= argc-1;
if(!strcmp("-h", arg) || !strcmp("--help",arg)) {
printf("nanoHttp v %s ", VERSION);
#if _NANOHTTP_THREADING == 1
printf(" (multithread)");
#endif
printf("\n");
printf(" Usage: %s [OPTIONS]\n", argv[0]);
printf("OPTIONS:\n");
printf("\t-h --help Print this help message\n");
printf("\t-D --daemon Run as daemon (fork)\n");
printf("\t-p --port PORT Define port\n");
printf("\t-w --cwd PATH Define working directory\n");
printf("\n2018, Felix Niederwanger\n");
return EXIT_SUCCESS;
} else if(!strcmp("-D", arg) || !strcmp("--daemon",arg)) {
daemon = true;
} else if(!strcmp("-p", arg) || !strcmp("--port",arg)) {
if(is_last) {
fprintf(stderr, "Missing argument: port\n");
return EXIT_FAILURE;
}
port = atoi(argv[++i]);
} else if(!strcmp("-w", arg) || !strcmp("--cwd", arg)) {
if(is_last) {
fprintf(stderr, "Missing argument: port\n");
return EXIT_FAILURE;
} else {
size_t len;
arg = argv[++i];
len = strlen(arg);
if(len > sizeof(working_dir)-1) len = sizeof(working_dir)-1;
strncpy(working_dir, arg, len);
working_dir[len] = '\0';
}
} else if(!strcmp("-v", arg) || !strcmp("--verbose", arg) ) {
verbose = false;
} else {
fprintf(stderr, "Error: Unknown argument: %s \n", arg);
exit(EXIT_FAILURE);
}
}
// Run as daemon
if(daemon == true) {
pid_t daemon_pid = fork();
if(daemon_pid < 0) {
fprintf(stderr, "Cannot fork: %s \n", strerror(errno));
return EXIT_FAILURE;
}
if(daemon_pid == 0) {
// Child process continues work as daemon
if(setsid() < 0) {
fprintf(stderr, "Failed to set new session id: %s\n", strerror(errno));
return EXIT_FAILURE;
}
} else {
// I am the parent process. My work here is done
printf("Daemon forked.\n");
return EXIT_SUCCESS;
}
}
// Assure, that the working dir ends with a /
if(working_dir[strlen(working_dir)] != '/') {
size_t len = strlen(working_dir);
if(len >= sizeof(working_dir)-1) {
fprintf(stderr, "Working directory exceeds buffer size\n");
return EXIT_FAILURE;
}
working_dir[len] = '/';
working_dir[len+1] = '\0';
}
printf("Working directory: %s\n", working_dir);
// Set signals
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGUSR1, sig_handler);
signal(SIGUSR2, sig_handler);
signal(SIGALRM, sig_handler);
//signal(SIGSEGV, sig_handler);
atexit(cleanup);
#if _NANOHTTP_THREADING == 1
for(int i=0;i<THREAD_COUNT;i++)
threads[i] = 0;
#endif
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
fprintf(stderr, "Error setting up the socket: %s \n", strerror(errno));
return EXIT_FAILURE;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
if(bind(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
fprintf(stderr, "Error binding socket to port %d: %s \n", port, strerror(errno));
return EXIT_FAILURE;
}
if(listen(sock, 5) < 0) {
fprintf(stderr, "Error listening for new connections on port %d: %s \n", port, strerror(errno));
return EXIT_FAILURE;
}
printf("HTTP server listening on port %d\n", port);
struct sockaddr clientaddr;
socklen_t clientaddr_size;
while (1) {
int s;
s = accept(sock, &clientaddr, &clientaddr_size);
if (s < 0) {
fprintf(stderr, "Error accepting new client: %s \n", strerror(errno));
break;
} else {
#if _NANOHTTP_THREADING == 1
pthread_t thread = 0;
thread_args *args = (thread_args*)malloc(sizeof(thread_args));
if(args == NULL) {
fprintf(stderr, "Error allocating memory for thread: %s \n", strerror(errno));
close(s);
continue;
} else {
// Get a thread that is not occupied
int x = -1;
// XXX: Busy waiting possible :-(
while(x < 0) {
for(int i=0;i<THREAD_COUNT;i++) {
if(threads[i] == 0) {
x = i; break;
}
}
}
threads[x] = -1; // Occupied but not yet populated.
// Create Thread
args->fd = s;
args->thread = x;
args->clientaddr_size = clientaddr_size;
memcpy(&args->clientaddr, &clientaddr, sizeof(clientaddr));
int res = pthread_create(&thread, NULL, (void * (*)(void *))process_thread, args);
if(res < 0) {
fprintf(stderr, "Error creating thread: %s \n", strerror(errno));
close(s);
free(args);
continue;
} else {
// Also set in the thread to ensure data consistency
threads[x] = thread;
}
}
#else
// Directly process request (single-thread)
process(s, clientaddr, clientaddr_size);
shutdown(s, SHUT_RDWR);
close(s);
#endif
}
}
close(sock);
printf("Bye\n");
return 0;
}