-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dockerfile.full
1276 lines (1269 loc) · 39.7 KB
/
Dockerfile.full
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
FROM ubuntu as builder
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install --yes build-essential file gcc-multilib upx
COPY Dockerfile /etc/Dockerfile
RUN touch /etc/Dockerfile
RUN test -f /tmp/tinyweb.c || sed -e '/^## -CODE/,/## CODE-$/!d' /etc/Dockerfile | sed 's/^#//' | sed '1d;$d' > /tmp/tinyweb.c
RUN gcc -m32 --static -o /usr/local/bin/tinyweb /tmp/tinyweb.c && upx -9f /usr/local/bin/tinyweb && /usr/local/bin/tinyweb -h && file /usr/local/bin/tinyweb | grep ELF
FROM alpine
COPY --from=builder /usr/local/bin/tinyweb /usr/local/bin/tinyweb
RUN chmod +x /usr/local/bin/tinyweb ; /usr/local/bin/tinyweb -h
EXPOSE 80
WORKDIR /www
RUN echo '<html><head><title>It works!</title></head><body>It works!</body></html>' > /www/index.html
RUN printf '#!/bin/sh\necho "TINYWEB_PORT=${TINYWEB_PORT}"\nPORT=${TINYWEB_PORT:-80}\nif [ ! -d /www ] ; then\n mkdir /www\nfi\n/usr/local/bin/tinyweb /www ${PORT}\n' > /entrypoint.sh && chmod +x /entrypoint.sh
ENTRYPOINT [ "/entrypoint.sh" ]
# docker build . -t tinyweb
# docker run --rm --detach --publish 8080:80 --name tinyweb tinyweb
## -CODE
##include <signal.h>
##include <dirent.h>
##include <errno.h>
##include <fcntl.h>
##include <time.h>
##include <stdio.h>
##include <stdlib.h>
##include <string.h>
##include <sys/stat.h>
##include <sys/types.h>
##include <unistd.h>
##include <ctype.h>
##include <limits.h>
#
##ifdef WIN32
##include <winsock2.h>
##include <ws2tcpip.h>
##define random rand
##else
##include <arpa/inet.h> /* inet_ntoa */
##include <netinet/in.h>
##include <netinet/tcp.h>
##include <sys/socket.h>
##include <sys/wait.h>
##endif
#
##ifndef NO_SENDFILE
##include <sys/sendfile.h>
##endif
#
##ifndef TCP_CORK
##define TCP_CORK 3
##endif
#
##define DEFAULT_PORT 9999
#
##define LISTENQ 1024 /* second argument to listen() */
##define MAXLINE 1024 /* max length of a line */
##define RIO_BUFSIZE 1024
#
##ifndef MAX_PATH
##define MAX_PATH 2048
##endif
#
#int debug_flag = 0 ; /* Debug mode flag */
#
#typedef struct {
# int rio_fd; /* descriptor for this buf */
# int rio_cnt; /* unread byte in this buf */
# char *rio_bufptr; /* next unread byte in this buf */
# char rio_buf[RIO_BUFSIZE]; /* internal buffer */
#} rio_t;
#
#/* Simplifies calls to bind(), connect(), and accept() */
#typedef struct sockaddr SA;
#
#typedef struct {
# char filename[MAX_PATH];
# char method[10];
# char uri[512];
# off_t offset; /* for support Range */
# size_t end;
# size_t length;
# char query[512];
# char auth[50];
# char type[50];
# char host[50];
# char vhost[50];
# size_t bodylen;
# char body[MAXLINE];
#} http_request;
#
#void print_http_request( http_request * r ) {
# printf("----\n");
# printf("#%s#\n",r->filename) ;
# printf("#%s#\n",r->method) ;
# printf("#%s#\n",r->uri) ;
# printf("#%ld#\n",r->offset) ;
##if defined __x86_64__
# printf("#%ld#\n",r->end) ;
# printf("#%ld#\n",r->length) ;
##else
# printf("#%d#\n",r->end) ;
# printf("#%d#\n",r->length) ;
##endif
# printf("#%s#\n",r->query) ;
# printf("#%s#\n",r->auth) ;
# printf("#%s#\n",r->type) ;
# printf("#%s#\n",r->host) ;
# printf("#%s#\n",r->vhost) ;
##if defined __x86_64__
# printf("#%ld#\n",r->bodylen) ;
##else
# printf("#%d#\n",r->bodylen) ;
##endif
# printf("#%s#\n",r->body) ;
#}
#
#typedef struct {
# const char *extension;
# const char *mime_type;
# const int expiration;
#} mime_map;
#
#mime_map mime_types [] = {
# {".htm", "text/html", 2},
# {".html", "text/html", 2},
# {".css", "text/css", 2},
# {".js", "text/javascript", 2},
# {".gif", "image/gif", 10},
# {".jpeg", "image/jpeg", 10},
# {".jpg", "image/jpeg", 10},
# {".png", "image/png", 10},
# {".ico", "image/x-icon", 10},
# {".svg", "image/svg+xml", 10},
# {".pdf", "application/pdf", 10},
# {".mp3", "audio/mpeg", 30},
# {".mp4", "video/mp4", 30},
# {".txt", "text/plain", 2},
# {".log", "text/plain", 2},
# {".xml", "text/xml", 2},
# {".yaml", "text/x-yaml", 2},
# {".yml", "text/x-yaml", 2},
# {".json", "application/json", 0},
# {NULL, NULL},
#};
#
#//char *default_mime_type = "text/plain";
#char *default_mime_type = "application/octet-stream";
#
#char *default_index_file = "index.html";
#
#char *server_software = "Tinyweb 1.0";
#
#char tmp_dir[256]="." ;
#
#int nb_forks = 10 ;
#
#char *dynamic_shell = "" ;
#char *dynamic_cat = "cat" ;
#
#char logtime[50] = "" ;
#char * logt() {
# time_t t = time(NULL);
# strftime(logtime,sizeof(logtime),"%a, %d %b %Y %T %z",localtime(&t));
# return logtime;
#}
#
#void rio_readinitb(rio_t *rp, int fd){
# rp->rio_fd = fd;
# rp->rio_cnt = 0;
# rp->rio_bufptr = rp->rio_buf;
#}
#
#ssize_t writen(int fd, void *usrbuf, size_t n){
# size_t nleft = n;
# ssize_t nwritten;
# char *bufp = usrbuf;
#
# while (nleft > 0){
##ifdef WIN32
# if ((nwritten = send(fd, bufp, nleft, 0)) <= 0){
##else
# if ((nwritten = write(fd, bufp, nleft)) <= 0){
##endif
# if (errno == EINTR) /* interrupted by sig handler return */
# nwritten = 0; /* and call write() again */
# else
# return -1; /* errorno set by write() */
# }
# nleft -= nwritten;
# bufp += nwritten;
# }
# return n;
#}
#
#
#/*
# * rio_read - This is a wrapper for the Unix read() function that
# * transfers min(n, rio_cnt) bytes from an internal buffer to a user
# * buffer, where n is the number of bytes requested by the user and
# * rio_cnt is the number of unread bytes in the internal buffer. On
# * entry, rio_read() refills the internal buffer via a call to
# * read() if the internal buffer is empty.
# */
#/* $begin rio_read */
#static ssize_t rio_read(rio_t *rp, char *usrbuf, size_t n){
# int cnt,i;
# while (rp->rio_cnt <= 0){ /* refill if buf is empty */
##ifdef WIN32
# rp->rio_cnt = recv(rp->rio_fd, rp->rio_buf, sizeof(rp->rio_buf), 0);
##else
# rp->rio_cnt = read(rp->rio_fd, rp->rio_buf, sizeof(rp->rio_buf));
##endif
# if( debug_flag && (rp->rio_cnt>0) ) { for(i=0;i<rp->rio_cnt;i++) printf( "%c", rp->rio_buf[i] ) ; }
# if (rp->rio_cnt < 0) {
# //if (errno == EBADF ) printf("Bad file descriptor !\n");
# if (errno != EINTR) /* interrupted by sig handler return */
# return -1;
# }
# else if (rp->rio_cnt == 0) /* EOF */
# return 0;
# else {
# rp->rio_bufptr = rp->rio_buf; /* reset buffer ptr */
# //printf("==>%s<==\n",rp->rio_buf);
# }
# }
#
# /* Copy min(n, rp->rio_cnt) bytes from internal buf to user buf */
# cnt = n;
# if (rp->rio_cnt < n)
# cnt = rp->rio_cnt;
# memcpy(usrbuf, rp->rio_bufptr, cnt);
# rp->rio_bufptr += cnt;
# rp->rio_cnt -= cnt;
# return cnt;
#}
#
#/*
# * rio_readlineb - robustly read a text line (buffered)
# */
#ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen){
# int n, rc;
# char c, *bufp = usrbuf;
#
# for (n = 1; n < maxlen; n++){
# if ((rc = rio_read(rp, &c, 1)) == 1){
# *bufp++ = c;
# if (c == '\n')
# break;
# } else if (rc == 0){
# if (n == 1)
# return 0; /* EOF, no data read */
# else
# break; /* EOF, some data was read */
# } else
# return -1; /* error */
# }
# *bufp = 0;
# return n;
#}
#
#/* The stristr() function finds the first occurrence of the substring needle in
# the string haystack, ignores the case of both arguments.*/
#char* stristr( const char* str1, const char* str2 ) {
# const char* p1 = str1 ;
# const char* p2 = str2 ;
# const char* r = *p2 == 0 ? str1 : 0 ;
#
# while( *p1 != 0 && *p2 != 0 ) {
# if( tolower( (unsigned char)*p1 ) == tolower( (unsigned char)*p2 ) ) {
# if( r == 0 ) { r = p1 ; }
# p2++ ;
# } else {
# p2 = str2 ;
# if( r != 0 ) { p1 = r + 1 ; }
# if( tolower( (unsigned char)*p1 ) == tolower( (unsigned char)*p2 ) ) {
# r = p1 ;
# p2++ ;
# } else { r = 0 ; }
# }
# p1++ ;
# }
# return *p2 == 0 ? (char*)r : 0 ;
#}
#
#void format_size(char* buf, struct stat *stat){
# if(S_ISDIR(stat->st_mode)){
# sprintf(buf, "%s", "[DIR]");
# } else {
# off_t size = stat->st_size;
# if(size < 1024){
# sprintf(buf, "%lu", size);
# } else if (size < 1024 * 1024){
# sprintf(buf, "%.1fK", (double)size / 1024);
# } else if (size < 1024 * 1024 * 1024){
# sprintf(buf, "%.1fM", (double)size / 1024 / 1024);
# } else {
# sprintf(buf, "%.1fG", (double)size / 1024 / 1024 / 1024);
# }
# }
#}
#
##ifdef WIN32
##include <windows.h>
##include <tchar.h>
##include <stdio.h>
##define BUFSIZE MAX_PATH
#int dirfd(DIR *dirp) { }
#/*
#DWORD GetFinalPathNameByHandleA(HANDLE hFile,LPSTR lpszFilePath,DWORD cchFilePath,DWORD dwFlags);
#int getfilename(HANDLE hFile, char* filename, int size) {
# return GetFinalPathNameByHandleA(hFile,filename,size,0) ;
#}
#*/
#DIR *fdopendir(int fd) {
# char filename[MAX_PATH]="";
#// getfilename( (void*)_get_osfhandle(fd),filename,MAX_PATH);
# return opendir(filename);
#}
#int openat(int fd, const char *pathname, int flags) {
# char filename[MAX_PATH]="";
#// getfilename( (void*)_get_osfhandle(fd),filename,MAX_PATH);
# if( (pathname[0]!='/') && (filename[strlen(filename)-1]!='/') ) { strcat(filename,"/"); }
# strcat( filename, pathname ) ;
# return open(filename,flags);
#}
##ifndef NO_ENV
#int setenv(const char *name, const char *value, int overwrite) {
# char *buf;
# int ret;
# buf = (char*)malloc( strlen(name)+strlen(value)+2 ) ;
# sprintf( buf, "%s=%s", name, value ) ;
# ret = _putenv( buf ) ;
# free( buf ) ;
# return ret ;
#}
#int unsetenv(const char *name) { return _putenv( name ) ; }
##endif
##endif
#
#void handle_directory_redirect(int out_fd, char *filename, char *redirect) {
# char buf[MAXLINE];
# sprintf(buf, "HTTP/1.1 302 Found\r\nLocation: ");
# if( (filename[0]!='.')&&(filename[0]!='/') ) { strcat(buf,"/" ); }
# if( strcmp(filename,".") ) { strcat(buf, filename); }
# strcat(buf, redirect);
# strcat(buf, "\r\n\r\n");
# writen(out_fd, buf, strlen(buf));
#}
#
#void handle_directory_request(int out_fd, DIR*dir, int dir_fd, char *filename){
# char buf[MAXLINE], m_time[32], size[16];
# struct stat statbuf;
# if( debug_flag ) { printf("Browsing directory %s\n",filename); }
# sprintf(buf, "HTTP/1.1 200 OK\r\n%s%s%s%s%s%s%s",
# "Expires: 0\r\nContent-Type: text/html\r\n\r\n",
# "<html><head><meta charset=\"utf-8\"><title>directory: ",filename,
# "</title><style>",
# "body{font-family: monospace; font-size: 13px;}",
# "td {padding: 1.5px 6px;}",
# "</style></head><body><table>\n" );
# writen(out_fd, buf, strlen(buf));
#
# DIR *d ;
# if( dir==NULL ) {
# d = (DIR*)fdopendir(dir_fd);
# } else {
# d = dir ;
# dir_fd = dirfd(d) ;
# }
# struct dirent *dp;
# int ffd;
# while ((dp = readdir(d)) != NULL){
# if(!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")){
# continue;
# }
##ifdef WIN32
# sprintf(buf, "<tr><td><a href=\"/%s/%s\">%s</a></td></tr>\n",
# filename,dp->d_name, dp->d_name );
# writen(out_fd, buf, strlen(buf));
##else
# if ((ffd = openat(dir_fd, dp->d_name, O_RDONLY)) == -1){
# perror(dp->d_name);
# continue;
# }
# fstat(ffd, &statbuf);
# strftime(m_time, sizeof(m_time),
# "%Y-%m-%d %H:%M", localtime(&statbuf.st_mtime));
# format_size(size, &statbuf);
# if(S_ISREG(statbuf.st_mode) || S_ISDIR(statbuf.st_mode)){
# char *d = S_ISDIR(statbuf.st_mode) ? "/" : "";
# sprintf(buf, "<tr><td><a href=\"%s%s\">%s%s</a></td><td>%s</td><td>%s</td></tr>\n",
# dp->d_name, d, dp->d_name, d, m_time, size);
# writen(out_fd, buf, strlen(buf));
# }
# close(ffd);
##endif
# }
# sprintf(buf, "</table></body></html>");
# writen(out_fd, buf, strlen(buf));
# closedir(d);
#}
#
#static const int get_expiration(char *filename){
# char *dot = strrchr(filename, '.');
# if(dot){ // strrchar Locate last occurrence of character in string
# mime_map *map = mime_types;
# while(map->extension){
# if(strcmp(map->extension, dot) == 0){
# return map->expiration;
# }
# map++;
# }
# }
# return 0;
#}
#
#static const char* get_mime_type(char *filename){
# char *dot = strrchr(filename, '.');
# if(dot){ // strrchar Locate last occurrence of character in string
# mime_map *map = mime_types;
# while(map->extension){
# if(strcmp(map->extension, dot) == 0){
# return map->mime_type;
# }
# map++;
# }
# }
# return default_mime_type;
#}
#
#
#int open_listenfd(int port){
# int listenfd, optval=1;
# struct sockaddr_in serveraddr;
#
# /* Create a socket descriptor */
# if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
# return -1;
#
##ifndef WIN32
# /* Eliminates "Address already in use" error from bind. */
# if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
# (const void *)&optval , sizeof(int)) < 0)
# return -1;
#
# // 6 is TCP's protocol number
# // enable this, much faster : 4000 req/s -> 17000 req/s
# if (setsockopt(listenfd, 6, TCP_CORK,
# (const void *)&optval , sizeof(int)) < 0)
# return -1;
##endif
#
# /* Listenfd will be an endpoint for all requests to port
# on any IP address for this host */
# memset(&serveraddr, 0, sizeof(serveraddr));
# serveraddr.sin_family = AF_INET;
# serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
# serveraddr.sin_port = htons((unsigned short)port);
#
# if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0)
# return -1;
#
# /* Make it a listening socket ready to accept connection requests */
# if (listen(listenfd, LISTENQ) < 0)
# return -1;
# return listenfd;
#}
#
#void url_decode(char* src, char* dest, int max) {
# char *p = src;
# char code[3] = { 0 };
# while(*p && --max) {
# if(*p == '%') {
# memcpy(code, ++p, 2);
# *dest++ = (char)strtoul(code, NULL, 16);
# p += 2;
# } else {
# *dest++ = *p++;
# }
# }
# *dest = '\0';
#}
#
#int parse_request(int fd, http_request *req){
# rio_t rio;
# char buf[MAXLINE], method[10], uri[MAXLINE], *pst;
# req->offset = 0;
# req->end = 0; /* default */
# req->query[0] = '\0';
# req->uri[0] = '\0';
# req->host[0] = '\0';
# req->vhost[0] = '\0';
# req->auth[0] = '\0';
# req->type[0] = '\0';
# req->length = 0;
# req->bodylen=0;
# req->body[0] = '\0';
# req->filename[0] = '\0';
#
# rio_readinitb(&rio, fd);
# rio_readlineb(&rio, buf, MAXLINE); if( (strlen(buf)<=2)||(buf[strlen(buf)-1]!='\n') ) return 0;
#
# sscanf(buf, "%s %s", method, uri); /* version is not cared */
# strcpy(req->method, method);
# /* read all */
# while(buf[0] != '\n' && buf[1] != '\n') { /* \n || \r\n */
# rio_readlineb(&rio, buf, MAXLINE);
# //if(buf[0] == 'R' && buf[1] == 'a' && buf[2] == 'n'){
# if( stristr(buf,"Range: ")==buf ) {
##if defined __x86_64__
# sscanf(buf, "Range: bytes=%lu-%lu", &req->offset, &req->end);
##else
# sscanf(buf, "Range: bytes=%lu-%u", &req->offset, &req->end);
##endif
# // Range: [start, end]
# if( req->end != 0) req->end ++;
# } else if( stristr(buf,"Host: ")==buf ) {
# sscanf(buf+6, "%s", &(req->host[0]) );
# strcpy(req->vhost,req->host);
# if( (pst=strstr(req->vhost,":"))!=NULL ) { pst[0]='\0'; }
# } else if( stristr(buf, "Content-length: ")==buf ) {
##if defined __x86_64__
# sscanf(buf+16, "%lu", &req->length );
##else
# sscanf(buf+16, "%u", &req->length );
##endif
# } else if( stristr(buf, "Authorization: ")==buf ) {
# sscanf(buf+15, "%[a-zA-Z0-9=+/ ]", &(req->auth[0]) );
# } else if( stristr(buf, "Content-type: ")==buf ) {
# sscanf(buf+14, "%s", &(req->type[0]) );
# } else if( stristr(buf, "Expect: 100-continue")==buf ) {
# writen(fd, "HTTP/1.1 100 Continue\r\n\r\n", 25);
# }
# }
# if( rio.rio_cnt>0 ) {
# req->bodylen=rio.rio_cnt;
# memcpy(req->body,rio.rio_bufptr,rio.rio_cnt);
# }
# if( uri[0]!='/' ) { uri[0]='/'; url_decode(uri, (req->uri)+1, MAXLINE); }
# else { url_decode(uri, req->uri, MAXLINE); }
# int i;
# for (i = 0; i < strlen(req->uri); i++) { if( req->uri[i]=='?' ) { req->uri[i]='\0'; break; } }
# char* filename = uri;
# if(uri[0] == '/'){
# filename = uri + 1;
# int length = strlen(filename);
# if (length == 0){
# filename = ".";
# } else {
# for (i = 0; i < length; ++ i) {
# if (filename[i] == '?') {
# url_decode(filename+i+1 , req->query, MAXLINE);
# filename[i] = '\0';
# break;
# }
# }
# }
# }
# url_decode(filename, req->filename, MAXLINE);
# return 1;
#}
#
#
#void log_access(int status, struct sockaddr_in *c_addr, http_request *req) {
##if defined __x86_64__
# printf("[%d][%s] %s:%d %d - %s %s/%s (%lu) ? %s\n", getpid(), logt(), inet_ntoa(c_addr->sin_addr),
# ntohs(c_addr->sin_port), status, req->method, req->host, req->filename, req->length, req->query);
##else
# printf("[%d][%s] %s:%d %d - %s %s/%s (%u) ? %s\n", getpid(), logt(), inet_ntoa(c_addr->sin_addr),
# ntohs(c_addr->sin_port), status, req->method, req->host, req->filename, req->length, req->query);
##endif
#}
#
#void client_error(int fd, int status, char *msg, char *headers, char *longmsg){
# char buf[MAXLINE];
# sprintf(buf, "HTTP/1.1 %d %s\r\n", status, msg);
# if(headers!=NULL) if(strlen(headers)>0) { sprintf(buf + strlen(buf), "%s", headers); }
# if( status==204 ) {
# sprintf(buf + strlen(buf), "Content-length: 0\r\n\r\n");
# } else {
# if( (longmsg!=NULL) && (strlen(longmsg)>0) ){
##if defined __x86_64__
# sprintf(buf + strlen(buf),
# "Content-length: %lu\r\n\r\n", strlen(longmsg));
##else
# sprintf(buf + strlen(buf),
# "Content-length: %u\r\n\r\n", strlen(longmsg));
##endif
# sprintf(buf + strlen(buf), "%s", longmsg);
# } else { sprintf(buf + strlen(buf), "Content-length: 0\r\n\r\n");}
# }
# writen(fd, buf, strlen(buf));
#}
#
#char *psttemp=NULL;
#char * mmktemp(char *template) {
# if(psttemp!=NULL) { free(psttemp); } psttemp=NULL;
# psttemp=(char*)malloc(256);psttemp[0]='\0';
# sprintf(psttemp,"%s/%s%ld",tmp_dir,template,random());
# return psttemp;
#}
#
#int write_file(int fd, http_request *req, char * filename) {
# char buf[512];
# int put_fd, n;
# ssize_t size=0;
# if( (put_fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR ))==-1 ) {
# return 500;
# } else {
# if( req->bodylen>0 ) { write(put_fd,req->body,req->bodylen) ; } /* Is there some data into buffer */
# fd_set fds;
# struct timeval timeout; timeout.tv_sec=2; timeout.tv_usec=0;
# FD_ZERO(&fds);
# FD_SET(fd, &fds);
# if( select(fd+1, &fds, 0, 0, &timeout)==1 ) { /* there is data available into socket */
##ifdef WIN32
# while( (n=recv(fd,buf,512,0))>0 ) {
##else
# while( (n=read(fd,buf,512))>0 ) {
##endif
# write(put_fd,buf,n);
# size+=n;
# if( select(fd+1, &fds, 0, 0, &timeout)!=1 ) break;
# }
##if defined __x86_64__
# printf("Writing %ld bytes into %s\n",size,req->filename);
##else
# printf("Writing %d bytes into %s\n",size,req->filename);
##endif
# close(put_fd);
# return 200;
# } else {
# close(put_fd);
# return 400;
# }
# close(put_fd);
# }
# return 500;
#}
#
##ifndef WIN32
##define READ 0
##define WRITE 1
#FILE * popen2(const char * command, const char * type, int * pid) {
# pid_t child_pid;
# int fd[2];
# pipe(fd);
#
# if((child_pid = fork()) == -1) {
# perror("fork");
# exit(1);
# }
#
# /* child process */
# if (child_pid == 0) {
# if (type == "r") {
# close(fd[READ]); //Close the READ end of the pipe since the child's fd is write-only
# dup2(fd[WRITE], 1); //Redirect stdout to pipe
# } else {
# close(fd[WRITE]); //Close the WRITE end of the pipe since the child's fd is read-only
# dup2(fd[READ], 0); //Redirect stdin to pipe
# }
# setpgid(child_pid, child_pid); //Needed so negative PIDs can kill children of /bin/sh
# //execl("/bin/sh", "/bin/sh", "-c", command.c_str(), NULL);
# execl("/bin/sh", "/bin/sh", "-c", command, "2>&1", NULL);
# exit(0);
# } else {
# if (type == "r") {
# close(fd[WRITE]); //Close the WRITE end of the pipe since parent's fd is read-only
# } else {
# close(fd[READ]); //Close the READ end of the pipe since parent's fd is write-only
# }
# }
#
# *pid = child_pid;
#
# if (type == "r") {
# return fdopen(fd[READ], "r");
# }
#
# return fdopen(fd[WRITE], "w");
#}
#
#int pclose2(FILE * fp, pid_t pid) {
# int stat;
#
# kill(-pid, 9);
# fclose(fp);
# while (waitpid(pid, &stat, 0) == -1) {
# if (errno != EINTR) {
# stat = -1;
# break;
# }
# }
# return stat;
#}
##endif
#
##define BUF_SIZE 512
#int serve_dynamic(int out_fd, http_request *req ) {
#/*# Exemple de shell dynamic
##!/bin/bash
#echo "Content-type: text/plain"
#echo
#echo "On est dans un shell: "
#echo "QUERY_STRING=${QUERY_STRING}"
#echo "REQUEST_METHOD=${REQUEST_METHOD}"
#echo "REQUEST_URI=${REQUEST_URI}"
#echo "DOCUMENT_ROOT=${DOCUMENT_ROOT}"
#echo "HTTP_HOST=${HTTP_HOST}"
#echo "SCRIPT_FILENAME=${SCRIPT_FILENAME}"
#echo "SERVER_SOFTWARE=${SERVER_SOFTWARE}"
#echo "SCRIPT_NAME=${SCRIPT_NAME}"
#echo "CONTENT_LENGTH=${CONTENT_LENGTH}"
#echo "CONTENT_TYPE=${CONTENT_TYPE}"
#*/
# char *cmd = NULL;
# char cwd[PATH_MAX];
# char buf[BUF_SIZE];
# FILE *fp ;
# int ret = 200 ;
# int pid;
#
# getcwd(cwd, sizeof(cwd));
#
# if( strlen(req->query)>0 ) { setenv("QUERY_STRING", req->query, 1); }
# if( strlen(req->method)>0 ) { setenv("REQUEST_METHOD", req->method, 1); }
# setenv("DOCUMENT_ROOT", cwd, 1);
# if( strlen(req->auth)>0 ) { setenv("HTTP_AUTHORIZATION", req->auth, 1); }
# if( strlen(req->host)>0 ) { setenv("HTTP_HOST", req->host, 1); }
#
# cmd = (char*)malloc(strlen(req->filename)+2);
# sprintf(cmd, "/%s", req->filename );
# if( strlen(req->filename)>0 ) { setenv("REQUEST_URI", cmd, 1); }
# free(cmd); cmd = NULL;
#
# cmd = (char*)malloc(strlen(cwd)+strlen(req->filename)+2);
# sprintf(cmd, "%s/%s", cwd, req->filename );
# setenv("SCRIPT_FILENAME", cmd, 1);
# free(cmd); cmd = NULL;
#
# setenv("SERVER_SOFTWARE", server_software, 1);
# setenv("CONTENT_TYPE", req->type, 1);
##if defined __x86_64__
# sprintf(buf,"%lu",req->length);
##else
# sprintf(buf,"%u",req->length);
##endif
# setenv("CONTENT_LENGTH", buf, 1);
# setenv("SCRIPT_NAME", req->uri, 1);
#
# char * tmpfilename=NULL;
# if( !strcmp(req->method,"POST") && (req->bodylen>0) ) {
# //tmpfilename = tmpnam(NULL) ;
# tmpfilename = mmktemp("tinyweb") ;
# if( tmpfilename!=NULL ) {
# int r = write_file(out_fd, req, tmpfilename);
# switch(r) {
# case 500: client_error(out_fd, 500, "Internal server error", NULL, "Unable to create temporary file.");
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif
# return 500; break;
# }
# if( strlen(dynamic_shell)>0 ) {
# cmd = (char*)malloc(strlen(dynamic_cat)+strlen(tmpfilename)+strlen(dynamic_shell)+strlen(cwd)+strlen(req->filename)+15) ;
# sprintf(cmd, "%s %s |%s \"%s/%s\" 2>&1", dynamic_cat, tmpfilename, dynamic_shell, cwd, req->filename) ;
# } else {
# cmd = (char*)malloc(strlen(dynamic_cat)+strlen(tmpfilename)+strlen(cwd)+strlen(req->filename)+15) ;
# sprintf(cmd, "%s %s | \"%s/%s\"", dynamic_cat, tmpfilename, cwd, req->filename) ;
# }
# } else {
# client_error(out_fd, 500, "Internal server error", NULL, "Unable to get temporary filename.");
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif
# return 500;
# }
# } else {
# if( strlen(dynamic_shell)>0 ) {
# cmd = (char*)malloc(strlen(dynamic_shell)+strlen(cwd)+strlen(req->filename)+15) ;
# sprintf(cmd, "%s \"%s/%s\" 2>&1", dynamic_shell, cwd, req->filename) ;
# } else {
# cmd = (char*)malloc(strlen(cwd)+strlen(req->filename)+15) ;
# sprintf(cmd, "\"%s/%s\"", cwd, req->filename) ;
# }
# }
#
#
##ifdef WIN32
# for( int i=0; i<strlen(cmd); i++) { if( cmd[i]=='/' ) { cmd[i]='\\' ; } }
#// for( int i=0; i<strlen(cmd); i++) { if( cmd[i]=='\\' ) { cmd[i]='/' ; } }
# fp = popen(cmd,"r");
# printf("running command %s\n",cmd);
##else
# fp = popen2(cmd,"r",&pid) ; printf("starting process %d\n",pid);
# printf("running command [/bin/sh -c] %s\n",cmd);
##endif
#
# int nb_read;
# if( fp!=NULL ) {
# if( !fgets(buf, BUF_SIZE, fp) ) {
# strcpy(buf,"HTTP/1.1 500 Internal server error\r\n");
# writen(out_fd, buf, strlen(buf));
# ret = 500 ;
# } else {
# if( stristr(buf,"Status:")==buf ) {
# writen(out_fd, "HTTP/1.1 ", 9);
# int i=8;
# while( buf[i]==' ' ) { i++; }
# writen(out_fd, buf+i, strlen(buf+i));
# ret = atoi(buf+i);
# } else {
# strcpy(buf,"HTTP/1.1 200 OK\r\n");
# }
# writen(out_fd, buf, strlen(buf));
# while( (nb_read=fread(buf,1,BUF_SIZE,fp))>0 ) {
# if( writen(out_fd, buf, nb_read)!=nb_read ) break ;
##ifndef WIN32
# fsync(out_fd);
##endif
# if( nb_read!=BUF_SIZE ) break;
# }
# }
# } else {
# strcpy(buf,"HTTP/1.1 500 Internal Server Error\r\n\r\nInternal Server Error");
# writen(out_fd, buf, strlen(buf));
# ret = 500 ;
# }
# if( tmpfilename!=NULL ) { unlink(tmpfilename) ;}
#
# if( cmd!=NULL ) {
# free(cmd) ;
# cmd=NULL ;
# }
#
##ifdef WIN32
# pclose(fp) ;
##else
# printf("killing process %d\n",pid) ; pclose2(fp,pid) ;
##endif
#
# unsetenv("QUERY_STRING");
# unsetenv("REQUEST_METHOD");
# unsetenv("DOCUMENT_ROOT");
# unsetenv("HTTP_AUTHORIZATION");
# unsetenv("HTTP_HOST");
# unsetenv("REQUEST_URI");
# unsetenv("SCRIPT_FILENAME");
# unsetenv("SERVER_SOFTWARE");
# unsetenv("CONTENT_TYPE");
# unsetenv("CONTENT_LENGTH");
# unsetenv("SCRIPT_NAME");
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif
# return ret ;
#}
#
#void serve_static_get(int out_fd, int in_fd, http_request *req,
# size_t total_size, time_t last_change_time) {
# char buf[256];
# int expiration=0,i;
# long size=0;
# if (req->offset > 0){
# sprintf(buf, "HTTP/1.1 206 Partial\r\n");
##if defined __x86_64__
# sprintf(buf + strlen(buf), "Content-Range: bytes %lu-%lu/%lu\r\n",
# req->offset, req->end, total_size);
##else
# sprintf(buf + strlen(buf), "Content-Range: bytes %lu-%u/%u\r\n",
# req->offset, req->end, total_size);
##endif
# } else {
# sprintf(buf, "HTTP/1.1 200 OK\r\nAccept-Ranges: bytes\r\n");
# }
# char date[100];
# strftime(date, 100, "%a, %d %b %Y %H:%M:%S %Z", localtime(&(last_change_time)));
# sprintf(buf + strlen(buf), "Last-Modified: %s\r\n", date);
#
# time_t current_time = time(0) ;
# strftime(date, 100, "%a, %d %b %Y %H:%M:%S %Z", localtime(&(current_time)));
# sprintf(buf + strlen(buf), "Date: %s\r\n", date);
#
# expiration = get_expiration(req->filename);
# if( expiration>0 ) {
# sprintf(buf + strlen(buf), "Cache-Control: public, max-age=%d\r\n",60*expiration);
# } else {
# sprintf(buf + strlen(buf), "Cache-Control: no-cache\r\nExpires: 0\r\n");
# }
# // sprintf(buf + strlen(buf), "Cache-Control: public, max-age=315360000\r\nExpires: Thu, 31 Dec 2037 23:55:55 GMT\r\n");
#
# sprintf(buf + strlen(buf), "Content-type: %s\r\n",
# get_mime_type(req->filename));
#
# if( strcmp(req->method,"HEAD") ) {
# sprintf(buf + strlen(buf), "Content-length: %lu\r\n\r\n",
# req->end - req->offset);
#
# writen(out_fd, buf, strlen(buf));
# off_t offset = req->offset; /* copy */
# while(offset < req->end){
##ifndef NO_SENDFILE
# if(sendfile(out_fd, in_fd, &offset, req->end - req->offset) <= 0) {
# break;
# }
##else
# int n;
#
# if( offset>0 ) {
# if( (int)(offset/256) > 0 ) for( i=0 ; i<(int)(offset/256) ; i++ ) {
# if( read(in_fd,buf,256)<0 ) { client_error(out_fd, 500, "Internal server error", "", "Bad offset") ; close(out_fd) ; return ; }
# }
# if( (offset%256)>0 ) { read(in_fd,buf,offset%256) ; }
# }
# while( (n=read(in_fd,buf,256))>0 ) {
##ifdef WIN32
# n = send(out_fd,buf,n,0);
##else
# write(out_fd,buf,n);
##endif
# size += n ;
# if(n!=256) break ;
# }
##endif
# if( debug_flag ) printf("[%d][%s] offset: %d size:%ld\n", getpid(), logt(), (int)offset, size);
#
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif
# break;
# }
# } else {
# sprintf(buf + strlen(buf), "\r\n" ) ;
# writen(out_fd, buf, strlen(buf));
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif
# }
#}
#
#void serve_static_put(int out_fd, http_request *req) { // curl -X PUT -H "Expect:" http://localhost:9999/1.txt --upload-file 1.txt --basic -u username:password
# if( getenv("TINYWEB_AUTH")!=NULL ) {
# if( (strlen(req->auth)==0) || strcmp(req->auth,getenv("TINYWEB_AUTH")) ) {
# client_error(out_fd, 401, "Unauthorized", "WWW-Authenticate: Basic realm=\"Enter credentials\"", "Unauthorized") ;
# close(out_fd) ;
# return ;
# }
# }
# int r = write_file( out_fd, req, req->filename ) ;
# switch( r ) {
# case 200: client_error(out_fd, 200, "OK", NULL, "File created"); break;
# case 400: client_error(out_fd, 400, "Bad request", NULL, "No data found");break;
# case 500:
# default: client_error(out_fd, 500, "Internal server error", NULL, "Internal server error: unable to create file");
#
# }
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif
#}
#
#void serve_static_delete(int out_fd, http_request *req) { // curl -X DELETE http://localhost:9999/1.txt --basic -u username:password
# if( getenv("TINYWEB_AUTH")!=NULL ) {
# if( (strlen(req->auth)==0) || strcmp(req->auth,getenv("TINYWEB_AUTH")) ) {
# client_error(out_fd, 401, "Unauthorized", "WWW-Authenticate: Basic realm=\"Enter credentials\"", "Unauthorized") ;
# close(out_fd) ;
# return ;
# }
# }
# if( remove( req->filename )==0 ) {
# printf("File %s removed !\n",req->filename);
# client_error(out_fd, 204, "No content", NULL, NULL);
# } else {
# client_error(out_fd, 500, "Internal server error", NULL, "Internal server error: unable to remove file");
# }
##ifdef WIN32
# closesocket(out_fd);
##else
# close(out_fd);
##endif