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
|
/* $Id: minihttptestserver.c,v 1.19 2015/11/17 09:07:17 nanard Exp $ */
/* Project : miniUPnP
* Author : Thomas Bernard
* Copyright (c) 2011-2016 Thomas Bernard
* This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution.
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK 0x7f000001
#endif
#define CRAP_LENGTH (2048)
volatile sig_atomic_t quit = 0;
volatile sig_atomic_t child_to_wait_for = 0;
/**
* signal handler for SIGCHLD (child status has changed)
*/
void handle_signal_chld(int sig)
{
(void)sig;
/* printf("handle_signal_chld(%d)\n", sig); */
++child_to_wait_for;
}
/**
* signal handler for SIGINT (CRTL C)
*/
void handle_signal_int(int sig)
{
(void)sig;
/* printf("handle_signal_int(%d)\n", sig); */
quit = 1;
}
/**
* build a text/plain content of the specified length
*/
void build_content(char * p, int n)
{
char line_buffer[80];
int k;
int i = 0;
while(n > 0) {
k = snprintf(line_buffer, sizeof(line_buffer),
"%04d_ABCDEFGHIJKL_This_line_is_64_bytes_long_ABCDEFGHIJKL_%04d\r\n",
i, i);
if(k != 64) {
fprintf(stderr, "snprintf() returned %d in build_content()\n", k);
}
++i;
if(n >= 64) {
memcpy(p, line_buffer, 64);
p += 64;
n -= 64;
} else {
memcpy(p, line_buffer, n);
p += n;
n = 0;
}
}
}
/**
* build crappy content
*/
void build_crap(char * p, int n)
{
static const char crap[] = "_CRAP_\r\n";
int i;
while(n > 0) {
i = sizeof(crap) - 1;
if(i > n)
i = n;
memcpy(p, crap, i);
p += i;
n -= i;
}
}
/**
* build chunked response.
* return a malloc'ed buffer
*/
char * build_chunked_response(int content_length, int * response_len)
{
char * response_buffer;
char * content_buffer;
int buffer_length;
int i, n;
/* allocate to have some margin */
buffer_length = 256 + content_length + (content_length >> 4);
response_buffer = malloc(buffer_length);
if(response_buffer == NULL)
return NULL;
*response_len = snprintf(response_buffer, buffer_length,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n");
/* build the content */
content_buffer = malloc(content_length);
if(content_buffer == NULL) {
free(response_buffer);
return NULL;
}
build_content(content_buffer, content_length);
/* chunk it */
i = 0;
while(i < content_length) {
n = (rand() % 199) + 1;
if(i + n > content_length) {
n = content_length - i;
}
/* TODO : check buffer size ! */
*response_len += snprintf(response_buffer + *response_len,
buffer_length - *response_len,
"%x\r\n", n);
memcpy(response_buffer + *response_len, content_buffer + i, n);
*response_len += n;
i += n;
response_buffer[(*response_len)++] = '\r';
response_buffer[(*response_len)++] = '\n';
}
/* the last chunk : "0\r\n" a empty body and then
* the final "\r\n" */
memcpy(response_buffer + *response_len, "0\r\n\r\n", 5);
*response_len += 5;
free(content_buffer);
printf("resp_length=%d buffer_length=%d content_length=%d\n",
*response_len, buffer_length, content_length);
return response_buffer;
}
/* favicon.ico generator */
#ifdef OLD_HEADER
#define FAVICON_LENGTH (6 + 16 + 12 + 8 + 32 * 4)
#else
#define FAVICON_LENGTH (6 + 16 + 40 + 8 + 32 * 4)
#endif
void build_favicon_content(char * p, int n)
{
int i;
if(n < FAVICON_LENGTH)
return;
/* header : 6 bytes */
*p++ = 0;
*p++ = 0;
*p++ = 1; /* type : ICO */
*p++ = 0;
*p++ = 1; /* number of images in file */
*p++ = 0;
/* image directory (1 entry) : 16 bytes */
*p++ = 16; /* width */
*p++ = 16; /* height */
*p++ = 2; /* number of colors in the palette. 0 = no palette */
*p++ = 0; /* reserved */
*p++ = 1; /* color planes */
*p++ = 0; /* " */
*p++ = 1; /* bpp */
*p++ = 0; /* " */
#ifdef OLD_HEADER
*p++ = 12 + 8 + 32 * 4; /* bmp size */
#else
*p++ = 40 + 8 + 32 * 4; /* bmp size */
#endif
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 6 + 16; /* bmp offset */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 0; /* " */
/* BMP */
#ifdef OLD_HEADER
/* BITMAPCOREHEADER */
*p++ = 12; /* size of this header */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 16; /* width */
*p++ = 0; /* " */
*p++ = 16 * 2; /* height x 2 ! */
*p++ = 0; /* " */
*p++ = 1; /* color planes */
*p++ = 0; /* " */
*p++ = 1; /* bpp */
*p++ = 0; /* " */
#else
/* BITMAPINFOHEADER */
*p++ = 40; /* size of this header */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 16; /* width */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 16 * 2; /* height x 2 ! */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 0; /* " */
*p++ = 1; /* color planes */
*p++ = 0; /* " */
*p++ = 1; /* bpp */
*p++ = 0; /* " */
/* compression method, image size, ppm x, ppm y */
/* colors in the palette ? */
/* important colors */
for(i = 4 * 6; i > 0; --i)
*p++ = 0;
#endif
/* palette */
*p++ = 0; /* b */
*p++ = 0; /* g */
*p++ = 0; /* r */
*p++ = 0; /* reserved */
*p++ = 255; /* b */
*p++ = 255; /* g */
*p++ = 255; /* r */
*p++ = 0; /* reserved */
/* pixel data */
for(i = 16; i > 0; --i) {
if(i & 1) {
*p++ = 0125;
*p++ = 0125;
} else {
*p++ = 0252;
*p++ = 0252;
}
*p++ = 0;
*p++ = 0;
}
/* Opacity MASK */
for(i = 16 * 4; i > 0; --i) {
*p++ = 0;
}
}
enum modes {
MODE_INVALID, MODE_CHUNKED, MODE_ADDCRAP, MODE_NORMAL, MODE_FAVICON
};
const struct {
const enum modes mode;
const char * text;
} modes_array[] = {
{MODE_CHUNKED, "chunked"},
{MODE_ADDCRAP, "addcrap"},
{MODE_NORMAL, "normal"},
{MODE_FAVICON, "favicon.ico"},
{MODE_INVALID, NULL}
};
/**
* write the response with random behaviour !
*/
void send_response(int c, const char * buffer, int len)
{
int n;
while(len > 0) {
n = (rand() % 99) + 1;
if(n > len)
n = len;
n = write(c, buffer, n);
if(n < 0) {
if(errno != EINTR) {
perror("write");
return;
}
/* if errno == EINTR, try again */
} else {
len -= n;
buffer += n;
}
usleep(10000); /* 10ms */
}
}
/**
* handle the HTTP connection
*/
void handle_http_connection(int c)
{
char request_buffer[2048];
int request_len = 0;
int headers_found = 0;
int n, i;
char request_method[16];
char request_uri[256];
char http_version[16];
char * p;
char * response_buffer;
int response_len;
enum modes mode;
int content_length = 16*1024;
/* read the request */
while(request_len < (int)sizeof(request_buffer) && !headers_found) {
n = read(c,
request_buffer + request_len,
sizeof(request_buffer) - request_len);
if(n < 0) {
if(errno == EINTR)
continue;
perror("read");
return;
} else if(n==0) {
/* remote host closed the connection */
break;
} else {
request_len += n;
for(i = 0; i < request_len - 3; i++) {
if(0 == memcmp(request_buffer + i, "\r\n\r\n", 4)) {
/* found the end of headers */
headers_found = 1;
break;
}
}
}
}
if(!headers_found) {
/* error */
printf("no HTTP header found in the request\n");
return;
}
printf("headers :\n%.*s", request_len, request_buffer);
/* the request have been received, now parse the request line */
p = request_buffer;
for(i = 0; i < (int)sizeof(request_method) - 1; i++) {
if(*p == ' ' || *p == '\r')
break;
request_method[i] = *p;
++p;
}
request_method[i] = '\0';
while(*p == ' ')
p++;
for(i = 0; i < (int)sizeof(request_uri) - 1; i++) {
if(*p == ' ' || *p == '\r')
break;
request_uri[i] = *p;
++p;
}
request_uri[i] = '\0';
while(*p == ' ')
p++;
for(i = 0; i < (int)sizeof(http_version) - 1; i++) {
if(*p == ' ' || *p == '\r')
break;
http_version[i] = *p;
++p;
}
http_version[i] = '\0';
printf("Method = %s, URI = %s, %s\n",
request_method, request_uri, http_version);
/* check if the request method is allowed */
if(0 != strcmp(request_method, "GET")) {
const char response405[] = "HTTP/1.1 405 Method Not Allowed\r\n"
"Allow: GET\r\n\r\n";
const char * pc;
/* 405 Method Not Allowed */
/* The response MUST include an Allow header containing a list
* of valid methods for the requested resource. */
n = sizeof(response405) - 1;
pc = response405;
while(n > 0) {
i = write(c, pc, n);
if(i<0) {
if(errno != EINTR) {
perror("write");
return;
}
} else {
n -= i;
pc += i;
}
}
return;
}
mode = MODE_INVALID;
/* use the request URI to know what to do */
for(i = 0; modes_array[i].mode != MODE_INVALID; i++) {
if(strstr(request_uri, modes_array[i].text)) {
mode = modes_array[i].mode; /* found */
break;
}
}
switch(mode) {
case MODE_CHUNKED:
response_buffer = build_chunked_response(content_length, &response_len);
break;
case MODE_ADDCRAP:
response_len = content_length+256;
response_buffer = malloc(response_len);
if(!response_buffer)
break;
n = snprintf(response_buffer, response_len,
"HTTP/1.1 200 OK\r\n"
"Server: minihttptestserver\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n"
"\r\n", content_length);
response_len = content_length+n+CRAP_LENGTH;
p = realloc(response_buffer, response_len);
if(p == NULL) {
/* error 500 */
free(response_buffer);
response_buffer = NULL;
break;
}
response_buffer = p;
build_content(response_buffer + n, content_length);
build_crap(response_buffer + n + content_length, CRAP_LENGTH);
break;
case MODE_FAVICON:
content_length = FAVICON_LENGTH;
response_len = content_length + 256;
response_buffer = malloc(response_len);
if(!response_buffer)
break;
n = snprintf(response_buffer, response_len,
"HTTP/1.1 200 OK\r\n"
"Server: minihttptestserver\r\n"
"Content-Type: image/vnd.microsoft.icon\r\n"
"Content-Length: %d\r\n"
"\r\n", content_length);
/* image/x-icon */
build_favicon_content(response_buffer + n, content_length);
response_len = content_length + n;
break;
default:
response_len = content_length+256;
response_buffer = malloc(response_len);
if(!response_buffer)
break;
n = snprintf(response_buffer, response_len,
"HTTP/1.1 200 OK\r\n"
"Server: minihttptestserver\r\n"
"Content-Type: text/plain\r\n"
"\r\n");
response_len = content_length+n;
p = realloc(response_buffer, response_len);
if(p == NULL) {
/* Error 500 */
free(response_buffer);
response_buffer = NULL;
break;
}
response_buffer = p;
build_content(response_buffer + n, response_len - n);
}
if(response_buffer) {
send_response(c, response_buffer, response_len);
free(response_buffer);
} else {
/* Error 500 */
}
}
/**
*/
int main(int argc, char * * argv) {
int ipv6 = 0;
int s, c, i;
unsigned short port = 0;
struct sockaddr_storage server_addr;
socklen_t server_addrlen;
struct sockaddr_storage client_addr;
socklen_t client_addrlen;
pid_t pid;
int child = 0;
int status;
const char * expected_file_name = NULL;
struct sigaction sa;
for(i = 1; i < argc; i++) {
if(argv[i][0] == '-') {
switch(argv[i][1]) {
case '6':
ipv6 = 1;
break;
case 'e':
/* write expected file ! */
expected_file_name = argv[++i];
break;
case 'p':
/* port */
if(++i < argc) {
port = (unsigned short)atoi(argv[i]);
}
break;
default:
fprintf(stderr, "unknown command line switch '%s'\n", argv[i]);
}
} else {
fprintf(stderr, "unknown command line argument '%s'\n", argv[i]);
}
}
srand(time(NULL));
memset(&sa, 0, sizeof(struct sigaction));
/*signal(SIGCHLD, handle_signal_chld);*/
sa.sa_handler = handle_signal_chld;
if(sigaction(SIGCHLD, &sa, NULL) < 0) {
perror("sigaction");
return 1;
}
/*signal(SIGINT, handle_signal_int);*/
sa.sa_handler = handle_signal_int;
if(sigaction(SIGINT, &sa, NULL) < 0) {
perror("sigaction");
return 1;
}
s = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
if(s < 0) {
perror("socket");
return 1;
}
memset(&server_addr, 0, sizeof(struct sockaddr_storage));
memset(&client_addr, 0, sizeof(struct sockaddr_storage));
if(ipv6) {
struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&server_addr;
addr->sin6_family = AF_INET6;
addr->sin6_port = htons(port);
addr->sin6_addr = in6addr_loopback;
} else {
struct sockaddr_in * addr = (struct sockaddr_in *)&server_addr;
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
}
if(bind(s, (struct sockaddr *)&server_addr,
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) < 0) {
perror("bind");
return 1;
}
if(listen(s, 5) < 0) {
perror("listen");
}
if(port == 0) {
server_addrlen = sizeof(struct sockaddr_storage);
if(getsockname(s, (struct sockaddr *)&server_addr, &server_addrlen) < 0) {
perror("getsockname");
return 1;
}
if(ipv6) {
struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&server_addr;
port = ntohs(addr->sin6_port);
} else {
struct sockaddr_in * addr = (struct sockaddr_in *)&server_addr;
port = ntohs(addr->sin_port);
}
printf("Listening on port %hu\n", port);
fflush(stdout);
}
/* write expected file */
if(expected_file_name) {
FILE * f;
f = fopen(expected_file_name, "wb");
if(f) {
char * buffer;
buffer = malloc(16*1024);
if(buffer == NULL) {
fprintf(stderr, "memory allocation error\n");
} else {
build_content(buffer, 16*1024);
i = fwrite(buffer, 1, 16*1024, f);
if(i != 16*1024) {
fprintf(stderr, "error writing to file %s : %dbytes written (out of %d)\n", expected_file_name, i, 16*1024);
}
free(buffer);
}
fclose(f);
} else {
fprintf(stderr, "error opening file %s for writing\n", expected_file_name);
}
}
/* fork() loop */
while(!child && !quit) {
while(child_to_wait_for > 0) {
pid = wait(&status);
if(pid < 0) {
perror("wait");
} else {
printf("child(%d) terminated with status %d\n", (int)pid, status);
}
--child_to_wait_for;
}
client_addrlen = sizeof(struct sockaddr_storage);
c = accept(s, (struct sockaddr *)&client_addr,
&client_addrlen);
if(c < 0) {
if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
continue;
perror("accept");
return 1;
}
printf("accept...\n");
pid = fork();
if(pid < 0) {
perror("fork");
return 1;
} else if(pid == 0) {
/* child */
child = 1;
close(s);
s = -1;
handle_http_connection(c);
}
close(c);
}
if(s >= 0) {
close(s);
s = -1;
}
if(!child) {
while(child_to_wait_for > 0) {
pid = wait(&status);
if(pid < 0) {
perror("wait");
} else {
printf("child(%d) terminated with status %d\n", (int)pid, status);
}
--child_to_wait_for;
}
printf("Bye...\n");
}
return 0;
}
|