0000: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 65 #include <sys/se
0010: 6e 64 66 69 6c 65 2e 68 3e 0a 23 69 6e 63 6c 75 ndfile.h>.#inclu
0020: 64 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 de <sys/socket.h
0030: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f >.#include <sys/
0040: 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 types.h>.#includ
0050: 65 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e 0a e <arpa/inet.h>.
0060: 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 6d 6d #include <sys/mm
0070: 61 6e 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c an.h>.#include <
0080: 73 79 73 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 sys/stat.h>.#inc
0090: 6c 75 64 65 20 3c 73 79 73 2f 77 61 69 74 2e 68 lude <sys/wait.h
00a0: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 >.#include <pthr
00b0: 65 61 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ead.h>.#include
00c0: 3c 73 74 72 69 6e 67 73 2e 68 3e 0a 23 69 6e 63 <strings.h>.#inc
00d0: 6c 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a lude <signal.h>.
00e0: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 #include <stdlib
00f0: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e .h>.#include <un
0100: 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 istd.h>.#include
0110: 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 <string.h>.#inc
0120: 6c 75 64 65 20 3c 67 65 74 6f 70 74 2e 68 3e 0a lude <getopt.h>.
0130: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 #include <stdarg
0140: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 .h>.#include <fc
0150: 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 ntl.h>.#include
0160: 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 <stdio.h>.#inclu
0170: 64 65 20 3c 65 72 72 6e 6f 2e 68 3e 0a 23 69 6e de <errno.h>.#in
0180: 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68 3e 0a 23 clude <time.h>.#
0190: 69 6e 63 6c 75 64 65 20 3c 70 77 64 2e 68 3e 0a include <pwd.h>.
01a0: 0a 2f 2a 20 43 6f 6d 70 69 6c 65 20 74 69 6d 65 ./* Compile time
01b0: 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a 23 64 constants */.#d
01c0: 65 66 69 6e 65 20 46 49 4c 45 44 5f 56 45 52 53 efine FILED_VERS
01d0: 49 4f 4e 20 22 31 2e 39 22 0a 23 64 65 66 69 6e ION "1.9".#defin
01e0: 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 e FILED_SENDFILE
01f0: 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23 64 _MAX 16777215.#d
0200: 65 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58 5f efine FILED_MAX_
0210: 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33 30 FAILURE_COUNT 30
0220: 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 44 .#define FILED_D
0230: 45 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70 70 EFAULT_TYPE "app
0240: 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d 73 lication/octet-s
0250: 74 72 65 61 6d 22 0a 23 64 65 66 69 6e 65 20 46 tream".#define F
0260: 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45 52 ILED_PATH_BUFFER
0270: 5f 53 49 5a 45 20 31 30 31 30 0a 0a 2f 2a 20 44 _SIZE 1010../* D
0280: 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f efault values */
0290: 0a 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38 30 .#define PORT 80
02a0: 0a 23 64 65 66 69 6e 65 20 54 48 52 45 41 44 5f .#define THREAD_
02b0: 43 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65 20 COUNT 5.#define
02c0: 42 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a 23 BIND_ADDR "::".#
02d0: 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49 5a define CACHE_SIZ
02e0: 45 20 38 32 30 39 0a 23 64 65 66 69 6e 65 20 4c E 8209.#define L
02f0: 4f 47 5f 46 49 4c 45 20 22 2d 22 0a 0a 2f 2a 20 OG_FILE "-"../*
0300: 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 Configuration op
0310: 74 69 6f 6e 73 20 74 68 61 74 20 77 6f 72 6b 20 tions that work
0320: 74 68 72 65 61 64 73 20 6e 65 65 64 20 74 6f 20 threads need to
0330: 62 65 20 61 77 61 72 65 20 6f 66 20 2a 2f 0a 73 be aware of */.s
0340: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 truct filed_opti
0350: 6f 6e 73 20 7b 0a 09 69 6e 74 20 76 68 6f 73 74 ons {..int vhost
0360: 73 5f 65 6e 61 62 6c 65 64 3b 0a 7d 3b 0a 0a 2f s_enabled;.};../
0370: 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 20 * Arguments for
0380: 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a worker threads *
0390: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 77 /.struct filed_w
03a0: 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72 67 orker_thread_arg
03b0: 73 20 7b 0a 09 69 6e 74 20 66 64 3b 0a 09 73 74 s {..int fd;..st
03c0: 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f ruct filed_optio
03d0: 6e 73 20 6f 70 74 69 6f 6e 73 3b 0a 7d 3b 0a 0a ns options;.};..
03e0: 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72 /* Arguments for
03f0: 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64 73 logging threads
0400: 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 */.struct filed
0410: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f _logging_thread_
0420: 61 72 67 73 20 7b 0a 09 46 49 4c 45 20 2a 66 70 args {..FILE *fp
0430: 3b 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c 65 20 69 6e ;.};../* File in
0440: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74 72 formation */.str
0450: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e uct filed_filein
0460: 66 6f 20 7b 0a 09 70 74 68 72 65 61 64 5f 6d 75 fo {..pthread_mu
0470: 74 65 78 5f 74 20 6d 75 74 65 78 3b 0a 09 63 68 tex_t mutex;..ch
0480: 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50 41 ar path[FILED_PA
0490: 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b TH_BUFFER_SIZE];
04a0: 0a 09 69 6e 74 20 66 64 3b 0a 09 6f 66 66 5f 74 ..int fd;..off_t
04b0: 20 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 6c 61 73 len;..char *las
04c0: 74 6d 6f 64 3b 0a 09 63 68 61 72 20 6c 61 73 74 tmod;..char last
04d0: 6d 6f 64 5f 62 5b 36 34 5d 3b 0a 09 63 6f 6e 73 mod_b[64];..cons
04e0: 74 20 63 68 61 72 20 2a 74 79 70 65 3b 0a 09 63 t char *type;..c
04f0: 68 61 72 20 65 74 61 67 5b 36 34 5d 3b 0a 7d 3b har etag[64];.};
0500: 0a 0a 2f 2a 20 52 65 71 75 65 73 74 20 76 61 72 ../* Request var
0510: 69 61 62 6c 65 73 20 2a 2f 0a 73 74 72 75 63 74 iables */.struct
0520: 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 filed_http_requ
0530: 65 73 74 20 7b 0a 09 2f 2a 2a 20 42 75 66 66 65 est {../** Buffe
0540: 72 73 20 2a 2a 2f 0a 09 73 74 72 75 63 74 20 66 rs **/..struct f
0550: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 66 69 iled_fileinfo fi
0560: 6c 65 69 6e 66 6f 3b 0a 09 63 68 61 72 20 74 6d leinfo;..char tm
0570: 70 62 75 66 5b 46 49 4c 45 44 5f 50 41 54 48 5f pbuf[FILED_PATH_
0580: 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a 09 BUFFER_SIZE];...
0590: 2f 2a 2a 20 48 54 54 50 20 52 65 71 75 65 73 74 /** HTTP Request
05a0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2a 2f information **/
05b0: 0a 09 2f 2a 2a 2a 20 54 79 70 65 20 6f 66 20 72 ../*** Type of r
05c0: 65 71 75 65 73 74 20 28 48 45 41 44 20 6f 72 20 equest (HEAD or
05d0: 47 45 54 29 20 2a 2a 2a 2f 0a 09 65 6e 75 6d 20 GET) ***/..enum
05e0: 7b 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45 53 {...FILED_REQUES
05f0: 54 5f 4d 45 54 48 4f 44 5f 47 45 54 2c 0a 09 09 T_METHOD_GET,...
0600: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 FILED_REQUEST_ME
0610: 54 48 4f 44 5f 48 45 41 44 0a 09 7d 20 6d 65 74 THOD_HEAD..} met
0620: 68 6f 64 3b 0a 0a 09 2f 2a 2a 2a 20 50 61 74 68 hod;.../*** Path
0630: 20 62 65 69 6e 67 20 72 65 71 75 65 73 74 65 64 being requested
0640: 20 2a 2a 2a 2f 0a 09 63 68 61 72 20 70 61 74 68 ***/..char path
0650: 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 [FILED_PATH_BUFF
0660: 45 52 5f 53 49 5a 45 5d 3b 20 0a 0a 09 2f 2a 2a ER_SIZE]; .../**
0670: 2a 20 50 61 74 68 20 74 79 70 65 20 2a 2a 2a 2f * Path type ***/
0680: 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45 44 ..enum {...FILED
0690: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44 49 _REQUEST_TYPE_DI
06a0: 52 45 43 54 4f 52 59 2c 0a 09 09 46 49 4c 45 44 RECTORY,...FILED
06b0: 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f 54 _REQUEST_TYPE_OT
06c0: 48 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a 09 73 HER..} type;...s
06d0: 74 72 75 63 74 20 7b 0a 09 09 73 74 72 75 63 74 truct {...struct
06e0: 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65 6e {....int presen
06f0: 74 3b 0a 09 09 09 6f 66 66 5f 74 20 6f 66 66 73 t;....off_t offs
0700: 65 74 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67 65 et; /*** Range
0710: 20 73 74 61 72 74 20 2a 2a 2a 2f 0a 09 09 09 6f start ***/....o
0720: 66 66 5f 74 20 6c 65 6e 67 74 68 3b 20 20 20 2f ff_t length; /
0730: 2a 2a 2a 20 52 61 6e 67 65 20 6c 65 6e 67 74 68 *** Range length
0740: 20 2a 2a 2a 2f 0a 09 09 7d 20 72 61 6e 67 65 3b ***/...} range;
0750: 0a 0a 09 09 73 74 72 75 63 74 20 7b 0a 09 09 09 ....struct {....
0760: 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a 09 09 09 int present;....
0770: 63 68 61 72 20 68 6f 73 74 5b 46 49 4c 45 44 5f char host[FILED_
0780: 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 PATH_BUFFER_SIZE
0790: 5d 3b 0a 09 09 7d 20 68 6f 73 74 3b 0a 0a 09 09 ];...} host;....
07a0: 65 6e 75 6d 20 7b 0a 09 09 09 46 49 4c 45 44 5f enum {....FILED_
07b0: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 CONNECTION_CLOSE
07c0: 2c 0a 09 09 09 46 49 4c 45 44 5f 43 4f 4e 4e 45 ,....FILED_CONNE
07d0: 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 CTION_KEEP_ALIVE
07e0: 0a 09 09 7d 20 63 6f 6e 6e 65 63 74 69 6f 6e 3b ...} connection;
07f0: 0a 09 7d 20 68 65 61 64 65 72 73 3b 0a 7d 3b 0a ..} headers;.};.
0800: 0a 2f 2a 20 4c 6f 67 20 72 65 63 6f 72 64 20 2a ./* Log record *
0810: 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c /.struct filed_l
0820: 6f 67 5f 65 6e 74 72 79 20 7b 0a 09 2f 2a 20 54 og_entry {../* T
0830: 79 70 65 20 6f 66 20 6c 6f 67 20 65 6e 74 72 79 ype of log entry
0840: 20 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 */..enum {...FI
0850: 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45 53 LED_LOG_TYPE_MES
0860: 53 41 47 45 2c 0a 09 09 46 49 4c 45 44 5f 4c 4f SAGE,...FILED_LO
0870: 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52 0a G_TYPE_TRANSFER.
0880: 09 7d 20 74 79 70 65 3b 0a 0a 09 2f 2a 20 4c 69 .} type;.../* Li
0890: 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 2f 74 nked list head/t
08a0: 61 69 6c 20 2a 2f 0a 09 73 74 72 75 63 74 20 66 ail */..struct f
08b0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
08c0: 5f 6e 65 78 74 3b 0a 09 73 74 72 75 63 74 20 66 _next;..struct f
08d0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
08e0: 5f 70 72 65 76 3b 0a 0a 09 2f 2a 20 54 68 72 65 _prev;.../* Thre
08f0: 61 64 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 ad from which th
0900: 69 73 20 6c 6f 67 20 65 6e 74 72 79 20 65 6d 69 is log entry emi
0910: 6e 61 74 65 73 20 2a 2f 0a 09 70 74 68 72 65 61 nates */..pthrea
0920: 64 5f 74 20 74 68 72 65 61 64 3b 0a 0a 09 2f 2a d_t thread;.../*
0930: 20 4d 65 73 73 61 67 65 20 62 75 66 66 65 72 20 Message buffer
0940: 66 6f 72 20 74 79 70 65 20 3d 20 4d 45 53 53 41 for type = MESSA
0950: 47 45 20 2a 2f 0a 09 2f 2a 20 50 61 74 68 20 62 GE */../* Path b
0960: 75 66 66 65 72 20 66 6f 72 20 74 79 70 65 20 3d uffer for type =
0970: 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a 09 63 68 TRANSFER */..ch
0980: 61 72 20 62 75 66 66 65 72 5b 46 49 4c 45 44 5f ar buffer[FILED_
0990: 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 PATH_BUFFER_SIZE
09a0: 5d 3b 0a 0a 09 2f 2a 20 49 74 65 6d 73 20 66 6f ];.../* Items fo
09b0: 72 20 74 79 70 65 20 3d 20 54 52 41 4e 53 46 45 r type = TRANSFE
09c0: 52 20 2a 2f 0a 09 69 6e 74 20 68 74 74 70 5f 63 R */..int http_c
09d0: 6f 64 65 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 ode;..const char
09e0: 20 2a 72 65 61 73 6f 6e 3b 0a 09 74 69 6d 65 5f *reason;..time_
09f0: 74 20 73 74 61 72 74 74 69 6d 65 3b 0a 09 74 69 t starttime;..ti
0a00: 6d 65 5f 74 20 65 6e 64 74 69 6d 65 3b 0a 09 6f me_t endtime;..o
0a10: 66 66 5f 74 20 72 65 71 5f 6f 66 66 73 65 74 3b ff_t req_offset;
0a20: 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6c 65 6e 67 ..off_t req_leng
0a30: 74 68 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 74 5f th;..off_t sent_
0a40: 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74 20 66 length;..off_t f
0a50: 69 6c 65 5f 6c 65 6e 67 74 68 3b 0a 09 63 68 61 ile_length;..cha
0a60: 72 20 69 70 5b 31 32 38 5d 3b 0a 09 69 6e 74 20 r ip[128];..int
0a70: 70 6f 72 74 3b 0a 09 69 6e 74 20 6d 65 74 68 6f port;..int metho
0a80: 64 3b 0a 7d 3b 0a 0a 2f 2a 20 47 6c 6f 62 61 6c d;.};../* Global
0a90: 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 2f 2a variables */./*
0aa0: 2a 20 4f 70 65 6e 20 46 69 6c 65 20 63 61 63 68 * Open File cach
0ab0: 65 20 2a 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c e **/.struct fil
0ac0: 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69 6c ed_fileinfo *fil
0ad0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 ed_fileinfo_fdca
0ae0: 63 68 65 20 3d 20 4e 55 4c 4c 3b 0a 75 6e 73 69 che = NULL;.unsi
0af0: 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f 66 gned int filed_f
0b00: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f ileinfo_fdcache_
0b10: 73 69 7a 65 20 3d 20 30 3b 0a 0a 2f 2a 2a 20 4c size = 0;../** L
0b20: 6f 67 67 69 6e 67 20 2a 2a 2f 0a 73 74 72 75 63 ogging **/.struc
0b30: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
0b40: 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 y *filed_log_msg
0b50: 5f 6c 69 73 74 3b 0a 70 74 68 72 65 61 64 5f 6d _list;.pthread_m
0b60: 75 74 65 78 5f 74 20 66 69 6c 65 64 5f 6c 6f 67 utex_t filed_log
0b70: 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 3b _msg_list_mutex;
0b80: 0a 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f 74 20 .pthread_cond_t
0b90: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 filed_log_msg_li
0ba0: 73 74 5f 72 65 61 64 79 3b 0a 0a 2f 2a 20 53 69 st_ready;../* Si
0bb0: 67 6e 61 6c 20 48 61 6e 64 6c 65 72 20 2a 2f 0a gnal Handler */.
0bc0: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 static void file
0bd0: 64 5f 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 d_signal_handler
0be0: 28 69 6e 74 20 73 69 67 6e 61 6c 5f 6e 75 6d 62 (int signal_numb
0bf0: 65 72 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 er) {..struct fi
0c00: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 led_fileinfo *ca
0c10: 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 che;..unsigned i
0c20: 6e 74 20 69 64 78 3b 0a 0a 09 73 77 69 74 63 68 nt idx;...switch
0c30: 20 28 73 69 67 6e 61 6c 5f 6e 75 6d 62 65 72 29 (signal_number)
0c40: 20 7b 0a 09 09 63 61 73 65 20 53 49 47 48 55 50 {...case SIGHUP
0c50: 3a 0a 09 09 09 66 6f 72 20 28 69 64 78 20 3d 20 :....for (idx =
0c60: 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 64 5f 66 0; idx < filed_f
0c70: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f ileinfo_fdcache_
0c80: 73 69 7a 65 3b 20 69 64 78 2b 2b 29 20 7b 0a 09 size; idx++) {..
0c90: 09 09 09 63 61 63 68 65 20 3d 20 26 66 69 6c 65 ...cache = &file
0ca0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
0cb0: 68 65 5b 69 64 78 5d 3b 0a 0a 09 09 09 09 70 74 he[idx];......pt
0cc0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b hread_mutex_lock
0cd0: 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b (&cache->mutex);
0ce0: 0a 0a 09 09 09 09 63 61 63 68 65 2d 3e 70 61 74 ......cache->pat
0cf0: 68 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09 09 h[0] = '\0';....
0d00: 09 69 66 20 28 63 61 63 68 65 2d 3e 66 64 20 3e .if (cache->fd >
0d10: 3d 20 30 29 20 7b 0a 09 09 09 09 09 63 6c 6f 73 = 0) {......clos
0d20: 65 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 0a 09 e(cache->fd);...
0d30: 09 09 09 09 63 61 63 68 65 2d 3e 66 64 20 3d 20 ....cache->fd =
0d40: 2d 31 3b 0a 09 09 09 09 7d 0a 0a 09 09 09 09 63 -1;.....}......c
0d50: 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 3d 20 ache->lastmod =
0d60: 22 22 3b 0a 09 09 09 09 63 61 63 68 65 2d 3e 74 "";.....cache->t
0d70: 79 70 65 20 3d 20 22 22 3b 0a 0a 09 09 09 09 70 ype = "";......p
0d80: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c thread_mutex_unl
0d90: 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 ock(&cache->mute
0da0: 78 29 3b 0a 09 09 09 7d 0a 09 09 09 62 72 65 61 x);....}....brea
0db0: 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b 0a k;..}...return;.
0dc0: 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 }../* Initialize
0dd0: 20 63 61 63 68 65 20 2a 2f 0a 73 74 61 74 69 63 cache */.static
0de0: 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 5f int filed_init_
0df0: 63 61 63 68 65 28 75 6e 73 69 67 6e 65 64 20 69 cache(unsigned i
0e00: 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 29 20 7b nt cache_size) {
0e10: 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 ..unsigned int i
0e20: 64 78 3b 0a 09 69 6e 74 20 6d 75 74 65 78 5f 69 dx;..int mutex_i
0e30: 6e 69 74 5f 72 65 74 3b 0a 0a 09 2f 2a 20 43 61 nit_ret;.../* Ca
0e40: 63 68 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 72 che may not be r
0e50: 65 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f e-initialized */
0e60: 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 ..if (filed_file
0e70: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a info_fdcache_siz
0e80: 65 20 21 3d 20 30 20 7c 7c 20 66 69 6c 65 64 5f e != 0 || filed_
0e90: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
0ea0: 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 != NULL) {...re
0eb0: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f 2a turn(1);..}.../*
0ec0: 20 41 6c 6c 6f 63 61 74 65 20 63 61 63 68 65 20 Allocate cache
0ed0: 2a 2f 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e */..filed_filein
0ee0: 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 20 fo_fdcache_size
0ef0: 3d 20 63 61 63 68 65 5f 73 69 7a 65 3b 0a 09 66 = cache_size;..f
0f00: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 iled_fileinfo_fd
0f10: 63 61 63 68 65 20 3d 20 6d 61 6c 6c 6f 63 28 73 cache = malloc(s
0f20: 69 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 66 69 6c izeof(*filed_fil
0f30: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 29 20 2a einfo_fdcache) *
0f40: 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f filed_fileinfo_
0f50: 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 fdcache_size);..
0f60: 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69 6e if (filed_filein
0f70: 66 6f 5f 66 64 63 61 63 68 65 20 3d 3d 20 4e 55 fo_fdcache == NU
0f80: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 LL) {...return(1
0f90: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74 69 );..}.../* Initi
0fa0: 61 6c 69 7a 65 20 63 61 63 68 65 20 65 6e 74 72 alize cache entr
0fb0: 69 65 73 20 2a 2f 0a 09 66 6f 72 20 28 69 64 78 ies */..for (idx
0fc0: 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 = 0; idx < file
0fd0: 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 d_fileinfo_fdcac
0fe0: 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29 20 he_size; idx++)
0ff0: 7b 0a 09 09 6d 75 74 65 78 5f 69 6e 69 74 5f 72 {...mutex_init_r
1000: 65 74 20 3d 20 70 74 68 72 65 61 64 5f 6d 75 74 et = pthread_mut
1010: 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f 66 ex_init(&filed_f
1020: 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b ileinfo_fdcache[
1030: 69 64 78 5d 2e 6d 75 74 65 78 2c 20 4e 55 4c 4c idx].mutex, NULL
1040: 29 3b 0a 09 09 69 66 20 28 6d 75 74 65 78 5f 69 );...if (mutex_i
1050: 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a nit_ret != 0) {.
1060: 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 ...return(1);...
1070: 7d 0a 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 }....filed_filei
1080: 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d nfo_fdcache[idx]
1090: 2e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b .path[0] = '\0';
10a0: 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ...filed_fileinf
10b0: 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e 66 o_fdcache[idx].f
10c0: 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65 64 5f d = -1;...filed_
10d0: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
10e0: 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20 3d 20 [idx].lastmod =
10f0: 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 "";...filed_file
1100: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 info_fdcache[idx
1110: 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09 7d 0a ].type = "";..}.
1120: 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a ..return(0);.}..
1130: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70 72 /* Initialize pr
1140: 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69 63 20 ocess */.static
1150: 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 28 75 int filed_init(u
1160: 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63 68 nsigned int cach
1170: 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61 74 69 e_size) {..stati
1180: 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d 20 30 c int called = 0
1190: 3b 0a 09 73 73 69 7a 65 5f 74 20 72 65 61 64 5f ;..ssize_t read_
11a0: 72 65 74 20 3d 20 30 3b 0a 09 75 6e 73 69 67 6e ret = 0;..unsign
11b0: 65 64 20 69 6e 74 20 72 61 6e 64 6f 6d 5f 76 61 ed int random_va
11c0: 6c 75 65 20 3d 20 30 3b 0a 09 69 6e 74 20 63 61 lue = 0;..int ca
11d0: 63 68 65 5f 72 65 74 3b 0a 09 69 6e 74 20 72 61 che_ret;..int ra
11e0: 6e 64 6f 6d 5f 66 64 3b 0a 0a 09 69 66 20 28 63 ndom_fd;...if (c
11f0: 61 6c 6c 65 64 29 20 7b 0a 09 09 72 65 74 75 72 alled) {...retur
1200: 6e 28 30 29 3b 0a 09 7d 0a 0a 09 63 61 6c 6c 65 n(0);..}...calle
1210: 64 20 3d 20 31 3b 0a 0a 09 2f 2a 20 41 74 74 65 d = 1;.../* Atte
1220: 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c 20 mpt to lock all
1230: 6d 65 6d 6f 72 79 20 74 6f 20 70 68 79 73 69 63 memory to physic
1240: 61 6c 20 52 41 4d 20 28 62 75 74 20 64 6f 6e 27 al RAM (but don'
1250: 74 20 63 61 72 65 20 69 66 20 77 65 20 63 61 6e t care if we can
1260: 27 74 29 20 2a 2f 0a 09 6d 6c 6f 63 6b 61 6c 6c 't) */..mlockall
1270: 28 4d 43 4c 5f 43 55 52 52 45 4e 54 20 7c 20 4d (MCL_CURRENT | M
1280: 43 4c 5f 46 55 54 55 52 45 29 3b 0a 0a 09 2f 2a CL_FUTURE);.../*
1290: 20 49 67 6e 6f 72 65 20 53 49 47 50 49 50 45 20 Ignore SIGPIPE
12a0: 2a 2f 0a 09 73 69 67 6e 61 6c 28 53 49 47 50 49 */..signal(SIGPI
12b0: 50 45 2c 20 53 49 47 5f 49 47 4e 29 3b 0a 0a 09 PE, SIG_IGN);...
12c0: 2f 2a 20 48 61 6e 64 6c 65 20 53 49 47 48 55 50 /* Handle SIGHUP
12d0: 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20 to release all
12e0: 63 61 63 68 65 73 20 2a 2f 0a 09 73 69 67 6e 61 caches */..signa
12f0: 6c 28 53 49 47 48 55 50 2c 20 66 69 6c 65 64 5f l(SIGHUP, filed_
1300: 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 29 3b signal_handler);
1310: 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 .../* Initialize
1320: 20 63 61 63 68 65 20 73 74 72 75 63 74 75 72 65 cache structure
1330: 20 2a 2f 0a 09 63 61 63 68 65 5f 72 65 74 20 3d */..cache_ret =
1340: 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63 68 filed_init_cach
1350: 65 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 e(cache_size);..
1360: 69 66 20 28 63 61 63 68 65 5f 72 65 74 20 21 3d if (cache_ret !=
1370: 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 63 0) {...return(c
1380: 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a 09 ache_ret);..}...
1390: 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 72 61 /* Initialize ra
13a0: 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 ndom number gene
13b0: 72 61 74 6f 72 20 2a 2f 0a 09 72 61 6e 64 6f 6d rator */..random
13c0: 5f 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 _fd = open("/dev
13d0: 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44 4f /urandom", O_RDO
13e0: 4e 4c 59 29 3b 0a 09 69 66 20 28 72 61 6e 64 6f NLY);..if (rando
13f0: 6d 5f 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09 72 m_fd >= 0) {...r
1400: 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28 72 ead_ret = read(r
1410: 61 6e 64 6f 6d 5f 66 64 2c 20 26 72 61 6e 64 6f andom_fd, &rando
1420: 6d 5f 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66 28 m_value, sizeof(
1430: 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 29 3b 0a random_value));.
1440: 0a 09 09 63 6c 6f 73 65 28 72 61 6e 64 6f 6d 5f ...close(random_
1450: 66 64 29 3b 0a 09 7d 0a 0a 09 72 61 6e 64 6f 6d fd);..}...random
1460: 5f 76 61 6c 75 65 20 5e 3d 20 67 65 74 70 69 64 _value ^= getpid
1470: 28 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 ();..random_valu
1480: 65 20 5e 3d 20 67 65 74 75 69 64 28 29 3b 0a 09 e ^= getuid();..
1490: 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d 20 random_value ^=
14a0: 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 73 72 time(NULL);...sr
14b0: 61 6e 64 6f 6d 28 72 61 6e 64 6f 6d 5f 76 61 6c andom(random_val
14c0: 75 65 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30 29 ue);...return(0)
14d0: 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 43 48 3a ;.../* NOTREACH:
14e0: 20 52 65 61 64 20 6d 61 79 20 66 61 69 6c 20 6f Read may fail o
14f0: 72 20 73 75 63 63 65 65 64 2c 20 77 65 20 64 6f r succeed, we do
1500: 6e 27 74 20 61 63 74 75 61 6c 6c 79 20 63 61 72 n't actually car
1510: 65 20 2a 2f 0a 09 72 65 61 64 5f 72 65 74 20 3d e */..read_ret =
1520: 20 72 65 61 64 5f 72 65 74 3b 0a 7d 0a 0a 2f 2a read_ret;.}../*
1530: 20 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70 61 72 Listen on a par
1540: 74 69 63 75 6c 61 72 20 61 64 64 72 65 73 73 2f ticular address/
1550: 70 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 port */.static i
1560: 6e 74 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e 28 nt filed_listen(
1570: 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 64 64 72 const char *addr
1580: 65 73 73 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e ess, unsigned in
1590: 74 20 70 6f 72 74 29 20 7b 0a 09 73 74 72 75 63 t port) {..struc
15a0: 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20 61 t sockaddr_in6 a
15b0: 64 64 72 5f 76 36 3b 0a 09 73 74 72 75 63 74 20 ddr_v6;..struct
15c0: 73 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64 64 72 sockaddr_in addr
15d0: 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f 63 _v4;..struct soc
15e0: 6b 61 64 64 72 20 2a 61 64 64 72 3b 0a 09 73 6f kaddr *addr;..so
15f0: 63 6b 6c 65 6e 5f 74 20 61 64 64 72 5f 6c 65 6e cklen_t addr_len
1600: 3b 0a 09 69 6e 74 20 70 74 6f 6e 5f 72 65 74 2c ;..int pton_ret,
1610: 20 62 69 6e 64 5f 72 65 74 2c 20 6c 69 73 74 65 bind_ret, liste
1620: 6e 5f 72 65 74 3b 0a 09 69 6e 74 20 66 61 6d 69 n_ret;..int fami
1630: 6c 79 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 0a 09 ly;..int fd;....
1640: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 family = AF_INET
1650: 36 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6;..pton_ret = i
1660: 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c net_pton(family,
1670: 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f address, &addr_
1680: 76 36 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36 5f v6.sin6_addr.s6_
1690: 61 64 64 72 29 3b 0a 09 69 66 20 28 70 74 6f 6e addr);..if (pton
16a0: 5f 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 66 _ret != 1) {...f
16b0: 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b amily = AF_INET;
16c0: 0a 09 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69 6e ...pton_ret = in
16d0: 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c 20 et_pton(family,
16e0: 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f 76 address, &addr_v
16f0: 34 2e 73 69 6e 5f 61 64 64 72 2e 73 5f 61 64 64 4.sin_addr.s_add
1700: 72 29 3b 0a 09 09 69 66 20 28 70 74 6f 6e 5f 72 r);...if (pton_r
1710: 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 09 72 65 et != 1) {....re
1720: 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a 09 turn(-1);...}...
1730: 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 66 61 6d .addr_v4.sin_fam
1740: 69 6c 79 20 3d 20 66 61 6d 69 6c 79 3b 0a 09 09 ily = family;...
1750: 61 64 64 72 5f 76 34 2e 73 69 6e 5f 70 6f 72 74 addr_v4.sin_port
1760: 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a = htons(port);.
1770: 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 ...addr = (struc
1780: 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 t sockaddr *) &a
1790: 64 64 72 5f 76 34 3b 0a 09 09 61 64 64 72 5f 6c ddr_v4;...addr_l
17a0: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 en = sizeof(addr
17b0: 5f 76 34 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a _v4);..} else {.
17c0: 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 66 ..addr_v6.sin6_f
17d0: 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 36 amily = AF_INET6
17e0: 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 ;...addr_v6.sin6
17f0: 5f 66 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a 09 _flowinfo = 0;..
1800: 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 73 63 .addr_v6.sin6_sc
1810: 6f 70 65 5f 69 64 20 3d 20 30 3b 0a 09 09 61 64 ope_id = 0;...ad
1820: 64 72 5f 76 36 2e 73 69 6e 36 5f 70 6f 72 74 20 dr_v6.sin6_port
1830: 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a 0a = htons(port);..
1840: 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63 74 ..addr = (struct
1850: 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 sockaddr *) &ad
1860: 64 72 5f 76 36 3b 0a 09 09 61 64 64 72 5f 6c 65 dr_v6;...addr_le
1870: 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72 5f n = sizeof(addr_
1880: 76 36 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20 73 v6);..}...fd = s
1890: 6f 63 6b 65 74 28 66 61 6d 69 6c 79 2c 20 53 4f ocket(family, SO
18a0: 43 4b 5f 53 54 52 45 41 4d 2c 20 30 29 3b 0a 09 CK_STREAM, 0);..
18b0: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
18c0: 72 65 74 75 72 6e 28 66 64 29 3b 0a 09 7d 0a 0a return(fd);..}..
18d0: 09 62 69 6e 64 5f 72 65 74 20 3d 20 62 69 6e 64 .bind_ret = bind
18e0: 28 66 64 2c 20 61 64 64 72 2c 20 61 64 64 72 5f (fd, addr, addr_
18f0: 6c 65 6e 29 3b 0a 09 69 66 20 28 62 69 6e 64 5f len);..if (bind_
1900: 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 63 6c 6f ret < 0) {...clo
1910: 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75 72 se(fd);....retur
1920: 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73 74 n(-1);..}...list
1930: 65 6e 5f 72 65 74 20 3d 20 6c 69 73 74 65 6e 28 en_ret = listen(
1940: 66 64 2c 20 31 32 38 29 3b 0a 09 69 66 20 28 6c fd, 128);..if (l
1950: 69 73 74 65 6e 5f 72 65 74 20 21 3d 20 30 29 20 isten_ret != 0)
1960: 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a 0a {...close(fd);..
1970: 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09 7d ..return(-1);..}
1980: 0a 0a 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 7d ...return(fd);.}
1990: 0a 0a 2f 2a 20 4c 6f 67 20 61 20 6d 65 73 73 61 ../* Log a messa
19a0: 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49 4c ge */.#ifdef FIL
19b0: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20 64 ED_DONT_LOG.# d
19c0: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 67 efine filed_logg
19d0: 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74 28 ing_thread_init(
19e0: 78 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20 66 x) 0.# define f
19f0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
1a00: 75 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a ug(x, ...) /**/.
1a10: 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64 5f # define filed_
1a20: 6c 6f 67 5f 6d 73 67 28 78 2c 20 2e 2e 2e 29 20 log_msg(x, ...)
1a30: 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 66 /**/.# define f
1a40: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 78 iled_log_entry(x
1a50: 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 ) /**/.# define
1a60: 20 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 78 2c filed_log_ip(x,
1a70: 20 2e 2e 2e 29 20 4e 55 4c 4c 0a 23 20 20 64 65 ...) NULL.# de
1a80: 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e fine filed_log_n
1a90: 65 77 28 78 29 20 26 6c 6f 63 61 6c 5f 64 75 6d ew(x) &local_dum
1aa0: 6d 79 5f 6c 6f 67 0a 23 20 20 64 65 66 69 6e 65 my_log.# define
1ab0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 28 filed_log_open(
1ac0: 78 29 20 73 74 64 6f 75 74 0a 23 65 6c 73 65 0a x) stdout.#else.
1ad0: 23 20 20 69 66 64 65 66 20 46 49 4c 45 44 5f 44 # ifdef FILED_D
1ae0: 45 42 55 47 0a 23 20 20 20 20 64 65 66 69 6e 65 EBUG.# define
1af0: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 filed_log_msg_d
1b00: 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 7b 20 66 ebug(x, ...) { f
1b10: 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 78 printf(stderr, x
1b20: 2c 20 5f 5f 56 41 5f 41 52 47 53 5f 5f 29 3b 20 , __VA_ARGS__);
1b30: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 fprintf(stderr,
1b40: 22 5c 6e 22 29 3b 20 66 66 6c 75 73 68 28 73 74 "\n"); fflush(st
1b50: 64 65 72 72 29 3b 20 7d 0a 23 20 20 65 6c 73 65 derr); }.# else
1b60: 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66 69 6c .# define fil
1b70: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
1b80: 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23 20 (x, ...) /**/.#
1b90: 20 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 69 74 69 endif../* Initi
1ba0: 61 6c 69 7a 65 20 6c 6f 67 67 69 6e 67 20 74 68 alize logging th
1bb0: 72 65 61 64 20 2a 2f 0a 73 74 61 74 69 63 20 76 read */.static v
1bc0: 6f 69 64 20 2a 66 69 6c 65 64 5f 6c 6f 67 67 69 oid *filed_loggi
1bd0: 6e 67 5f 74 68 72 65 61 64 28 76 6f 69 64 20 2a ng_thread(void *
1be0: 61 72 67 5f 70 29 20 7b 0a 09 73 74 72 75 63 74 arg_p) {..struct
1bf0: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 filed_logging_t
1c00: 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 3b hread_args *arg;
1c10: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
1c20: 6f 67 5f 65 6e 74 72 79 20 2a 63 75 72 72 2c 20 og_entry *curr,
1c30: 2a 70 72 65 76 3b 0a 09 63 6f 6e 73 74 20 63 68 *prev;..const ch
1c40: 61 72 20 2a 6d 65 74 68 6f 64 3b 0a 09 74 69 6d ar *method;..tim
1c50: 65 5f 74 20 6e 6f 77 3b 0a 09 46 49 4c 45 20 2a e_t now;..FILE *
1c60: 66 70 3b 0a 0a 09 61 72 67 20 3d 20 61 72 67 5f fp;...arg = arg_
1c70: 70 3b 0a 0a 09 66 70 20 3d 20 61 72 67 2d 3e 66 p;...fp = arg->f
1c80: 70 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b p;...while (1) {
1c90: 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 ...pthread_mutex
1ca0: 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 _lock(&filed_log
1cb0: 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 _msg_list_mutex)
1cc0: 3b 0a 09 09 70 74 68 72 65 61 64 5f 63 6f 6e 64 ;...pthread_cond
1cd0: 5f 77 61 69 74 28 26 66 69 6c 65 64 5f 6c 6f 67 _wait(&filed_log
1ce0: 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79 2c _msg_list_ready,
1cf0: 20 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f &filed_log_msg_
1d00: 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 list_mutex);....
1d10: 63 75 72 72 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 curr = filed_log
1d20: 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 09 66 69 6c _msg_list;...fil
1d30: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 ed_log_msg_list
1d40: 3d 20 4e 55 4c 4c 3b 0a 0a 09 09 70 74 68 72 65 = NULL;....pthre
1d50: 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 ad_mutex_unlock(
1d60: 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c &filed_log_msg_l
1d70: 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09 6e ist_mutex);....n
1d80: 6f 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b ow = time(NULL);
1d90: 0a 0a 09 09 70 72 65 76 20 3d 20 4e 55 4c 4c 3b ....prev = NULL;
1da0: 0a 09 09 66 6f 72 20 28 3b 20 63 75 72 72 3b 20 ...for (; curr;
1db0: 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 6e 65 curr = curr->_ne
1dc0: 78 74 29 20 7b 0a 09 09 09 63 75 72 72 2d 3e 5f xt) {....curr->_
1dd0: 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 0a 09 09 prev = prev;....
1de0: 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 .prev = curr;...
1df0: 7d 0a 0a 09 09 63 75 72 72 20 3d 20 70 72 65 76 }....curr = prev
1e00: 3b 0a 09 09 77 68 69 6c 65 20 28 63 75 72 72 29 ;...while (curr)
1e10: 20 7b 0a 09 09 09 73 77 69 74 63 68 20 28 63 75 {....switch (cu
1e20: 72 72 2d 3e 74 79 70 65 29 20 7b 0a 09 09 09 09 rr->type) {.....
1e30: 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 case FILED_LOG_T
1e40: 59 50 45 5f 4d 45 53 53 41 47 45 3a 0a 09 09 09 YPE_MESSAGE:....
1e50: 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 25 ..fprintf(fp, "%
1e60: 73 22 2c 20 63 75 72 72 2d 3e 62 75 66 66 65 72 s", curr->buffer
1e70: 29 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b 0a );.......break;.
1e80: 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f 4c ....case FILED_L
1e90: 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52 OG_TYPE_TRANSFER
1ea0: 3a 0a 09 09 09 09 09 73 77 69 74 63 68 20 28 63 :......switch (c
1eb0: 75 72 72 2d 3e 6d 65 74 68 6f 64 29 20 7b 0a 09 urr->method) {..
1ec0: 09 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f .....case FILED_
1ed0: 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 REQUEST_METHOD_G
1ee0: 45 54 3a 0a 09 09 09 09 09 09 09 6d 65 74 68 6f ET:........metho
1ef0: 64 3d 22 47 45 54 22 3b 0a 09 09 09 09 09 09 09 d="GET";........
1f00: 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 63 61 73 break;.......cas
1f10: 65 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f e FILED_REQUEST_
1f20: 4d 45 54 48 4f 44 5f 48 45 41 44 3a 0a 09 09 09 METHOD_HEAD:....
1f30: 09 09 09 09 6d 65 74 68 6f 64 3d 22 48 45 41 44 ....method="HEAD
1f40: 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b ";........break;
1f50: 0a 09 09 09 09 09 09 64 65 66 61 75 6c 74 3a 0a .......default:.
1f60: 09 09 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 3c .......method="<
1f70: 75 6e 6b 6e 6f 77 6e 3e 22 3b 0a 09 09 09 09 09 unknown>";......
1f80: 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a ..break;......}.
1f90: 0a 09 09 09 09 09 69 66 20 28 63 75 72 72 2d 3e ......if (curr->
1fa0: 65 6e 64 74 69 6d 65 20 3d 3d 20 28 28 74 69 6d endtime == ((tim
1fb0: 65 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 e_t) -1)) {.....
1fc0: 09 09 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65 20 ..curr->endtime
1fd0: 3d 20 6e 6f 77 3b 0a 09 09 09 09 09 7d 0a 0a 09 = now;......}...
1fe0: 09 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 ....fprintf(fp,
1ff0: 22 54 52 41 4e 53 46 45 52 20 4d 45 54 48 4f 44 "TRANSFER METHOD
2000: 3d 25 73 20 50 41 54 48 3d 25 73 20 53 52 43 3d =%s PATH=%s SRC=
2010: 25 73 3a 25 69 20 54 49 4d 45 2e 53 54 41 52 54 %s:%i TIME.START
2020: 3d 25 6c 6c 75 20 54 49 4d 45 2e 45 4e 44 3d 25 =%llu TIME.END=%
2030: 6c 6c 75 20 43 4f 44 45 2e 56 41 4c 55 45 3d 25 llu CODE.VALUE=%
2040: 75 20 43 4f 44 45 2e 52 45 41 53 4f 4e 3d 25 73 u CODE.REASON=%s
2050: 20 52 45 51 55 45 53 54 2e 4f 46 46 53 45 54 3d REQUEST.OFFSET=
2060: 25 6c 6c 75 20 52 45 51 55 45 53 54 2e 4c 45 4e %llu REQUEST.LEN
2070: 47 54 48 3d 25 6c 6c 75 20 46 49 4c 45 2e 4c 45 GTH=%llu FILE.LE
2080: 4e 47 54 48 3d 25 6c 6c 75 20 54 52 41 4e 53 46 NGTH=%llu TRANSF
2090: 45 52 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 22 2c ER.LENGTH=%llu",
20a0: 0a 09 09 09 09 09 09 6d 65 74 68 6f 64 2c 0a 09 .......method,..
20b0: 09 09 09 09 09 63 75 72 72 2d 3e 62 75 66 66 65 .....curr->buffe
20c0: 72 2c 0a 09 09 09 09 09 09 63 75 72 72 2d 3e 69 r,.......curr->i
20d0: 70 2c 20 63 75 72 72 2d 3e 70 6f 72 74 2c 0a 09 p, curr->port,..
20e0: 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c .....(unsigned l
20f0: 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e ong long) curr->
2100: 73 74 61 72 74 74 69 6d 65 2c 0a 09 09 09 09 09 starttime,......
2110: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
2120: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 65 6e 64 74 long) curr->endt
2130: 69 6d 65 2c 0a 09 09 09 09 09 09 63 75 72 72 2d ime,.......curr-
2140: 3e 68 74 74 70 5f 63 6f 64 65 2c 20 63 75 72 72 >http_code, curr
2150: 2d 3e 72 65 61 73 6f 6e 2c 0a 09 09 09 09 09 09 ->reason,.......
2160: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
2170: 6f 6e 67 29 20 63 75 72 72 2d 3e 72 65 71 5f 6f ong) curr->req_o
2180: 66 66 73 65 74 2c 0a 09 09 09 09 09 09 28 75 6e ffset,.......(un
2190: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 signed long long
21a0: 29 20 63 75 72 72 2d 3e 72 65 71 5f 6c 65 6e 67 ) curr->req_leng
21b0: 74 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 th,.......(unsig
21c0: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 ned long long) c
21d0: 75 72 72 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74 68 urr->file_length
21e0: 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 ,.......(unsigne
21f0: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 d long long) cur
2200: 72 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 0a 09 r->sent_length..
2210: 09 09 09 09 29 3b 0a 0a 09 09 09 09 09 62 72 65 ....);.......bre
2220: 61 6b 3b 0a 09 09 09 7d 0a 09 09 09 66 70 72 69 ak;....}....fpri
2230: 6e 74 66 28 66 70 2c 20 22 20 54 48 52 45 41 44 ntf(fp, " THREAD
2240: 3d 25 6c 6c 75 20 54 49 4d 45 3d 25 6c 6c 75 5c =%llu TIME=%llu\
2250: 6e 22 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e 65 n",.....(unsigne
2260: 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 d long long) cur
2270: 72 2d 3e 74 68 72 65 61 64 2c 0a 09 09 09 09 28 r->thread,.....(
2280: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
2290: 6e 67 29 20 6e 6f 77 0a 09 09 09 29 3b 0a 09 09 ng) now....);...
22a0: 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09 09 .fflush(fp);....
22b0: 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09 09 .prev = curr;...
22c0: 09 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 70 .curr = curr->_p
22d0: 72 65 76 3b 0a 0a 09 09 09 66 72 65 65 28 70 72 rev;.....free(pr
22e0: 65 76 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 ev);...}..}...re
22f0: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a 0a 73 turn(NULL);.}..s
2300: 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 tatic void filed
2310: 5f 6c 6f 67 5f 65 6e 74 72 79 28 73 74 72 75 63 _log_entry(struc
2320: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
2330: 79 20 2a 65 6e 74 72 79 29 20 7b 0a 09 65 6e 74 y *entry) {..ent
2340: 72 79 2d 3e 74 68 72 65 61 64 20 3d 20 70 74 68 ry->thread = pth
2350: 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 0a 09 70 read_self();...p
2360: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 thread_mutex_loc
2370: 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 k(&filed_log_msg
2380: 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 _list_mutex);...
2390: 65 6e 74 72 79 2d 3e 5f 6e 65 78 74 20 3d 20 66 entry->_next = f
23a0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 iled_log_msg_lis
23b0: 74 3b 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 t;..filed_log_ms
23c0: 67 5f 6c 69 73 74 20 3d 20 65 6e 74 72 79 3b 0a g_list = entry;.
23d0: 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f ..pthread_mutex_
23e0: 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f unlock(&filed_lo
23f0: 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78 g_msg_list_mutex
2400: 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 6f 6e );...pthread_con
2410: 64 5f 73 69 67 6e 61 6c 28 26 66 69 6c 65 64 5f d_signal(&filed_
2420: 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 log_msg_list_rea
2430: 64 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d dy);...return;.}
2440: 0a 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20 ..static struct
2450: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 filed_log_entry
2460: 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 69 *filed_log_new(i
2470: 6e 74 20 69 6e 69 74 69 61 6c 69 7a 65 29 20 7b nt initialize) {
2480: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c ..struct filed_l
2490: 6f 67 5f 65 6e 74 72 79 20 2a 72 65 74 76 61 6c og_entry *retval
24a0: 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d 61 6c ;...retval = mal
24b0: 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 72 65 74 76 loc(sizeof(*retv
24c0: 61 6c 29 29 3b 0a 0a 09 69 66 20 28 69 6e 69 74 al));...if (init
24d0: 69 61 6c 69 7a 65 29 20 7b 0a 09 09 72 65 74 76 ialize) {...retv
24e0: 61 6c 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 al->buffer[0] =
24f0: 27 5c 30 27 3b 0a 09 09 72 65 74 76 61 6c 2d 3e '\0';...retval->
2500: 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b 0a http_code = -1;.
2510: 09 09 72 65 74 76 61 6c 2d 3e 73 74 61 72 74 74 ..retval->startt
2520: 69 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76 61 ime = 0;...retva
2530: 6c 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 30 3b 0a l->endtime = 0;.
2540: 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6f 66 ..retval->req_of
2550: 66 73 65 74 20 3d 20 30 3b 0a 09 09 72 65 74 76 fset = 0;...retv
2560: 61 6c 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20 3d al->req_length =
2570: 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 73 65 0;...retval->se
2580: 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a 09 nt_length = 0;..
2590: 09 72 65 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c 65 .retval->file_le
25a0: 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74 76 ngth = 0;...retv
25b0: 61 6c 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 27 al->ip[0] = '\0'
25c0: 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 70 6f 72 74 ;...retval->port
25d0: 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c 2d = -1;...retval-
25e0: 3e 6d 65 74 68 6f 64 20 3d 20 2d 31 3b 0a 09 7d >method = -1;..}
25f0: 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c ...return(retval
2600: 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 );.}..static voi
2610: 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 d filed_log_msg(
2620: 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d 74 2c const char *fmt,
2630: 20 2e 2e 2e 29 20 7b 0a 09 73 74 72 75 63 74 20 ...) {..struct
2640: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 filed_log_entry
2650: 2a 65 6e 74 72 79 3b 0a 09 76 61 5f 6c 69 73 74 *entry;..va_list
2660: 20 61 72 67 73 3b 0a 0a 09 65 6e 74 72 79 20 3d args;...entry =
2670: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 30 filed_log_new(0
2680: 29 3b 0a 0a 09 76 61 5f 73 74 61 72 74 28 61 72 );...va_start(ar
2690: 67 73 2c 20 66 6d 74 29 3b 0a 0a 09 76 73 6e 70 gs, fmt);...vsnp
26a0: 72 69 6e 74 66 28 65 6e 74 72 79 2d 3e 62 75 66 rintf(entry->buf
26b0: 66 65 72 2c 20 73 69 7a 65 6f 66 28 65 6e 74 72 fer, sizeof(entr
26c0: 79 2d 3e 62 75 66 66 65 72 29 2c 20 66 6d 74 2c y->buffer), fmt,
26d0: 20 61 72 67 73 29 3b 0a 0a 09 76 61 5f 65 6e 64 args);...va_end
26e0: 28 61 72 67 73 29 3b 0a 0a 09 65 6e 74 72 79 2d (args);...entry-
26f0: 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f >type = FILED_LO
2700: 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 3b 0a G_TYPE_MESSAGE;.
2710: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 ..filed_log_entr
2720: 79 28 65 6e 74 72 79 29 3b 0a 0a 09 72 65 74 75 y(entry);...retu
2730: 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f rn;.}..static co
2740: 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f nst char *filed_
2750: 6c 6f 67 5f 69 70 28 73 74 72 75 63 74 20 73 6f log_ip(struct so
2760: 63 6b 61 64 64 72 20 2a 61 64 64 72 2c 20 63 68 ckaddr *addr, ch
2770: 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 ar *buffer, size
2780: 5f 74 20 62 75 66 66 65 72 6c 65 6e 29 20 7b 0a _t bufferlen) {.
2790: 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 .struct sockaddr
27a0: 5f 69 6e 20 2a 61 64 64 72 5f 76 34 3b 0a 09 73 _in *addr_v4;..s
27b0: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 truct sockaddr_i
27c0: 6e 36 20 2a 61 64 64 72 5f 76 36 3b 0a 09 63 6f n6 *addr_v6;..co
27d0: 6e 73 74 20 63 68 61 72 20 2a 72 65 74 76 61 6c nst char *retval
27e0: 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72 5f = NULL;...addr_
27f0: 76 36 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 v6 = (struct soc
2800: 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 64 64 kaddr_in6 *) add
2810: 72 3b 0a 0a 09 73 77 69 74 63 68 20 28 61 64 64 r;...switch (add
2820: 72 5f 76 36 2d 3e 73 69 6e 36 5f 66 61 6d 69 6c r_v6->sin6_famil
2830: 79 29 20 7b 0a 09 09 63 61 73 65 20 41 46 5f 49 y) {...case AF_I
2840: 4e 45 54 3a 0a 09 09 09 61 64 64 72 5f 76 34 20 NET:....addr_v4
2850: 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 = (struct sockad
2860: 64 72 5f 69 6e 20 2a 29 20 61 64 64 72 3b 0a 09 dr_in *) addr;..
2870: 09 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74 5f ..retval = inet_
2880: 6e 74 6f 70 28 41 46 5f 49 4e 45 54 2c 20 26 61 ntop(AF_INET, &a
2890: 64 64 72 5f 76 34 2d 3e 73 69 6e 5f 61 64 64 72 ddr_v4->sin_addr
28a0: 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 , buffer, buffer
28b0: 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b 0a len);....break;.
28c0: 09 09 63 61 73 65 20 41 46 5f 49 4e 45 54 36 3a ..case AF_INET6:
28d0: 0a 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 ....retval = ine
28e0: 74 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 36 2c t_ntop(AF_INET6,
28f0: 20 26 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36 5f &addr_v6->sin6_
2900: 61 64 64 72 2c 20 62 75 66 66 65 72 2c 20 62 75 addr, buffer, bu
2910: 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 fferlen);....bre
2920: 61 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 ak;..}...return(
2930: 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61 74 retval);.}..stat
2940: 69 63 20 46 49 4c 45 20 2a 66 69 6c 65 64 5f 6c ic FILE *filed_l
2950: 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 63 68 og_open(const ch
2960: 61 72 20 2a 66 69 6c 65 29 20 7b 0a 09 46 49 4c ar *file) {..FIL
2970: 45 20 2a 72 65 74 76 61 6c 3b 0a 0a 09 69 66 20 E *retval;...if
2980: 28 73 74 72 63 6d 70 28 66 69 6c 65 2c 20 22 2d (strcmp(file, "-
2990: 22 29 20 3d 3d 20 30 29 20 7b 0a 09 09 72 65 74 ") == 0) {...ret
29a0: 76 61 6c 20 3d 20 73 74 64 6f 75 74 3b 0a 09 7d val = stdout;..}
29b0: 20 65 6c 73 65 20 69 66 20 28 66 69 6c 65 5b 30 else if (file[0
29c0: 5d 20 3d 3d 20 27 7c 27 29 20 7b 0a 09 09 66 69 ] == '|') {...fi
29d0: 6c 65 2b 2b 3b 0a 09 09 72 65 74 76 61 6c 20 3d le++;...retval =
29e0: 20 70 6f 70 65 6e 28 66 69 6c 65 2c 20 22 77 22 popen(file, "w"
29f0: 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 72 );..} else {...r
2a00: 65 74 76 61 6c 20 3d 20 66 6f 70 65 6e 28 66 69 etval = fopen(fi
2a10: 6c 65 2c 20 22 61 2b 22 29 3b 0a 09 7d 0a 0a 09 le, "a+");..}...
2a20: 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b 0a return(retval);.
2a30: 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 }..static int fi
2a40: 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 led_logging_thre
2a50: 61 64 5f 69 6e 69 74 28 46 49 4c 45 20 2a 6c 6f ad_init(FILE *lo
2a60: 67 66 70 29 20 7b 0a 09 73 74 72 75 63 74 20 66 gfp) {..struct f
2a70: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 iled_logging_thr
2a80: 65 61 64 5f 61 72 67 73 20 2a 61 72 67 73 3b 0a ead_args *args;.
2a90: 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65 61 .pthread_t threa
2aa0: 64 5f 69 64 3b 0a 0a 09 61 72 67 73 20 3d 20 6d d_id;...args = m
2ab0: 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61 72 alloc(sizeof(*ar
2ac0: 67 73 29 29 3b 0a 09 61 72 67 73 2d 3e 66 70 20 gs));..args->fp
2ad0: 3d 20 6c 6f 67 66 70 3b 0a 0a 09 66 69 6c 65 64 = logfp;...filed
2ae0: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d 20 _log_msg_list =
2af0: 4e 55 4c 4c 3b 0a 0a 09 70 74 68 72 65 61 64 5f NULL;...pthread_
2b00: 6d 75 74 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 mutex_init(&file
2b10: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d d_log_msg_list_m
2b20: 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 70 utex, NULL);...p
2b30: 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 thread_create(&t
2b40: 68 72 65 61 64 5f 69 64 2c 20 4e 55 4c 4c 2c 20 hread_id, NULL,
2b50: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 filed_logging_th
2b60: 72 65 61 64 2c 20 61 72 67 73 29 3b 0a 0a 09 66 read, args);...f
2b70: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 53 54 iled_log_msg("ST
2b80: 41 52 54 22 29 3b 0a 0a 09 72 65 74 75 72 6e 28 ART");...return(
2b90: 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0);.}.#endif../*
2ba0: 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 65 72 Format time per
2bb0: 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 61 74 RFC2616 */.stat
2bc0: 69 63 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 66 ic char *filed_f
2bd0: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 72 20 ormat_time(char
2be0: 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74 20 *buffer, size_t
2bf0: 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e 73 buffer_len, cons
2c00: 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 6e 66 t time_t timeinf
2c10: 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 74 6d 20 o) {..struct tm
2c20: 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74 69 timeinfo_tm, *ti
2c30: 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09 74 meinfo_tm_p;...t
2c40: 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20 67 imeinfo_tm_p = g
2c50: 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 6e 66 mtime_r(&timeinf
2c60: 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 6d 29 o, &timeinfo_tm)
2c70: 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 6f 5f ;..if (timeinfo_
2c80: 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a tm_p == NULL) {.
2c90: 09 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e 6f 77 ..return("unknow
2ca0: 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 65 72 n");..}...buffer
2cb0: 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31 5d [buffer_len - 1]
2cc0: 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 = '\0';..buffer
2cd0: 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d 65 28 _len = strftime(
2ce0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c buffer, buffer_l
2cf0: 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 64 20 en - 1, "%a, %d
2d00: 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 20 47 %b %Y %H:%M:%S G
2d10: 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f 74 6d MT", timeinfo_tm
2d20: 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 _p);...return(bu
2d30: 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61 73 ffer);.}../* has
2d40: 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73 69 h */.static unsi
2d50: 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f 68 gned int filed_h
2d60: 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e ash(const unsign
2d70: 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65 2c 20 ed char *value,
2d80: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 6f 64 unsigned int mod
2d90: 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 6e 65 ulus) {..unsigne
2da0: 64 20 63 68 61 72 20 63 75 72 72 2c 20 70 72 65 d char curr, pre
2db0: 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a 09 75 v;..int diff;..u
2dc0: 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65 74 76 nsigned int retv
2dd0: 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d al;...retval = m
2de0: 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 72 65 odulus - 1;..pre
2df0: 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 32 35 v = modulus % 25
2e00: 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 75 72 5;...while ((cur
2e10: 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b 0a 09 r = *value)) {..
2e20: 09 69 66 20 28 63 75 72 72 20 3c 20 33 32 29 20 .if (curr < 32)
2e30: 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35 35 20 {....curr = 255
2e40: 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 73 65 - curr;...} else
2e50: 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 33 32 {....curr -= 32
2e60: 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 72 65 ;...}....if (pre
2e70: 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 09 64 v < curr) {....d
2e80: 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70 72 65 iff = curr - pre
2e90: 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 v;...} else {...
2ea0: 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d 20 63 .diff = prev - c
2eb0: 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 65 76 urr;...}....prev
2ec0: 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 74 76 = curr;....retv
2ed0: 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 74 76 al <<= 3;...retv
2ee0: 61 6c 20 26 3d 20 30 78 46 46 46 46 46 46 46 46 al &= 0xFFFFFFFF
2ef0: 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e 3d 20 LU;...retval ^=
2f00: 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 2b 2b diff;....value++
2f10: 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 3d 20 ;..}...retval =
2f20: 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c 75 73 retval % modulus
2f30: 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61 ;...return(retva
2f40: 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20 61 l);.}../* Find a
2f50: 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 65 64 mime-type based
2f60: 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 on the filename
2f70: 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 */.static const
2f80: 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 65 74 char *filed_det
2f90: 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65 28 ermine_mimetype(
2fa0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 const char *path
2fb0: 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 ) {..const char
2fc0: 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 63 68 *p;...p = strrch
2fd0: 72 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a 09 69 r(path, '.');..i
2fe0: 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b 0a f (p == NULL) {.
2ff0: 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f 44 ..return(FILED_D
3000: 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09 7d EFAULT_TYPE);..}
3010: 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a 70 20 ...p++;..if (*p
3020: 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65 74 == '\0') {...ret
3030: 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c urn(FILED_DEFAUL
3040: 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 66 69 T_TYPE);..}...fi
3050: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 led_log_msg_debu
3060: 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d 49 g("Looking up MI
3070: 4d 45 20 74 79 70 65 20 66 6f 72 20 25 73 20 28 ME type for %s (
3080: 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c 20 70 hash = %llu)", p
3090: 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 , (unsigned long
30a0: 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 61 73 long) filed_has
30b0: 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 h((const unsigne
30c0: 64 20 63 68 61 72 20 2a 29 20 70 2c 20 31 36 37 d char *) p, 167
30d0: 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6c 75 77259));..#inclu
30e0: 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 2d 74 de "filed-mime-t
30f0: 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 72 6e ypes.h"...return
3100: 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f 54 (FILED_DEFAULT_T
3110: 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e 65 YPE);.}../* Gene
3120: 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 69 64 rate a unique id
3130: 65 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 61 74 entifier */.stat
3140: 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67 65 ic void filed_ge
3150: 6e 65 72 61 74 65 5f 65 74 61 67 28 63 68 61 72 nerate_etag(char
3160: 20 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 20 6c *etag, size_t l
3170: 65 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 69 6e ength) {..snprin
3180: 74 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 68 2c tf(etag, length,
3190: 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78 25 "%llx-%llx%llx%
31a0: 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 6e 73 llx%llx",...(uns
31b0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
31c0: 20 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09 28 time(NULL),...(
31d0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f unsigned long lo
31e0: 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 09 ng) random(),...
31f0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
3200: 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09 ong) random(),..
3210: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 .(unsigned long
3220: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a long) random(),.
3230: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
3240: 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 0a long) random().
3250: 09 29 3b 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e 20 61 .);.}../* Open a
3260: 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72 6e file and return
3270: 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f file informatio
3280: 6e 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 n */.static stru
3290: 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 ct filed_fileinf
32a0: 6f 20 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f 66 69 o *filed_open_fi
32b0: 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 le(const char *p
32c0: 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c 65 ath, struct file
32d0: 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 75 66 66 d_fileinfo *buff
32e0: 65 72 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 er) {..struct fi
32f0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63 61 led_fileinfo *ca
3300: 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20 69 che;..unsigned i
3310: 6e 74 20 63 61 63 68 65 5f 69 64 78 3b 0a 09 6f nt cache_idx;..o
3320: 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 20 66 ff_t len;..int f
3330: 64 3b 0a 0a 09 63 61 63 68 65 5f 69 64 78 20 3d d;...cache_idx =
3340: 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f 6e filed_hash((con
3350: 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 st unsigned char
3360: 20 2a 29 20 70 61 74 68 2c 20 66 69 6c 65 64 5f *) path, filed_
3370: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 fileinfo_fdcache
3380: 5f 73 69 7a 65 29 3b 0a 0a 09 63 61 63 68 65 20 _size);...cache
3390: 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 = &filed_fileinf
33a0: 6f 5f 66 64 63 61 63 68 65 5b 63 61 63 68 65 5f o_fdcache[cache_
33b0: 69 64 78 5d 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f idx];...filed_lo
33c0: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f 63 g_msg_debug("Loc
33d0: 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69 king mutex for i
33e0: 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 dx: %lu", (unsig
33f0: 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f ned long) cache_
3400: 69 64 78 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f idx);...pthread_
3410: 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63 68 mutex_lock(&cach
3420: 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 66 69 6c e->mutex);...fil
3430: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
3440: 28 22 43 6f 6d 70 6c 65 74 65 64 20 6c 6f 63 6b ("Completed lock
3450: 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69 64 ing mutex for id
3460: 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67 6e x: %lu", (unsign
3470: 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 ed long) cache_i
3480: 64 78 29 3b 0a 0a 09 69 66 20 28 73 74 72 63 6d dx);...if (strcm
3490: 70 28 70 61 74 68 2c 20 63 61 63 68 65 2d 3e 70 p(path, cache->p
34a0: 61 74 68 29 20 21 3d 20 30 29 20 7b 0a 09 09 66 ath) != 0) {...f
34b0: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 iled_log_msg_deb
34c0: 75 67 28 22 43 61 63 68 65 20 6d 69 73 73 20 66 ug("Cache miss f
34d0: 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 4f 4c 44 or idx: %lu: OLD
34e0: 20 5c 22 25 73 5c 22 2c 20 4e 45 57 20 5c 22 25 \"%s\", NEW \"%
34f0: 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 s\"", (unsigned
3500: 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78 2c long) cache_idx,
3510: 20 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 70 61 cache->path, pa
3520: 74 68 29 3b 0a 0a 09 09 66 64 20 3d 20 6f 70 65 th);....fd = ope
3530: 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 n(path, O_RDONLY
3540: 20 7c 20 4f 5f 4c 41 52 47 45 46 49 4c 45 29 3b | O_LARGEFILE);
3550: 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b ...if (fd < 0) {
3560: 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65 ....pthread_mute
3570: 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65 2d x_unlock(&cache-
3580: 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 72 65 74 >mutex);.....ret
3590: 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a 0a urn(NULL);...}..
35a0: 09 09 69 66 20 28 63 61 63 68 65 2d 3e 66 64 20 ..if (cache->fd
35b0: 3e 3d 20 30 29 20 7b 0a 09 09 09 63 6c 6f 73 65 >= 0) {....close
35c0: 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09 7d (cache->fd);...}
35d0: 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73 65 65 6b 28 ....len = lseek(
35e0: 66 64 2c 20 30 2c 20 53 45 45 4b 5f 45 4e 44 29 fd, 0, SEEK_END)
35f0: 3b 0a 09 09 6c 73 65 65 6b 28 66 64 2c 20 30 2c ;...lseek(fd, 0,
3600: 20 53 45 45 4b 5f 53 45 54 29 3b 0a 0a 09 09 63 SEEK_SET);....c
3610: 61 63 68 65 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 ache->fd = fd;..
3620: 09 63 61 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c 65 .cache->len = le
3630: 6e 3b 0a 09 09 73 74 72 63 70 79 28 63 61 63 68 n;...strcpy(cach
3640: 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 3b 0a e->path, path);.
3650: 09 09 63 61 63 68 65 2d 3e 74 79 70 65 20 3d 20 ..cache->type =
3660: 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65 5f filed_determine_
3670: 6d 69 6d 65 74 79 70 65 28 70 61 74 68 29 3b 0a mimetype(path);.
3680: 09 09 66 69 6c 65 64 5f 67 65 6e 65 72 61 74 65 ..filed_generate
3690: 5f 65 74 61 67 28 63 61 63 68 65 2d 3e 65 74 61 _etag(cache->eta
36a0: 67 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65 2d g, sizeof(cache-
36b0: 3e 65 74 61 67 29 29 3b 0a 0a 09 09 2f 2a 20 58 >etag));..../* X
36c0: 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d 69 XX:TODO: Determi
36d0: 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e 6c ne */...cache->l
36e0: 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f 66 astmod = filed_f
36f0: 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 68 65 ormat_time(cache
3700: 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a ->lastmod_b, siz
3710: 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 74 6d eof(cache->lastm
3720: 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c 4c od_b), time(NULL
3730: 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73 65 ) - 30);..} else
3740: 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d {...filed_log_m
3750: 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65 20 sg_debug("Cache
3760: 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 6c 75 hit for idx: %lu
3770: 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 2c 20 : PATH \"%s\"",
3780: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 (unsigned long)
3790: 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 68 29 cache_idx, path)
37a0: 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57 65 ;..}.../*.. * We
37b0: 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 have to make a
37c0: 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 62 65 duplicate FD, be
37d0: 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 72 65 cause once we re
37e0: 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 65 0a lease the cache.
37f0: 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65 20 66 . * mutex, the f
3800: 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d ile descriptor m
3810: 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09 20 2a ay be closed.. *
3820: 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61 63 68 /..fd = dup(cach
3830: 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66 64 20 e->fd);..if (fd
3840: 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65 61 64 < 0) {...pthread
3850: 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 _mutex_unlock(&c
3860: 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 ache->mutex);...
3870: 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 .return(NULL);..
3880: 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64 20 3d }...buffer->fd =
3890: 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e 6c 65 fd;..buffer->le
38a0: 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e 3b 0a n = cache->len;.
38b0: 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 3d 20 .buffer->type =
38c0: 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 6d 65 cache->type;..me
38d0: 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61 73 mcpy(buffer->las
38e0: 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e 6c tmod_b, cache->l
38f0: 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f 66 astmod_b, sizeof
3900: 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 (buffer->lastmod
3910: 5f 62 29 29 3b 0a 09 6d 65 6d 63 70 79 28 62 75 _b));..memcpy(bu
3920: 66 66 65 72 2d 3e 65 74 61 67 2c 20 63 61 63 68 ffer->etag, cach
3930: 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66 28 e->etag, sizeof(
3940: 62 75 66 66 65 72 2d 3e 65 74 61 67 29 29 3b 0a buffer->etag));.
3950: 09 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f 64 .buffer->lastmod
3960: 20 3d 20 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d = buffer->lastm
3970: 6f 64 5f 62 20 2b 20 28 63 61 63 68 65 2d 3e 6c od_b + (cache->l
3980: 61 73 74 6d 6f 64 20 2d 20 63 61 63 68 65 2d 3e astmod - cache->
3990: 6c 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a 09 70 74 lastmod_b);...pt
39a0: 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f hread_mutex_unlo
39b0: 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 ck(&cache->mutex
39c0: 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 66 66 );...return(buff
39d0: 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f 63 65 er);.}../* Proce
39e0: 73 73 20 61 6e 20 48 54 54 50 20 72 65 71 75 65 ss an HTTP reque
39f0: 73 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 st and return th
3a00: 65 20 70 61 74 68 20 72 65 71 75 65 73 74 65 64 e path requested
3a10: 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 */.static struc
3a20: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 t filed_http_req
3a30: 75 65 73 74 20 2a 66 69 6c 65 64 5f 67 65 74 5f uest *filed_get_
3a40: 68 74 74 70 5f 72 65 71 75 65 73 74 28 46 49 4c http_request(FIL
3a50: 45 20 2a 66 70 2c 20 73 74 72 75 63 74 20 66 69 E *fp, struct fi
3a60: 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73 74 led_http_request
3a70: 20 2a 62 75 66 66 65 72 5f 73 74 2c 20 73 74 72 *buffer_st, str
3a80: 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e uct filed_option
3a90: 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 63 s *options) {..c
3aa0: 68 61 72 20 2a 6d 65 74 68 6f 64 2c 20 2a 70 61 har *method, *pa
3ab0: 74 68 3b 0a 09 63 68 61 72 20 2a 62 75 66 66 65 th;..char *buffe
3ac0: 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 2c 20 r, *workbuffer,
3ad0: 2a 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 74 *workbuffer_next
3ae0: 3b 0a 09 63 68 61 72 20 2a 66 67 65 74 73 5f 72 ;..char *fgets_r
3af0: 65 74 3b 0a 09 73 69 7a 65 5f 74 20 62 75 66 66 et;..size_t buff
3b00: 65 72 5f 6c 65 6e 2c 20 70 61 74 68 5f 6c 65 6e er_len, path_len
3b10: 3b 0a 09 6f 66 66 5f 74 20 72 61 6e 67 65 5f 73 ;..off_t range_s
3b20: 74 61 72 74 2c 20 72 61 6e 67 65 5f 65 6e 64 2c tart, range_end,
3b30: 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 09 range_length;..
3b40: 69 6e 74 20 72 61 6e 67 65 5f 72 65 71 75 65 73 int range_reques
3b50: 74 3b 0a 09 69 6e 74 20 73 6e 70 72 69 6e 74 66 t;..int snprintf
3b60: 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a 09 _ret;..int i;...
3b70: 2f 2a 20 53 65 74 20 74 6f 20 64 65 66 61 75 6c /* Set to defaul
3b80: 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 72 61 6e t values */..ran
3b90: 67 65 5f 73 74 61 72 74 20 3d 20 30 3b 0a 09 72 ge_start = 0;..r
3ba0: 61 6e 67 65 5f 65 6e 64 20 20 20 3d 20 30 3b 0a ange_end = 0;.
3bb0: 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 20 3d .range_request =
3bc0: 20 30 3b 0a 09 72 61 6e 67 65 5f 6c 65 6e 67 74 0;..range_lengt
3bd0: 68 20 3d 20 2d 31 3b 0a 09 62 75 66 66 65 72 5f h = -1;..buffer_
3be0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 st->headers.host
3bf0: 2e 70 72 65 73 65 6e 74 20 3d 20 30 3b 0a 09 62 .present = 0;..b
3c00: 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 uffer_st->header
3c10: 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 46 s.connection = F
3c20: 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f ILED_CONNECTION_
3c30: 43 4c 4f 53 45 3b 0a 0a 09 62 75 66 66 65 72 20 CLOSE;...buffer
3c40: 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 = buffer_st->tmp
3c50: 62 75 66 3b 0a 09 62 75 66 66 65 72 5f 6c 65 6e buf;..buffer_len
3c60: 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 65 72 = sizeof(buffer
3c70: 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a 09 _st->tmpbuf);...
3c80: 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67 65 74 fgets_ret = fget
3c90: 73 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 s(buffer, buffer
3ca0: 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 69 66 20 28 _len, fp);..if (
3cb0: 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c fgets_ret == NUL
3cc0: 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 L) {...return(NU
3cd0: 4c 4c 29 3b 0a 09 7d 0a 0a 09 6d 65 74 68 6f 64 LL);..}...method
3ce0: 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09 62 75 66 = buffer;...buf
3cf0: 66 65 72 20 3d 20 73 74 72 63 68 72 28 62 75 66 fer = strchr(buf
3d00: 66 65 72 2c 20 27 20 27 29 3b 0a 09 69 66 20 28 fer, ' ');..if (
3d10: 62 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29 20 buffer == NULL)
3d20: 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 {...return(NULL)
3d30: 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66 65 72 20 3d ;..}...*buffer =
3d40: 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 2b 2b '\0';..buffer++
3d50: 3b 0a 0a 09 70 61 74 68 20 3d 20 62 75 66 66 65 ;...path = buffe
3d60: 72 3b 0a 0a 09 2f 2a 20 54 65 72 6d 69 6e 61 74 r;.../* Terminat
3d70: 65 20 70 61 74 68 20 63 6f 6d 70 6f 6e 65 6e 74 e path component
3d80: 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 73 74 */..buffer = st
3d90: 72 70 62 72 6b 28 70 61 74 68 2c 20 22 5c 72 5c rpbrk(path, "\r\
3da0: 6e 20 22 29 3b 0a 09 69 66 20 28 62 75 66 66 65 n ");..if (buffe
3db0: 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 2a r != NULL) {...*
3dc0: 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 buffer = '\0';..
3dd0: 09 62 75 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a 09 .buffer++;..}...
3de0: 2f 2a 20 57 65 20 6f 6e 6c 79 20 68 61 6e 64 6c /* We only handl
3df0: 65 20 74 68 65 20 22 47 45 54 22 20 61 6e 64 20 e the "GET" and
3e00: 22 48 45 41 44 27 20 6d 65 74 68 6f 64 73 20 2a "HEAD' methods *
3e10: 2f 0a 09 69 66 20 28 73 74 72 63 61 73 65 63 6d /..if (strcasecm
3e20: 70 28 6d 65 74 68 6f 64 2c 20 22 68 65 61 64 22 p(method, "head"
3e30: 29 20 21 3d 20 30 29 20 7b 0a 09 09 69 66 20 28 ) != 0) {...if (
3e40: 73 74 72 63 61 73 65 63 6d 70 28 6d 65 74 68 6f strcasecmp(metho
3e50: 64 2c 20 22 67 65 74 22 29 20 21 3d 20 30 29 20 d, "get") != 0)
3e60: 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c {....return(NULL
3e70: 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 47 45 54 );...}..../* GET
3e80: 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62 75 request */...bu
3e90: 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 ffer_st->method
3ea0: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f = FILED_REQUEST_
3eb0: 4d 45 54 48 4f 44 5f 47 45 54 3b 0a 09 7d 20 65 METHOD_GET;..} e
3ec0: 6c 73 65 20 7b 0a 09 09 2f 2a 20 48 45 41 44 20 lse {.../* HEAD
3ed0: 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62 75 66 request */...buf
3ee0: 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20 3d fer_st->method =
3ef0: 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d FILED_REQUEST_M
3f00: 45 54 48 4f 44 5f 48 45 41 44 3b 0a 09 7d 0a 0a ETHOD_HEAD;..}..
3f10: 09 2f 2a 20 4e 6f 74 65 20 70 61 74 68 20 2a 2f ./* Note path */
3f20: 0a 09 70 61 74 68 5f 6c 65 6e 20 3d 20 73 74 72 ..path_len = str
3f30: 6c 65 6e 28 70 61 74 68 29 3b 0a 09 6d 65 6d 63 len(path);..memc
3f40: 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 py(buffer_st->pa
3f50: 74 68 2c 20 70 61 74 68 2c 20 70 61 74 68 5f 6c th, path, path_l
3f60: 65 6e 20 2b 20 31 29 3b 0a 0a 09 2f 2a 20 44 65 en + 1);.../* De
3f70: 74 65 72 6d 69 6e 65 20 74 79 70 65 20 6f 66 20 termine type of
3f80: 72 65 71 75 65 73 74 20 66 72 6f 6d 20 70 61 74 request from pat
3f90: 68 20 2a 2f 0a 09 69 66 20 28 70 61 74 68 5f 6c h */..if (path_l
3fa0: 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 62 75 66 en == 0) {...buf
3fb0: 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 fer_st->type = F
3fc0: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 ILED_REQUEST_TYP
3fd0: 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 7d 20 E_DIRECTORY;..}
3fe0: 65 6c 73 65 20 7b 0a 09 09 69 66 20 28 70 61 74 else {...if (pat
3ff0: 68 5b 70 61 74 68 5f 6c 65 6e 20 2d 20 31 5d 20 h[path_len - 1]
4000: 3d 3d 20 27 2f 27 29 20 7b 0a 09 09 09 62 75 66 == '/') {....buf
4010: 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 fer_st->type = F
4020: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 ILED_REQUEST_TYP
4030: 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 09 7d E_DIRECTORY;...}
4040: 20 65 6c 73 65 20 7b 0a 09 09 09 62 75 66 66 65 else {....buffe
4050: 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49 4c r_st->type = FIL
4060: 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f ED_REQUEST_TYPE_
4070: 4f 54 48 45 52 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 OTHER;...}..}...
4080: 2f 2a 20 52 65 73 65 74 20 62 75 66 66 65 72 20 /* Reset buffer
4090: 66 6f 72 20 6c 61 74 65 72 20 75 73 65 20 2a 2f for later use */
40a0: 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66 66 65 ..buffer = buffe
40b0: 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 0a 09 r_st->tmpbuf;...
40c0: 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 3c 20 for (i = 0; i <
40d0: 31 30 30 3b 20 69 2b 2b 29 20 7b 0a 09 09 66 67 100; i++) {...fg
40e0: 65 74 73 5f 72 65 74 20 3d 20 66 67 65 74 73 28 ets_ret = fgets(
40f0: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c buffer, buffer_l
4100: 65 6e 2c 20 66 70 29 3b 0a 09 09 69 66 20 28 66 en, fp);...if (f
4110: 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c 4c gets_ret == NULL
4120: 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 ) {....break;...
4130: 7d 0a 0a 09 09 69 66 20 28 73 74 72 6e 63 61 73 }....if (strncas
4140: 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 52 61 ecmp(buffer, "Ra
4150: 6e 67 65 3a 20 22 2c 20 37 29 20 3d 3d 20 30 29 nge: ", 7) == 0)
4160: 20 7b 0a 09 09 09 77 6f 72 6b 62 75 66 66 65 72 {....workbuffer
4170: 20 3d 20 62 75 66 66 65 72 20 2b 20 37 3b 0a 0a = buffer + 7;..
4180: 09 09 09 69 66 20 28 73 74 72 6e 63 61 73 65 63 ...if (strncasec
4190: 6d 70 28 77 6f 72 6b 62 75 66 66 65 72 2c 20 22 mp(workbuffer, "
41a0: 62 79 74 65 73 3d 22 2c 20 36 29 20 3d 3d 20 30 bytes=", 6) == 0
41b0: 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 ) {.....workbuff
41c0: 65 72 20 2b 3d 20 36 3b 0a 0a 09 09 09 09 72 61 er += 6;......ra
41d0: 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 31 3b nge_request = 1;
41e0: 0a 0a 09 09 09 09 72 61 6e 67 65 5f 73 74 61 72 ......range_star
41f0: 74 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f 72 t = strtoull(wor
4200: 6b 62 75 66 66 65 72 2c 20 26 77 6f 72 6b 62 75 kbuffer, &workbu
4210: 66 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b 0a ffer_next, 10);.
4220: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 20 .....workbuffer
4230: 3d 20 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78 = workbuffer_nex
4240: 74 3b 0a 0a 09 09 09 09 69 66 20 28 2a 77 6f 72 t;......if (*wor
4250: 6b 62 75 66 66 65 72 20 3d 3d 20 27 2d 27 29 20 kbuffer == '-')
4260: 7b 0a 09 09 09 09 09 77 6f 72 6b 62 75 66 66 65 {......workbuffe
4270: 72 2b 2b 3b 0a 0a 09 09 09 09 09 69 66 20 28 2a r++;.......if (*
4280: 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 27 5c workbuffer != '\
4290: 72 27 20 26 26 20 2a 77 6f 72 6b 62 75 66 66 65 r' && *workbuffe
42a0: 72 20 21 3d 20 27 5c 6e 27 29 20 7b 0a 09 09 09 r != '\n') {....
42b0: 09 09 09 72 61 6e 67 65 5f 65 6e 64 20 3d 20 73 ...range_end = s
42c0: 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66 66 trtoull(workbuff
42d0: 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72 5f er, &workbuffer_
42e0: 6e 65 78 74 2c 20 31 30 29 3b 0a 09 09 09 09 09 next, 10);......
42f0: 7d 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09 7d }.....}....}...}
4300: 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e 63 61 else if (strnca
4310: 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 48 secmp(buffer, "H
4320: 6f 73 74 3a 20 22 2c 20 35 29 20 3d 3d 20 30 29 ost: ", 5) == 0)
4330: 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74 2d {....buffer_st-
4340: 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 >headers.host.pr
4350: 65 73 65 6e 74 20 3d 20 31 3b 0a 0a 09 09 09 77 esent = 1;.....w
4360: 6f 72 6b 62 75 66 66 65 72 20 3d 20 73 74 72 70 orkbuffer = strp
4370: 62 72 6b 28 62 75 66 66 65 72 20 2b 20 35 2c 20 brk(buffer + 5,
4380: 22 5c 72 5c 6e 3a 22 29 3b 0a 09 09 09 69 66 20 "\r\n:");....if
4390: 28 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 4e (workbuffer != N
43a0: 55 4c 4c 29 20 7b 0a 09 09 09 09 2a 77 6f 72 6b ULL) {.....*work
43b0: 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a 09 buffer = '\0';..
43c0: 09 09 7d 0a 0a 09 09 09 77 6f 72 6b 62 75 66 66 ..}.....workbuff
43d0: 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20 35 3b er = buffer + 5;
43e0: 0a 09 09 09 77 68 69 6c 65 20 28 2a 77 6f 72 6b ....while (*work
43f0: 62 75 66 66 65 72 20 3d 3d 20 27 20 27 29 20 7b buffer == ' ') {
4400: 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72 2b .....workbuffer+
4410: 2b 3b 0a 09 09 09 7d 0a 0a 09 09 09 73 74 72 63 +;....}.....strc
4420: 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 py(buffer_st->he
4430: 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c aders.host.host,
4440: 20 77 6f 72 6b 62 75 66 66 65 72 29 3b 0a 09 09 workbuffer);...
4450: 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e 63 } else if (strnc
4460: 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 asecmp(buffer, "
4470: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 Connection: Keep
4480: 2d 41 6c 69 76 65 22 2c 20 32 32 29 20 3d 3d 20 -Alive", 22) ==
4490: 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 0) {....buffer_s
44a0: 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 t->headers.conne
44b0: 63 74 69 6f 6e 20 3d 20 46 49 4c 45 44 5f 43 4f ction = FILED_CO
44c0: 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c NNECTION_KEEP_AL
44d0: 49 56 45 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 IVE;...}....if (
44e0: 6d 65 6d 63 6d 70 28 62 75 66 66 65 72 2c 20 22 memcmp(buffer, "
44f0: 5c 72 5c 6e 22 2c 20 32 29 20 3d 3d 20 30 29 20 \r\n", 2) == 0)
4500: 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a {....break;...}.
4510: 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69 6e .}.../* Determin
4520: 65 20 72 61 6e 67 65 20 2a 2f 0a 09 69 66 20 28 e range */..if (
4530: 72 61 6e 67 65 5f 65 6e 64 20 21 3d 20 30 29 20 range_end != 0)
4540: 7b 0a 09 09 69 66 20 28 72 61 6e 67 65 5f 65 6e {...if (range_en
4550: 64 20 3c 3d 20 72 61 6e 67 65 5f 73 74 61 72 74 d <= range_start
4560: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 ) {....return(NU
4570: 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e 67 LL);...}....rang
4580: 65 5f 6c 65 6e 67 74 68 20 3d 20 72 61 6e 67 65 e_length = range
4590: 5f 65 6e 64 20 2d 20 72 61 6e 67 65 5f 73 74 61 _end - range_sta
45a0: 72 74 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 rt;....filed_log
45b0: 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d 70 _msg_debug("Comp
45c0: 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 70 61 72 uting length par
45d0: 61 6d 65 74 65 72 3a 20 25 6c 6c 75 20 3d 20 25 ameter: %llu = %
45e0: 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c 0a 09 09 09 llu - %llu",....
45f0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
4600: 6f 6e 67 29 20 72 61 6e 67 65 5f 6c 65 6e 67 74 ong) range_lengt
4610: 68 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 h,....(unsigned
4620: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67 65 long long) range
4630: 5f 65 6e 64 2c 0a 09 09 09 28 75 6e 73 69 67 6e _end,....(unsign
4640: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 ed long long) ra
4650: 6e 67 65 5f 73 74 61 72 74 0a 09 09 29 3b 0a 09 nge_start...);..
4660: 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 75 70 20 73 }.../* Fill up s
4670: 74 72 75 63 74 75 72 65 20 74 6f 20 72 65 74 75 tructure to retu
4680: 72 6e 20 2a 2f 0a 09 62 75 66 66 65 72 5f 73 74 rn */..buffer_st
4690: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e ->headers.range.
46a0: 70 72 65 73 65 6e 74 20 3d 20 72 61 6e 67 65 5f present = range_
46b0: 72 65 71 75 65 73 74 3b 0a 09 62 75 66 66 65 72 request;..buffer
46c0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e _st->headers.ran
46d0: 67 65 2e 6f 66 66 73 65 74 20 20 3d 20 72 61 6e ge.offset = ran
46e0: 67 65 5f 73 74 61 72 74 3b 0a 09 62 75 66 66 65 ge_start;..buffe
46f0: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 r_st->headers.ra
4700: 6e 67 65 2e 6c 65 6e 67 74 68 20 20 3d 20 72 61 nge.length = ra
4710: 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 0a 09 2f 2a nge_length;.../*
4720: 20 49 66 20 76 68 6f 73 74 73 20 61 72 65 20 65 If vhosts are e
4730: 6e 61 62 6c 65 64 2c 20 63 6f 6d 70 75 74 65 20 nabled, compute
4740: 6e 65 77 20 70 61 74 68 20 2a 2f 0a 09 69 66 20 new path */..if
4750: 28 6f 70 74 69 6f 6e 73 2d 3e 76 68 6f 73 74 73 (options->vhosts
4760: 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 69 66 _enabled) {...if
4770: 20 28 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 (buffer_st->hea
4780: 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65 6e ders.host.presen
4790: 74 20 3d 3d 20 31 29 20 7b 0a 09 09 09 62 75 66 t == 1) {....buf
47a0: 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74 2d fer = buffer_st-
47b0: 3e 74 6d 70 62 75 66 3b 0a 09 09 09 62 75 66 66 >tmpbuf;....buff
47c0: 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 er_len = sizeof(
47d0: 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62 75 buffer_st->tmpbu
47e0: 66 29 3b 0a 0a 09 09 09 73 6e 70 72 69 6e 74 66 f);.....snprintf
47f0: 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e 74 66 28 _ret = snprintf(
4800: 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f 6c buffer, buffer_l
4810: 65 6e 2c 20 22 2f 25 73 25 73 25 73 22 2c 0a 09 en, "/%s%s%s",..
4820: 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 ...buffer_st->he
4830: 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 74 2c aders.host.host,
4840: 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e .....buffer_st->
4850: 70 61 74 68 5b 30 5d 20 3d 3d 20 27 2f 27 20 3f path[0] == '/' ?
4860: 20 22 22 20 3a 20 22 2f 22 2c 0a 09 09 09 09 62 "" : "/",.....b
4870: 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 0a 09 uffer_st->path..
4880: 09 09 29 3b 0a 09 09 09 69 66 20 28 73 6e 70 72 ..);....if (snpr
4890: 69 6e 74 66 5f 72 65 74 20 3e 3d 20 30 29 20 7b intf_ret >= 0) {
48a0: 0a 09 09 09 09 69 66 20 28 28 28 75 6e 73 69 67 .....if (((unsig
48b0: 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 69 6e 74 ned int) snprint
48c0: 66 5f 72 65 74 29 20 3c 20 62 75 66 66 65 72 5f f_ret) < buffer_
48d0: 6c 65 6e 29 20 7b 0a 09 09 09 09 09 73 74 72 63 len) {......strc
48e0: 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 py(buffer_st->pa
48f0: 74 68 2c 20 62 75 66 66 65 72 29 3b 0a 09 09 09 th, buffer);....
4900: 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a 0a .}....}...}..}..
4910: 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 5f 73 .return(buffer_s
4920: 74 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e t);.}../* Return
4930: 20 61 6e 20 65 72 72 6f 72 20 70 61 67 65 20 2a an error page *
4940: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 /.static void fi
4950: 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28 46 led_error_page(F
4960: 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20 63 ILE *fp, const c
4970: 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65 6e har *date_curren
4980: 74 2c 20 69 6e 74 20 65 72 72 6f 72 5f 6e 75 6d t, int error_num
4990: 62 65 72 2c 20 69 6e 74 20 6d 65 74 68 6f 64 2c ber, int method,
49a0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 61 const char *rea
49b0: 73 6f 6e 2c 20 73 74 72 75 63 74 20 66 69 6c 65 son, struct file
49c0: 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 d_log_entry *log
49d0: 29 20 7b 0a 09 63 68 61 72 20 2a 65 72 72 6f 72 ) {..char *error
49e0: 5f 73 74 72 69 6e 67 20 3d 20 22 3c 68 74 6d 6c _string = "<html
49f0: 3e 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 45 52 ><head><title>ER
4a00: 52 4f 52 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65 61 ROR</title></hea
4a10: 64 3e 3c 62 6f 64 79 3e 55 6e 61 62 6c 65 20 74 d><body>Unable t
4a20: 6f 20 70 72 6f 63 65 73 73 20 72 65 71 75 65 73 o process reques
4a30: 74 3c 2f 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e 22 t</body></html>"
4a40: 3b 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 ;...fprintf(fp,
4a50: 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4e 6f 74 "HTTP/1.1 %i Not
4a60: 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c OK\r\nDate: %s\
4a70: 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 64 r\nServer: filed
4a80: 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65 \r\nLast-Modifie
4a90: 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74 d: %s\r\nContent
4aa0: 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c -Length: %llu\r\
4ab0: 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 nContent-Type: %
4ac0: 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a s\r\nConnection:
4ad0: 20 63 6c 6f 73 65 5c 72 5c 6e 5c 72 5c 6e 22 2c close\r\n\r\n",
4ae0: 0a 09 09 65 72 72 6f 72 5f 6e 75 6d 62 65 72 2c ...error_number,
4af0: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c ...date_current,
4b00: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c ...date_current,
4b10: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e ...(unsigned lon
4b20: 67 20 6c 6f 6e 67 29 20 73 74 72 6c 65 6e 28 65 g long) strlen(e
4b30: 72 72 6f 72 5f 73 74 72 69 6e 67 29 2c 0a 09 09 rror_string),...
4b40: 22 74 65 78 74 2f 68 74 6d 6c 22 0a 09 29 3b 0a "text/html"..);.
4b50: 0a 09 2f 2a 20 73 69 6c 65 6e 63 65 20 65 72 72 ../* silence err
4b60: 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 48 45 or string for HE
4b70: 41 44 20 72 65 71 75 65 73 74 73 20 2a 2f 0a 09 AD requests */..
4b80: 69 66 20 28 6d 65 74 68 6f 64 20 21 3d 20 46 49 if (method != FI
4b90: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 LED_REQUEST_METH
4ba0: 4f 44 5f 48 45 41 44 29 20 7b 0a 09 09 66 70 72 OD_HEAD) {...fpr
4bb0: 69 6e 74 66 28 66 70 2c 20 22 25 73 22 2c 20 65 intf(fp, "%s", e
4bc0: 72 72 6f 72 5f 73 74 72 69 6e 67 29 3b 0a 09 7d rror_string);..}
4bd0: 0a 0a 09 2f 2a 20 4c 6f 67 20 65 72 72 6f 72 20 .../* Log error
4be0: 2a 2f 0a 09 2f 2a 2a 20 72 65 61 73 6f 6e 20 6d */../** reason m
4bf0: 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 67 ust point to a g
4c00: 6c 6f 62 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 lobally allocate
4c10: 64 20 76 61 6c 75 65 20 2a 2a 2f 0a 09 6c 6f 67 d value **/..log
4c20: 2d 3e 72 65 61 73 6f 6e 20 3d 20 72 65 61 73 6f ->reason = reaso
4c30: 6e 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f n;..log->http_co
4c40: 64 65 20 3d 20 65 72 72 6f 72 5f 6e 75 6d 62 65 de = error_numbe
4c50: 72 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 r;...filed_log_e
4c60: 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 ntry(log);.../*
4c70: 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e Close connection
4c80: 20 2a 2f 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b */..fclose(fp);
4c90: 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a ...return;.}../*
4ca0: 20 52 65 74 75 72 6e 20 61 20 72 65 64 69 72 65 Return a redire
4cb0: 63 74 20 74 6f 20 69 6e 64 65 78 2e 68 74 6d 6c ct to index.html
4cc0: 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 */.static void
4cd0: 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74 5f 69 filed_redirect_i
4ce0: 6e 64 65 78 28 46 49 4c 45 20 2a 66 70 2c 20 63 ndex(FILE *fp, c
4cf0: 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65 5f onst char *date_
4d00: 63 75 72 72 65 6e 74 2c 20 63 6f 6e 73 74 20 63 current, const c
4d10: 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75 63 har *path, struc
4d20: 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 t filed_log_entr
4d30: 79 20 2a 6c 6f 67 29 20 7b 0a 09 69 6e 74 20 68 y *log) {..int h
4d40: 74 74 70 5f 63 6f 64 65 20 3d 20 33 30 31 3b 0a ttp_code = 301;.
4d50: 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48 54 .fprintf(fp, "HT
4d60: 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e TP/1.1 %i OK\r\n
4d70: 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 Date: %s\r\nServ
4d80: 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 er: filed\r\nLas
4d90: 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 t-Modified: %s\r
4da0: 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 \nContent-Length
4db0: 3a 20 30 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f : 0\r\nConnectio
4dc0: 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 4c 6f 63 61 n: close\r\nLoca
4dd0: 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 5c 72 5c 6e tion: %s\r\n\r\n
4de0: 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a ",...http_code,.
4df0: 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a ..date_current,.
4e00: 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a ..date_current,.
4e10: 09 09 22 69 6e 64 65 78 2e 68 74 6d 6c 22 0a 09 .."index.html"..
4e20: 29 3b 0a 0a 09 2f 2a 20 4c 6f 67 20 72 65 64 69 );.../* Log redi
4e30: 72 65 63 74 20 2a 2f 0a 09 6c 6f 67 2d 3e 72 65 rect */..log->re
4e40: 61 73 6f 6e 20 3d 20 22 72 65 64 69 72 65 63 74 ason = "redirect
4e50: 22 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f ";..log->http_co
4e60: 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 3b 0a de = http_code;.
4e70: 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 ..filed_log_entr
4e80: 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c 6f y(log);.../* Clo
4e90: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f se connection */
4ea0: 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a 09 ..fclose(fp);...
4eb0: 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 43 75 72 return;.../* Cur
4ec0: 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 3a 20 70 rently unused: p
4ed0: 61 74 68 20 2a 2f 0a 09 70 61 74 68 20 3d 20 70 ath */..path = p
4ee0: 61 74 68 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76 65 ath;.}../* Conve
4ef0: 72 74 20 61 6e 20 65 6e 75 6d 20 72 65 70 72 65 rt an enum repre
4f00: 73 65 6e 74 69 6e 67 20 74 68 65 20 22 43 6f 6e senting the "Con
4f10: 6e 65 63 74 69 6f 6e 22 20 68 65 61 64 65 72 20 nection" header
4f20: 76 61 6c 75 65 20 74 6f 20 61 20 73 74 72 69 6e value to a strin
4f30: 67 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 g */.static cons
4f40: 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 63 6f t char *filed_co
4f50: 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 69 6e 74 nnection_str(int
4f60: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 connection_valu
4f70: 65 29 20 7b 0a 09 73 77 69 74 63 68 20 28 63 6f e) {..switch (co
4f80: 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65 29 20 nnection_value)
4f90: 7b 0a 09 09 63 61 73 65 20 46 49 4c 45 44 5f 43 {...case FILED_C
4fa0: 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45 3a ONNECTION_CLOSE:
4fb0: 0a 09 09 09 72 65 74 75 72 6e 28 22 63 6c 6f 73 ....return("clos
4fc0: 65 22 29 3b 0a 09 09 63 61 73 65 20 46 49 4c 45 e");...case FILE
4fd0: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 D_CONNECTION_KEE
4fe0: 50 5f 41 4c 49 56 45 3a 0a 09 09 09 72 65 74 75 P_ALIVE:....retu
4ff0: 72 6e 28 22 6b 65 65 70 2d 61 6c 69 76 65 22 29 rn("keep-alive")
5000: 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 22 63 ;..}...return("c
5010: 6c 6f 73 65 22 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 lose");.}../* Ha
5020: 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65 20 72 65 ndle a single re
5030: 71 75 65 73 74 20 66 72 6f 6d 20 61 20 63 6c 69 quest from a cli
5040: 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e ent */.static in
5050: 74 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f 63 t filed_handle_c
5060: 6c 69 65 6e 74 28 69 6e 74 20 66 64 2c 20 73 74 lient(int fd, st
5070: 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f ruct filed_http_
5080: 72 65 71 75 65 73 74 20 2a 72 65 71 75 65 73 74 request *request
5090: 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c , struct filed_l
50a0: 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 73 og_entry *log, s
50b0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 truct filed_opti
50c0: 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a ons *options) {.
50d0: 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66 69 .struct filed_fi
50e0: 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 69 6e 66 6f leinfo *fileinfo
50f0: 3b 0a 09 73 73 69 7a 65 5f 74 20 73 65 6e 64 66 ;..ssize_t sendf
5100: 69 6c 65 5f 72 65 74 3b 0a 09 73 69 7a 65 5f 74 ile_ret;..size_t
5110: 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 3b 0a sendfile_size;.
5120: 09 6f 66 66 5f 74 20 73 65 6e 64 66 69 6c 65 5f .off_t sendfile_
5130: 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 offset, sendfile
5140: 5f 73 65 6e 74 2c 20 73 65 6e 64 66 69 6c 65 5f _sent, sendfile_
5150: 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 70 61 74 68 len;..char *path
5160: 3b 0a 09 63 68 61 72 20 2a 64 61 74 65 5f 63 75 ;..char *date_cu
5170: 72 72 65 6e 74 2c 20 64 61 74 65 5f 63 75 72 72 rrent, date_curr
5180: 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 69 6e 74 20 ent_b[64];..int
5190: 68 74 74 70 5f 63 6f 64 65 3b 0a 09 46 49 4c 45 http_code;..FILE
51a0: 20 2a 66 70 3b 0a 0a 09 2f 2a 20 44 65 74 65 72 *fp;.../* Deter
51b0: 6d 69 6e 65 20 63 75 72 72 65 6e 74 20 74 69 6d mine current tim
51c0: 65 20 2a 2f 0a 09 64 61 74 65 5f 63 75 72 72 65 e */..date_curre
51d0: 6e 74 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d 61 nt = filed_forma
51e0: 74 5f 74 69 6d 65 28 64 61 74 65 5f 63 75 72 72 t_time(date_curr
51f0: 65 6e 74 5f 62 2c 20 73 69 7a 65 6f 66 28 64 61 ent_b, sizeof(da
5200: 74 65 5f 63 75 72 72 65 6e 74 5f 62 29 2c 20 74 te_current_b), t
5210: 69 6d 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f 2a ime(NULL));.../*
5220: 20 4f 70 65 6e 20 73 6f 63 6b 65 74 20 61 73 20 Open socket as
5230: 41 4e 53 49 20 49 2f 4f 20 66 6f 72 20 65 61 73 ANSI I/O for eas
5240: 65 20 6f 66 20 75 73 65 20 2a 2f 0a 09 66 70 20 e of use */..fp
5250: 3d 20 66 64 6f 70 65 6e 28 66 64 2c 20 22 77 2b = fdopen(fd, "w+
5260: 62 22 29 3b 0a 09 69 66 20 28 66 70 20 3d 3d 20 b");..if (fp ==
5270: 4e 55 4c 4c 29 20 7b 0a 09 09 63 6c 6f 73 65 28 NULL) {...close(
5280: 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d 3e 62 75 66 fd);....log->buf
5290: 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 fer[0] = '\0';..
52a0: 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 .log->http_code
52b0: 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d 3e 72 65 61 = -1;...log->rea
52c0: 73 6f 6e 20 3d 20 22 66 64 6f 70 65 6e 5f 66 61 son = "fdopen_fa
52d0: 69 6c 65 64 22 3b 0a 0a 09 09 66 69 6c 65 64 5f iled";....filed_
52e0: 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a log_entry(log);.
52f0: 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d ...return(0);..}
5300: 0a 0a 09 72 65 71 75 65 73 74 20 3d 20 66 69 6c ...request = fil
5310: 65 64 5f 67 65 74 5f 68 74 74 70 5f 72 65 71 75 ed_get_http_requ
5320: 65 73 74 28 66 70 2c 20 72 65 71 75 65 73 74 2c est(fp, request,
5330: 20 6f 70 74 69 6f 6e 73 29 3b 0a 0a 09 69 66 20 options);...if
5340: 28 72 65 71 75 65 73 74 20 3d 3d 20 4e 55 4c 4c (request == NULL
5350: 29 20 7b 0a 09 09 6c 6f 67 2d 3e 62 75 66 66 65 ) {...log->buffe
5360: 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 09 09 r[0] = '\0';....
5370: 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 filed_error_page
5380: 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 6e (fp, date_curren
5390: 74 2c 20 35 30 30 2c 20 46 49 4c 45 44 5f 52 45 t, 500, FILED_RE
53a0: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
53b0: 2c 20 22 66 6f 72 6d 61 74 22 2c 20 6c 6f 67 29 , "format", log)
53c0: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a ;....return(0);.
53d0: 09 7d 0a 0a 09 70 61 74 68 20 3d 20 72 65 71 75 .}...path = requ
53e0: 65 73 74 2d 3e 70 61 74 68 3b 0a 09 73 74 72 63 est->path;..strc
53f0: 70 79 28 6c 6f 67 2d 3e 62 75 66 66 65 72 2c 20 py(log->buffer,
5400: 70 61 74 68 29 3b 0a 09 6c 6f 67 2d 3e 6d 65 74 path);..log->met
5410: 68 6f 64 20 3d 20 72 65 71 75 65 73 74 2d 3e 6d hod = request->m
5420: 65 74 68 6f 64 3b 0a 0a 09 2f 2a 20 49 66 20 74 ethod;.../* If t
5430: 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61 74 he requested pat
5440: 68 20 69 73 20 61 20 64 69 72 65 63 74 6f 72 79 h is a directory
5450: 2c 20 72 65 64 69 72 65 63 74 20 74 6f 20 69 6e , redirect to in
5460: 64 65 78 20 70 61 67 65 20 2a 2f 0a 09 69 66 20 dex page */..if
5470: 28 72 65 71 75 65 73 74 2d 3e 74 79 70 65 20 3d (request->type =
5480: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f = FILED_REQUEST_
5490: 54 59 50 45 5f 44 49 52 45 43 54 4f 52 59 29 20 TYPE_DIRECTORY)
54a0: 7b 0a 09 09 66 69 6c 65 64 5f 72 65 64 69 72 65 {...filed_redire
54b0: 63 74 5f 69 6e 64 65 78 28 66 70 2c 20 64 61 74 ct_index(fp, dat
54c0: 65 5f 63 75 72 72 65 6e 74 2c 20 70 61 74 68 2c e_current, path,
54d0: 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72 6e log);....return
54e0: 28 30 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 69 6e (0);..}...filein
54f0: 66 6f 20 3d 20 66 69 6c 65 64 5f 6f 70 65 6e 5f fo = filed_open_
5500: 66 69 6c 65 28 70 61 74 68 2c 20 26 72 65 71 75 file(path, &requ
5510: 65 73 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 3b 0a est->fileinfo);.
5520: 09 69 66 20 28 66 69 6c 65 69 6e 66 6f 20 3d 3d .if (fileinfo ==
5530: 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64 NULL) {...filed
5540: 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20 _error_page(fp,
5550: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 30 date_current, 40
5560: 34 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 4, request->meth
5570: 6f 64 2c 20 22 6f 70 65 6e 5f 66 61 69 6c 65 64 od, "open_failed
5580: 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 ", log);....retu
5590: 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 rn(0);..}...if (
55a0: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 request->headers
55b0: 2e 72 61 6e 67 65 2e 70 72 65 73 65 6e 74 29 20 .range.present)
55c0: 7b 0a 09 09 69 66 20 28 72 65 71 75 65 73 74 2d {...if (request-
55d0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f >headers.range.o
55e0: 66 66 73 65 74 20 21 3d 20 30 20 7c 7c 20 72 65 ffset != 0 || re
55f0: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 quest->headers.r
5600: 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3e 3d 20 30 ange.length >= 0
5610: 29 20 7b 0a 09 09 09 69 66 20 28 72 65 71 75 65 ) {....if (reque
5620: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
5630: 65 2e 6f 66 66 73 65 74 20 3e 3d 20 66 69 6c 65 e.offset >= file
5640: 69 6e 66 6f 2d 3e 6c 65 6e 29 20 7b 0a 09 09 09 info->len) {....
5650: 09 66 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 .filed_error_pag
5660: 65 28 66 70 2c 20 64 61 74 65 5f 63 75 72 72 65 e(fp, date_curre
5670: 6e 74 2c 20 34 31 36 2c 20 72 65 71 75 65 73 74 nt, 416, request
5680: 2d 3e 6d 65 74 68 6f 64 2c 20 22 72 61 6e 67 65 ->method, "range
5690: 5f 69 6e 76 61 6c 69 64 22 2c 20 6c 6f 67 29 3b _invalid", log);
56a0: 0a 0a 09 09 09 09 63 6c 6f 73 65 28 66 69 6c 65 ......close(file
56b0: 69 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 09 09 info->fd);......
56c0: 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 09 7d 0a return(0);....}.
56d0: 0a 09 09 09 69 66 20 28 72 65 71 75 65 73 74 2d ....if (request-
56e0: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6c >headers.range.l
56f0: 65 6e 67 74 68 20 3d 3d 20 28 28 6f 66 66 5f 74 ength == ((off_t
5700: 29 20 2d 31 29 29 20 7b 0a 09 09 09 09 66 69 6c ) -1)) {.....fil
5710: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75 67 ed_log_msg_debug
5720: 28 22 43 6f 6d 70 75 74 69 6e 67 20 6c 65 6e 67 ("Computing leng
5730: 74 68 20 74 6f 20 66 69 74 20 69 6e 20 62 6f 75 th to fit in bou
5740: 6e 64 73 3a 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c nds: fileinfo->l
5750: 65 6e 20 3d 20 25 6c 6c 75 2c 20 72 65 71 75 65 en = %llu, reque
5760: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
5770: 65 2e 6f 66 66 73 65 74 20 3d 20 25 6c 6c 75 22 e.offset = %llu"
5780: 2c 0a 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 ,......(unsigned
5790: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 long long) file
57a0: 69 6e 66 6f 2d 3e 6c 65 6e 2c 0a 09 09 09 09 09 info->len,......
57b0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
57c0: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 ong) request->he
57d0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
57e0: 65 74 0a 09 09 09 09 29 3b 0a 0a 09 09 09 09 72 et.....);......r
57f0: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
5800: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 3d 20 66 range.length = f
5810: 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 2d 20 72 ileinfo->len - r
5820: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
5830: 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b 0a 09 09 range.offset;...
5840: 09 7d 0a 0a 09 09 09 66 69 6c 65 64 5f 6c 6f 67 .}.....filed_log
5850: 5f 6d 73 67 5f 64 65 62 75 67 28 22 50 61 72 74 _msg_debug("Part
5860: 69 61 6c 20 72 65 71 75 65 73 74 2c 20 73 74 61 ial request, sta
5870: 72 74 69 6e 67 20 61 74 3a 20 25 6c 6c 75 20 61 rting at: %llu a
5880: 6e 64 20 72 75 6e 6e 69 6e 67 20 66 6f 72 20 25 nd running for %
5890: 6c 6c 69 20 62 79 74 65 73 22 2c 0a 09 09 09 09 lli bytes",.....
58a0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c (unsigned long l
58b0: 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 ong) request->he
58c0: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
58d0: 65 74 2c 0a 09 09 09 09 28 6c 6f 6e 67 20 6c 6f et,.....(long lo
58e0: 6e 67 29 20 72 65 71 75 65 73 74 2d 3e 68 65 61 ng) request->hea
58f0: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 ders.range.lengt
5900: 68 0a 09 09 09 29 3b 0a 0a 09 09 7d 0a 0a 09 09 h....);....}....
5910: 68 74 74 70 5f 63 6f 64 65 20 3d 20 32 30 36 3b http_code = 206;
5920: 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09 68 74 74 ..} else {...htt
5930: 70 5f 63 6f 64 65 20 3d 20 32 30 30 3b 0a 0a 09 p_code = 200;...
5940: 09 2f 2a 20 43 6f 6d 70 75 74 65 20 66 61 6b 65 ./* Compute fake
5950: 20 72 61 6e 67 65 20 70 61 72 61 6d 65 74 65 72 range parameter
5960: 73 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73 20 s that includes
5970: 74 68 65 20 65 6e 74 69 72 65 20 66 69 6c 65 20 the entire file
5980: 2a 2f 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 65 */...request->he
5990: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 aders.range.offs
59a0: 65 74 20 3d 20 30 3b 0a 09 09 72 65 71 75 65 73 et = 0;...reques
59b0: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
59c0: 2e 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e .length = filein
59d0: 66 6f 2d 3e 6c 65 6e 3b 0a 09 7d 0a 0a 09 66 70 fo->len;..}...fp
59e0: 72 69 6e 74 66 28 66 70 2c 20 22 48 54 54 50 2f rintf(fp, "HTTP/
59f0: 31 2e 31 20 25 69 20 4f 4b 5c 72 5c 6e 44 61 74 1.1 %i OK\r\nDat
5a00: 65 3a 20 25 73 5c 72 5c 6e 53 65 72 76 65 72 3a e: %s\r\nServer:
5a10: 20 66 69 6c 65 64 5c 72 5c 6e 4c 61 73 74 2d 4d filed\r\nLast-M
5a20: 6f 64 69 66 69 65 64 3a 20 25 73 5c 72 5c 6e 43 odified: %s\r\nC
5a30: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 ontent-Length: %
5a40: 6c 6c 75 5c 72 5c 6e 41 63 63 65 70 74 2d 52 61 llu\r\nAccept-Ra
5a50: 6e 67 65 73 3a 20 62 79 74 65 73 5c 72 5c 6e 43 nges: bytes\r\nC
5a60: 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 25 73 5c ontent-Type: %s\
5a70: 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 25 r\nConnection: %
5a80: 73 5c 72 5c 6e 45 54 61 67 3a 20 5c 22 25 73 5c s\r\nETag: \"%s\
5a90: 22 5c 72 5c 6e 22 2c 0a 09 09 68 74 74 70 5f 63 "\r\n",...http_c
5aa0: 6f 64 65 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 ode,...date_curr
5ab0: 65 6e 74 2c 0a 09 09 66 69 6c 65 69 6e 66 6f 2d ent,...fileinfo-
5ac0: 3e 6c 61 73 74 6d 6f 64 2c 0a 09 09 28 75 6e 73 >lastmod,...(uns
5ad0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 igned long long)
5ae0: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 request->header
5af0: 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 2c 0a s.range.length,.
5b00: 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 74 79 70 65 ..fileinfo->type
5b10: 2c 0a 09 09 66 69 6c 65 64 5f 63 6f 6e 6e 65 63 ,...filed_connec
5b20: 74 69 6f 6e 5f 73 74 72 28 72 65 71 75 65 73 74 tion_str(request
5b30: 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 ->headers.connec
5b40: 74 69 6f 6e 29 2c 0a 09 09 66 69 6c 65 69 6e 66 tion),...fileinf
5b50: 6f 2d 3e 65 74 61 67 0a 09 29 3b 0a 0a 09 69 66 o->etag..);...if
5b60: 20 28 68 74 74 70 5f 63 6f 64 65 20 3d 3d 20 32 (http_code == 2
5b70: 30 36 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 06) {...fprintf(
5b80: 66 70 2c 20 22 43 6f 6e 74 65 6e 74 2d 52 61 6e fp, "Content-Ran
5b90: 67 65 3a 20 62 79 74 65 73 20 25 6c 6c 75 2d 25 ge: bytes %llu-%
5ba0: 6c 6c 75 2f 25 6c 6c 75 5c 72 5c 6e 22 2c 0a 09 llu/%llu\r\n",..
5bb0: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 ..(unsigned long
5bc0: 20 6c 6f 6e 67 29 20 72 65 71 75 65 73 74 2d 3e long) request->
5bd0: 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 headers.range.of
5be0: 66 73 65 74 2c 0a 09 09 09 28 75 6e 73 69 67 6e fset,....(unsign
5bf0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 28 72 ed long long) (r
5c00: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
5c10: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 2b 20 72 range.offset + r
5c20: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e equest->headers.
5c30: 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20 2d 20 31 range.length - 1
5c40: 29 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64 20 ),....(unsigned
5c50: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 66 69 6c 65 69 long long) filei
5c60: 6e 66 6f 2d 3e 6c 65 6e 0a 09 09 29 3b 0a 09 7d nfo->len...);..}
5c70: 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 5c ..fprintf(fp, "\
5c80: 72 5c 6e 22 29 3b 0a 09 66 66 6c 75 73 68 28 66 r\n");..fflush(f
5c90: 70 29 3b 0a 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f p);...log->http_
5ca0: 63 6f 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 code = http_code
5cb0: 3b 0a 09 6c 6f 67 2d 3e 72 65 61 73 6f 6e 20 3d ;..log->reason =
5cc0: 20 22 4f 4b 22 3b 0a 09 6c 6f 67 2d 3e 73 74 61 "OK";..log->sta
5cd0: 72 74 74 69 6d 65 20 3d 20 74 69 6d 65 28 4e 55 rttime = time(NU
5ce0: 4c 4c 29 3b 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6f LL);..log->req_o
5cf0: 66 66 73 65 74 20 3d 20 72 65 71 75 65 73 74 2d ffset = request-
5d00: 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f >headers.range.o
5d10: 66 66 73 65 74 3b 0a 09 6c 6f 67 2d 3e 72 65 71 ffset;..log->req
5d20: 5f 6c 65 6e 67 74 68 20 3d 20 72 65 71 75 65 73 _length = reques
5d30: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
5d40: 2e 6c 65 6e 67 74 68 3b 0a 09 6c 6f 67 2d 3e 66 .length;..log->f
5d50: 69 6c 65 5f 6c 65 6e 67 74 68 20 3d 20 66 69 6c ile_length = fil
5d60: 65 69 6e 66 6f 2d 3e 6c 65 6e 3b 0a 0a 23 69 66 einfo->len;..#if
5d70: 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f def FILED_NONBLO
5d80: 43 4b 5f 48 54 54 50 0a 09 69 6e 74 20 73 6f 63 CK_HTTP..int soc
5d90: 6b 65 74 5f 66 6c 61 67 73 3b 0a 09 66 64 5f 73 ket_flags;..fd_s
5da0: 65 74 20 72 66 64 2c 20 77 66 64 3b 0a 09 63 68 et rfd, wfd;..ch
5db0: 61 72 20 73 69 6e 6b 62 75 66 5b 38 31 39 32 5d ar sinkbuf[8192]
5dc0: 3b 0a 09 73 73 69 7a 65 5f 74 20 72 65 61 64 5f ;..ssize_t read_
5dd0: 72 65 74 3b 0a 0a 09 46 44 5f 5a 45 52 4f 28 26 ret;...FD_ZERO(&
5de0: 72 66 64 29 3b 0a 09 46 44 5f 5a 45 52 4f 28 26 rfd);..FD_ZERO(&
5df0: 77 66 64 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 wfd);..FD_SET(fd
5e00: 2c 20 26 72 66 64 29 3b 0a 09 46 44 5f 53 45 54 , &rfd);..FD_SET
5e10: 28 66 64 2c 20 26 77 66 64 29 3b 0a 0a 09 73 6f (fd, &wfd);...so
5e20: 63 6b 65 74 5f 66 6c 61 67 73 20 3d 20 66 63 6e cket_flags = fcn
5e30: 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 4c 29 3b tl(fd, F_GETFL);
5e40: 0a 09 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 ..fcntl(fd, F_SE
5e50: 54 46 4c 2c 20 73 6f 63 6b 65 74 5f 66 6c 61 67 TFL, socket_flag
5e60: 73 20 7c 20 4f 5f 4e 4f 4e 42 4c 4f 43 4b 29 3b s | O_NONBLOCK);
5e70: 0a 23 65 6e 64 69 66 0a 09 73 65 6e 64 66 69 6c .#endif..sendfil
5e80: 65 5f 6f 66 66 73 65 74 20 3d 20 72 65 71 75 65 e_offset = reque
5e90: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 st->headers.rang
5ea0: 65 2e 6f 66 66 73 65 74 3b 0a 09 73 65 6e 64 66 e.offset;..sendf
5eb0: 69 6c 65 5f 6c 65 6e 20 3d 20 72 65 71 75 65 73 ile_len = reques
5ec0: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 t->headers.range
5ed0: 2e 6c 65 6e 67 74 68 3b 0a 09 73 65 6e 64 66 69 .length;..sendfi
5ee0: 6c 65 5f 73 65 6e 74 20 3d 20 30 3b 0a 09 77 68 le_sent = 0;..wh
5ef0: 69 6c 65 20 28 72 65 71 75 65 73 74 2d 3e 6d 65 ile (request->me
5f00: 74 68 6f 64 20 3d 3d 20 46 49 4c 45 44 5f 52 45 thod == FILED_RE
5f10: 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 QUEST_METHOD_GET
5f20: 29 20 7b 0a 09 09 69 66 20 28 73 65 6e 64 66 69 ) {...if (sendfi
5f30: 6c 65 5f 6c 65 6e 20 3e 20 46 49 4c 45 44 5f 53 le_len > FILED_S
5f40: 45 4e 44 46 49 4c 45 5f 4d 41 58 29 20 7b 0a 09 ENDFILE_MAX) {..
5f50: 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 20 ..sendfile_size
5f60: 3d 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c 45 = FILED_SENDFILE
5f70: 5f 4d 41 58 3b 0a 09 09 7d 20 65 6c 73 65 20 7b _MAX;...} else {
5f80: 0a 09 09 09 73 65 6e 64 66 69 6c 65 5f 73 69 7a ....sendfile_siz
5f90: 65 20 3d 20 73 65 6e 64 66 69 6c 65 5f 6c 65 6e e = sendfile_len
5fa0: 3b 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 66 69 6c ;...}....sendfil
5fb0: 65 5f 72 65 74 20 3d 20 73 65 6e 64 66 69 6c 65 e_ret = sendfile
5fc0: 28 66 64 2c 20 66 69 6c 65 69 6e 66 6f 2d 3e 66 (fd, fileinfo->f
5fd0: 64 2c 20 26 73 65 6e 64 66 69 6c 65 5f 6f 66 66 d, &sendfile_off
5fe0: 73 65 74 2c 20 73 65 6e 64 66 69 6c 65 5f 73 69 set, sendfile_si
5ff0: 7a 65 29 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 ze);...if (sendf
6000: 69 6c 65 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a ile_ret <= 0) {.
6010: 23 69 66 64 65 66 20 46 49 4c 45 44 5f 4e 4f 4e #ifdef FILED_NON
6020: 42 4c 4f 43 4b 5f 48 54 54 50 0a 09 09 09 69 66 BLOCK_HTTP....if
6030: 20 28 65 72 72 6e 6f 20 3d 3d 20 45 41 47 41 49 (errno == EAGAI
6040: 4e 29 20 7b 0a 09 09 09 09 73 65 6e 64 66 69 6c N) {.....sendfil
6050: 65 5f 72 65 74 20 3d 20 30 3b 0a 0a 09 09 09 09 e_ret = 0;......
6060: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 09 09 while (1) {.....
6070: 09 73 65 6c 65 63 74 28 66 64 20 2b 20 31 2c 20 .select(fd + 1,
6080: 26 72 66 64 2c 20 26 77 66 64 2c 20 4e 55 4c 4c &rfd, &wfd, NULL
6090: 2c 20 4e 55 4c 4c 29 3b 0a 09 09 09 09 09 69 66 , NULL);......if
60a0: 20 28 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 (FD_ISSET(fd, &
60b0: 72 66 64 29 29 20 7b 0a 09 09 09 09 09 09 72 65 rfd)) {.......re
60c0: 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28 66 64 ad_ret = read(fd
60d0: 2c 20 73 69 6e 6b 62 75 66 2c 20 73 69 7a 65 6f , sinkbuf, sizeo
60e0: 66 28 73 69 6e 6b 62 75 66 29 29 3b 0a 0a 09 09 f(sinkbuf));....
60f0: 09 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 74 ....if (read_ret
6100: 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 09 09 <= 0) {........
6110: 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 7d 0a 09 break;.......}..
6120: 09 09 09 09 7d 0a 0a 09 09 09 09 09 69 66 20 28 ....}.......if (
6130: 46 44 5f 49 53 53 45 54 28 66 64 2c 20 26 77 66 FD_ISSET(fd, &wf
6140: 64 29 29 20 7b 0a 09 09 09 09 09 09 72 65 61 64 d)) {.......read
6150: 5f 72 65 74 20 3d 20 31 3b 0a 0a 09 09 09 09 09 _ret = 1;.......
6160: 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d 0a 09 .break;......}..
6170: 09 09 09 7d 0a 0a 09 09 09 09 69 66 20 28 72 65 ...}......if (re
6180: 61 64 5f 72 65 74 20 3c 3d 20 30 29 20 7b 0a 09 ad_ret <= 0) {..
6190: 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 7d ....break;.....}
61a0: 0a 09 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 ....} else {....
61b0: 09 62 72 65 61 6b 3b 0a 09 09 09 7d 0a 23 65 6c .break;....}.#el
61c0: 73 65 0a 09 09 09 62 72 65 61 6b 3b 0a 23 65 6e se....break;.#en
61d0: 64 69 66 0a 09 09 7d 0a 0a 09 09 73 65 6e 64 66 dif...}....sendf
61e0: 69 6c 65 5f 6c 65 6e 20 2d 3d 20 73 65 6e 64 66 ile_len -= sendf
61f0: 69 6c 65 5f 72 65 74 3b 0a 09 09 73 65 6e 64 66 ile_ret;...sendf
6200: 69 6c 65 5f 73 65 6e 74 20 2b 3d 20 73 65 6e 64 ile_sent += send
6210: 66 69 6c 65 5f 72 65 74 3b 0a 09 09 69 66 20 28 file_ret;...if (
6220: 73 65 6e 64 66 69 6c 65 5f 6c 65 6e 20 3d 3d 20 sendfile_len ==
6230: 30 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 0) {....break;..
6240: 09 7d 0a 09 7d 0a 0a 09 6c 6f 67 2d 3e 65 6e 64 .}..}...log->end
6250: 74 69 6d 65 20 3d 20 28 74 69 6d 65 5f 74 29 20 time = (time_t)
6260: 2d 31 3b 0a 09 6c 6f 67 2d 3e 73 65 6e 74 5f 6c -1;..log->sent_l
6270: 65 6e 67 74 68 20 3d 20 73 65 6e 64 66 69 6c 65 ength = sendfile
6280: 5f 73 65 6e 74 3b 0a 0a 09 66 69 6c 65 64 5f 6c _sent;...filed_l
6290: 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a og_entry(log);..
62a0: 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65 .if (request->he
62b0: 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e aders.connection
62c0: 20 21 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 != FILED_CONNEC
62d0: 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 TION_KEEP_ALIVE)
62e0: 20 7b 0a 09 09 63 6c 6f 73 65 28 66 69 6c 65 69 {...close(filei
62f0: 6e 66 6f 2d 3e 66 64 29 3b 0a 0a 09 09 66 63 6c nfo->fd);....fcl
6300: 6f 73 65 28 66 70 29 3b 0a 0a 09 09 72 65 74 75 ose(fp);....retu
6310: 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 rn(0);..}...retu
6320: 72 6e 28 31 29 3b 0a 7d 0a 0a 2f 2a 20 48 61 6e rn(1);.}../* Han
6330: 64 6c 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 6e dle incoming con
6340: 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 73 74 61 74 nections */.stat
6350: 69 63 20 76 6f 69 64 20 2a 66 69 6c 65 64 5f 77 ic void *filed_w
6360: 6f 72 6b 65 72 5f 74 68 72 65 61 64 28 76 6f 69 orker_thread(voi
6370: 64 20 2a 61 72 67 5f 76 29 20 7b 0a 09 73 74 72 d *arg_v) {..str
6380: 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 uct filed_worker
6390: 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 _thread_args *ar
63a0: 67 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 g;..struct filed
63b0: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 20 72 65 _http_request re
63c0: 71 75 65 73 74 3b 0a 09 73 74 72 75 63 74 20 66 quest;..struct f
63d0: 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a iled_log_entry *
63e0: 6c 6f 67 2c 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 log, local_dummy
63f0: 5f 6c 6f 67 3b 0a 09 73 74 72 75 63 74 20 66 69 _log;..struct fi
6400: 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 2a 6f 70 74 led_options *opt
6410: 69 6f 6e 73 3b 0a 09 73 74 72 75 63 74 20 73 6f ions;..struct so
6420: 63 6b 61 64 64 72 5f 69 6e 36 20 61 64 64 72 3b ckaddr_in6 addr;
6430: 0a 09 73 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72 ..socklen_t addr
6440: 6c 65 6e 3b 0a 09 69 6e 74 20 66 61 69 6c 75 72 len;..int failur
6450: 65 5f 63 6f 75 6e 74 20 3d 20 30 2c 20 6d 61 78 e_count = 0, max
6460: 5f 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 3d _failure_count =
6470: 20 46 49 4c 45 44 5f 4d 41 58 5f 46 41 49 4c 55 FILED_MAX_FAILU
6480: 52 45 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 61 RE_COUNT;..int a
6490: 63 63 65 70 74 5f 6e 65 77 20 3d 20 31 3b 0a 09 ccept_new = 1;..
64a0: 69 6e 74 20 6d 61 73 74 65 72 5f 66 64 2c 20 66 int master_fd, f
64b0: 64 20 3d 20 2d 31 3b 0a 0a 09 2f 2a 20 52 65 61 d = -1;.../* Rea
64c0: 64 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 d arguments */..
64d0: 61 72 67 20 3d 20 61 72 67 5f 76 3b 0a 0a 09 6d arg = arg_v;...m
64e0: 61 73 74 65 72 5f 66 64 20 3d 20 61 72 67 2d 3e aster_fd = arg->
64f0: 66 64 3b 0a 09 6f 70 74 69 6f 6e 73 20 3d 20 26 fd;..options = &
6500: 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b 0a 0a 09 arg->options;...
6510: 77 68 69 6c 65 20 28 31 29 20 7b 0a 09 09 2f 2a while (1) {.../*
6520: 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70 20 70 72 Failure loop pr
6530: 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09 09 69 66 evention */...if
6540: 20 28 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20 (failure_count
6550: 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65 5f 63 6f > max_failure_co
6560: 75 6e 74 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b unt) {....break;
6570: 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c 6c 6f 63 ...}..../* Alloc
6580: 61 74 65 20 61 20 6e 65 77 20 6c 6f 67 20 62 75 ate a new log bu
6590: 66 66 65 72 20 2a 2f 0a 09 09 6c 6f 67 20 3d 20 ffer */...log =
65a0: 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28 31 29 filed_log_new(1)
65b0: 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d 3d 20 4e ;...if (log == N
65c0: 55 4c 4c 29 20 7b 0a 09 09 09 66 69 6c 65 64 5f ULL) {....filed_
65d0: 6c 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f 43 41 54 log_msg("ALLOCAT
65e0: 45 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49 4c 45 44 E_LOG_MSG_FAILED
65f0: 22 29 3b 0a 0a 09 09 09 62 72 65 61 6b 3b 0a 09 ");.....break;..
6600: 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79 70 65 20 .}....log->type
6610: 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 = FILED_LOG_TYPE
6620: 5f 54 52 41 4e 53 46 45 52 3b 0a 0a 09 09 2f 2a _TRANSFER;..../*
6630: 20 41 63 63 65 70 74 20 61 20 6e 65 77 20 63 6c Accept a new cl
6640: 69 65 6e 74 20 2a 2f 0a 09 09 69 66 20 28 61 63 ient */...if (ac
6650: 63 65 70 74 5f 6e 65 77 29 20 7b 0a 09 09 09 61 cept_new) {....a
6660: 64 64 72 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 ddrlen = sizeof(
6670: 61 64 64 72 29 3b 0a 09 09 09 66 64 20 3d 20 61 addr);....fd = a
6680: 63 63 65 70 74 28 6d 61 73 74 65 72 5f 66 64 2c ccept(master_fd,
6690: 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 (struct sockadd
66a0: 72 20 2a 29 20 26 61 64 64 72 2c 20 26 61 64 64 r *) &addr, &add
66b0: 72 6c 65 6e 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a rlen);...}..../*
66c0: 0a 09 09 20 2a 20 49 66 20 77 65 20 66 61 69 6c ... * If we fail
66d0: 2c 20 6d 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 , make a note of
66e0: 20 69 74 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 it so we don't
66f0: 67 6f 20 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f go into a loop o
6700: 66 0a 09 09 20 2a 20 61 63 63 65 70 74 28 29 20 f... * accept()
6710: 66 61 69 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 failing... */...
6720: 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 if (fd < 0) {...
6730: 09 2f 2a 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 ./* Log the new
6740: 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 connection */...
6750: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 .filed_log_msg("
6760: 41 43 43 45 50 54 5f 46 41 49 4c 45 44 22 29 3b ACCEPT_FAILED");
6770: 0a 0a 09 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 .....failure_cou
6780: 6e 74 2b 2b 3b 0a 0a 09 09 09 66 72 65 65 28 6c nt++;.....free(l
6790: 6f 67 29 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 og);.....continu
67a0: 65 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 46 69 6c e;...}..../* Fil
67b0: 6c 20 69 6e 20 6c 6f 67 20 73 74 72 75 63 74 75 l in log structu
67c0: 72 65 20 2a 2f 0a 09 09 69 66 20 28 66 69 6c 65 re */...if (file
67d0: 64 5f 6c 6f 67 5f 69 70 28 28 73 74 72 75 63 74 d_log_ip((struct
67e0: 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 sockaddr *) &ad
67f0: 64 72 2c 20 6c 6f 67 2d 3e 69 70 2c 20 73 69 7a dr, log->ip, siz
6800: 65 6f 66 28 6c 6f 67 2d 3e 69 70 29 29 20 3d 3d eof(log->ip)) ==
6810: 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 6c 6f 67 2d NULL) {....log-
6820: 3e 69 70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 >ip[0] = '\0';..
6830: 09 09 6c 6f 67 2d 3e 70 6f 72 74 20 3d 20 30 3b ..log->port = 0;
6840: 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 6c ...} else {....l
6850: 6f 67 2d 3e 70 6f 72 74 20 3d 20 61 64 64 72 2e og->port = addr.
6860: 73 69 6e 36 5f 70 6f 72 74 3b 0a 09 09 7d 0a 0a sin6_port;...}..
6870: 09 09 2f 2a 20 52 65 73 65 74 20 66 61 69 6c 75 ../* Reset failu
6880: 72 65 20 63 6f 75 6e 74 2a 2f 0a 09 09 66 61 69 re count*/...fai
6890: 6c 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a lure_count = 0;.
68a0: 0a 09 09 2f 2a 20 48 61 6e 64 6c 65 20 73 6f 63 .../* Handle soc
68b0: 6b 65 74 20 2a 2f 0a 09 09 61 63 63 65 70 74 5f ket */...accept_
68c0: 6e 65 77 20 3d 20 21 66 69 6c 65 64 5f 68 61 6e new = !filed_han
68d0: 64 6c 65 5f 63 6c 69 65 6e 74 28 66 64 2c 20 26 dle_client(fd, &
68e0: 72 65 71 75 65 73 74 2c 20 6c 6f 67 2c 20 6f 70 request, log, op
68f0: 74 69 6f 6e 73 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 tions);..}.../*
6900: 52 65 70 6f 72 74 20 65 72 72 6f 72 20 2a 2f 0a Report error */.
6910: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 .filed_log_msg("
6920: 54 48 52 45 41 44 5f 44 49 45 44 20 41 42 4e 4f THREAD_DIED ABNO
6930: 52 4d 41 4c 22 29 3b 0a 0a 09 72 65 74 75 72 6e RMAL");...return
6940: 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a 20 6c 6f 63 (NULL);.../* loc
6950: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 20 69 73 20 al_dummy_log is
6960: 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 46 49 4c only used if FIL
6970: 45 44 5f 44 4f 4e 54 5f 4c 4f 47 20 69 73 20 65 ED_DONT_LOG is e
6980: 6e 61 62 6c 65 64 2c 20 6f 74 68 65 72 77 69 73 nabled, otherwis
6990: 65 20 69 74 27 73 20 6e 6f 74 20 75 73 65 64 2c e it's not used,
69a0: 20 62 75 74 20 74 68 65 20 63 6f 6d 70 69 6c 65 but the compile
69b0: 72 20 68 61 74 65 73 20 74 68 61 74 20 69 64 65 r hates that ide
69c0: 61 2e 20 2a 2f 0a 09 6c 6f 63 61 6c 5f 64 75 6d a. */..local_dum
69d0: 6d 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 30 3b my_log.type = 0;
69e0: 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f ..local_dummy_lo
69f0: 67 2e 74 79 70 65 20 3d 20 6c 6f 63 61 6c 5f 64 g.type = local_d
6a00: 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 3b 0a 7d ummy_log.type;.}
6a10: 0a 0a 2f 2a 20 43 72 65 61 74 65 20 77 6f 72 6b ../* Create work
6a20: 65 72 20 74 68 72 65 61 64 73 20 2a 2f 0a 73 74 er threads */.st
6a30: 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f 77 atic int filed_w
6a40: 6f 72 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e orker_threads_in
6a50: 69 74 28 69 6e 74 20 66 64 2c 20 69 6e 74 20 74 it(int fd, int t
6a60: 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20 73 74 72 hread_count, str
6a70: 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e uct filed_option
6a80: 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09 73 s *options) {..s
6a90: 74 72 75 63 74 20 66 69 6c 65 64 5f 77 6f 72 6b truct filed_work
6aa0: 65 72 5f 74 68 72 65 61 64 5f 61 72 67 73 20 2a er_thread_args *
6ab0: 61 72 67 3b 0a 09 70 74 68 72 65 61 64 5f 74 20 arg;..pthread_t
6ac0: 74 68 72 65 61 64 69 64 3b 0a 09 69 6e 74 20 70 threadid;..int p
6ad0: 74 68 72 65 61 64 5f 72 65 74 3b 0a 09 69 6e 74 thread_ret;..int
6ae0: 20 69 3b 0a 0a 09 66 6f 72 20 28 69 20 3d 20 30 i;...for (i = 0
6af0: 3b 20 69 20 3c 20 74 68 72 65 61 64 5f 63 6f 75 ; i < thread_cou
6b00: 6e 74 3b 20 69 2b 2b 29 20 7b 0a 09 09 61 72 67 nt; i++) {...arg
6b10: 20 3d 20 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 = malloc(sizeof
6b20: 28 2a 61 72 67 29 29 3b 0a 0a 09 09 61 72 67 2d (*arg));....arg-
6b30: 3e 66 64 20 3d 20 66 64 3b 0a 09 09 6d 65 6d 63 >fd = fd;...memc
6b40: 70 79 28 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 py(&arg->options
6b50: 2c 20 6f 70 74 69 6f 6e 73 2c 20 73 69 7a 65 6f , options, sizeo
6b60: 66 28 2a 6f 70 74 69 6f 6e 73 29 29 3b 0a 0a 09 f(*options));...
6b70: 09 70 74 68 72 65 61 64 5f 72 65 74 20 3d 20 70 .pthread_ret = p
6b80: 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26 74 thread_create(&t
6b90: 68 72 65 61 64 69 64 2c 20 4e 55 4c 4c 2c 20 66 hreadid, NULL, f
6ba0: 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 iled_worker_thre
6bb0: 61 64 2c 20 61 72 67 29 3b 0a 09 09 69 66 20 28 ad, arg);...if (
6bc0: 70 74 68 72 65 61 64 5f 72 65 74 20 21 3d 20 30 pthread_ret != 0
6bd0: 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 2d 31 ) {....return(-1
6be0: 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72 65 74 75 );...}..}...retu
6bf0: 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 69 73 rn(0);.}../* Dis
6c00: 70 6c 61 79 20 68 65 6c 70 20 2a 2f 0a 73 74 61 play help */.sta
6c10: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 70 tic void filed_p
6c20: 72 69 6e 74 5f 68 65 6c 70 28 46 49 4c 45 20 2a rint_help(FILE *
6c30: 6f 75 74 70 75 74 2c 20 69 6e 74 20 6c 6f 6e 67 output, int long
6c40: 5f 68 65 6c 70 2c 20 63 6f 6e 73 74 20 63 68 61 _help, const cha
6c50: 72 20 2a 65 78 74 72 61 29 20 7b 0a 09 69 66 20 r *extra) {..if
6c60: 28 65 78 74 72 61 29 20 7b 0a 09 09 66 70 72 69 (extra) {...fpri
6c70: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 25 73 5c ntf(output, "%s\
6c80: 6e 22 2c 20 65 78 74 72 61 29 3b 0a 09 7d 0a 0a n", extra);..}..
6c90: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6ca0: 20 22 55 73 61 67 65 3a 20 66 69 6c 65 64 20 5b "Usage: filed [
6cb0: 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e 22 29 3b 0a <options>]\n");.
6cc0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6cd0: 20 22 20 20 4f 70 74 69 6f 6e 73 3a 5c 6e 22 29 " Options:\n")
6ce0: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
6cf0: 74 2c 20 22 20 20 20 20 20 20 2d 68 2c 20 2d 2d t, " -h, --
6d00: 68 65 6c 70 5c 6e 22 29 3b 0a 09 66 70 72 69 6e help\n");..fprin
6d10: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6d20: 20 20 2d 64 2c 20 2d 2d 64 61 65 6d 6f 6e 5c 6e -d, --daemon\n
6d30: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 ");..fprintf(out
6d40: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 2c 20 put, " -v,
6d50: 2d 2d 76 65 72 73 69 6f 6e 5c 6e 22 29 3b 0a 09 --version\n");..
6d60: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
6d70: 22 20 20 20 20 20 20 2d 56 2c 20 2d 2d 76 68 6f " -V, --vho
6d80: 73 74 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 st\n");..fprintf
6d90: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
6da0: 2d 62 20 3c 61 64 64 72 65 73 73 3e 2c 20 2d 2d -b <address>, --
6db0: 62 69 6e 64 20 3c 61 64 64 72 65 73 73 3e 5c 6e bind <address>\n
6dc0: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 ");..fprintf(out
6dd0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 70 20 3c put, " -p <
6de0: 70 6f 72 74 3e 2c 20 2d 2d 70 6f 72 74 20 3c 70 port>, --port <p
6df0: 6f 72 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e ort>\n");..fprin
6e00: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
6e10: 20 20 2d 74 20 3c 63 6f 75 6e 74 3e 2c 20 2d 2d -t <count>, --
6e20: 74 68 72 65 61 64 73 20 3c 63 6f 75 6e 74 3e 5c threads <count>\
6e30: 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 n");..fprintf(ou
6e40: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 63 20 tput, " -c
6e50: 3c 65 6e 74 72 69 65 73 3e 2c 20 2d 2d 63 61 63 <entries>, --cac
6e60: 68 65 20 3c 65 6e 74 72 69 65 73 3e 5c 6e 22 29 he <entries>\n")
6e70: 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ;..fprintf(outpu
6e80: 74 2c 20 22 20 20 20 20 20 20 2d 6c 20 3c 66 69 t, " -l <fi
6e90: 6c 65 3e 2c 20 2d 2d 6c 6f 67 20 3c 66 69 6c 65 le>, --log <file
6ea0: 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 >\n");..fprintf(
6eb0: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d output, " -
6ec0: 75 20 3c 75 73 65 72 3e 2c 20 2d 2d 75 73 65 72 u <user>, --user
6ed0: 20 3c 75 73 65 72 3e 5c 6e 22 29 3b 0a 09 66 70 <user>\n");..fp
6ee0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
6ef0: 20 20 20 20 20 2d 72 20 3c 64 69 72 65 63 74 6f -r <directo
6f00: 72 79 3e 2c 20 2d 2d 72 6f 6f 74 20 3c 64 69 72 ry>, --root <dir
6f10: 65 63 74 6f 72 79 3e 5c 6e 22 29 3b 0a 0a 09 69 ectory>\n");...i
6f20: 66 20 28 6c 6f 6e 67 5f 68 65 6c 70 29 20 7b 0a f (long_help) {.
6f30: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
6f40: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
6f50: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 55 73 tf(output, " Us
6f60: 61 67 65 3a 5c 6e 22 29 3b 0a 09 09 66 70 72 69 age:\n");...fpri
6f70: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
6f80: 20 20 20 2d 68 20 28 6f 72 20 2d 2d 68 65 6c 70 -h (or --help
6f90: 29 20 70 72 69 6e 74 73 20 74 68 69 73 20 75 73 ) prints this us
6fa0: 61 67 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e age information.
6fb0: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 \n");...fprintf(
6fc0: 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 output, "\n");..
6fd0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
6fe0: 20 22 20 20 20 20 20 20 2d 64 20 28 6f 72 20 2d " -d (or -
6ff0: 2d 64 61 65 6d 6f 6e 29 20 69 6e 73 74 72 75 63 -daemon) instruc
7000: 74 73 20 66 69 6c 65 64 20 74 6f 20 62 65 63 6f ts filed to beco
7010: 6d 65 20 61 20 64 61 65 6d 6f 6e 20 61 66 74 65 me a daemon afte
7020: 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 5c 6e r initializing\n
7030: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
7040: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
7050: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 th
7060: 65 20 6c 69 73 74 65 6e 69 6e 67 20 54 43 50 20 e listening TCP
7070: 73 6f 63 6b 65 74 20 61 6e 64 20 6c 6f 67 20 66 socket and log f
7080: 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 iles.\n");...fpr
7090: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e intf(output, "\n
70a0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
70b0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 76 20 tput, " -v
70c0: 28 6f 72 20 2d 2d 76 65 72 73 69 6f 6e 29 20 69 (or --version) i
70d0: 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64 20 70 nstructs filed p
70e0: 72 69 6e 74 20 6f 75 74 20 74 68 65 20 76 65 72 rint out the ver
70f0: 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20 sion number and
7100: 65 78 69 74 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 exit.\n");...fpr
7110: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e intf(output, "\n
7120: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
7130: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 56 20 tput, " -V
7140: 28 6f 72 20 2d 2d 76 68 6f 73 74 29 20 69 6e 73 (or --vhost) ins
7150: 74 72 75 63 74 73 20 66 69 6c 65 64 20 74 6f 20 tructs filed to
7160: 70 72 65 70 65 6e 64 20 61 6c 6c 20 72 65 71 75 prepend all requ
7170: 65 73 74 73 20 77 69 74 68 20 74 68 65 69 72 20 ests with their
7180: 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 70 72 69 HTTP\n");...fpri
7190: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
71a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
71b0: 20 20 20 48 6f 73 74 20 68 65 61 64 65 72 2e 5c Host header.\
71c0: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
71d0: 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 utput, "\n");...
71e0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
71f0: 22 20 20 20 20 20 20 2d 62 20 28 6f 72 20 2d 2d " -b (or --
7200: 62 69 6e 64 29 20 73 70 65 63 69 66 69 65 73 20 bind) specifies
7210: 74 68 65 20 61 64 64 72 65 73 73 20 74 6f 20 6c the address to l
7220: 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 6f 6d 69 isten for incomi
7230: 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09 66 ng HTTP\n");...f
7240: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
7250: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7260: 20 20 20 20 20 72 65 71 75 65 73 74 73 20 6f 6e requests on
7270: 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 . The default v
7280: 61 6c 75 65 20 69 73 20 5c 22 25 73 5c 22 2e 5c alue is \"%s\".\
7290: 6e 22 2c 20 42 49 4e 44 5f 41 44 44 52 29 3b 0a n", BIND_ADDR);.
72a0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
72b0: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
72c0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
72d0: 20 20 2d 70 20 28 6f 72 20 2d 2d 70 6f 72 74 29 -p (or --port)
72e0: 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 54 specifies the T
72f0: 43 50 20 70 6f 72 74 20 6e 75 6d 62 65 72 20 74 CP port number t
7300: 6f 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e 63 o listen for inc
7310: 6f 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b 0a oming HTTP\n");.
7320: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
7330: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
7340: 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74 73 requests
7350: 20 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c on. The defaul
7360: 74 20 69 73 20 25 75 2e 5c 6e 22 2c 20 28 75 6e t is %u.\n", (un
7370: 73 69 67 6e 65 64 20 69 6e 74 29 20 50 4f 52 54 signed int) PORT
7380: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7390: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
73a0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
73b0: 20 20 20 20 20 2d 74 20 28 6f 72 20 2d 2d 74 68 -t (or --th
73c0: 72 65 61 64 73 29 20 73 70 65 63 69 66 69 65 73 reads) specifies
73d0: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 the number of w
73e0: 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 74 6f orker threads to
73f0: 20 63 72 65 61 74 65 2e 20 45 61 63 68 5c 6e 22 create. Each\n"
7400: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7410: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
7420: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 6f wo
7430: 72 6b 65 72 20 74 68 72 65 61 64 20 63 61 6e 20 rker thread can
7440: 73 65 72 76 69 63 65 20 6f 6e 65 20 63 6f 6e 63 service one conc
7450: 75 72 72 65 6e 74 20 48 54 54 50 20 73 65 73 73 urrent HTTP sess
7460: 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 ion.\n");...fpri
7470: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
7480: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7490: 20 20 20 20 20 54 68 75 73 20 74 68 65 20 6e 75 Thus the nu
74a0: 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 20 mber of threads
74b0: 63 72 65 61 74 65 64 20 77 69 6c 6c 20 64 65 74 created will det
74c0: 65 72 6d 69 6e 65 20 68 6f 77 5c 6e 22 29 3b 0a ermine how\n");.
74d0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
74e0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
74f0: 20 20 20 20 20 20 20 20 20 20 20 6d 61 6e 79 20 many
7500: 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 74 72 61 simultaneous tra
7510: 6e 73 66 65 72 73 20 77 69 6c 6c 20 62 65 20 70 nsfers will be p
7520: 6f 73 73 69 62 6c 65 2e 20 54 68 65 5c 6e 22 29 ossible. The\n")
7530: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 ;...fprintf(outp
7540: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 ut, "
7550: 20 20 20 20 20 20 20 20 20 20 20 20 20 64 65 66 def
7560: 61 75 6c 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c ault is %lu.\n",
7570: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 (unsigned long)
7580: 20 54 48 52 45 41 44 5f 43 4f 55 4e 54 29 3b 0a THREAD_COUNT);.
7590: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
75a0: 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e , "\n");...fprin
75b0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
75c0: 20 20 2d 63 20 28 6f 72 20 2d 2d 63 61 63 68 65 -c (or --cache
75d0: 29 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 ) specifies the
75e0: 6e 75 6d 62 65 72 20 6f 66 20 66 69 6c 65 20 69 number of file i
75f0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 63 61 63 68 65 nformation cache
7600: 20 65 6e 74 72 69 65 73 5c 6e 22 29 3b 0a 09 09 entries\n");...
7610: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
7620: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
7630: 20 20 20 20 20 20 20 74 6f 20 61 6c 6c 6f 63 61 to alloca
7640: 74 65 2e 20 20 45 61 63 68 20 63 61 63 68 65 20 te. Each cache
7650: 65 6e 74 72 79 20 68 6f 6c 64 73 20 66 69 6c 65 entry holds file
7660: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 5c information as\
7670: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f n");...fprintf(o
7680: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 utput, "
7690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 65 we
76a0: 6c 6c 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 ll as an open fi
76b0: 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f le descriptor to
76c0: 20 74 68 65 20 66 69 6c 65 2c 20 73 6f 20 72 65 the file, so re
76d0: 73 6f 75 72 63 65 5c 6e 22 29 3b 0a 09 09 66 70 source\n");...fp
76e0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
76f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7700: 20 20 20 20 20 6c 69 6d 69 74 73 20 28 69 2e 65 limits (i.e
7710: 2e 2c 20 75 6c 69 6d 69 74 29 20 73 68 6f 75 6c ., ulimit) shoul
7720: 64 20 62 65 20 63 6f 6e 73 69 64 65 72 65 64 2e d be considered.
7730: 20 20 54 68 69 73 20 73 68 6f 75 6c 64 5c 6e 22 This should\n"
7740: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7750: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
7760: 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20 61 be a
7770: 20 70 72 69 6d 65 20 6e 75 6d 62 65 72 20 66 6f prime number fo
7780: 72 20 69 64 65 61 6c 20 75 73 65 20 77 69 74 68 r ideal use with
7790: 20 74 68 65 20 6c 6f 6f 6b 75 70 20 6d 65 74 68 the lookup meth
77a0: 6f 64 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e od.\n");...fprin
77b0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
77c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
77d0: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 73 The default is
77e0: 20 25 6c 75 2e 5c 6e 22 2c 20 28 75 6e 73 69 67 %lu.\n", (unsig
77f0: 6e 65 64 20 6c 6f 6e 67 29 20 43 41 43 48 45 5f ned long) CACHE_
7800: 53 49 5a 45 29 3b 0a 09 09 66 70 72 69 6e 74 66 SIZE);...fprintf
7810: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a (output, "\n");.
7820: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
7830: 2c 20 22 20 20 20 20 20 20 2d 6c 20 28 6f 72 20 , " -l (or
7840: 2d 2d 6c 6f 67 29 20 73 70 65 63 69 66 69 65 73 --log) specifies
7850: 20 61 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 6f a filename to o
7860: 70 65 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 20 pen for writing
7870: 6c 6f 67 20 65 6e 74 72 69 65 73 2e 20 20 4c 6f log entries. Lo
7880: 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 g\n");...fprintf
7890: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 (output, "
78a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 65 6e en
78b0: 74 72 69 65 73 20 61 72 65 20 6d 61 64 65 20 66 tries are made f
78c0: 6f 72 20 76 61 72 69 6f 75 73 20 73 74 61 67 65 or various stage
78d0: 73 20 69 6e 20 74 72 61 6e 73 66 65 72 69 6e 67 s in transfering
78e0: 20 66 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09 09 66 files.\n");...f
78f0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
7900: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7910: 20 20 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 The log file
7920: 20 69 73 20 6f 70 65 6e 65 64 20 62 65 66 6f 72 is opened befor
7930: 65 20 73 77 69 74 63 68 69 6e 67 20 75 73 65 72 e switching user
7940: 73 20 28 73 65 65 20 5c 22 2d 75 5c 22 29 5c 6e s (see \"-u\")\n
7950: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
7960: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
7970: 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 72 and r
7980: 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 65 73 20 oot directories
7990: 28 73 65 65 20 5c 22 2d 72 5c 22 29 2e 20 20 54 (see \"-r\"). T
79a0: 68 65 20 6c 6f 67 20 66 69 6c 65 20 69 73 20 6e he log file is n
79b0: 65 76 65 72 5c 6e 22 29 3b 0a 09 09 66 70 72 69 ever\n");...fpri
79c0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
79d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
79e0: 20 63 6c 6f 73 65 64 20 73 6f 20 6c 6f 67 20 72 closed so log r
79f0: 6f 74 61 74 69 6f 6e 20 77 69 74 68 6f 75 74 20 otation without
7a00: 73 74 6f 70 70 69 6e 67 20 74 68 65 20 64 61 65 stopping the dae
7a10: 6d 6f 6e 20 69 73 20 77 69 6c 6c 5c 6e 22 29 3b mon is will\n");
7a20: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
7a30: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
7a40: 20 20 20 20 20 20 20 20 6e 6f 74 20 77 6f 72 6b not work
7a50: 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 . The value of
7a60: 5c 22 2d 5c 22 20 69 6e 64 69 63 61 74 65 73 20 \"-\" indicates
7a70: 74 68 61 74 20 73 74 61 6e 64 61 72 64 20 6f 75 that standard ou
7a80: 74 70 75 74 5c 6e 22 29 3b 0a 09 09 66 70 72 69 tput\n");...fpri
7a90: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 ntf(output, "
7aa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ab0: 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 should be used
7ac0: 66 6f 72 20 6c 6f 67 67 69 6e 67 2e 20 20 49 66 for logging. If
7ad0: 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 62 65 the filename be
7ae0: 67 69 6e 73 20 77 69 74 68 20 61 5c 6e 22 29 3b gins with a\n");
7af0: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 ...fprintf(outpu
7b00: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 t, "
7b10: 20 20 20 20 20 20 20 20 70 69 70 65 20 28 5c 22 pipe (\"
7b20: 7c 5c 22 29 20 74 68 65 6e 20 61 20 70 72 6f 63 |\") then a proc
7b30: 65 73 73 20 69 73 20 73 74 61 72 74 65 64 20 61 ess is started a
7b40: 6e 64 20 75 73 65 64 20 66 6f 72 20 6c 6f 67 67 nd used for logg
7b50: 69 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e ing\n");...fprin
7b60: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
7b70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7b80: 69 6e 73 74 65 61 64 20 6f 66 20 61 20 66 69 6c instead of a fil
7b90: 65 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 e. The default
7ba0: 69 73 20 5c 22 25 73 5c 22 2e 5c 6e 22 2c 20 4c is \"%s\".\n", L
7bb0: 4f 47 5f 46 49 4c 45 29 3b 0a 23 69 66 64 65 66 OG_FILE);.#ifdef
7bc0: 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a FILED_DONT_LOG.
7bd0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 ..fprintf(output
7be0: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20 , "
7bf0: 20 20 20 20 20 20 20 4e 6f 74 65 20 74 68 61 74 Note that
7c00: 20 6c 6f 67 67 69 6e 67 20 69 73 20 63 6f 6d 70 logging is comp
7c10: 6c 65 74 65 6c 79 20 64 69 73 61 62 6c 65 64 20 letely disabled
7c20: 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 64 so this option d
7c30: 6f 65 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e oes\n");...fprin
7c40: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
7c50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7c60: 6e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69 73 20 nothing in this
7c70: 62 75 69 6c 64 2e 5c 6e 22 29 3b 0a 23 65 6e 64 build.\n");.#end
7c80: 69 66 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 if...fprintf(out
7c90: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70 put, "\n");...fp
7ca0: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 rintf(output, "
7cb0: 20 20 20 20 20 2d 75 20 28 6f 72 20 2d 2d 75 73 -u (or --us
7cc0: 65 72 29 20 73 70 65 63 69 66 69 65 73 20 74 68 er) specifies th
7cd0: 65 20 75 73 65 72 20 74 6f 20 73 77 69 74 63 68 e user to switch
7ce0: 20 75 73 65 72 20 49 44 73 20 74 6f 20 62 65 66 user IDs to bef
7cf0: 6f 72 65 20 73 65 72 76 69 63 69 6e 67 5c 6e 22 ore servicing\n"
7d00: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7d10: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
7d20: 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75 65 reque
7d30: 73 74 73 2e 20 20 54 68 65 20 64 65 66 61 75 6c sts. The defaul
7d40: 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 t is not change
7d50: 75 73 65 72 20 49 44 73 2e 5c 6e 22 29 3b 0a 09 user IDs.\n");..
7d60: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c .fprintf(output,
7d70: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 "\n");...fprint
7d80: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 f(output, "
7d90: 20 2d 72 20 28 6f 72 20 2d 2d 72 6f 6f 74 29 20 -r (or --root)
7da0: 73 70 65 63 69 66 69 65 73 20 74 68 65 20 64 69 specifies the di
7db0: 72 65 63 74 6f 72 79 20 74 6f 20 61 63 74 20 61 rectory to act a
7dc0: 73 20 74 68 65 20 72 6f 6f 74 20 64 69 72 65 63 s the root direc
7dd0: 74 6f 72 79 20 66 6f 72 5c 6e 22 29 3b 0a 09 09 tory for\n");...
7de0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 fprintf(output,
7df0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
7e00: 20 20 20 20 20 20 74 68 65 20 66 69 6c 65 20 73 the file s
7e10: 65 72 76 65 72 2e 20 20 49 66 20 74 68 69 73 20 erver. If this
7e20: 6f 70 74 69 6f 6e 20 69 73 20 73 70 65 63 69 66 option is specif
7e30: 69 65 64 2c 20 63 68 72 6f 6f 74 28 32 29 5c 6e ied, chroot(2)\n
7e40: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 ");...fprintf(ou
7e50: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 tput, "
7e60: 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20 63 is c
7e70: 61 6c 6c 65 64 2e 20 20 54 68 65 20 64 65 66 61 alled. The defa
7e80: 75 6c 74 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 ult is not chang
7e90: 65 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72 69 e root directori
7ea0: 65 73 2c 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e es,\n");...fprin
7eb0: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 tf(output, "
7ec0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7ed0: 20 74 68 61 74 20 69 73 2c 20 74 68 65 20 5c 22 that is, the \"
7ee0: 2f 5c 22 20 64 69 72 65 63 74 6f 72 79 20 69 73 /\" directory is
7ef0: 20 73 68 61 72 65 64 20 6f 75 74 2e 20 20 54 68 shared out. Th
7f00: 69 73 20 77 69 6c 6c 5c 6e 22 29 3b 0a 09 09 66 is will\n");...f
7f10: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 printf(output, "
7f20: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
7f30: 20 20 20 20 20 6c 69 6b 65 6c 79 20 62 65 20 61 likely be a
7f40: 20 73 65 63 75 72 69 74 79 20 69 73 73 75 65 2c security issue,
7f50: 20 73 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 20 so this option
7f60: 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 5c 6e 22 should always\n"
7f70: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 );...fprintf(out
7f80: 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 put, "
7f90: 20 20 20 20 20 20 20 20 20 20 20 62 65 20 75 73 be us
7fa0: 65 64 2e 5c 6e 22 29 3b 0a 09 7d 0a 0a 09 72 65 ed.\n");..}...re
7fb0: 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 turn;.}../* Add
7fc0: 61 20 67 65 74 6f 70 74 20 6f 70 74 69 6f 6e 20 a getopt option
7fd0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 */.static void f
7fe0: 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 iled_getopt_long
7ff0: 5f 73 65 74 6f 70 74 28 73 74 72 75 63 74 20 6f _setopt(struct o
8000: 70 74 69 6f 6e 20 2a 6f 70 74 2c 20 63 6f 6e 73 ption *opt, cons
8010: 74 20 63 68 61 72 20 2a 6e 61 6d 65 2c 20 69 6e t char *name, in
8020: 74 20 68 61 73 5f 61 72 67 2c 20 69 6e 74 20 76 t has_arg, int v
8030: 61 6c 29 20 7b 0a 09 6f 70 74 2d 3e 6e 61 6d 65 al) {..opt->name
8040: 20 20 20 20 20 3d 20 6e 61 6d 65 3b 0a 09 6f 70 = name;..op
8050: 74 2d 3e 68 61 73 5f 61 72 67 20 20 3d 20 68 61 t->has_arg = ha
8060: 73 5f 61 72 67 3b 0a 09 6f 70 74 2d 3e 66 6c 61 s_arg;..opt->fla
8070: 67 20 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 09 6f g = NULL;..o
8080: 70 74 2d 3e 76 61 6c 20 20 20 20 20 20 3d 20 76 pt->val = v
8090: 61 6c 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a al;...return;.}.
80a0: 0a 2f 2a 20 52 65 73 6f 6c 76 65 20 61 20 75 73 ./* Resolve a us
80b0: 65 72 6e 61 6d 65 20 74 6f 20 61 20 55 49 44 20 ername to a UID
80c0: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 */.static int fi
80d0: 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 led_user_lookup(
80e0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 75 73 65 72 const char *user
80f0: 2c 20 75 69 64 5f 74 20 2a 75 73 65 72 5f 69 64 , uid_t *user_id
8100: 29 20 7b 0a 09 63 68 61 72 20 2a 6e 65 78 74 3b ) {..char *next;
8110: 0a 09 75 69 64 5f 74 20 75 73 65 72 5f 69 64 5f ..uid_t user_id_
8120: 63 68 65 63 6b 3b 0a 23 69 66 6e 64 65 66 20 46 check;.#ifndef F
8130: 49 4c 45 44 5f 4e 4f 5f 47 45 54 50 57 4e 41 4d ILED_NO_GETPWNAM
8140: 0a 09 73 74 72 75 63 74 20 70 61 73 73 77 64 20 ..struct passwd
8150: 2a 65 6e 74 3b 0a 0a 09 65 6e 74 20 3d 20 67 65 *ent;...ent = ge
8160: 74 70 77 6e 61 6d 28 75 73 65 72 29 3b 0a 09 69 tpwnam(user);..i
8170: 66 20 28 65 6e 74 20 21 3d 20 4e 55 4c 4c 29 20 f (ent != NULL)
8180: 7b 0a 09 09 2a 75 73 65 72 5f 69 64 20 3d 20 65 {...*user_id = e
8190: 6e 74 2d 3e 70 77 5f 75 69 64 3b 0a 0a 09 09 72 nt->pw_uid;....r
81a0: 65 74 75 72 6e 28 30 29 3b 0a 09 7d 0a 23 65 6e eturn(0);..}.#en
81b0: 64 69 66 0a 0a 09 75 73 65 72 5f 69 64 5f 63 68 dif...user_id_ch
81c0: 65 63 6b 20 3d 20 73 74 72 74 6f 75 6c 6c 28 75 eck = strtoull(u
81d0: 73 65 72 2c 20 26 6e 65 78 74 2c 20 31 30 29 3b ser, &next, 10);
81e0: 0a 09 69 66 20 28 6e 65 78 74 20 3d 3d 20 4e 55 ..if (next == NU
81f0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 LL) {...return(1
8200: 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 6e 65 78 74 );..}...if (next
8210: 5b 30 5d 20 21 3d 20 27 5c 30 27 29 20 7b 0a 09 [0] != '\0') {..
8220: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a .return(1);..}..
8230: 09 2a 75 73 65 72 5f 69 64 20 3d 20 75 73 65 72 .*user_id = user
8240: 5f 69 64 5f 63 68 65 63 6b 3b 0a 0a 09 72 65 74 _id_check;...ret
8250: 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 44 61 urn(0);.}../* Da
8260: 65 6d 6f 6e 69 7a 65 20 2a 2f 0a 73 74 61 74 69 emonize */.stati
8270: 63 20 69 6e 74 20 66 69 6c 65 64 5f 64 61 65 6d c int filed_daem
8280: 6f 6e 69 7a 65 28 76 6f 69 64 29 20 7b 0a 09 70 onize(void) {..p
8290: 69 64 5f 74 20 73 65 74 73 69 64 5f 72 65 74 2c id_t setsid_ret,
82a0: 20 66 6f 72 6b 5f 72 65 74 3b 0a 09 69 6e 74 20 fork_ret;..int
82b0: 63 68 64 69 72 5f 72 65 74 2c 20 64 75 70 32 5f chdir_ret, dup2_
82c0: 72 65 74 3b 0a 09 69 6e 74 20 66 64 5f 69 6e 2c ret;..int fd_in,
82d0: 20 66 64 5f 6f 75 74 3b 0a 0a 09 63 68 64 69 72 fd_out;...chdir
82e0: 5f 72 65 74 20 3d 20 63 68 64 69 72 28 22 2f 22 _ret = chdir("/"
82f0: 29 3b 0a 09 69 66 20 28 63 68 64 69 72 5f 72 65 );..if (chdir_re
8300: 74 20 21 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 t != 0) {...retu
8310: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 6f 72 6b rn(1);..}...fork
8320: 5f 72 65 74 20 3d 20 66 6f 72 6b 28 29 3b 0a 09 _ret = fork();..
8330: 69 66 20 28 66 6f 72 6b 5f 72 65 74 20 3c 20 30 if (fork_ret < 0
8340: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b ) {...return(1);
8350: 0a 09 7d 0a 0a 09 69 66 20 28 66 6f 72 6b 5f 72 ..}...if (fork_r
8360: 65 74 20 3e 20 30 29 20 7b 0a 09 09 2f 2a 20 50 et > 0) {.../* P
8370: 61 72 65 6e 74 20 2a 2f 0a 09 09 77 61 69 74 70 arent */...waitp
8380: 69 64 28 66 6f 72 6b 5f 72 65 74 2c 20 4e 55 4c id(fork_ret, NUL
8390: 4c 2c 20 30 29 3b 0a 0a 09 09 65 78 69 74 28 45 L, 0);....exit(E
83a0: 58 49 54 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d XIT_SUCCESS);..}
83b0: 0a 0a 09 2f 2a 20 43 68 69 6c 64 20 2a 2f 0a 09 .../* Child */..
83c0: 69 66 20 28 66 6f 72 6b 28 29 20 21 3d 20 30 29 if (fork() != 0)
83d0: 20 7b 0a 09 09 2f 2a 20 43 68 69 6c 64 20 2a 2f {.../* Child */
83e0: 0a 09 09 65 78 69 74 28 45 58 49 54 5f 53 55 43 ...exit(EXIT_SUC
83f0: 43 45 53 53 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 47 CESS);..}.../* G
8400: 72 61 6e 64 20 63 68 69 6c 64 20 2a 2f 0a 09 73 rand child */..s
8410: 65 74 73 69 64 5f 72 65 74 20 3d 20 73 65 74 73 etsid_ret = sets
8420: 69 64 28 29 3b 0a 09 69 66 20 28 73 65 74 73 69 id();..if (setsi
8430: 64 5f 72 65 74 20 3d 3d 20 28 28 70 69 64 5f 74 d_ret == ((pid_t
8440: 29 20 2d 31 29 29 20 7b 0a 09 09 72 65 74 75 72 ) -1)) {...retur
8450: 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66 64 5f 69 6e n(1);..}...fd_in
8460: 20 3d 20 6f 70 65 6e 28 22 2f 64 65 76 2f 6e 75 = open("/dev/nu
8470: 6c 6c 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a ll", O_RDONLY);.
8480: 09 66 64 5f 6f 75 74 20 3d 20 6f 70 65 6e 28 22 .fd_out = open("
8490: 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 57 52 /dev/null", O_WR
84a0: 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 66 64 5f 69 ONLY);..if (fd_i
84b0: 6e 20 3c 20 30 20 7c 7c 20 66 64 5f 6f 75 74 20 n < 0 || fd_out
84c0: 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 < 0) {...return(
84d0: 31 29 3b 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 1);..}...dup2_re
84e0: 74 20 3d 20 64 75 70 32 28 66 64 5f 69 6e 2c 20 t = dup2(fd_in,
84f0: 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 3b 0a 09 STDIN_FILENO);..
8500: 69 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 if (dup2_ret !=
8510: 53 54 44 49 4e 5f 46 49 4c 45 4e 4f 29 20 7b 0a STDIN_FILENO) {.
8520: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a ..return(1);..}.
8530: 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70 ..dup2_ret = dup
8540: 32 28 66 64 5f 6f 75 74 2c 20 53 54 44 4f 55 54 2(fd_out, STDOUT
8550: 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64 _FILENO);..if (d
8560: 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 4f 55 up2_ret != STDOU
8570: 54 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 T_FILENO) {...re
8580: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 turn(1);..}...du
8590: 70 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 p2_ret = dup2(fd
85a0: 5f 6f 75 74 2c 20 53 54 44 45 52 52 5f 46 49 4c _out, STDERR_FIL
85b0: 45 4e 4f 29 3b 0a 09 69 66 20 28 64 75 70 32 5f ENO);..if (dup2_
85c0: 72 65 74 20 21 3d 20 53 54 44 45 52 52 5f 46 49 ret != STDERR_FI
85d0: 4c 45 4e 4f 29 20 7b 0a 09 09 72 65 74 75 72 6e LENO) {...return
85e0: 28 31 29 3b 0a 09 7d 0a 0a 09 63 6c 6f 73 65 28 (1);..}...close(
85f0: 66 64 5f 69 6e 29 3b 0a 09 63 6c 6f 73 65 28 66 fd_in);..close(f
8600: 64 5f 6f 75 74 29 3b 0a 0a 09 72 65 74 75 72 6e d_out);...return
8610: 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 52 75 6e 20 70 (0);.}../* Run p
8620: 72 6f 63 65 73 73 20 2a 2f 0a 69 6e 74 20 6d 61 rocess */.int ma
8630: 69 6e 28 69 6e 74 20 61 72 67 63 2c 20 63 68 61 in(int argc, cha
8640: 72 20 2a 2a 61 72 67 76 29 20 7b 0a 09 73 74 72 r **argv) {..str
8650: 75 63 74 20 6f 70 74 69 6f 6e 20 6f 70 74 69 6f uct option optio
8660: 6e 73 5b 31 32 5d 3b 0a 09 73 74 72 75 63 74 20 ns[12];..struct
8670: 66 69 6c 65 64 5f 6f 70 74 69 6f 6e 73 20 74 68 filed_options th
8680: 72 65 61 64 5f 6f 70 74 69 6f 6e 73 3b 0a 09 63 read_options;..c
8690: 6f 6e 73 74 20 63 68 61 72 20 2a 62 69 6e 64 5f onst char *bind_
86a0: 61 64 64 72 20 3d 20 42 49 4e 44 5f 41 44 44 52 addr = BIND_ADDR
86b0: 2c 20 2a 6e 65 77 72 6f 6f 74 20 3d 20 4e 55 4c , *newroot = NUL
86c0: 4c 2c 20 2a 6c 6f 67 5f 66 69 6c 65 20 3d 20 4c L, *log_file = L
86d0: 4f 47 5f 46 49 4c 45 3b 0a 09 46 49 4c 45 20 2a OG_FILE;..FILE *
86e0: 6c 6f 67 5f 66 70 3b 0a 09 75 69 64 5f 74 20 75 log_fp;..uid_t u
86f0: 73 65 72 20 3d 20 30 3b 0a 09 69 6e 74 20 70 6f ser = 0;..int po
8700: 72 74 20 3d 20 50 4f 52 54 2c 20 74 68 72 65 61 rt = PORT, threa
8710: 64 5f 63 6f 75 6e 74 20 3d 20 54 48 52 45 41 44 d_count = THREAD
8720: 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74 20 63 61 63 _COUNT;..int cac
8730: 68 65 5f 73 69 7a 65 20 3d 20 43 41 43 48 45 5f he_size = CACHE_
8740: 53 49 5a 45 3b 0a 09 69 6e 74 20 69 6e 69 74 5f SIZE;..int init_
8750: 72 65 74 2c 20 63 68 72 6f 6f 74 5f 72 65 74 2c ret, chroot_ret,
8760: 20 73 65 74 75 69 64 5f 72 65 74 2c 20 6c 6f 6f setuid_ret, loo
8770: 6b 75 70 5f 72 65 74 2c 20 63 68 64 69 72 5f 72 kup_ret, chdir_r
8780: 65 74 3b 0a 09 69 6e 74 20 73 65 74 75 69 64 5f et;..int setuid_
8790: 65 6e 61 62 6c 65 64 20 3d 20 30 2c 20 64 61 65 enabled = 0, dae
87a0: 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 30 3b mon_enabled = 0;
87b0: 0a 09 69 6e 74 20 63 68 3b 0a 09 69 6e 74 20 66 ..int ch;..int f
87c0: 64 3b 0a 0a 09 2f 2a 20 53 65 74 20 64 65 66 61 d;.../* Set defa
87d0: 75 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 74 ult values */..t
87e0: 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 2e 76 68 hread_options.vh
87f0: 6f 73 74 73 5f 65 6e 61 62 6c 65 64 20 3d 20 30 osts_enabled = 0
8800: 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65 73 73 20 61 ;.../* Process a
8810: 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 09 66 69 6c rguments */..fil
8820: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 ed_getopt_long_s
8830: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 30 etopt(&options[0
8840: 5d 2c 20 22 70 6f 72 74 22 2c 20 72 65 71 75 69 ], "port", requi
8850: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 70 red_argument, 'p
8860: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 ');..filed_getop
8870: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f t_long_setopt(&o
8880: 70 74 69 6f 6e 73 5b 31 5d 2c 20 22 74 68 72 65 ptions[1], "thre
8890: 61 64 73 22 2c 20 72 65 71 75 69 72 65 64 5f 61 ads", required_a
88a0: 72 67 75 6d 65 6e 74 2c 20 27 74 27 29 3b 0a 09 rgument, 't');..
88b0: 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e filed_getopt_lon
88c0: 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e g_setopt(&option
88d0: 73 5b 32 5d 2c 20 22 63 61 63 68 65 22 2c 20 72 s[2], "cache", r
88e0: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
88f0: 2c 20 27 63 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'c');..filed_g
8900: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
8910: 74 28 26 6f 70 74 69 6f 6e 73 5b 33 5d 2c 20 22 t(&options[3], "
8920: 62 69 6e 64 22 2c 20 72 65 71 75 69 72 65 64 5f bind", required_
8930: 61 72 67 75 6d 65 6e 74 2c 20 27 62 27 29 3b 0a argument, 'b');.
8940: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
8950: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
8960: 6e 73 5b 34 5d 2c 20 22 75 73 65 72 22 2c 20 72 ns[4], "user", r
8970: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74 equired_argument
8980: 2c 20 27 75 27 29 3b 0a 09 66 69 6c 65 64 5f 67 , 'u');..filed_g
8990: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 etopt_long_setop
89a0: 74 28 26 6f 70 74 69 6f 6e 73 5b 35 5d 2c 20 22 t(&options[5], "
89b0: 72 6f 6f 74 22 2c 20 72 65 71 75 69 72 65 64 5f root", required_
89c0: 61 72 67 75 6d 65 6e 74 2c 20 27 72 27 29 3b 0a argument, 'r');.
89d0: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f .filed_getopt_lo
89e0: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f ng_setopt(&optio
89f0: 6e 73 5b 36 5d 2c 20 22 68 65 6c 70 22 2c 20 6e ns[6], "help", n
8a00: 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 68 27 29 o_argument, 'h')
8a10: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f ;..filed_getopt_
8a20: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 long_setopt(&opt
8a30: 69 6f 6e 73 5b 37 5d 2c 20 22 64 61 65 6d 6f 6e ions[7], "daemon
8a40: 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 ", no_argument,
8a50: 27 64 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 'd');..filed_get
8a60: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
8a70: 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c 20 22 6c 6f &options[8], "lo
8a80: 67 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 g", required_arg
8a90: 75 6d 65 6e 74 2c 20 27 6c 27 29 3b 0a 09 66 69 ument, 'l');..fi
8aa0: 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f led_getopt_long_
8ab0: 73 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b setopt(&options[
8ac0: 39 5d 2c 20 22 76 65 72 73 69 6f 6e 22 2c 20 6e 9], "version", n
8ad0: 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 27 76 27 29 o_argument, 'v')
8ae0: 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f ;..filed_getopt_
8af0: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 long_setopt(&opt
8b00: 69 6f 6e 73 5b 31 30 5d 2c 20 22 76 68 6f 73 74 ions[10], "vhost
8b10: 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20 ", no_argument,
8b20: 27 56 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 'V');..filed_get
8b30: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 opt_long_setopt(
8b40: 26 6f 70 74 69 6f 6e 73 5b 31 31 5d 2c 20 4e 55 &options[11], NU
8b50: 4c 4c 2c 20 30 2c 20 30 29 3b 0a 09 77 68 69 6c LL, 0, 0);..whil
8b60: 65 20 28 28 63 68 20 3d 20 67 65 74 6f 70 74 5f e ((ch = getopt_
8b70: 6c 6f 6e 67 28 61 72 67 63 2c 20 61 72 67 76 2c long(argc, argv,
8b80: 20 22 70 3a 74 3a 63 3a 62 3a 75 3a 72 3a 6c 3a "p:t:c:b:u:r:l:
8b90: 68 64 76 56 22 2c 20 6f 70 74 69 6f 6e 73 2c 20 hdvV", options,
8ba0: 4e 55 4c 4c 29 29 20 21 3d 20 2d 31 29 20 7b 0a NULL)) != -1) {.
8bb0: 09 09 73 77 69 74 63 68 28 63 68 29 20 7b 0a 09 ..switch(ch) {..
8bc0: 09 09 63 61 73 65 20 27 70 27 3a 0a 09 09 09 09 ..case 'p':.....
8bd0: 70 6f 72 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 port = atoi(opta
8be0: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a rg);.....break;.
8bf0: 09 09 09 63 61 73 65 20 27 74 27 3a 0a 09 09 09 ...case 't':....
8c00: 09 74 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20 .thread_count =
8c10: 61 74 6f 69 28 6f 70 74 61 72 67 29 3b 0a 09 09 atoi(optarg);...
8c20: 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 ..break;....case
8c30: 20 27 63 27 3a 0a 09 09 09 09 63 61 63 68 65 5f 'c':.....cache_
8c40: 73 69 7a 65 20 3d 20 61 74 6f 69 28 6f 70 74 61 size = atoi(opta
8c50: 72 67 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a rg);.....break;.
8c60: 09 09 09 63 61 73 65 20 27 62 27 3a 0a 09 09 09 ...case 'b':....
8c70: 09 62 69 6e 64 5f 61 64 64 72 20 3d 20 73 74 72 .bind_addr = str
8c80: 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 dup(optarg);....
8c90: 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 .break;....case
8ca0: 27 75 27 3a 0a 09 09 09 09 73 65 74 75 69 64 5f 'u':.....setuid_
8cb0: 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 09 09 enabled = 1;....
8cc0: 09 6c 6f 6f 6b 75 70 5f 72 65 74 20 3d 20 66 69 .lookup_ret = fi
8cd0: 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f 6b 75 70 28 led_user_lookup(
8ce0: 6f 70 74 61 72 67 2c 20 26 75 73 65 72 29 3b 0a optarg, &user);.
8cf0: 09 09 09 09 69 66 20 28 6c 6f 6f 6b 75 70 5f 72 ....if (lookup_r
8d00: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 09 09 et != 0) {......
8d10: 66 69 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 filed_print_help
8d20: 28 73 74 64 65 72 72 2c 20 30 2c 20 22 49 6e 76 (stderr, 0, "Inv
8d30: 61 6c 69 64 20 75 73 65 72 6e 61 6d 65 20 73 70 alid username sp
8d40: 65 63 69 66 69 65 64 22 29 3b 0a 0a 09 09 09 09 ecified");......
8d50: 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 09 09 .return(1);.....
8d60: 7d 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 }.....break;....
8d70: 63 61 73 65 20 27 72 27 3a 0a 09 09 09 09 6e 65 case 'r':.....ne
8d80: 77 72 6f 6f 74 20 3d 20 73 74 72 64 75 70 28 6f wroot = strdup(o
8d90: 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65 61 ptarg);.....brea
8da0: 6b 3b 0a 09 09 09 63 61 73 65 20 27 6c 27 3a 0a k;....case 'l':.
8db0: 09 09 09 09 6c 6f 67 5f 66 69 6c 65 20 3d 20 73 ....log_file = s
8dc0: 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 trdup(optarg);..
8dd0: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 ...break;....cas
8de0: 65 20 27 64 27 3a 0a 09 09 09 09 64 61 65 6d 6f e 'd':.....daemo
8df0: 6e 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 09 n_enabled = 1;..
8e00: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 ...break;....cas
8e10: 65 20 27 56 27 3a 0a 09 09 09 09 74 68 72 65 61 e 'V':.....threa
8e20: 64 5f 6f 70 74 69 6f 6e 73 2e 76 68 6f 73 74 73 d_options.vhosts
8e30: 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 0a 09 _enabled = 1;...
8e40: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 ...break;....cas
8e50: 65 20 27 76 27 3a 0a 09 09 09 09 70 72 69 6e 74 e 'v':.....print
8e60: 66 28 22 66 69 6c 65 64 20 76 65 72 73 69 6f 6e f("filed version
8e70: 20 25 73 5c 6e 22 2c 20 46 49 4c 45 44 5f 56 45 %s\n", FILED_VE
8e80: 52 53 49 4f 4e 29 3b 0a 0a 09 09 09 09 72 65 74 RSION);......ret
8e90: 75 72 6e 28 30 29 3b 0a 09 09 09 63 61 73 65 20 urn(0);....case
8ea0: 27 3f 27 3a 0a 09 09 09 63 61 73 65 20 27 3a 27 '?':....case ':'
8eb0: 3a 0a 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e :.....filed_prin
8ec0: 74 5f 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 t_help(stderr, 0
8ed0: 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 09 09 72 65 , NULL);......re
8ee0: 74 75 72 6e 28 31 29 3b 0a 09 09 09 63 61 73 65 turn(1);....case
8ef0: 20 27 68 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f 'h':.....filed_
8f00: 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 6f 75 print_help(stdou
8f10: 74 2c 20 31 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09 t, 1, NULL);....
8f20: 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 7d ..return(0);...}
8f30: 0a 09 7d 0a 0a 09 2f 2a 20 4f 70 65 6e 20 6c 6f ..}.../* Open lo
8f40: 67 20 66 69 6c 65 20 2a 2f 0a 09 6c 6f 67 5f 66 g file */..log_f
8f50: 70 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 p = filed_log_op
8f60: 65 6e 28 6c 6f 67 5f 66 69 6c 65 29 3b 0a 09 69 en(log_file);..i
8f70: 66 20 28 6c 6f 67 5f 66 70 20 3d 3d 20 4e 55 4c f (log_fp == NUL
8f80: 4c 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 L) {...perror("f
8f90: 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e 22 29 3b iled_log_open");
8fa0: 0a 0a 09 09 72 65 74 75 72 6e 28 34 29 3b 0a 09 ....return(4);..
8fb0: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 69 }.../* Create li
8fc0: 73 74 65 6e 69 6e 67 20 73 6f 63 6b 65 74 20 2a stening socket *
8fd0: 2f 0a 09 66 64 20 3d 20 66 69 6c 65 64 5f 6c 69 /..fd = filed_li
8fe0: 73 74 65 6e 28 62 69 6e 64 5f 61 64 64 72 2c 20 sten(bind_addr,
8ff0: 70 6f 72 74 29 3b 0a 09 69 66 20 28 66 64 20 3c port);..if (fd <
9000: 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 0) {...perror("
9010: 66 69 6c 65 64 5f 6c 69 73 74 65 6e 22 29 3b 0a filed_listen");.
9020: 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d ...return(1);..}
9030: 0a 0a 09 2f 2a 20 42 65 63 6f 6d 65 20 61 20 64 .../* Become a d
9040: 61 65 6d 6f 6e 20 2a 2f 0a 09 69 66 20 28 64 61 aemon */..if (da
9050: 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 29 20 7b 0a emon_enabled) {.
9060: 09 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c ..init_ret = fil
9070: 65 64 5f 64 61 65 6d 6f 6e 69 7a 65 28 29 3b 0a ed_daemonize();.
9080: 09 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 ..if (init_ret !
9090: 3d 20 30 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 = 0) {....perror
90a0: 28 22 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a ("filed_daemoniz
90b0: 65 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 e");.....return(
90c0: 36 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 6);...}..}.../*
90d0: 43 68 72 6f 6f 74 2c 20 69 66 20 61 70 70 72 6f Chroot, if appro
90e0: 70 72 69 61 74 65 20 2a 2f 0a 09 69 66 20 28 6e priate */..if (n
90f0: 65 77 72 6f 6f 74 29 20 7b 0a 09 09 63 68 64 69 ewroot) {...chdi
9100: 72 5f 72 65 74 20 3d 20 63 68 64 69 72 28 6e 65 r_ret = chdir(ne
9110: 77 72 6f 6f 74 29 3b 0a 09 09 69 66 20 28 63 68 wroot);...if (ch
9120: 64 69 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a dir_ret != 0) {.
9130: 09 09 09 70 65 72 72 6f 72 28 22 63 68 64 69 72 ...perror("chdir
9140: 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 ");.....return(1
9150: 29 3b 0a 09 09 7d 0a 0a 09 09 63 68 72 6f 6f 74 );...}....chroot
9160: 5f 72 65 74 20 3d 20 63 68 72 6f 6f 74 28 22 2e _ret = chroot(".
9170: 22 29 3b 0a 09 09 69 66 20 28 63 68 72 6f 6f 74 ");...if (chroot
9180: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 _ret != 0) {....
9190: 70 65 72 72 6f 72 28 22 63 68 72 6f 6f 74 22 29 perror("chroot")
91a0: 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b ;.....return(1);
91b0: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 44 72 6f ...}..}.../* Dro
91c0: 70 20 70 72 69 76 69 6c 65 67 65 73 2c 20 69 66 p privileges, if
91d0: 20 61 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a appropriate */.
91e0: 09 69 66 20 28 73 65 74 75 69 64 5f 65 6e 61 62 .if (setuid_enab
91f0: 6c 65 64 29 20 7b 0a 09 09 73 65 74 75 69 64 5f led) {...setuid_
9200: 72 65 74 20 3d 20 73 65 74 75 69 64 28 75 73 65 ret = setuid(use
9210: 72 29 3b 0a 09 09 69 66 20 28 73 65 74 75 69 64 r);...if (setuid
9220: 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 _ret != 0) {....
9230: 70 65 72 72 6f 72 28 22 73 65 74 75 69 64 22 29 perror("setuid")
9240: 3b 0a 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b ;.....return(1);
9250: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 ...}..}.../* Ini
9260: 74 69 61 6c 69 7a 65 20 2a 2f 0a 09 69 6e 69 74 tialize */..init
9270: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 69 6e 69 _ret = filed_ini
9280: 74 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 09 t(cache_size);..
9290: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20 if (init_ret !=
92a0: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66 0) {...perror("f
92b0: 69 6c 65 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 iled_init");....
92c0: 72 65 74 75 72 6e 28 33 29 3b 0a 09 7d 0a 0a 09 return(3);..}...
92d0: 2f 2a 20 43 72 65 61 74 65 20 6c 6f 67 67 69 6e /* Create loggin
92e0: 67 20 74 68 72 65 61 64 20 2a 2f 0a 09 69 6e 69 g thread */..ini
92f0: 74 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 6c 6f t_ret = filed_lo
9300: 67 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 gging_thread_ini
9310: 74 28 6c 6f 67 5f 66 70 29 3b 0a 09 69 66 20 28 t(log_fp);..if (
9320: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b init_ret != 0) {
9330: 0a 09 09 70 65 72 72 6f 72 28 22 66 69 6c 65 64 ...perror("filed
9340: 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64 5f _logging_thread_
9350: 69 6e 69 74 22 29 3b 0a 0a 09 09 72 65 74 75 72 init");....retur
9360: 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 n(4);..}.../* Cr
9370: 65 61 74 65 20 77 6f 72 6b 65 72 20 74 68 72 65 eate worker thre
9380: 61 64 73 20 2a 2f 0a 09 69 6e 69 74 5f 72 65 74 ads */..init_ret
9390: 20 3d 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f = filed_worker_
93a0: 74 68 72 65 61 64 73 5f 69 6e 69 74 28 66 64 2c threads_init(fd,
93b0: 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 2c 20 26 thread_count, &
93c0: 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 29 3b thread_options);
93d0: 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 ..if (init_ret !
93e0: 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 = 0) {...perror(
93f0: 22 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68 "filed_worker_th
9400: 72 65 61 64 73 5f 69 6e 69 74 22 29 3b 0a 0a 09 reads_init");...
9410: 09 72 65 74 75 72 6e 28 35 29 3b 0a 09 7d 0a 0a .return(5);..}..
9420: 09 2f 2a 20 57 61 69 74 20 66 6f 72 20 74 68 72 ./* Wait for thr
9430: 65 61 64 73 20 74 6f 20 65 78 69 74 20 2a 2f 0a eads to exit */.
9440: 09 2f 2a 20 58 58 58 3a 54 4f 44 4f 3a 20 4d 6f ./* XXX:TODO: Mo
9450: 6e 69 74 6f 72 20 74 68 72 65 61 64 20 75 73 61 nitor thread usa
9460: 67 65 20 2a 2f 0a 09 77 68 69 6c 65 20 28 31 29 ge */..while (1)
9470: 20 7b 0a 09 09 73 6c 65 65 70 28 36 30 29 3b 0a {...sleep(60);.
9480: 09 7d 0a 0a 09 2f 2a 20 52 65 74 75 72 6e 20 69 .}.../* Return i
9490: 6e 20 66 61 69 6c 75 72 65 20 2a 2f 0a 09 72 65 n failure */..re
94a0: 74 75 72 6e 28 32 29 3b 0a 7d 0a turn(2);.}.