Hex Artifact Content

Artifact 390765784daf96c457e92a6635f5f24f2a1dd908:


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 31 31 22 0a 23 64 65 66 69  ION "1.11".#defi
01e0: 6e 65 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c  ne FILED_SENDFIL
01f0: 45 5f 4d 41 58 20 31 36 37 37 37 32 31 35 0a 23  E_MAX 16777215.#
0200: 64 65 66 69 6e 65 20 46 49 4c 45 44 5f 4d 41 58  define FILED_MAX
0210: 5f 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54 20 33  _FAILURE_COUNT 3
0220: 30 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 44 5f  0.#define FILED_
0230: 44 45 46 41 55 4c 54 5f 54 59 50 45 20 22 61 70  DEFAULT_TYPE "ap
0240: 70 6c 69 63 61 74 69 6f 6e 2f 6f 63 74 65 74 2d  plication/octet-
0250: 73 74 72 65 61 6d 22 0a 23 64 65 66 69 6e 65 20  stream".#define 
0260: 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46 46 45  FILED_PATH_BUFFE
0270: 52 5f 53 49 5a 45 20 31 30 31 30 0a 0a 2f 2a 20  R_SIZE 1010../* 
0280: 44 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a  Default values *
0290: 2f 0a 23 64 65 66 69 6e 65 20 50 4f 52 54 20 38  /.#define PORT 8
02a0: 30 0a 23 64 65 66 69 6e 65 20 54 48 52 45 41 44  0.#define THREAD
02b0: 5f 43 4f 55 4e 54 20 35 0a 23 64 65 66 69 6e 65  _COUNT 5.#define
02c0: 20 42 49 4e 44 5f 41 44 44 52 20 22 3a 3a 22 0a   BIND_ADDR "::".
02d0: 23 64 65 66 69 6e 65 20 43 41 43 48 45 5f 53 49  #define CACHE_SI
02e0: 5a 45 20 38 32 30 39 0a 23 64 65 66 69 6e 65 20  ZE 8209.#define 
02f0: 4c 4f 47 5f 46 49 4c 45 20 22 2d 22 0a 0a 2f 2a  LOG_FILE "-"../*
0300: 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   Configuration o
0310: 70 74 69 6f 6e 73 20 74 68 61 74 20 77 6f 72 6b  ptions that work
0320: 20 74 68 72 65 61 64 73 20 6e 65 65 64 20 74 6f   threads need to
0330: 20 62 65 20 61 77 61 72 65 20 6f 66 20 2a 2f 0a   be aware of */.
0340: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74  struct filed_opt
0350: 69 6f 6e 73 20 7b 0a 09 69 6e 74 20 76 68 6f 73  ions {..int vhos
0360: 74 73 5f 65 6e 61 62 6c 65 64 3b 0a 7d 3b 0a 0a  ts_enabled;.};..
0370: 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f 72  /* Arguments for
0380: 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20   worker threads 
0390: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f  */.struct filed_
03a0: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72  worker_thread_ar
03b0: 67 73 20 7b 0a 09 69 6e 74 20 66 64 3b 0a 09 73  gs {..int fd;..s
03c0: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69  truct filed_opti
03d0: 6f 6e 73 20 6f 70 74 69 6f 6e 73 3b 0a 7d 3b 0a  ons options;.};.
03e0: 0a 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 66 6f  ./* Arguments fo
03f0: 72 20 6c 6f 67 67 69 6e 67 20 74 68 72 65 61 64  r logging thread
0400: 73 20 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65  s */.struct file
0410: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
0420: 5f 61 72 67 73 20 7b 0a 09 46 49 4c 45 20 2a 66  _args {..FILE *f
0430: 70 3b 0a 7d 3b 0a 0a 2f 2a 20 46 69 6c 65 20 69  p;.};../* File i
0440: 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 73 74  nformation */.st
0450: 72 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69  ruct filed_filei
0460: 6e 66 6f 20 7b 0a 09 70 74 68 72 65 61 64 5f 6d  nfo {..pthread_m
0470: 75 74 65 78 5f 74 20 6d 75 74 65 78 3b 0a 09 63  utex_t mutex;..c
0480: 68 61 72 20 70 61 74 68 5b 46 49 4c 45 44 5f 50  har path[FILED_P
0490: 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d  ATH_BUFFER_SIZE]
04a0: 3b 0a 09 69 6e 74 20 66 64 3b 0a 09 6f 66 66 5f  ;..int fd;..off_
04b0: 74 20 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 6c 61  t len;..char *la
04c0: 73 74 6d 6f 64 3b 0a 09 63 68 61 72 20 6c 61 73  stmod;..char las
04d0: 74 6d 6f 64 5f 62 5b 36 34 5d 3b 0a 09 63 6f 6e  tmod_b[64];..con
04e0: 73 74 20 63 68 61 72 20 2a 74 79 70 65 3b 0a 09  st char *type;..
04f0: 63 68 61 72 20 65 74 61 67 5b 36 34 5d 3b 0a 7d  char etag[64];.}
0500: 3b 0a 0a 2f 2a 20 52 65 71 75 65 73 74 20 76 61  ;../* Request va
0510: 72 69 61 62 6c 65 73 20 2a 2f 0a 73 74 72 75 63  riables */.struc
0520: 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65 71  t filed_http_req
0530: 75 65 73 74 20 7b 0a 09 2f 2a 2a 20 42 75 66 66  uest {../** Buff
0540: 65 72 73 20 2a 2a 2f 0a 09 73 74 72 75 63 74 20  ers **/..struct 
0550: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 66  filed_fileinfo f
0560: 69 6c 65 69 6e 66 6f 3b 0a 09 63 68 61 72 20 74  ileinfo;..char t
0570: 6d 70 62 75 66 5b 46 49 4c 45 44 5f 50 41 54 48  mpbuf[FILED_PATH
0580: 5f 42 55 46 46 45 52 5f 53 49 5a 45 5d 3b 0a 0a  _BUFFER_SIZE];..
0590: 09 2f 2a 2a 20 48 54 54 50 20 52 65 71 75 65 73  ./** HTTP Reques
05a0: 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2a  t information **
05b0: 2f 0a 09 2f 2a 2a 2a 20 54 79 70 65 20 6f 66 20  /../*** Type of 
05c0: 72 65 71 75 65 73 74 20 28 48 45 41 44 20 6f 72  request (HEAD or
05d0: 20 47 45 54 29 20 2a 2a 2a 2f 0a 09 65 6e 75 6d   GET) ***/..enum
05e0: 20 7b 0a 09 09 46 49 4c 45 44 5f 52 45 51 55 45   {...FILED_REQUE
05f0: 53 54 5f 4d 45 54 48 4f 44 5f 47 45 54 2c 0a 09  ST_METHOD_GET,..
0600: 09 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d  .FILED_REQUEST_M
0610: 45 54 48 4f 44 5f 48 45 41 44 0a 09 7d 20 6d 65  ETHOD_HEAD..} me
0620: 74 68 6f 64 3b 0a 0a 09 2f 2a 2a 2a 20 50 61 74  thod;.../*** Pat
0630: 68 20 62 65 69 6e 67 20 72 65 71 75 65 73 74 65  h being requeste
0640: 64 20 2a 2a 2a 2f 0a 09 63 68 61 72 20 70 61 74  d ***/..char pat
0650: 68 5b 46 49 4c 45 44 5f 50 41 54 48 5f 42 55 46  h[FILED_PATH_BUF
0660: 46 45 52 5f 53 49 5a 45 5d 3b 20 0a 0a 09 2f 2a  FER_SIZE]; .../*
0670: 2a 2a 20 50 61 74 68 20 74 79 70 65 20 2a 2a 2a  ** Path type ***
0680: 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46 49 4c 45  /..enum {...FILE
0690: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 44  D_REQUEST_TYPE_D
06a0: 49 52 45 43 54 4f 52 59 2c 0a 09 09 46 49 4c 45  IRECTORY,...FILE
06b0: 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f 4f  D_REQUEST_TYPE_O
06c0: 54 48 45 52 0a 09 7d 20 74 79 70 65 3b 0a 0a 09  THER..} type;...
06d0: 73 74 72 75 63 74 20 7b 0a 09 09 73 74 72 75 63  struct {...struc
06e0: 74 20 7b 0a 09 09 09 69 6e 74 20 70 72 65 73 65  t {....int prese
06f0: 6e 74 3b 0a 09 09 09 6f 66 66 5f 74 20 6f 66 66  nt;....off_t off
0700: 73 65 74 3b 20 20 20 2f 2a 2a 2a 20 52 61 6e 67  set;   /*** Rang
0710: 65 20 73 74 61 72 74 20 2a 2a 2a 2f 0a 09 09 09  e start ***/....
0720: 6f 66 66 5f 74 20 6c 65 6e 67 74 68 3b 20 20 20  off_t length;   
0730: 2f 2a 2a 2a 20 52 61 6e 67 65 20 6c 65 6e 67 74  /*** Range lengt
0740: 68 20 2a 2a 2a 2f 0a 09 09 7d 20 72 61 6e 67 65  h ***/...} range
0750: 3b 0a 0a 09 09 73 74 72 75 63 74 20 7b 0a 09 09  ;....struct {...
0760: 09 69 6e 74 20 70 72 65 73 65 6e 74 3b 0a 09 09  .int present;...
0770: 09 63 68 61 72 20 68 6f 73 74 5b 46 49 4c 45 44  .char host[FILED
0780: 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a  _PATH_BUFFER_SIZ
0790: 45 5d 3b 0a 09 09 7d 20 68 6f 73 74 3b 0a 0a 09  E];...} host;...
07a0: 09 65 6e 75 6d 20 7b 0a 09 09 09 46 49 4c 45 44  .enum {....FILED
07b0: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
07c0: 45 2c 0a 09 09 09 46 49 4c 45 44 5f 43 4f 4e 4e  E,....FILED_CONN
07d0: 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56  ECTION_KEEP_ALIV
07e0: 45 0a 09 09 7d 20 63 6f 6e 6e 65 63 74 69 6f 6e  E...} connection
07f0: 3b 0a 09 7d 20 68 65 61 64 65 72 73 3b 0a 7d 3b  ;..} headers;.};
0800: 0a 0a 2f 2a 20 4c 6f 67 20 72 65 63 6f 72 64 20  ../* Log record 
0810: 2a 2f 0a 73 74 72 75 63 74 20 66 69 6c 65 64 5f  */.struct filed_
0820: 6c 6f 67 5f 65 6e 74 72 79 20 7b 0a 09 2f 2a 20  log_entry {../* 
0830: 54 79 70 65 20 6f 66 20 6c 6f 67 20 65 6e 74 72  Type of log entr
0840: 79 20 2a 2f 0a 09 65 6e 75 6d 20 7b 0a 09 09 46  y */..enum {...F
0850: 49 4c 45 44 5f 4c 4f 47 5f 54 59 50 45 5f 4d 45  ILED_LOG_TYPE_ME
0860: 53 53 41 47 45 2c 0a 09 09 46 49 4c 45 44 5f 4c  SSAGE,...FILED_L
0870: 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45 52  OG_TYPE_TRANSFER
0880: 0a 09 7d 20 74 79 70 65 3b 0a 0a 09 2f 2a 20 4c  ..} type;.../* L
0890: 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 2f  inked list head/
08a0: 74 61 69 6c 20 2a 2f 0a 09 73 74 72 75 63 74 20  tail */..struct 
08b0: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
08c0: 2a 5f 6e 65 78 74 3b 0a 09 73 74 72 75 63 74 20  *_next;..struct 
08d0: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20  filed_log_entry 
08e0: 2a 5f 70 72 65 76 3b 0a 0a 09 2f 2a 20 54 68 72  *_prev;.../* Thr
08f0: 65 61 64 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ead from which t
0900: 68 69 73 20 6c 6f 67 20 65 6e 74 72 79 20 65 6d  his log entry em
0910: 69 6e 61 74 65 73 20 2a 2f 0a 09 70 74 68 72 65  inates */..pthre
0920: 61 64 5f 74 20 74 68 72 65 61 64 3b 0a 0a 09 2f  ad_t thread;.../
0930: 2a 20 4d 65 73 73 61 67 65 20 62 75 66 66 65 72  * Message buffer
0940: 20 66 6f 72 20 74 79 70 65 20 3d 20 4d 45 53 53   for type = MESS
0950: 41 47 45 20 2a 2f 0a 09 2f 2a 20 50 61 74 68 20  AGE */../* Path 
0960: 62 75 66 66 65 72 20 66 6f 72 20 74 79 70 65 20  buffer for type 
0970: 3d 20 54 52 41 4e 53 46 45 52 20 2a 2f 0a 09 63  = TRANSFER */..c
0980: 68 61 72 20 62 75 66 66 65 72 5b 46 49 4c 45 44  har buffer[FILED
0990: 5f 50 41 54 48 5f 42 55 46 46 45 52 5f 53 49 5a  _PATH_BUFFER_SIZ
09a0: 45 5d 3b 0a 0a 09 2f 2a 20 49 74 65 6d 73 20 66  E];.../* Items f
09b0: 6f 72 20 74 79 70 65 20 3d 20 54 52 41 4e 53 46  or type = TRANSF
09c0: 45 52 20 2a 2f 0a 09 69 6e 74 20 68 74 74 70 5f  ER */..int http_
09d0: 63 6f 64 65 3b 0a 09 63 6f 6e 73 74 20 63 68 61  code;..const cha
09e0: 72 20 2a 72 65 61 73 6f 6e 3b 0a 09 74 69 6d 65  r *reason;..time
09f0: 5f 74 20 73 74 61 72 74 74 69 6d 65 3b 0a 09 74  _t starttime;..t
0a00: 69 6d 65 5f 74 20 65 6e 64 74 69 6d 65 3b 0a 09  ime_t endtime;..
0a10: 6f 66 66 5f 74 20 72 65 71 5f 6f 66 66 73 65 74  off_t req_offset
0a20: 3b 0a 09 6f 66 66 5f 74 20 72 65 71 5f 6c 65 6e  ;..off_t req_len
0a30: 67 74 68 3b 0a 09 6f 66 66 5f 74 20 73 65 6e 74  gth;..off_t sent
0a40: 5f 6c 65 6e 67 74 68 3b 0a 09 6f 66 66 5f 74 20  _length;..off_t 
0a50: 66 69 6c 65 5f 6c 65 6e 67 74 68 3b 0a 09 63 68  file_length;..ch
0a60: 61 72 20 69 70 5b 31 32 38 5d 3b 0a 09 69 6e 74  ar ip[128];..int
0a70: 20 70 6f 72 74 3b 0a 09 69 6e 74 20 6d 65 74 68   port;..int meth
0a80: 6f 64 3b 0a 7d 3b 0a 0a 2f 2a 20 47 6c 6f 62 61  od;.};../* Globa
0a90: 6c 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 2f  l variables */./
0aa0: 2a 2a 20 4f 70 65 6e 20 46 69 6c 65 20 63 61 63  ** Open File cac
0ab0: 68 65 20 2a 2a 2f 0a 73 74 72 75 63 74 20 66 69  he **/.struct fi
0ac0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 66 69  led_fileinfo *fi
0ad0: 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63  led_fileinfo_fdc
0ae0: 61 63 68 65 20 3d 20 4e 55 4c 4c 3b 0a 75 6e 73  ache = NULL;.uns
0af0: 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f  igned int filed_
0b00: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65  fileinfo_fdcache
0b10: 5f 73 69 7a 65 20 3d 20 30 3b 0a 0a 2f 2a 2a 20  _size = 0;../** 
0b20: 4c 6f 67 67 69 6e 67 20 2a 2a 2f 0a 73 74 72 75  Logging **/.stru
0b30: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ct filed_log_ent
0b40: 72 79 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ry *filed_log_ms
0b50: 67 5f 6c 69 73 74 3b 0a 70 74 68 72 65 61 64 5f  g_list;.pthread_
0b60: 6d 75 74 65 78 5f 74 20 66 69 6c 65 64 5f 6c 6f  mutex_t filed_lo
0b70: 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78  g_msg_list_mutex
0b80: 3b 0a 70 74 68 72 65 61 64 5f 63 6f 6e 64 5f 74  ;.pthread_cond_t
0b90: 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c   filed_log_msg_l
0ba0: 69 73 74 5f 72 65 61 64 79 3b 0a 0a 2f 2a 20 53  ist_ready;../* S
0bb0: 69 67 6e 61 6c 20 48 61 6e 64 6c 65 72 20 2a 2f  ignal Handler */
0bc0: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
0bd0: 65 64 5f 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65  ed_signal_handle
0be0: 72 28 69 6e 74 20 73 69 67 6e 61 6c 5f 6e 75 6d  r(int signal_num
0bf0: 62 65 72 29 20 7b 0a 09 73 74 72 75 63 74 20 66  ber) {..struct f
0c00: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63  iled_fileinfo *c
0c10: 61 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20  ache;..unsigned 
0c20: 69 6e 74 20 69 64 78 3b 0a 0a 09 73 77 69 74 63  int idx;...switc
0c30: 68 20 28 73 69 67 6e 61 6c 5f 6e 75 6d 62 65 72  h (signal_number
0c40: 29 20 7b 0a 09 09 63 61 73 65 20 53 49 47 48 55  ) {...case SIGHU
0c50: 50 3a 0a 09 09 09 66 6f 72 20 28 69 64 78 20 3d  P:....for (idx =
0c60: 20 30 3b 20 69 64 78 20 3c 20 66 69 6c 65 64 5f   0; idx < filed_
0c70: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65  fileinfo_fdcache
0c80: 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29 20 7b 0a  _size; idx++) {.
0c90: 09 09 09 09 63 61 63 68 65 20 3d 20 26 66 69 6c  ....cache = &fil
0ca0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61  ed_fileinfo_fdca
0cb0: 63 68 65 5b 69 64 78 5d 3b 0a 0a 09 09 09 09 70  che[idx];......p
0cc0: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63  thread_mutex_loc
0cd0: 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65 78 29  k(&cache->mutex)
0ce0: 3b 0a 0a 09 09 09 09 63 61 63 68 65 2d 3e 70 61  ;......cache->pa
0cf0: 74 68 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09  th[0] = '\0';...
0d00: 09 09 69 66 20 28 63 61 63 68 65 2d 3e 66 64 20  ..if (cache->fd 
0d10: 3e 3d 20 30 29 20 7b 0a 09 09 09 09 09 63 6c 6f  >= 0) {......clo
0d20: 73 65 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 0a  se(cache->fd);..
0d30: 09 09 09 09 09 63 61 63 68 65 2d 3e 66 64 20 3d  .....cache->fd =
0d40: 20 2d 31 3b 0a 09 09 09 09 7d 0a 0a 09 09 09 09   -1;.....}......
0d50: 63 61 63 68 65 2d 3e 6c 61 73 74 6d 6f 64 20 3d  cache->lastmod =
0d60: 20 22 22 3b 0a 09 09 09 09 63 61 63 68 65 2d 3e   "";.....cache->
0d70: 74 79 70 65 20 3d 20 22 22 3b 0a 0a 09 09 09 09  type = "";......
0d80: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e  pthread_mutex_un
0d90: 6c 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74  lock(&cache->mut
0da0: 65 78 29 3b 0a 09 09 09 7d 0a 09 09 09 62 72 65  ex);....}....bre
0db0: 61 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 3b  ak;..}...return;
0dc0: 0a 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  .}../* Initializ
0dd0: 65 20 63 61 63 68 65 20 2a 2f 0a 73 74 61 74 69  e cache */.stati
0de0: 63 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74  c int filed_init
0df0: 5f 63 61 63 68 65 28 75 6e 73 69 67 6e 65 64 20  _cache(unsigned 
0e00: 69 6e 74 20 63 61 63 68 65 5f 73 69 7a 65 29 20  int cache_size) 
0e10: 7b 0a 09 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  {..unsigned int 
0e20: 69 64 78 3b 0a 09 69 6e 74 20 6d 75 74 65 78 5f  idx;..int mutex_
0e30: 69 6e 69 74 5f 72 65 74 3b 0a 0a 09 2f 2a 20 43  init_ret;.../* C
0e40: 61 63 68 65 20 6d 61 79 20 6e 6f 74 20 62 65 20  ache may not be 
0e50: 72 65 2d 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a  re-initialized *
0e60: 2f 0a 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c  /..if (filed_fil
0e70: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69  einfo_fdcache_si
0e80: 7a 65 20 21 3d 20 30 20 7c 7c 20 66 69 6c 65 64  ze != 0 || filed
0e90: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
0ea0: 65 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72  e != NULL) {...r
0eb0: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 2f  eturn(1);..}.../
0ec0: 2a 20 41 6c 6c 6f 63 61 74 65 20 63 61 63 68 65  * Allocate cache
0ed0: 20 2a 2f 0a 09 66 69 6c 65 64 5f 66 69 6c 65 69   */..filed_filei
0ee0: 6e 66 6f 5f 66 64 63 61 63 68 65 5f 73 69 7a 65  nfo_fdcache_size
0ef0: 20 3d 20 63 61 63 68 65 5f 73 69 7a 65 3b 0a 09   = cache_size;..
0f00: 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66  filed_fileinfo_f
0f10: 64 63 61 63 68 65 20 3d 20 6d 61 6c 6c 6f 63 28  dcache = malloc(
0f20: 73 69 7a 65 6f 66 28 2a 66 69 6c 65 64 5f 66 69  sizeof(*filed_fi
0f30: 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 29 20  leinfo_fdcache) 
0f40: 2a 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f  * filed_fileinfo
0f50: 5f 66 64 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  _fdcache_size);.
0f60: 09 69 66 20 28 66 69 6c 65 64 5f 66 69 6c 65 69  .if (filed_filei
0f70: 6e 66 6f 5f 66 64 63 61 63 68 65 20 3d 3d 20 4e  nfo_fdcache == N
0f80: 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  ULL) {...return(
0f90: 31 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 49 6e 69 74  1);..}.../* Init
0fa0: 69 61 6c 69 7a 65 20 63 61 63 68 65 20 65 6e 74  ialize cache ent
0fb0: 72 69 65 73 20 2a 2f 0a 09 66 6f 72 20 28 69 64  ries */..for (id
0fc0: 78 20 3d 20 30 3b 20 69 64 78 20 3c 20 66 69 6c  x = 0; idx < fil
0fd0: 65 64 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61  ed_fileinfo_fdca
0fe0: 63 68 65 5f 73 69 7a 65 3b 20 69 64 78 2b 2b 29  che_size; idx++)
0ff0: 20 7b 0a 09 09 6d 75 74 65 78 5f 69 6e 69 74 5f   {...mutex_init_
1000: 72 65 74 20 3d 20 70 74 68 72 65 61 64 5f 6d 75  ret = pthread_mu
1010: 74 65 78 5f 69 6e 69 74 28 26 66 69 6c 65 64 5f  tex_init(&filed_
1020: 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68 65  fileinfo_fdcache
1030: 5b 69 64 78 5d 2e 6d 75 74 65 78 2c 20 4e 55 4c  [idx].mutex, NUL
1040: 4c 29 3b 0a 09 09 69 66 20 28 6d 75 74 65 78 5f  L);...if (mutex_
1050: 69 6e 69 74 5f 72 65 74 20 21 3d 20 30 29 20 7b  init_ret != 0) {
1060: 0a 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09  ....return(1);..
1070: 09 7d 0a 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65  .}....filed_file
1080: 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78  info_fdcache[idx
1090: 5d 2e 70 61 74 68 5b 30 5d 20 3d 20 27 5c 30 27  ].path[0] = '\0'
10a0: 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c 65 69 6e  ;...filed_filein
10b0: 66 6f 5f 66 64 63 61 63 68 65 5b 69 64 78 5d 2e  fo_fdcache[idx].
10c0: 66 64 20 3d 20 2d 31 3b 0a 09 09 66 69 6c 65 64  fd = -1;...filed
10d0: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
10e0: 65 5b 69 64 78 5d 2e 6c 61 73 74 6d 6f 64 20 3d  e[idx].lastmod =
10f0: 20 22 22 3b 0a 09 09 66 69 6c 65 64 5f 66 69 6c   "";...filed_fil
1100: 65 69 6e 66 6f 5f 66 64 63 61 63 68 65 5b 69 64  einfo_fdcache[id
1110: 78 5d 2e 74 79 70 65 20 3d 20 22 22 3b 0a 09 7d  x].type = "";..}
1120: 0a 0a 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a  ...return(0);.}.
1130: 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 70  ./* Initialize p
1140: 72 6f 63 65 73 73 20 2a 2f 0a 73 74 61 74 69 63  rocess */.static
1150: 20 69 6e 74 20 66 69 6c 65 64 5f 69 6e 69 74 28   int filed_init(
1160: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 61 63  unsigned int cac
1170: 68 65 5f 73 69 7a 65 29 20 7b 0a 09 73 74 61 74  he_size) {..stat
1180: 69 63 20 69 6e 74 20 63 61 6c 6c 65 64 20 3d 20  ic int called = 
1190: 30 3b 0a 09 73 73 69 7a 65 5f 74 20 72 65 61 64  0;..ssize_t read
11a0: 5f 72 65 74 20 3d 20 30 3b 0a 09 75 6e 73 69 67  _ret = 0;..unsig
11b0: 6e 65 64 20 69 6e 74 20 72 61 6e 64 6f 6d 5f 76  ned int random_v
11c0: 61 6c 75 65 20 3d 20 30 3b 0a 09 69 6e 74 20 63  alue = 0;..int c
11d0: 61 63 68 65 5f 72 65 74 3b 0a 09 69 6e 74 20 72  ache_ret;..int r
11e0: 61 6e 64 6f 6d 5f 66 64 3b 0a 0a 09 69 66 20 28  andom_fd;...if (
11f0: 63 61 6c 6c 65 64 29 20 7b 0a 09 09 72 65 74 75  called) {...retu
1200: 72 6e 28 30 29 3b 0a 09 7d 0a 0a 09 63 61 6c 6c  rn(0);..}...call
1210: 65 64 20 3d 20 31 3b 0a 0a 09 2f 2a 20 41 74 74  ed = 1;.../* Att
1220: 65 6d 70 74 20 74 6f 20 6c 6f 63 6b 20 61 6c 6c  empt to lock all
1230: 20 6d 65 6d 6f 72 79 20 74 6f 20 70 68 79 73 69   memory to physi
1240: 63 61 6c 20 52 41 4d 20 28 62 75 74 20 64 6f 6e  cal RAM (but don
1250: 27 74 20 63 61 72 65 20 69 66 20 77 65 20 63 61  't care if we ca
1260: 6e 27 74 29 20 2a 2f 0a 09 6d 6c 6f 63 6b 61 6c  n't) */..mlockal
1270: 6c 28 4d 43 4c 5f 43 55 52 52 45 4e 54 20 7c 20  l(MCL_CURRENT | 
1280: 4d 43 4c 5f 46 55 54 55 52 45 29 3b 0a 0a 09 2f  MCL_FUTURE);.../
1290: 2a 20 49 67 6e 6f 72 65 20 53 49 47 50 49 50 45  * Ignore SIGPIPE
12a0: 20 2a 2f 0a 09 73 69 67 6e 61 6c 28 53 49 47 50   */..signal(SIGP
12b0: 49 50 45 2c 20 53 49 47 5f 49 47 4e 29 3b 0a 0a  IPE, SIG_IGN);..
12c0: 09 2f 2a 20 48 61 6e 64 6c 65 20 53 49 47 48 55  ./* Handle SIGHU
12d0: 50 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c  P to release all
12e0: 20 63 61 63 68 65 73 20 2a 2f 0a 09 73 69 67 6e   caches */..sign
12f0: 61 6c 28 53 49 47 48 55 50 2c 20 66 69 6c 65 64  al(SIGHUP, filed
1300: 5f 73 69 67 6e 61 6c 5f 68 61 6e 64 6c 65 72 29  _signal_handler)
1310: 3b 0a 0a 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ;.../* Initializ
1320: 65 20 63 61 63 68 65 20 73 74 72 75 63 74 75 72  e cache structur
1330: 65 20 2a 2f 0a 09 63 61 63 68 65 5f 72 65 74 20  e */..cache_ret 
1340: 3d 20 66 69 6c 65 64 5f 69 6e 69 74 5f 63 61 63  = filed_init_cac
1350: 68 65 28 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  he(cache_size);.
1360: 09 69 66 20 28 63 61 63 68 65 5f 72 65 74 20 21  .if (cache_ret !
1370: 3d 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e 28  = 0) {...return(
1380: 63 61 63 68 65 5f 72 65 74 29 3b 0a 09 7d 0a 0a  cache_ret);..}..
1390: 09 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 72  ./* Initialize r
13a0: 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e  andom number gen
13b0: 65 72 61 74 6f 72 20 2a 2f 0a 09 72 61 6e 64 6f  erator */..rando
13c0: 6d 5f 66 64 20 3d 20 6f 70 65 6e 28 22 2f 64 65  m_fd = open("/de
13d0: 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f 52 44  v/urandom", O_RD
13e0: 4f 4e 4c 59 29 3b 0a 09 69 66 20 28 72 61 6e 64  ONLY);..if (rand
13f0: 6f 6d 5f 66 64 20 3e 3d 20 30 29 20 7b 0a 09 09  om_fd >= 0) {...
1400: 72 65 61 64 5f 72 65 74 20 3d 20 72 65 61 64 28  read_ret = read(
1410: 72 61 6e 64 6f 6d 5f 66 64 2c 20 26 72 61 6e 64  random_fd, &rand
1420: 6f 6d 5f 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66  om_value, sizeof
1430: 28 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 29 29 3b  (random_value));
1440: 0a 0a 09 09 63 6c 6f 73 65 28 72 61 6e 64 6f 6d  ....close(random
1450: 5f 66 64 29 3b 0a 09 7d 0a 0a 09 72 61 6e 64 6f  _fd);..}...rando
1460: 6d 5f 76 61 6c 75 65 20 5e 3d 20 67 65 74 70 69  m_value ^= getpi
1470: 64 28 29 3b 0a 09 72 61 6e 64 6f 6d 5f 76 61 6c  d();..random_val
1480: 75 65 20 5e 3d 20 67 65 74 75 69 64 28 29 3b 0a  ue ^= getuid();.
1490: 09 72 61 6e 64 6f 6d 5f 76 61 6c 75 65 20 5e 3d  .random_value ^=
14a0: 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 0a 09 73   time(NULL);...s
14b0: 72 61 6e 64 6f 6d 28 72 61 6e 64 6f 6d 5f 76 61  random(random_va
14c0: 6c 75 65 29 3b 0a 0a 09 72 65 74 75 72 6e 28 30  lue);...return(0
14d0: 29 3b 0a 0a 09 2f 2a 20 4e 4f 54 52 45 41 43 48  );.../* NOTREACH
14e0: 3a 20 52 65 61 64 20 6d 61 79 20 66 61 69 6c 20  : Read may fail 
14f0: 6f 72 20 73 75 63 63 65 65 64 2c 20 77 65 20 64  or succeed, we d
1500: 6f 6e 27 74 20 61 63 74 75 61 6c 6c 79 20 63 61  on't actually ca
1510: 72 65 20 2a 2f 0a 09 72 65 61 64 5f 72 65 74 20  re */..read_ret 
1520: 3d 20 72 65 61 64 5f 72 65 74 3b 0a 7d 0a 0a 2f  = read_ret;.}../
1530: 2a 20 4c 69 73 74 65 6e 20 6f 6e 20 61 20 70 61  * Listen on a pa
1540: 72 74 69 63 75 6c 61 72 20 61 64 64 72 65 73 73  rticular address
1550: 2f 70 6f 72 74 20 2a 2f 0a 73 74 61 74 69 63 20  /port */.static 
1560: 69 6e 74 20 66 69 6c 65 64 5f 6c 69 73 74 65 6e  int filed_listen
1570: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 64 64  (const char *add
1580: 72 65 73 73 2c 20 75 6e 73 69 67 6e 65 64 20 69  ress, unsigned i
1590: 6e 74 20 70 6f 72 74 29 20 7b 0a 09 73 74 72 75  nt port) {..stru
15a0: 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e 36 20  ct sockaddr_in6 
15b0: 61 64 64 72 5f 76 36 3b 0a 09 73 74 72 75 63 74  addr_v6;..struct
15c0: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64 64   sockaddr_in add
15d0: 72 5f 76 34 3b 0a 09 73 74 72 75 63 74 20 73 6f  r_v4;..struct so
15e0: 63 6b 61 64 64 72 20 2a 61 64 64 72 3b 0a 09 73  ckaddr *addr;..s
15f0: 6f 63 6b 6c 65 6e 5f 74 20 61 64 64 72 5f 6c 65  ocklen_t addr_le
1600: 6e 3b 0a 09 69 6e 74 20 70 74 6f 6e 5f 72 65 74  n;..int pton_ret
1610: 2c 20 62 69 6e 64 5f 72 65 74 2c 20 6c 69 73 74  , bind_ret, list
1620: 65 6e 5f 72 65 74 3b 0a 09 69 6e 74 20 66 61 6d  en_ret;..int fam
1630: 69 6c 79 3b 0a 09 69 6e 74 20 66 64 3b 0a 0a 0a  ily;..int fd;...
1640: 09 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45  .family = AF_INE
1650: 54 36 3b 0a 09 70 74 6f 6e 5f 72 65 74 20 3d 20  T6;..pton_ret = 
1660: 69 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79  inet_pton(family
1670: 2c 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72  , address, &addr
1680: 5f 76 36 2e 73 69 6e 36 5f 61 64 64 72 2e 73 36  _v6.sin6_addr.s6
1690: 5f 61 64 64 72 29 3b 0a 09 69 66 20 28 70 74 6f  _addr);..if (pto
16a0: 6e 5f 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09  n_ret != 1) {...
16b0: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54  family = AF_INET
16c0: 3b 0a 09 09 70 74 6f 6e 5f 72 65 74 20 3d 20 69  ;...pton_ret = i
16d0: 6e 65 74 5f 70 74 6f 6e 28 66 61 6d 69 6c 79 2c  net_pton(family,
16e0: 20 61 64 64 72 65 73 73 2c 20 26 61 64 64 72 5f   address, &addr_
16f0: 76 34 2e 73 69 6e 5f 61 64 64 72 2e 73 5f 61 64  v4.sin_addr.s_ad
1700: 64 72 29 3b 0a 09 09 69 66 20 28 70 74 6f 6e 5f  dr);...if (pton_
1710: 72 65 74 20 21 3d 20 31 29 20 7b 0a 09 09 09 72  ret != 1) {....r
1720: 65 74 75 72 6e 28 2d 31 29 3b 0a 09 09 7d 0a 0a  eturn(-1);...}..
1730: 09 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 66 61  ..addr_v4.sin_fa
1740: 6d 69 6c 79 20 3d 20 66 61 6d 69 6c 79 3b 0a 09  mily = family;..
1750: 09 61 64 64 72 5f 76 34 2e 73 69 6e 5f 70 6f 72  .addr_v4.sin_por
1760: 74 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b  t = htons(port);
1770: 0a 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75  ....addr = (stru
1780: 63 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26  ct sockaddr *) &
1790: 61 64 64 72 5f 76 34 3b 0a 09 09 61 64 64 72 5f  addr_v4;...addr_
17a0: 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64  len = sizeof(add
17b0: 72 5f 76 34 29 3b 0a 09 7d 20 65 6c 73 65 20 7b  r_v4);..} else {
17c0: 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f  ...addr_v6.sin6_
17d0: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54  family = AF_INET
17e0: 36 3b 0a 09 09 61 64 64 72 5f 76 36 2e 73 69 6e  6;...addr_v6.sin
17f0: 36 5f 66 6c 6f 77 69 6e 66 6f 20 3d 20 30 3b 0a  6_flowinfo = 0;.
1800: 09 09 61 64 64 72 5f 76 36 2e 73 69 6e 36 5f 73  ..addr_v6.sin6_s
1810: 63 6f 70 65 5f 69 64 20 3d 20 30 3b 0a 09 09 61  cope_id = 0;...a
1820: 64 64 72 5f 76 36 2e 73 69 6e 36 5f 70 6f 72 74  ddr_v6.sin6_port
1830: 20 3d 20 68 74 6f 6e 73 28 70 6f 72 74 29 3b 0a   = htons(port);.
1840: 0a 09 09 61 64 64 72 20 3d 20 28 73 74 72 75 63  ...addr = (struc
1850: 74 20 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61  t sockaddr *) &a
1860: 64 64 72 5f 76 36 3b 0a 09 09 61 64 64 72 5f 6c  ddr_v6;...addr_l
1870: 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64 64 72  en = sizeof(addr
1880: 5f 76 36 29 3b 0a 09 7d 0a 0a 09 66 64 20 3d 20  _v6);..}...fd = 
1890: 73 6f 63 6b 65 74 28 66 61 6d 69 6c 79 2c 20 53  socket(family, S
18a0: 4f 43 4b 5f 53 54 52 45 41 4d 2c 20 30 29 3b 0a  OCK_STREAM, 0);.
18b0: 09 69 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09  .if (fd < 0) {..
18c0: 09 72 65 74 75 72 6e 28 66 64 29 3b 0a 09 7d 0a  .return(fd);..}.
18d0: 0a 09 62 69 6e 64 5f 72 65 74 20 3d 20 62 69 6e  ..bind_ret = bin
18e0: 64 28 66 64 2c 20 61 64 64 72 2c 20 61 64 64 72  d(fd, addr, addr
18f0: 5f 6c 65 6e 29 3b 0a 09 69 66 20 28 62 69 6e 64  _len);..if (bind
1900: 5f 72 65 74 20 3c 20 30 29 20 7b 0a 09 09 63 6c  _ret < 0) {...cl
1910: 6f 73 65 28 66 64 29 3b 0a 0a 09 09 72 65 74 75  ose(fd);....retu
1920: 72 6e 28 2d 31 29 3b 0a 09 7d 0a 0a 09 6c 69 73  rn(-1);..}...lis
1930: 74 65 6e 5f 72 65 74 20 3d 20 6c 69 73 74 65 6e  ten_ret = listen
1940: 28 66 64 2c 20 31 32 38 29 3b 0a 09 69 66 20 28  (fd, 128);..if (
1950: 6c 69 73 74 65 6e 5f 72 65 74 20 21 3d 20 30 29  listen_ret != 0)
1960: 20 7b 0a 09 09 63 6c 6f 73 65 28 66 64 29 3b 0a   {...close(fd);.
1970: 0a 09 09 72 65 74 75 72 6e 28 2d 31 29 3b 0a 09  ...return(-1);..
1980: 7d 0a 0a 09 72 65 74 75 72 6e 28 66 64 29 3b 0a  }...return(fd);.
1990: 7d 0a 0a 2f 2a 20 4c 6f 67 20 61 20 6d 65 73 73  }../* Log a mess
19a0: 61 67 65 20 2a 2f 0a 23 69 66 64 65 66 20 46 49  age */.#ifdef FI
19b0: 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 0a 23 20 20  LED_DONT_LOG.#  
19c0: 64 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67  define filed_log
19d0: 67 69 6e 67 5f 74 68 72 65 61 64 5f 69 6e 69 74  ging_thread_init
19e0: 28 78 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20  (x) 0.#  define 
19f0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65  filed_log_msg_de
1a00: 62 75 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f  bug(x, ...) /**/
1a10: 0a 23 20 20 64 65 66 69 6e 65 20 66 69 6c 65 64  .#  define filed
1a20: 5f 6c 6f 67 5f 6d 73 67 28 78 2c 20 2e 2e 2e 29  _log_msg(x, ...)
1a30: 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20   /**/.#  define 
1a40: 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 28  filed_log_entry(
1a50: 78 29 20 2f 2a 2a 2f 0a 23 20 20 64 65 66 69 6e  x) /**/.#  defin
1a60: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 69 70 28 78  e filed_log_ip(x
1a70: 2c 20 2e 2e 2e 29 20 4e 55 4c 4c 0a 23 20 20 64  , ...) NULL.#  d
1a80: 65 66 69 6e 65 20 66 69 6c 65 64 5f 6c 6f 67 5f  efine filed_log_
1a90: 6e 65 77 28 78 29 20 26 6c 6f 63 61 6c 5f 64 75  new(x) &local_du
1aa0: 6d 6d 79 5f 6c 6f 67 0a 23 20 20 64 65 66 69 6e  mmy_log.#  defin
1ab0: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65 6e  e filed_log_open
1ac0: 28 78 29 20 73 74 64 6f 75 74 0a 23 65 6c 73 65  (x) stdout.#else
1ad0: 0a 23 20 20 69 66 64 65 66 20 46 49 4c 45 44 5f  .#  ifdef FILED_
1ae0: 44 45 42 55 47 0a 23 20 20 20 20 64 65 66 69 6e  DEBUG.#    defin
1af0: 65 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  e filed_log_msg_
1b00: 64 65 62 75 67 28 78 2c 20 2e 2e 2e 29 20 7b 20  debug(x, ...) { 
1b10: 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
1b20: 78 2c 20 5f 5f 56 41 5f 41 52 47 53 5f 5f 29 3b  x, __VA_ARGS__);
1b30: 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
1b40: 20 22 5c 6e 22 29 3b 20 66 66 6c 75 73 68 28 73   "\n"); fflush(s
1b50: 74 64 65 72 72 29 3b 20 7d 0a 23 20 20 65 6c 73  tderr); }.#  els
1b60: 65 0a 23 20 20 20 20 64 65 66 69 6e 65 20 66 69  e.#    define fi
1b70: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
1b80: 67 28 78 2c 20 2e 2e 2e 29 20 2f 2a 2a 2f 0a 23  g(x, ...) /**/.#
1b90: 20 20 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 69 74    endif../* Init
1ba0: 69 61 6c 69 7a 65 20 6c 6f 67 67 69 6e 67 20 74  ialize logging t
1bb0: 68 72 65 61 64 20 2a 2f 0a 73 74 61 74 69 63 20  hread */.static 
1bc0: 76 6f 69 64 20 2a 66 69 6c 65 64 5f 6c 6f 67 67  void *filed_logg
1bd0: 69 6e 67 5f 74 68 72 65 61 64 28 76 6f 69 64 20  ing_thread(void 
1be0: 2a 61 72 67 5f 70 29 20 7b 0a 09 73 74 72 75 63  *arg_p) {..struc
1bf0: 74 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f  t filed_logging_
1c00: 74 68 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67  thread_args *arg
1c10: 3b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f  ;..struct filed_
1c20: 6c 6f 67 5f 65 6e 74 72 79 20 2a 63 75 72 72 2c  log_entry *curr,
1c30: 20 2a 70 72 65 76 3b 0a 09 63 6f 6e 73 74 20 63   *prev;..const c
1c40: 68 61 72 20 2a 6d 65 74 68 6f 64 3b 0a 09 74 69  har *method;..ti
1c50: 6d 65 5f 74 20 6e 6f 77 3b 0a 09 46 49 4c 45 20  me_t now;..FILE 
1c60: 2a 66 70 3b 0a 0a 09 61 72 67 20 3d 20 61 72 67  *fp;...arg = arg
1c70: 5f 70 3b 0a 0a 09 66 70 20 3d 20 61 72 67 2d 3e  _p;...fp = arg->
1c80: 66 70 3b 0a 0a 09 77 68 69 6c 65 20 28 31 29 20  fp;...while (1) 
1c90: 7b 0a 09 09 70 74 68 72 65 61 64 5f 6d 75 74 65  {...pthread_mute
1ca0: 78 5f 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c 6f  x_lock(&filed_lo
1cb0: 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65 78  g_msg_list_mutex
1cc0: 29 3b 0a 09 09 70 74 68 72 65 61 64 5f 63 6f 6e  );...pthread_con
1cd0: 64 5f 77 61 69 74 28 26 66 69 6c 65 64 5f 6c 6f  d_wait(&filed_lo
1ce0: 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65 61 64 79  g_msg_list_ready
1cf0: 2c 20 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  , &filed_log_msg
1d00: 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09  _list_mutex);...
1d10: 09 63 75 72 72 20 3d 20 66 69 6c 65 64 5f 6c 6f  .curr = filed_lo
1d20: 67 5f 6d 73 67 5f 6c 69 73 74 3b 0a 09 09 66 69  g_msg_list;...fi
1d30: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74  led_log_msg_list
1d40: 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 09 70 74 68 72   = NULL;....pthr
1d50: 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b  ead_mutex_unlock
1d60: 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f  (&filed_log_msg_
1d70: 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a 09 09  list_mutex);....
1d80: 6e 6f 77 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29  now = time(NULL)
1d90: 3b 0a 0a 09 09 70 72 65 76 20 3d 20 4e 55 4c 4c  ;....prev = NULL
1da0: 3b 0a 09 09 66 6f 72 20 28 3b 20 63 75 72 72 3b  ;...for (; curr;
1db0: 20 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f 6e   curr = curr->_n
1dc0: 65 78 74 29 20 7b 0a 09 09 09 63 75 72 72 2d 3e  ext) {....curr->
1dd0: 5f 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 0a 09  _prev = prev;...
1de0: 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09  ..prev = curr;..
1df0: 09 7d 0a 0a 09 09 63 75 72 72 20 3d 20 70 72 65  .}....curr = pre
1e00: 76 3b 0a 09 09 77 68 69 6c 65 20 28 63 75 72 72  v;...while (curr
1e10: 29 20 7b 0a 09 09 09 73 77 69 74 63 68 20 28 63  ) {....switch (c
1e20: 75 72 72 2d 3e 74 79 70 65 29 20 7b 0a 09 09 09  urr->type) {....
1e30: 09 63 61 73 65 20 46 49 4c 45 44 5f 4c 4f 47 5f  .case FILED_LOG_
1e40: 54 59 50 45 5f 4d 45 53 53 41 47 45 3a 0a 09 09  TYPE_MESSAGE:...
1e50: 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22  ...fprintf(fp, "
1e60: 25 73 22 2c 20 63 75 72 72 2d 3e 62 75 66 66 65  %s", curr->buffe
1e70: 72 29 3b 0a 0a 09 09 09 09 09 62 72 65 61 6b 3b  r);.......break;
1e80: 0a 09 09 09 09 63 61 73 65 20 46 49 4c 45 44 5f  .....case FILED_
1e90: 4c 4f 47 5f 54 59 50 45 5f 54 52 41 4e 53 46 45  LOG_TYPE_TRANSFE
1ea0: 52 3a 0a 09 09 09 09 09 73 77 69 74 63 68 20 28  R:......switch (
1eb0: 63 75 72 72 2d 3e 6d 65 74 68 6f 64 29 20 7b 0a  curr->method) {.
1ec0: 09 09 09 09 09 09 63 61 73 65 20 46 49 4c 45 44  ......case FILED
1ed0: 5f 52 45 51 55 45 53 54 5f 4d 45 54 48 4f 44 5f  _REQUEST_METHOD_
1ee0: 47 45 54 3a 0a 09 09 09 09 09 09 09 6d 65 74 68  GET:........meth
1ef0: 6f 64 3d 22 47 45 54 22 3b 0a 09 09 09 09 09 09  od="GET";.......
1f00: 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 09 63 61  .break;.......ca
1f10: 73 65 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54  se FILED_REQUEST
1f20: 5f 4d 45 54 48 4f 44 5f 48 45 41 44 3a 0a 09 09  _METHOD_HEAD:...
1f30: 09 09 09 09 09 6d 65 74 68 6f 64 3d 22 48 45 41  .....method="HEA
1f40: 44 22 3b 0a 09 09 09 09 09 09 09 62 72 65 61 6b  D";........break
1f50: 3b 0a 09 09 09 09 09 09 64 65 66 61 75 6c 74 3a  ;.......default:
1f60: 0a 09 09 09 09 09 09 09 6d 65 74 68 6f 64 3d 22  ........method="
1f70: 3c 75 6e 6b 6e 6f 77 6e 3e 22 3b 0a 09 09 09 09  <unknown>";.....
1f80: 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 09 09 7d  ...break;......}
1f90: 0a 0a 09 09 09 09 09 69 66 20 28 63 75 72 72 2d  .......if (curr-
1fa0: 3e 65 6e 64 74 69 6d 65 20 3d 3d 20 28 28 74 69  >endtime == ((ti
1fb0: 6d 65 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 09  me_t) -1)) {....
1fc0: 09 09 09 63 75 72 72 2d 3e 65 6e 64 74 69 6d 65  ...curr->endtime
1fd0: 20 3d 20 6e 6f 77 3b 0a 09 09 09 09 09 7d 0a 0a   = now;......}..
1fe0: 09 09 09 09 09 66 70 72 69 6e 74 66 28 66 70 2c  .....fprintf(fp,
1ff0: 20 22 54 52 41 4e 53 46 45 52 20 4d 45 54 48 4f   "TRANSFER METHO
2000: 44 3d 25 73 20 50 41 54 48 3d 25 73 20 53 52 43  D=%s PATH=%s SRC
2010: 3d 25 73 3a 25 69 20 54 49 4d 45 2e 53 54 41 52  =%s:%i TIME.STAR
2020: 54 3d 25 6c 6c 75 20 54 49 4d 45 2e 45 4e 44 3d  T=%llu TIME.END=
2030: 25 6c 6c 75 20 43 4f 44 45 2e 56 41 4c 55 45 3d  %llu CODE.VALUE=
2040: 25 75 20 43 4f 44 45 2e 52 45 41 53 4f 4e 3d 25  %u CODE.REASON=%
2050: 73 20 52 45 51 55 45 53 54 2e 4f 46 46 53 45 54  s REQUEST.OFFSET
2060: 3d 25 6c 6c 75 20 52 45 51 55 45 53 54 2e 4c 45  =%llu REQUEST.LE
2070: 4e 47 54 48 3d 25 6c 6c 75 20 46 49 4c 45 2e 4c  NGTH=%llu FILE.L
2080: 45 4e 47 54 48 3d 25 6c 6c 75 20 54 52 41 4e 53  ENGTH=%llu TRANS
2090: 46 45 52 2e 4c 45 4e 47 54 48 3d 25 6c 6c 75 22  FER.LENGTH=%llu"
20a0: 2c 0a 09 09 09 09 09 09 6d 65 74 68 6f 64 2c 0a  ,.......method,.
20b0: 09 09 09 09 09 09 63 75 72 72 2d 3e 62 75 66 66  ......curr->buff
20c0: 65 72 2c 0a 09 09 09 09 09 09 63 75 72 72 2d 3e  er,.......curr->
20d0: 69 70 2c 20 63 75 72 72 2d 3e 70 6f 72 74 2c 0a  ip, curr->port,.
20e0: 09 09 09 09 09 09 28 75 6e 73 69 67 6e 65 64 20  ......(unsigned 
20f0: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75 72 72 2d  long long) curr-
2100: 3e 73 74 61 72 74 74 69 6d 65 2c 0a 09 09 09 09  >starttime,.....
2110: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
2120: 20 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 65 6e 64   long) curr->end
2130: 74 69 6d 65 2c 0a 09 09 09 09 09 09 63 75 72 72  time,.......curr
2140: 2d 3e 68 74 74 70 5f 63 6f 64 65 2c 20 63 75 72  ->http_code, cur
2150: 72 2d 3e 72 65 61 73 6f 6e 2c 0a 09 09 09 09 09  r->reason,......
2160: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
2170: 6c 6f 6e 67 29 20 63 75 72 72 2d 3e 72 65 71 5f  long) curr->req_
2180: 6f 66 66 73 65 74 2c 0a 09 09 09 09 09 09 28 75  offset,.......(u
2190: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
21a0: 67 29 20 63 75 72 72 2d 3e 72 65 71 5f 6c 65 6e  g) curr->req_len
21b0: 67 74 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69  gth,.......(unsi
21c0: 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20  gned long long) 
21d0: 63 75 72 72 2d 3e 66 69 6c 65 5f 6c 65 6e 67 74  curr->file_lengt
21e0: 68 2c 0a 09 09 09 09 09 09 28 75 6e 73 69 67 6e  h,.......(unsign
21f0: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75  ed long long) cu
2200: 72 72 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 0a  rr->sent_length.
2210: 09 09 09 09 09 29 3b 0a 0a 09 09 09 09 09 62 72  .....);.......br
2220: 65 61 6b 3b 0a 09 09 09 7d 0a 09 09 09 66 70 72  eak;....}....fpr
2230: 69 6e 74 66 28 66 70 2c 20 22 20 54 48 52 45 41  intf(fp, " THREA
2240: 44 3d 25 6c 6c 75 20 54 49 4d 45 3d 25 6c 6c 75  D=%llu TIME=%llu
2250: 5c 6e 22 2c 0a 09 09 09 09 28 75 6e 73 69 67 6e  \n",.....(unsign
2260: 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 63 75  ed long long) cu
2270: 72 72 2d 3e 74 68 72 65 61 64 2c 0a 09 09 09 09  rr->thread,.....
2280: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
2290: 6f 6e 67 29 20 6e 6f 77 0a 09 09 09 29 3b 0a 09  ong) now....);..
22a0: 09 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09  ..fflush(fp);...
22b0: 09 09 70 72 65 76 20 3d 20 63 75 72 72 3b 0a 09  ..prev = curr;..
22c0: 09 09 63 75 72 72 20 3d 20 63 75 72 72 2d 3e 5f  ..curr = curr->_
22d0: 70 72 65 76 3b 0a 0a 09 09 09 66 72 65 65 28 70  prev;.....free(p
22e0: 72 65 76 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 72  rev);...}..}...r
22f0: 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 7d 0a 0a  eturn(NULL);.}..
2300: 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c 65  static void file
2310: 64 5f 6c 6f 67 5f 65 6e 74 72 79 28 73 74 72 75  d_log_entry(stru
2320: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ct filed_log_ent
2330: 72 79 20 2a 65 6e 74 72 79 29 20 7b 0a 09 65 6e  ry *entry) {..en
2340: 74 72 79 2d 3e 74 68 72 65 61 64 20 3d 20 70 74  try->thread = pt
2350: 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 0a 09  hread_self();...
2360: 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f  pthread_mutex_lo
2370: 63 6b 28 26 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73  ck(&filed_log_ms
2380: 67 5f 6c 69 73 74 5f 6d 75 74 65 78 29 3b 0a 0a  g_list_mutex);..
2390: 09 65 6e 74 72 79 2d 3e 5f 6e 65 78 74 20 3d 20  .entry->_next = 
23a0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69  filed_log_msg_li
23b0: 73 74 3b 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d  st;..filed_log_m
23c0: 73 67 5f 6c 69 73 74 20 3d 20 65 6e 74 72 79 3b  sg_list = entry;
23d0: 0a 0a 09 70 74 68 72 65 61 64 5f 6d 75 74 65 78  ...pthread_mutex
23e0: 5f 75 6e 6c 6f 63 6b 28 26 66 69 6c 65 64 5f 6c  _unlock(&filed_l
23f0: 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 6d 75 74 65  og_msg_list_mute
2400: 78 29 3b 0a 0a 09 70 74 68 72 65 61 64 5f 63 6f  x);...pthread_co
2410: 6e 64 5f 73 69 67 6e 61 6c 28 26 66 69 6c 65 64  nd_signal(&filed
2420: 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f 72 65  _log_msg_list_re
2430: 61 64 79 29 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a  ady);...return;.
2440: 7d 0a 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  }..static struct
2450: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
2460: 20 2a 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28   *filed_log_new(
2470: 69 6e 74 20 69 6e 69 74 69 61 6c 69 7a 65 29 20  int initialize) 
2480: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f  {..struct filed_
2490: 6c 6f 67 5f 65 6e 74 72 79 20 2a 72 65 74 76 61  log_entry *retva
24a0: 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20 6d 61  l;...retval = ma
24b0: 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 72 65 74  lloc(sizeof(*ret
24c0: 76 61 6c 29 29 3b 0a 0a 09 69 66 20 28 69 6e 69  val));...if (ini
24d0: 74 69 61 6c 69 7a 65 29 20 7b 0a 09 09 72 65 74  tialize) {...ret
24e0: 76 61 6c 2d 3e 62 75 66 66 65 72 5b 30 5d 20 3d  val->buffer[0] =
24f0: 20 27 5c 30 27 3b 0a 09 09 72 65 74 76 61 6c 2d   '\0';...retval-
2500: 3e 68 74 74 70 5f 63 6f 64 65 20 3d 20 2d 31 3b  >http_code = -1;
2510: 0a 09 09 72 65 74 76 61 6c 2d 3e 73 74 61 72 74  ...retval->start
2520: 74 69 6d 65 20 3d 20 30 3b 0a 09 09 72 65 74 76  time = 0;...retv
2530: 61 6c 2d 3e 65 6e 64 74 69 6d 65 20 3d 20 30 3b  al->endtime = 0;
2540: 0a 09 09 72 65 74 76 61 6c 2d 3e 72 65 71 5f 6f  ...retval->req_o
2550: 66 66 73 65 74 20 3d 20 30 3b 0a 09 09 72 65 74  ffset = 0;...ret
2560: 76 61 6c 2d 3e 72 65 71 5f 6c 65 6e 67 74 68 20  val->req_length 
2570: 3d 20 30 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 73  = 0;...retval->s
2580: 65 6e 74 5f 6c 65 6e 67 74 68 20 3d 20 30 3b 0a  ent_length = 0;.
2590: 09 09 72 65 74 76 61 6c 2d 3e 66 69 6c 65 5f 6c  ..retval->file_l
25a0: 65 6e 67 74 68 20 3d 20 30 3b 0a 09 09 72 65 74  ength = 0;...ret
25b0: 76 61 6c 2d 3e 69 70 5b 30 5d 20 3d 20 27 5c 30  val->ip[0] = '\0
25c0: 27 3b 0a 09 09 72 65 74 76 61 6c 2d 3e 70 6f 72  ';...retval->por
25d0: 74 20 3d 20 2d 31 3b 0a 09 09 72 65 74 76 61 6c  t = -1;...retval
25e0: 2d 3e 6d 65 74 68 6f 64 20 3d 20 2d 31 3b 0a 09  ->method = -1;..
25f0: 7d 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76 61  }...return(retva
2600: 6c 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  l);.}..static vo
2610: 69 64 20 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67  id filed_log_msg
2620: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d 74  (const char *fmt
2630: 2c 20 2e 2e 2e 29 20 7b 0a 09 73 74 72 75 63 74  , ...) {..struct
2640: 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74 72 79   filed_log_entry
2650: 20 2a 65 6e 74 72 79 3b 0a 09 76 61 5f 6c 69 73   *entry;..va_lis
2660: 74 20 61 72 67 73 3b 0a 0a 09 65 6e 74 72 79 20  t args;...entry 
2670: 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77 28  = filed_log_new(
2680: 30 29 3b 0a 0a 09 76 61 5f 73 74 61 72 74 28 61  0);...va_start(a
2690: 72 67 73 2c 20 66 6d 74 29 3b 0a 0a 09 76 73 6e  rgs, fmt);...vsn
26a0: 70 72 69 6e 74 66 28 65 6e 74 72 79 2d 3e 62 75  printf(entry->bu
26b0: 66 66 65 72 2c 20 73 69 7a 65 6f 66 28 65 6e 74  ffer, sizeof(ent
26c0: 72 79 2d 3e 62 75 66 66 65 72 29 2c 20 66 6d 74  ry->buffer), fmt
26d0: 2c 20 61 72 67 73 29 3b 0a 0a 09 76 61 5f 65 6e  , args);...va_en
26e0: 64 28 61 72 67 73 29 3b 0a 0a 09 65 6e 74 72 79  d(args);...entry
26f0: 2d 3e 74 79 70 65 20 3d 20 46 49 4c 45 44 5f 4c  ->type = FILED_L
2700: 4f 47 5f 54 59 50 45 5f 4d 45 53 53 41 47 45 3b  OG_TYPE_MESSAGE;
2710: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ...filed_log_ent
2720: 72 79 28 65 6e 74 72 79 29 3b 0a 0a 09 72 65 74  ry(entry);...ret
2730: 75 72 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63  urn;.}..static c
2740: 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64  onst char *filed
2750: 5f 6c 6f 67 5f 69 70 28 73 74 72 75 63 74 20 73  _log_ip(struct s
2760: 6f 63 6b 61 64 64 72 20 2a 61 64 64 72 2c 20 63  ockaddr *addr, c
2770: 68 61 72 20 2a 62 75 66 66 65 72 2c 20 73 69 7a  har *buffer, siz
2780: 65 5f 74 20 62 75 66 66 65 72 6c 65 6e 29 20 7b  e_t bufferlen) {
2790: 0a 09 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64  ..struct sockadd
27a0: 72 5f 69 6e 20 2a 61 64 64 72 5f 76 34 3b 0a 09  r_in *addr_v4;..
27b0: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f  struct sockaddr_
27c0: 69 6e 36 20 2a 61 64 64 72 5f 76 36 3b 0a 09 63  in6 *addr_v6;..c
27d0: 6f 6e 73 74 20 63 68 61 72 20 2a 72 65 74 76 61  onst char *retva
27e0: 6c 20 3d 20 4e 55 4c 4c 3b 0a 0a 09 61 64 64 72  l = NULL;...addr
27f0: 5f 76 36 20 3d 20 28 73 74 72 75 63 74 20 73 6f  _v6 = (struct so
2800: 63 6b 61 64 64 72 5f 69 6e 36 20 2a 29 20 61 64  ckaddr_in6 *) ad
2810: 64 72 3b 0a 0a 09 73 77 69 74 63 68 20 28 61 64  dr;...switch (ad
2820: 64 72 5f 76 36 2d 3e 73 69 6e 36 5f 66 61 6d 69  dr_v6->sin6_fami
2830: 6c 79 29 20 7b 0a 09 09 63 61 73 65 20 41 46 5f  ly) {...case AF_
2840: 49 4e 45 54 3a 0a 09 09 09 61 64 64 72 5f 76 34  INET:....addr_v4
2850: 20 3d 20 28 73 74 72 75 63 74 20 73 6f 63 6b 61   = (struct socka
2860: 64 64 72 5f 69 6e 20 2a 29 20 61 64 64 72 3b 0a  ddr_in *) addr;.
2870: 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e 65 74  ...retval = inet
2880: 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 2c 20 26  _ntop(AF_INET, &
2890: 61 64 64 72 5f 76 34 2d 3e 73 69 6e 5f 61 64 64  addr_v4->sin_add
28a0: 72 2c 20 62 75 66 66 65 72 2c 20 62 75 66 66 65  r, buffer, buffe
28b0: 72 6c 65 6e 29 3b 0a 09 09 09 62 72 65 61 6b 3b  rlen);....break;
28c0: 0a 09 09 63 61 73 65 20 41 46 5f 49 4e 45 54 36  ...case AF_INET6
28d0: 3a 0a 09 09 09 72 65 74 76 61 6c 20 3d 20 69 6e  :....retval = in
28e0: 65 74 5f 6e 74 6f 70 28 41 46 5f 49 4e 45 54 36  et_ntop(AF_INET6
28f0: 2c 20 26 61 64 64 72 5f 76 36 2d 3e 73 69 6e 36  , &addr_v6->sin6
2900: 5f 61 64 64 72 2c 20 62 75 66 66 65 72 2c 20 62  _addr, buffer, b
2910: 75 66 66 65 72 6c 65 6e 29 3b 0a 09 09 09 62 72  ufferlen);....br
2920: 65 61 6b 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  eak;..}...return
2930: 28 72 65 74 76 61 6c 29 3b 0a 7d 0a 0a 73 74 61  (retval);.}..sta
2940: 74 69 63 20 46 49 4c 45 20 2a 66 69 6c 65 64 5f  tic FILE *filed_
2950: 6c 6f 67 5f 6f 70 65 6e 28 63 6f 6e 73 74 20 63  log_open(const c
2960: 68 61 72 20 2a 66 69 6c 65 29 20 7b 0a 09 46 49  har *file) {..FI
2970: 4c 45 20 2a 72 65 74 76 61 6c 3b 0a 0a 09 69 66  LE *retval;...if
2980: 20 28 73 74 72 63 6d 70 28 66 69 6c 65 2c 20 22   (strcmp(file, "
2990: 2d 22 29 20 3d 3d 20 30 29 20 7b 0a 09 09 72 65  -") == 0) {...re
29a0: 74 76 61 6c 20 3d 20 73 74 64 6f 75 74 3b 0a 09  tval = stdout;..
29b0: 7d 20 65 6c 73 65 20 69 66 20 28 66 69 6c 65 5b  } else if (file[
29c0: 30 5d 20 3d 3d 20 27 7c 27 29 20 7b 0a 09 09 66  0] == '|') {...f
29d0: 69 6c 65 2b 2b 3b 0a 09 09 72 65 74 76 61 6c 20  ile++;...retval 
29e0: 3d 20 70 6f 70 65 6e 28 66 69 6c 65 2c 20 22 77  = popen(file, "w
29f0: 22 29 3b 0a 09 7d 20 65 6c 73 65 20 7b 0a 09 09  ");..} else {...
2a00: 72 65 74 76 61 6c 20 3d 20 66 6f 70 65 6e 28 66  retval = fopen(f
2a10: 69 6c 65 2c 20 22 61 2b 22 29 3b 0a 09 7d 0a 0a  ile, "a+");..}..
2a20: 09 72 65 74 75 72 6e 28 72 65 74 76 61 6c 29 3b  .return(retval);
2a30: 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .}..static int f
2a40: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72  iled_logging_thr
2a50: 65 61 64 5f 69 6e 69 74 28 46 49 4c 45 20 2a 6c  ead_init(FILE *l
2a60: 6f 67 66 70 29 20 7b 0a 09 73 74 72 75 63 74 20  ogfp) {..struct 
2a70: 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68  filed_logging_th
2a80: 72 65 61 64 5f 61 72 67 73 20 2a 61 72 67 73 3b  read_args *args;
2a90: 0a 09 70 74 68 72 65 61 64 5f 74 20 74 68 72 65  ..pthread_t thre
2aa0: 61 64 5f 69 64 3b 0a 0a 09 61 72 67 73 20 3d 20  ad_id;...args = 
2ab0: 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 61  malloc(sizeof(*a
2ac0: 72 67 73 29 29 3b 0a 09 61 72 67 73 2d 3e 66 70  rgs));..args->fp
2ad0: 20 3d 20 6c 6f 67 66 70 3b 0a 0a 09 66 69 6c 65   = logfp;...file
2ae0: 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 20 3d  d_log_msg_list =
2af0: 20 4e 55 4c 4c 3b 0a 0a 09 70 74 68 72 65 61 64   NULL;...pthread
2b00: 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 66 69 6c  _mutex_init(&fil
2b10: 65 64 5f 6c 6f 67 5f 6d 73 67 5f 6c 69 73 74 5f  ed_log_msg_list_
2b20: 6d 75 74 65 78 2c 20 4e 55 4c 4c 29 3b 0a 0a 09  mutex, NULL);...
2b30: 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28 26  pthread_create(&
2b40: 74 68 72 65 61 64 5f 69 64 2c 20 4e 55 4c 4c 2c  thread_id, NULL,
2b50: 20 66 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74   filed_logging_t
2b60: 68 72 65 61 64 2c 20 61 72 67 73 29 3b 0a 0a 09  hread, args);...
2b70: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 53  filed_log_msg("S
2b80: 54 41 52 54 22 29 3b 0a 0a 09 72 65 74 75 72 6e  TART");...return
2b90: 28 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  (0);.}.#endif../
2ba0: 2a 20 46 6f 72 6d 61 74 20 74 69 6d 65 20 70 65  * Format time pe
2bb0: 72 20 52 46 43 32 36 31 36 20 2a 2f 0a 73 74 61  r RFC2616 */.sta
2bc0: 74 69 63 20 63 68 61 72 20 2a 66 69 6c 65 64 5f  tic char *filed_
2bd0: 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 68 61 72  format_time(char
2be0: 20 2a 62 75 66 66 65 72 2c 20 73 69 7a 65 5f 74   *buffer, size_t
2bf0: 20 62 75 66 66 65 72 5f 6c 65 6e 2c 20 63 6f 6e   buffer_len, con
2c00: 73 74 20 74 69 6d 65 5f 74 20 74 69 6d 65 69 6e  st time_t timein
2c10: 66 6f 29 20 7b 0a 09 73 74 72 75 63 74 20 74 6d  fo) {..struct tm
2c20: 20 74 69 6d 65 69 6e 66 6f 5f 74 6d 2c 20 2a 74   timeinfo_tm, *t
2c30: 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 3b 0a 0a 09  imeinfo_tm_p;...
2c40: 74 69 6d 65 69 6e 66 6f 5f 74 6d 5f 70 20 3d 20  timeinfo_tm_p = 
2c50: 67 6d 74 69 6d 65 5f 72 28 26 74 69 6d 65 69 6e  gmtime_r(&timein
2c60: 66 6f 2c 20 26 74 69 6d 65 69 6e 66 6f 5f 74 6d  fo, &timeinfo_tm
2c70: 29 3b 0a 09 69 66 20 28 74 69 6d 65 69 6e 66 6f  );..if (timeinfo
2c80: 5f 74 6d 5f 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b  _tm_p == NULL) {
2c90: 0a 09 09 72 65 74 75 72 6e 28 22 75 6e 6b 6e 6f  ...return("unkno
2ca0: 77 6e 22 29 3b 0a 09 7d 0a 0a 09 62 75 66 66 65  wn");..}...buffe
2cb0: 72 5b 62 75 66 66 65 72 5f 6c 65 6e 20 2d 20 31  r[buffer_len - 1
2cc0: 5d 20 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65  ] = '\0';..buffe
2cd0: 72 5f 6c 65 6e 20 3d 20 73 74 72 66 74 69 6d 65  r_len = strftime
2ce0: 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f  (buffer, buffer_
2cf0: 6c 65 6e 20 2d 20 31 2c 20 22 25 61 2c 20 25 64  len - 1, "%a, %d
2d00: 20 25 62 20 25 59 20 25 48 3a 25 4d 3a 25 53 20   %b %Y %H:%M:%S 
2d10: 47 4d 54 22 2c 20 74 69 6d 65 69 6e 66 6f 5f 74  GMT", timeinfo_t
2d20: 6d 5f 70 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62  m_p);...return(b
2d30: 75 66 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 68 61  uffer);.}../* ha
2d40: 73 68 20 2a 2f 0a 73 74 61 74 69 63 20 75 6e 73  sh */.static uns
2d50: 69 67 6e 65 64 20 69 6e 74 20 66 69 6c 65 64 5f  igned int filed_
2d60: 68 61 73 68 28 63 6f 6e 73 74 20 75 6e 73 69 67  hash(const unsig
2d70: 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 75 65 2c  ned char *value,
2d80: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 6f   unsigned int mo
2d90: 64 75 6c 75 73 29 20 7b 0a 09 75 6e 73 69 67 6e  dulus) {..unsign
2da0: 65 64 20 63 68 61 72 20 63 75 72 72 2c 20 70 72  ed char curr, pr
2db0: 65 76 3b 0a 09 69 6e 74 20 64 69 66 66 3b 0a 09  ev;..int diff;..
2dc0: 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 72 65 74  unsigned int ret
2dd0: 76 61 6c 3b 0a 0a 09 72 65 74 76 61 6c 20 3d 20  val;...retval = 
2de0: 6d 6f 64 75 6c 75 73 20 2d 20 31 3b 0a 09 70 72  modulus - 1;..pr
2df0: 65 76 20 3d 20 6d 6f 64 75 6c 75 73 20 25 20 32  ev = modulus % 2
2e00: 35 35 3b 0a 0a 09 77 68 69 6c 65 20 28 28 63 75  55;...while ((cu
2e10: 72 72 20 3d 20 2a 76 61 6c 75 65 29 29 20 7b 0a  rr = *value)) {.
2e20: 09 09 69 66 20 28 63 75 72 72 20 3c 20 33 32 29  ..if (curr < 32)
2e30: 20 7b 0a 09 09 09 63 75 72 72 20 3d 20 32 35 35   {....curr = 255
2e40: 20 2d 20 63 75 72 72 3b 0a 09 09 7d 20 65 6c 73   - curr;...} els
2e50: 65 20 7b 0a 09 09 09 63 75 72 72 20 2d 3d 20 33  e {....curr -= 3
2e60: 32 3b 0a 09 09 7d 0a 0a 09 09 69 66 20 28 70 72  2;...}....if (pr
2e70: 65 76 20 3c 20 63 75 72 72 29 20 7b 0a 09 09 09  ev < curr) {....
2e80: 64 69 66 66 20 3d 20 63 75 72 72 20 2d 20 70 72  diff = curr - pr
2e90: 65 76 3b 0a 09 09 7d 20 65 6c 73 65 20 7b 0a 09  ev;...} else {..
2ea0: 09 09 64 69 66 66 20 3d 20 70 72 65 76 20 2d 20  ..diff = prev - 
2eb0: 63 75 72 72 3b 0a 09 09 7d 0a 0a 09 09 70 72 65  curr;...}....pre
2ec0: 76 20 3d 20 63 75 72 72 3b 0a 0a 09 09 72 65 74  v = curr;....ret
2ed0: 76 61 6c 20 3c 3c 3d 20 33 3b 0a 09 09 72 65 74  val <<= 3;...ret
2ee0: 76 61 6c 20 26 3d 20 30 78 46 46 46 46 46 46 46  val &= 0xFFFFFFF
2ef0: 46 4c 55 3b 0a 09 09 72 65 74 76 61 6c 20 5e 3d  FLU;...retval ^=
2f00: 20 64 69 66 66 3b 0a 0a 09 09 76 61 6c 75 65 2b   diff;....value+
2f10: 2b 3b 0a 09 7d 0a 0a 09 72 65 74 76 61 6c 20 3d  +;..}...retval =
2f20: 20 72 65 74 76 61 6c 20 25 20 6d 6f 64 75 6c 75   retval % modulu
2f30: 73 3b 0a 0a 09 72 65 74 75 72 6e 28 72 65 74 76  s;...return(retv
2f40: 61 6c 29 3b 0a 7d 0a 0a 2f 2a 20 46 69 6e 64 20  al);.}../* Find 
2f50: 61 20 6d 69 6d 65 2d 74 79 70 65 20 62 61 73 65  a mime-type base
2f60: 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 6e 61 6d  d on the filenam
2f70: 65 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  e */.static cons
2f80: 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 64 65  t char *filed_de
2f90: 74 65 72 6d 69 6e 65 5f 6d 69 6d 65 74 79 70 65  termine_mimetype
2fa0: 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74  (const char *pat
2fb0: 68 29 20 7b 0a 09 63 6f 6e 73 74 20 63 68 61 72  h) {..const char
2fc0: 20 2a 70 3b 0a 0a 09 70 20 3d 20 73 74 72 72 63   *p;...p = strrc
2fd0: 68 72 28 70 61 74 68 2c 20 27 2e 27 29 3b 0a 09  hr(path, '.');..
2fe0: 69 66 20 28 70 20 3d 3d 20 4e 55 4c 4c 29 20 7b  if (p == NULL) {
2ff0: 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44 5f  ...return(FILED_
3000: 44 45 46 41 55 4c 54 5f 54 59 50 45 29 3b 0a 09  DEFAULT_TYPE);..
3010: 7d 0a 0a 09 70 2b 2b 3b 0a 09 69 66 20 28 2a 70  }...p++;..if (*p
3020: 20 3d 3d 20 27 5c 30 27 29 20 7b 0a 09 09 72 65   == '\0') {...re
3030: 74 75 72 6e 28 46 49 4c 45 44 5f 44 45 46 41 55  turn(FILED_DEFAU
3040: 4c 54 5f 54 59 50 45 29 3b 0a 09 7d 0a 0a 09 66  LT_TYPE);..}...f
3050: 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62  iled_log_msg_deb
3060: 75 67 28 22 4c 6f 6f 6b 69 6e 67 20 75 70 20 4d  ug("Looking up M
3070: 49 4d 45 20 74 79 70 65 20 66 6f 72 20 25 73 20  IME type for %s 
3080: 28 68 61 73 68 20 3d 20 25 6c 6c 75 29 22 2c 20  (hash = %llu)", 
3090: 70 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  p, (unsigned lon
30a0: 67 20 6c 6f 6e 67 29 20 66 69 6c 65 64 5f 68 61  g long) filed_ha
30b0: 73 68 28 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  sh((const unsign
30c0: 65 64 20 63 68 61 72 20 2a 29 20 70 2c 20 31 36  ed char *) p, 16
30d0: 37 37 37 32 35 39 29 29 3b 0a 0a 23 69 6e 63 6c  777259));..#incl
30e0: 75 64 65 20 22 66 69 6c 65 64 2d 6d 69 6d 65 2d  ude "filed-mime-
30f0: 74 79 70 65 73 2e 68 22 0a 0a 09 72 65 74 75 72  types.h"...retur
3100: 6e 28 46 49 4c 45 44 5f 44 45 46 41 55 4c 54 5f  n(FILED_DEFAULT_
3110: 54 59 50 45 29 3b 0a 7d 0a 0a 2f 2a 20 47 65 6e  TYPE);.}../* Gen
3120: 65 72 61 74 65 20 61 20 75 6e 69 71 75 65 20 69  erate a unique i
3130: 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 73 74 61  dentifier */.sta
3140: 74 69 63 20 76 6f 69 64 20 66 69 6c 65 64 5f 67  tic void filed_g
3150: 65 6e 65 72 61 74 65 5f 65 74 61 67 28 63 68 61  enerate_etag(cha
3160: 72 20 2a 65 74 61 67 2c 20 73 69 7a 65 5f 74 20  r *etag, size_t 
3170: 6c 65 6e 67 74 68 29 20 7b 0a 09 73 6e 70 72 69  length) {..snpri
3180: 6e 74 66 28 65 74 61 67 2c 20 6c 65 6e 67 74 68  ntf(etag, length
3190: 2c 20 22 25 6c 6c 78 2d 25 6c 6c 78 25 6c 6c 78  , "%llx-%llx%llx
31a0: 25 6c 6c 78 25 6c 6c 78 22 2c 0a 09 09 28 75 6e  %llx%llx",...(un
31b0: 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
31c0: 29 20 74 69 6d 65 28 4e 55 4c 4c 29 2c 0a 09 09  ) time(NULL),...
31d0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
31e0: 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a 09  ong) random(),..
31f0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
3200: 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c 0a  long) random(),.
3210: 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  ..(unsigned long
3220: 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29 2c   long) random(),
3230: 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
3240: 67 20 6c 6f 6e 67 29 20 72 61 6e 64 6f 6d 28 29  g long) random()
3250: 0a 09 29 3b 0a 7d 0a 0a 2f 2a 20 4f 70 65 6e 20  ..);.}../* Open 
3260: 61 20 66 69 6c 65 20 61 6e 64 20 72 65 74 75 72  a file and retur
3270: 6e 20 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69  n file informati
3280: 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  on */.static str
3290: 75 63 74 20 66 69 6c 65 64 5f 66 69 6c 65 69 6e  uct filed_filein
32a0: 66 6f 20 2a 66 69 6c 65 64 5f 6f 70 65 6e 5f 66  fo *filed_open_f
32b0: 69 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ile(const char *
32c0: 70 61 74 68 2c 20 73 74 72 75 63 74 20 66 69 6c  path, struct fil
32d0: 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 62 75 66  ed_fileinfo *buf
32e0: 66 65 72 29 20 7b 0a 09 73 74 72 75 63 74 20 66  fer) {..struct f
32f0: 69 6c 65 64 5f 66 69 6c 65 69 6e 66 6f 20 2a 63  iled_fileinfo *c
3300: 61 63 68 65 3b 0a 09 75 6e 73 69 67 6e 65 64 20  ache;..unsigned 
3310: 69 6e 74 20 63 61 63 68 65 5f 69 64 78 3b 0a 09  int cache_idx;..
3320: 6f 66 66 5f 74 20 6c 65 6e 3b 0a 09 69 6e 74 20  off_t len;..int 
3330: 66 64 3b 0a 0a 09 63 61 63 68 65 5f 69 64 78 20  fd;...cache_idx 
3340: 3d 20 66 69 6c 65 64 5f 68 61 73 68 28 28 63 6f  = filed_hash((co
3350: 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
3360: 72 20 2a 29 20 70 61 74 68 2c 20 66 69 6c 65 64  r *) path, filed
3370: 5f 66 69 6c 65 69 6e 66 6f 5f 66 64 63 61 63 68  _fileinfo_fdcach
3380: 65 5f 73 69 7a 65 29 3b 0a 0a 09 63 61 63 68 65  e_size);...cache
3390: 20 3d 20 26 66 69 6c 65 64 5f 66 69 6c 65 69 6e   = &filed_filein
33a0: 66 6f 5f 66 64 63 61 63 68 65 5b 63 61 63 68 65  fo_fdcache[cache
33b0: 5f 69 64 78 5d 3b 0a 0a 09 66 69 6c 65 64 5f 6c  _idx];...filed_l
33c0: 6f 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 4c 6f  og_msg_debug("Lo
33d0: 63 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20  cking mutex for 
33e0: 69 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69  idx: %lu", (unsi
33f0: 67 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65  gned long) cache
3400: 5f 69 64 78 29 3b 0a 0a 09 70 74 68 72 65 61 64  _idx);...pthread
3410: 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 63 61 63  _mutex_lock(&cac
3420: 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 66 69  he->mutex);...fi
3430: 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65 62 75  led_log_msg_debu
3440: 67 28 22 43 6f 6d 70 6c 65 74 65 64 20 6c 6f 63  g("Completed loc
3450: 6b 69 6e 67 20 6d 75 74 65 78 20 66 6f 72 20 69  king mutex for i
3460: 64 78 3a 20 25 6c 75 22 2c 20 28 75 6e 73 69 67  dx: %lu", (unsig
3470: 6e 65 64 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f  ned long) cache_
3480: 69 64 78 29 3b 0a 0a 09 69 66 20 28 73 74 72 63  idx);...if (strc
3490: 6d 70 28 70 61 74 68 2c 20 63 61 63 68 65 2d 3e  mp(path, cache->
34a0: 70 61 74 68 29 20 21 3d 20 30 29 20 7b 0a 09 09  path) != 0) {...
34b0: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64 65  filed_log_msg_de
34c0: 62 75 67 28 22 43 61 63 68 65 20 6d 69 73 73 20  bug("Cache miss 
34d0: 66 6f 72 20 69 64 78 3a 20 25 6c 75 3a 20 4f 4c  for idx: %lu: OL
34e0: 44 20 5c 22 25 73 5c 22 2c 20 4e 45 57 20 5c 22  D \"%s\", NEW \"
34f0: 25 73 5c 22 22 2c 20 28 75 6e 73 69 67 6e 65 64  %s\"", (unsigned
3500: 20 6c 6f 6e 67 29 20 63 61 63 68 65 5f 69 64 78   long) cache_idx
3510: 2c 20 63 61 63 68 65 2d 3e 70 61 74 68 2c 20 70  , cache->path, p
3520: 61 74 68 29 3b 0a 0a 09 09 66 64 20 3d 20 6f 70  ath);....fd = op
3530: 65 6e 28 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c  en(path, O_RDONL
3540: 59 20 7c 20 4f 5f 4c 41 52 47 45 46 49 4c 45 29  Y | O_LARGEFILE)
3550: 3b 0a 09 09 69 66 20 28 66 64 20 3c 20 30 29 20  ;...if (fd < 0) 
3560: 7b 0a 09 09 09 70 74 68 72 65 61 64 5f 6d 75 74  {....pthread_mut
3570: 65 78 5f 75 6e 6c 6f 63 6b 28 26 63 61 63 68 65  ex_unlock(&cache
3580: 2d 3e 6d 75 74 65 78 29 3b 0a 0a 09 09 09 72 65  ->mutex);.....re
3590: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 09 09 7d 0a  turn(NULL);...}.
35a0: 0a 09 09 69 66 20 28 63 61 63 68 65 2d 3e 66 64  ...if (cache->fd
35b0: 20 3e 3d 20 30 29 20 7b 0a 09 09 09 63 6c 6f 73   >= 0) {....clos
35c0: 65 28 63 61 63 68 65 2d 3e 66 64 29 3b 0a 09 09  e(cache->fd);...
35d0: 7d 0a 0a 09 09 6c 65 6e 20 3d 20 6c 73 65 65 6b  }....len = lseek
35e0: 28 66 64 2c 20 30 2c 20 53 45 45 4b 5f 45 4e 44  (fd, 0, SEEK_END
35f0: 29 3b 0a 09 09 6c 73 65 65 6b 28 66 64 2c 20 30  );...lseek(fd, 0
3600: 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 0a 09 09  , SEEK_SET);....
3610: 63 61 63 68 65 2d 3e 66 64 20 3d 20 66 64 3b 0a  cache->fd = fd;.
3620: 09 09 63 61 63 68 65 2d 3e 6c 65 6e 20 3d 20 6c  ..cache->len = l
3630: 65 6e 3b 0a 09 09 73 74 72 63 70 79 28 63 61 63  en;...strcpy(cac
3640: 68 65 2d 3e 70 61 74 68 2c 20 70 61 74 68 29 3b  he->path, path);
3650: 0a 09 09 63 61 63 68 65 2d 3e 74 79 70 65 20 3d  ...cache->type =
3660: 20 66 69 6c 65 64 5f 64 65 74 65 72 6d 69 6e 65   filed_determine
3670: 5f 6d 69 6d 65 74 79 70 65 28 70 61 74 68 29 3b  _mimetype(path);
3680: 0a 09 09 66 69 6c 65 64 5f 67 65 6e 65 72 61 74  ...filed_generat
3690: 65 5f 65 74 61 67 28 63 61 63 68 65 2d 3e 65 74  e_etag(cache->et
36a0: 61 67 2c 20 73 69 7a 65 6f 66 28 63 61 63 68 65  ag, sizeof(cache
36b0: 2d 3e 65 74 61 67 29 29 3b 0a 0a 09 09 2f 2a 20  ->etag));..../* 
36c0: 58 58 58 3a 54 4f 44 4f 3a 20 44 65 74 65 72 6d  XXX:TODO: Determ
36d0: 69 6e 65 20 2a 2f 0a 09 09 63 61 63 68 65 2d 3e  ine */...cache->
36e0: 6c 61 73 74 6d 6f 64 20 3d 20 66 69 6c 65 64 5f  lastmod = filed_
36f0: 66 6f 72 6d 61 74 5f 74 69 6d 65 28 63 61 63 68  format_time(cach
3700: 65 2d 3e 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69  e->lastmod_b, si
3710: 7a 65 6f 66 28 63 61 63 68 65 2d 3e 6c 61 73 74  zeof(cache->last
3720: 6d 6f 64 5f 62 29 2c 20 74 69 6d 65 28 4e 55 4c  mod_b), time(NUL
3730: 4c 29 20 2d 20 33 30 29 3b 0a 09 7d 20 65 6c 73  L) - 30);..} els
3740: 65 20 7b 0a 09 09 66 69 6c 65 64 5f 6c 6f 67 5f  e {...filed_log_
3750: 6d 73 67 5f 64 65 62 75 67 28 22 43 61 63 68 65  msg_debug("Cache
3760: 20 68 69 74 20 66 6f 72 20 69 64 78 3a 20 25 6c   hit for idx: %l
3770: 75 3a 20 50 41 54 48 20 5c 22 25 73 5c 22 22 2c  u: PATH \"%s\"",
3780: 20 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29   (unsigned long)
3790: 20 63 61 63 68 65 5f 69 64 78 2c 20 70 61 74 68   cache_idx, path
37a0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 0a 09 20 2a 20 57  );..}.../*.. * W
37b0: 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61  e have to make a
37c0: 20 64 75 70 6c 69 63 61 74 65 20 46 44 2c 20 62   duplicate FD, b
37d0: 65 63 61 75 73 65 20 6f 6e 63 65 20 77 65 20 72  ecause once we r
37e0: 65 6c 65 61 73 65 20 74 68 65 20 63 61 63 68 65  elease the cache
37f0: 0a 09 20 2a 20 6d 75 74 65 78 2c 20 74 68 65 20  .. * mutex, the 
3800: 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
3810: 6d 61 79 20 62 65 20 63 6c 6f 73 65 64 0a 09 20  may be closed.. 
3820: 2a 2f 0a 09 66 64 20 3d 20 64 75 70 28 63 61 63  */..fd = dup(cac
3830: 68 65 2d 3e 66 64 29 3b 0a 09 69 66 20 28 66 64  he->fd);..if (fd
3840: 20 3c 20 30 29 20 7b 0a 09 09 70 74 68 72 65 61   < 0) {...pthrea
3850: 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26  d_mutex_unlock(&
3860: 63 61 63 68 65 2d 3e 6d 75 74 65 78 29 3b 0a 0a  cache->mutex);..
3870: 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a  ..return(NULL);.
3880: 09 7d 0a 0a 09 62 75 66 66 65 72 2d 3e 66 64 20  .}...buffer->fd 
3890: 3d 20 66 64 3b 0a 09 62 75 66 66 65 72 2d 3e 6c  = fd;..buffer->l
38a0: 65 6e 20 3d 20 63 61 63 68 65 2d 3e 6c 65 6e 3b  en = cache->len;
38b0: 0a 09 62 75 66 66 65 72 2d 3e 74 79 70 65 20 3d  ..buffer->type =
38c0: 20 63 61 63 68 65 2d 3e 74 79 70 65 3b 0a 09 6d   cache->type;..m
38d0: 65 6d 63 70 79 28 62 75 66 66 65 72 2d 3e 6c 61  emcpy(buffer->la
38e0: 73 74 6d 6f 64 5f 62 2c 20 63 61 63 68 65 2d 3e  stmod_b, cache->
38f0: 6c 61 73 74 6d 6f 64 5f 62 2c 20 73 69 7a 65 6f  lastmod_b, sizeo
3900: 66 28 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f  f(buffer->lastmo
3910: 64 5f 62 29 29 3b 0a 09 6d 65 6d 63 70 79 28 62  d_b));..memcpy(b
3920: 75 66 66 65 72 2d 3e 65 74 61 67 2c 20 63 61 63  uffer->etag, cac
3930: 68 65 2d 3e 65 74 61 67 2c 20 73 69 7a 65 6f 66  he->etag, sizeof
3940: 28 62 75 66 66 65 72 2d 3e 65 74 61 67 29 29 3b  (buffer->etag));
3950: 0a 09 62 75 66 66 65 72 2d 3e 6c 61 73 74 6d 6f  ..buffer->lastmo
3960: 64 20 3d 20 62 75 66 66 65 72 2d 3e 6c 61 73 74  d = buffer->last
3970: 6d 6f 64 5f 62 20 2b 20 28 63 61 63 68 65 2d 3e  mod_b + (cache->
3980: 6c 61 73 74 6d 6f 64 20 2d 20 63 61 63 68 65 2d  lastmod - cache-
3990: 3e 6c 61 73 74 6d 6f 64 5f 62 29 3b 0a 0a 09 70  >lastmod_b);...p
39a0: 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c  thread_mutex_unl
39b0: 6f 63 6b 28 26 63 61 63 68 65 2d 3e 6d 75 74 65  ock(&cache->mute
39c0: 78 29 3b 0a 0a 09 72 65 74 75 72 6e 28 62 75 66  x);...return(buf
39d0: 66 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 72 6f 63  fer);.}../* Proc
39e0: 65 73 73 20 61 6e 20 48 54 54 50 20 72 65 71 75  ess an HTTP requ
39f0: 65 73 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74  est and return t
3a00: 68 65 20 70 61 74 68 20 72 65 71 75 65 73 74 65  he path requeste
3a10: 64 20 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  d */.static stru
3a20: 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65  ct filed_http_re
3a30: 71 75 65 73 74 20 2a 66 69 6c 65 64 5f 67 65 74  quest *filed_get
3a40: 5f 68 74 74 70 5f 72 65 71 75 65 73 74 28 46 49  _http_request(FI
3a50: 4c 45 20 2a 66 70 2c 20 73 74 72 75 63 74 20 66  LE *fp, struct f
3a60: 69 6c 65 64 5f 68 74 74 70 5f 72 65 71 75 65 73  iled_http_reques
3a70: 74 20 2a 62 75 66 66 65 72 5f 73 74 2c 20 73 74  t *buffer_st, st
3a80: 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f  ruct filed_optio
3a90: 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b 0a 09  ns *options) {..
3aa0: 63 68 61 72 20 2a 6d 65 74 68 6f 64 2c 20 2a 70  char *method, *p
3ab0: 61 74 68 3b 0a 09 63 68 61 72 20 2a 62 75 66 66  ath;..char *buff
3ac0: 65 72 2c 20 2a 77 6f 72 6b 62 75 66 66 65 72 2c  er, *workbuffer,
3ad0: 20 2a 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65 78   *workbuffer_nex
3ae0: 74 3b 0a 09 63 68 61 72 20 2a 66 67 65 74 73 5f  t;..char *fgets_
3af0: 72 65 74 3b 0a 09 73 69 7a 65 5f 74 20 62 75 66  ret;..size_t buf
3b00: 66 65 72 5f 6c 65 6e 2c 20 70 61 74 68 5f 6c 65  fer_len, path_le
3b10: 6e 3b 0a 09 6f 66 66 5f 74 20 72 61 6e 67 65 5f  n;..off_t range_
3b20: 73 74 61 72 74 2c 20 72 61 6e 67 65 5f 65 6e 64  start, range_end
3b30: 2c 20 72 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a  , range_length;.
3b40: 09 69 6e 74 20 72 61 6e 67 65 5f 72 65 71 75 65  .int range_reque
3b50: 73 74 3b 0a 09 69 6e 74 20 73 6e 70 72 69 6e 74  st;..int snprint
3b60: 66 5f 72 65 74 3b 0a 09 69 6e 74 20 69 3b 0a 0a  f_ret;..int i;..
3b70: 09 2f 2a 20 53 65 74 20 74 6f 20 64 65 66 61 75  ./* Set to defau
3b80: 6c 74 20 76 61 6c 75 65 73 20 2a 2f 0a 09 72 61  lt values */..ra
3b90: 6e 67 65 5f 73 74 61 72 74 20 3d 20 30 3b 0a 09  nge_start = 0;..
3ba0: 72 61 6e 67 65 5f 65 6e 64 20 20 20 3d 20 30 3b  range_end   = 0;
3bb0: 0a 09 72 61 6e 67 65 5f 72 65 71 75 65 73 74 20  ..range_request 
3bc0: 3d 20 30 3b 0a 09 72 61 6e 67 65 5f 6c 65 6e 67  = 0;..range_leng
3bd0: 74 68 20 3d 20 2d 31 3b 0a 09 62 75 66 66 65 72  th = -1;..buffer
3be0: 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73  _st->headers.hos
3bf0: 74 2e 70 72 65 73 65 6e 74 20 3d 20 30 3b 0a 09  t.present = 0;..
3c00: 62 75 66 66 65 72 5f 73 74 2d 3e 68 65 61 64 65  buffer_st->heade
3c10: 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20  rs.connection = 
3c20: 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e  FILED_CONNECTION
3c30: 5f 43 4c 4f 53 45 3b 0a 0a 09 62 75 66 66 65 72  _CLOSE;...buffer
3c40: 20 3d 20 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d   = buffer_st->tm
3c50: 70 62 75 66 3b 0a 09 62 75 66 66 65 72 5f 6c 65  pbuf;..buffer_le
3c60: 6e 20 3d 20 73 69 7a 65 6f 66 28 62 75 66 66 65  n = sizeof(buffe
3c70: 72 5f 73 74 2d 3e 74 6d 70 62 75 66 29 3b 0a 0a  r_st->tmpbuf);..
3c80: 09 66 67 65 74 73 5f 72 65 74 20 3d 20 66 67 65  .fgets_ret = fge
3c90: 74 73 28 62 75 66 66 65 72 2c 20 62 75 66 66 65  ts(buffer, buffe
3ca0: 72 5f 6c 65 6e 2c 20 66 70 29 3b 0a 09 69 66 20  r_len, fp);..if 
3cb0: 28 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55  (fgets_ret == NU
3cc0: 4c 4c 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e  LL) {...return(N
3cd0: 55 4c 4c 29 3b 0a 09 7d 0a 0a 09 6d 65 74 68 6f  ULL);..}...metho
3ce0: 64 20 3d 20 62 75 66 66 65 72 3b 0a 0a 09 62 75  d = buffer;...bu
3cf0: 66 66 65 72 20 3d 20 73 74 72 63 68 72 28 62 75  ffer = strchr(bu
3d00: 66 66 65 72 2c 20 27 20 27 29 3b 0a 09 69 66 20  ffer, ' ');..if 
3d10: 28 62 75 66 66 65 72 20 3d 3d 20 4e 55 4c 4c 29  (buffer == NULL)
3d20: 20 7b 0a 09 09 72 65 74 75 72 6e 28 4e 55 4c 4c   {...return(NULL
3d30: 29 3b 0a 09 7d 0a 0a 09 2a 62 75 66 66 65 72 20  );..}...*buffer 
3d40: 3d 20 27 5c 30 27 3b 0a 09 62 75 66 66 65 72 2b  = '\0';..buffer+
3d50: 2b 3b 0a 0a 09 70 61 74 68 20 3d 20 62 75 66 66  +;...path = buff
3d60: 65 72 3b 0a 0a 09 2f 2a 20 54 65 72 6d 69 6e 61  er;.../* Termina
3d70: 74 65 20 70 61 74 68 20 63 6f 6d 70 6f 6e 65 6e  te path componen
3d80: 74 20 2a 2f 0a 09 62 75 66 66 65 72 20 3d 20 73  t */..buffer = s
3d90: 74 72 70 62 72 6b 28 70 61 74 68 2c 20 22 5c 72  trpbrk(path, "\r
3da0: 5c 6e 20 22 29 3b 0a 09 69 66 20 28 62 75 66 66  \n ");..if (buff
3db0: 65 72 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09  er != NULL) {...
3dc0: 2a 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a  *buffer = '\0';.
3dd0: 09 09 62 75 66 66 65 72 2b 2b 3b 0a 09 7d 0a 0a  ..buffer++;..}..
3de0: 09 2f 2a 20 57 65 20 6f 6e 6c 79 20 68 61 6e 64  ./* We only hand
3df0: 6c 65 20 74 68 65 20 22 47 45 54 22 20 61 6e 64  le the "GET" and
3e00: 20 22 48 45 41 44 27 20 6d 65 74 68 6f 64 73 20   "HEAD' methods 
3e10: 2a 2f 0a 09 69 66 20 28 73 74 72 63 61 73 65 63  */..if (strcasec
3e20: 6d 70 28 6d 65 74 68 6f 64 2c 20 22 68 65 61 64  mp(method, "head
3e30: 22 29 20 21 3d 20 30 29 20 7b 0a 09 09 69 66 20  ") != 0) {...if 
3e40: 28 73 74 72 63 61 73 65 63 6d 70 28 6d 65 74 68  (strcasecmp(meth
3e50: 6f 64 2c 20 22 67 65 74 22 29 20 21 3d 20 30 29  od, "get") != 0)
3e60: 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e 55 4c   {....return(NUL
3e70: 4c 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 47 45  L);...}..../* GE
3e80: 54 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62  T request */...b
3e90: 75 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64  uffer_st->method
3ea0: 20 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54   = FILED_REQUEST
3eb0: 5f 4d 45 54 48 4f 44 5f 47 45 54 3b 0a 09 7d 20  _METHOD_GET;..} 
3ec0: 65 6c 73 65 20 7b 0a 09 09 2f 2a 20 48 45 41 44  else {.../* HEAD
3ed0: 20 72 65 71 75 65 73 74 20 2a 2f 0a 09 09 62 75   request */...bu
3ee0: 66 66 65 72 5f 73 74 2d 3e 6d 65 74 68 6f 64 20  ffer_st->method 
3ef0: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f  = FILED_REQUEST_
3f00: 4d 45 54 48 4f 44 5f 48 45 41 44 3b 0a 09 7d 0a  METHOD_HEAD;..}.
3f10: 0a 09 2f 2a 20 4e 6f 74 65 20 70 61 74 68 20 2a  ../* Note path *
3f20: 2f 0a 09 70 61 74 68 5f 6c 65 6e 20 3d 20 73 74  /..path_len = st
3f30: 72 6c 65 6e 28 70 61 74 68 29 3b 0a 09 6d 65 6d  rlen(path);..mem
3f40: 63 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 70  cpy(buffer_st->p
3f50: 61 74 68 2c 20 70 61 74 68 2c 20 70 61 74 68 5f  ath, path, path_
3f60: 6c 65 6e 20 2b 20 31 29 3b 0a 0a 09 2f 2a 20 44  len + 1);.../* D
3f70: 65 74 65 72 6d 69 6e 65 20 74 79 70 65 20 6f 66  etermine type of
3f80: 20 72 65 71 75 65 73 74 20 66 72 6f 6d 20 70 61   request from pa
3f90: 74 68 20 2a 2f 0a 09 69 66 20 28 70 61 74 68 5f  th */..if (path_
3fa0: 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09 09 62 75  len == 0) {...bu
3fb0: 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20  ffer_st->type = 
3fc0: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59  FILED_REQUEST_TY
3fd0: 50 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 7d  PE_DIRECTORY;..}
3fe0: 20 65 6c 73 65 20 7b 0a 09 09 69 66 20 28 70 61   else {...if (pa
3ff0: 74 68 5b 70 61 74 68 5f 6c 65 6e 20 2d 20 31 5d  th[path_len - 1]
4000: 20 3d 3d 20 27 2f 27 29 20 7b 0a 09 09 09 62 75   == '/') {....bu
4010: 66 66 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20  ffer_st->type = 
4020: 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59  FILED_REQUEST_TY
4030: 50 45 5f 44 49 52 45 43 54 4f 52 59 3b 0a 09 09  PE_DIRECTORY;...
4040: 7d 20 65 6c 73 65 20 7b 0a 09 09 09 62 75 66 66  } else {....buff
4050: 65 72 5f 73 74 2d 3e 74 79 70 65 20 3d 20 46 49  er_st->type = FI
4060: 4c 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45  LED_REQUEST_TYPE
4070: 5f 4f 54 48 45 52 3b 0a 09 09 7d 0a 09 7d 0a 0a  _OTHER;...}..}..
4080: 09 2f 2a 20 52 65 73 65 74 20 62 75 66 66 65 72  ./* Reset buffer
4090: 20 66 6f 72 20 6c 61 74 65 72 20 75 73 65 20 2a   for later use *
40a0: 2f 0a 09 62 75 66 66 65 72 20 3d 20 62 75 66 66  /..buffer = buff
40b0: 65 72 5f 73 74 2d 3e 74 6d 70 62 75 66 3b 0a 0a  er_st->tmpbuf;..
40c0: 09 66 6f 72 20 28 69 20 3d 20 30 3b 20 69 20 3c  .for (i = 0; i <
40d0: 20 31 30 30 3b 20 69 2b 2b 29 20 7b 0a 09 09 66   100; i++) {...f
40e0: 67 65 74 73 5f 72 65 74 20 3d 20 66 67 65 74 73  gets_ret = fgets
40f0: 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f  (buffer, buffer_
4100: 6c 65 6e 2c 20 66 70 29 3b 0a 09 09 69 66 20 28  len, fp);...if (
4110: 66 67 65 74 73 5f 72 65 74 20 3d 3d 20 4e 55 4c  fgets_ret == NUL
4120: 4c 29 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09  L) {....break;..
4130: 09 7d 0a 0a 09 09 69 66 20 28 73 74 72 6e 63 61  .}....if (strnca
4140: 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22 52  secmp(buffer, "R
4150: 61 6e 67 65 3a 20 22 2c 20 37 29 20 3d 3d 20 30  ange: ", 7) == 0
4160: 29 20 7b 0a 09 09 09 77 6f 72 6b 62 75 66 66 65  ) {....workbuffe
4170: 72 20 3d 20 62 75 66 66 65 72 20 2b 20 37 3b 0a  r = buffer + 7;.
4180: 0a 09 09 09 69 66 20 28 73 74 72 6e 63 61 73 65  ....if (strncase
4190: 63 6d 70 28 77 6f 72 6b 62 75 66 66 65 72 2c 20  cmp(workbuffer, 
41a0: 22 62 79 74 65 73 3d 22 2c 20 36 29 20 3d 3d 20  "bytes=", 6) == 
41b0: 30 29 20 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66  0) {.....workbuf
41c0: 66 65 72 20 2b 3d 20 36 3b 0a 0a 09 09 09 09 72  fer += 6;......r
41d0: 61 6e 67 65 5f 72 65 71 75 65 73 74 20 3d 20 31  ange_request = 1
41e0: 3b 0a 0a 09 09 09 09 72 61 6e 67 65 5f 73 74 61  ;......range_sta
41f0: 72 74 20 3d 20 73 74 72 74 6f 75 6c 6c 28 77 6f  rt = strtoull(wo
4200: 72 6b 62 75 66 66 65 72 2c 20 26 77 6f 72 6b 62  rkbuffer, &workb
4210: 75 66 66 65 72 5f 6e 65 78 74 2c 20 31 30 29 3b  uffer_next, 10);
4220: 0a 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72  ......workbuffer
4230: 20 3d 20 77 6f 72 6b 62 75 66 66 65 72 5f 6e 65   = workbuffer_ne
4240: 78 74 3b 0a 0a 09 09 09 09 69 66 20 28 2a 77 6f  xt;......if (*wo
4250: 72 6b 62 75 66 66 65 72 20 3d 3d 20 27 2d 27 29  rkbuffer == '-')
4260: 20 7b 0a 09 09 09 09 09 77 6f 72 6b 62 75 66 66   {......workbuff
4270: 65 72 2b 2b 3b 0a 0a 09 09 09 09 09 69 66 20 28  er++;.......if (
4280: 2a 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20 27  *workbuffer != '
4290: 5c 72 27 20 26 26 20 2a 77 6f 72 6b 62 75 66 66  \r' && *workbuff
42a0: 65 72 20 21 3d 20 27 5c 6e 27 29 20 7b 0a 09 09  er != '\n') {...
42b0: 09 09 09 09 72 61 6e 67 65 5f 65 6e 64 20 3d 20  ....range_end = 
42c0: 73 74 72 74 6f 75 6c 6c 28 77 6f 72 6b 62 75 66  strtoull(workbuf
42d0: 66 65 72 2c 20 26 77 6f 72 6b 62 75 66 66 65 72  fer, &workbuffer
42e0: 5f 6e 65 78 74 2c 20 31 30 29 3b 0a 09 09 09 09  _next, 10);.....
42f0: 09 7d 0a 09 09 09 09 7d 0a 09 09 09 7d 0a 09 09  .}.....}....}...
4300: 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e 63  } else if (strnc
4310: 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20 22  asecmp(buffer, "
4320: 48 6f 73 74 3a 20 22 2c 20 35 29 20 3d 3d 20 30  Host: ", 5) == 0
4330: 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f 73 74  ) {....buffer_st
4340: 2d 3e 68 65 61 64 65 72 73 2e 68 6f 73 74 2e 70  ->headers.host.p
4350: 72 65 73 65 6e 74 20 3d 20 31 3b 0a 0a 09 09 09  resent = 1;.....
4360: 77 6f 72 6b 62 75 66 66 65 72 20 3d 20 73 74 72  workbuffer = str
4370: 70 62 72 6b 28 62 75 66 66 65 72 20 2b 20 35 2c  pbrk(buffer + 5,
4380: 20 22 5c 72 5c 6e 3a 22 29 3b 0a 09 09 09 69 66   "\r\n:");....if
4390: 20 28 77 6f 72 6b 62 75 66 66 65 72 20 21 3d 20   (workbuffer != 
43a0: 4e 55 4c 4c 29 20 7b 0a 09 09 09 09 2a 77 6f 72  NULL) {.....*wor
43b0: 6b 62 75 66 66 65 72 20 3d 20 27 5c 30 27 3b 0a  kbuffer = '\0';.
43c0: 09 09 09 7d 0a 0a 09 09 09 77 6f 72 6b 62 75 66  ...}.....workbuf
43d0: 66 65 72 20 3d 20 62 75 66 66 65 72 20 2b 20 35  fer = buffer + 5
43e0: 3b 0a 09 09 09 77 68 69 6c 65 20 28 2a 77 6f 72  ;....while (*wor
43f0: 6b 62 75 66 66 65 72 20 3d 3d 20 27 20 27 29 20  kbuffer == ' ') 
4400: 7b 0a 09 09 09 09 77 6f 72 6b 62 75 66 66 65 72  {.....workbuffer
4410: 2b 2b 3b 0a 09 09 09 7d 0a 0a 09 09 09 73 74 72  ++;....}.....str
4420: 63 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 68  cpy(buffer_st->h
4430: 65 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 74  eaders.host.host
4440: 2c 20 77 6f 72 6b 62 75 66 66 65 72 29 3b 0a 09  , workbuffer);..
4450: 09 7d 20 65 6c 73 65 20 69 66 20 28 73 74 72 6e  .} else if (strn
4460: 63 61 73 65 63 6d 70 28 62 75 66 66 65 72 2c 20  casecmp(buffer, 
4470: 22 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65  "Connection: Kee
4480: 70 2d 41 6c 69 76 65 22 2c 20 32 32 29 20 3d 3d  p-Alive", 22) ==
4490: 20 30 29 20 7b 0a 09 09 09 62 75 66 66 65 72 5f   0) {....buffer_
44a0: 73 74 2d 3e 68 65 61 64 65 72 73 2e 63 6f 6e 6e  st->headers.conn
44b0: 65 63 74 69 6f 6e 20 3d 20 46 49 4c 45 44 5f 43  ection = FILED_C
44c0: 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45 45 50 5f 41  ONNECTION_KEEP_A
44d0: 4c 49 56 45 3b 0a 09 09 7d 0a 0a 09 09 69 66 20  LIVE;...}....if 
44e0: 28 6d 65 6d 63 6d 70 28 62 75 66 66 65 72 2c 20  (memcmp(buffer, 
44f0: 22 5c 72 5c 6e 22 2c 20 32 29 20 3d 3d 20 30 29  "\r\n", 2) == 0)
4500: 20 7b 0a 09 09 09 62 72 65 61 6b 3b 0a 09 09 7d   {....break;...}
4510: 0a 09 7d 0a 0a 09 2f 2a 20 44 65 74 65 72 6d 69  ..}.../* Determi
4520: 6e 65 20 72 61 6e 67 65 20 2a 2f 0a 09 69 66 20  ne range */..if 
4530: 28 72 61 6e 67 65 5f 65 6e 64 20 21 3d 20 30 29  (range_end != 0)
4540: 20 7b 0a 09 09 69 66 20 28 72 61 6e 67 65 5f 65   {...if (range_e
4550: 6e 64 20 3c 3d 20 72 61 6e 67 65 5f 73 74 61 72  nd <= range_star
4560: 74 29 20 7b 0a 09 09 09 72 65 74 75 72 6e 28 4e  t) {....return(N
4570: 55 4c 4c 29 3b 0a 09 09 7d 0a 0a 09 09 72 61 6e  ULL);...}....ran
4580: 67 65 5f 6c 65 6e 67 74 68 20 3d 20 72 61 6e 67  ge_length = rang
4590: 65 5f 65 6e 64 20 2d 20 72 61 6e 67 65 5f 73 74  e_end - range_st
45a0: 61 72 74 3b 0a 0a 09 09 66 69 6c 65 64 5f 6c 6f  art;....filed_lo
45b0: 67 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d  g_msg_debug("Com
45c0: 70 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 70 61  puting length pa
45d0: 72 61 6d 65 74 65 72 3a 20 25 6c 6c 75 20 3d 20  rameter: %llu = 
45e0: 25 6c 6c 75 20 2d 20 25 6c 6c 75 22 2c 0a 09 09  %llu - %llu",...
45f0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
4600: 6c 6f 6e 67 29 20 72 61 6e 67 65 5f 6c 65 6e 67  long) range_leng
4610: 74 68 2c 0a 09 09 09 28 75 6e 73 69 67 6e 65 64  th,....(unsigned
4620: 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 61 6e 67   long long) rang
4630: 65 5f 65 6e 64 2c 0a 09 09 09 28 75 6e 73 69 67  e_end,....(unsig
4640: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72  ned long long) r
4650: 61 6e 67 65 5f 73 74 61 72 74 0a 09 09 29 3b 0a  ange_start...);.
4660: 09 7d 0a 0a 09 2f 2a 20 46 69 6c 6c 20 75 70 20  .}.../* Fill up 
4670: 73 74 72 75 63 74 75 72 65 20 74 6f 20 72 65 74  structure to ret
4680: 75 72 6e 20 2a 2f 0a 09 62 75 66 66 65 72 5f 73  urn */..buffer_s
4690: 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65  t->headers.range
46a0: 2e 70 72 65 73 65 6e 74 20 3d 20 72 61 6e 67 65  .present = range
46b0: 5f 72 65 71 75 65 73 74 3b 0a 09 62 75 66 66 65  _request;..buffe
46c0: 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  r_st->headers.ra
46d0: 6e 67 65 2e 6f 66 66 73 65 74 20 20 3d 20 72 61  nge.offset  = ra
46e0: 6e 67 65 5f 73 74 61 72 74 3b 0a 09 62 75 66 66  nge_start;..buff
46f0: 65 72 5f 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  er_st->headers.r
4700: 61 6e 67 65 2e 6c 65 6e 67 74 68 20 20 3d 20 72  ange.length  = r
4710: 61 6e 67 65 5f 6c 65 6e 67 74 68 3b 0a 0a 09 2f  ange_length;.../
4720: 2a 20 49 66 20 76 68 6f 73 74 73 20 61 72 65 20  * If vhosts are 
4730: 65 6e 61 62 6c 65 64 2c 20 63 6f 6d 70 75 74 65  enabled, compute
4740: 20 6e 65 77 20 70 61 74 68 20 2a 2f 0a 09 69 66   new path */..if
4750: 20 28 6f 70 74 69 6f 6e 73 2d 3e 76 68 6f 73 74   (options->vhost
4760: 73 5f 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 69  s_enabled) {...i
4770: 66 20 28 62 75 66 66 65 72 5f 73 74 2d 3e 68 65  f (buffer_st->he
4780: 61 64 65 72 73 2e 68 6f 73 74 2e 70 72 65 73 65  aders.host.prese
4790: 6e 74 20 3d 3d 20 31 29 20 7b 0a 09 09 09 62 75  nt == 1) {....bu
47a0: 66 66 65 72 20 3d 20 62 75 66 66 65 72 5f 73 74  ffer = buffer_st
47b0: 2d 3e 74 6d 70 62 75 66 3b 0a 09 09 09 62 75 66  ->tmpbuf;....buf
47c0: 66 65 72 5f 6c 65 6e 20 3d 20 73 69 7a 65 6f 66  fer_len = sizeof
47d0: 28 62 75 66 66 65 72 5f 73 74 2d 3e 74 6d 70 62  (buffer_st->tmpb
47e0: 75 66 29 3b 0a 0a 09 09 09 73 6e 70 72 69 6e 74  uf);.....snprint
47f0: 66 5f 72 65 74 20 3d 20 73 6e 70 72 69 6e 74 66  f_ret = snprintf
4800: 28 62 75 66 66 65 72 2c 20 62 75 66 66 65 72 5f  (buffer, buffer_
4810: 6c 65 6e 2c 20 22 2f 25 73 25 73 25 73 22 2c 0a  len, "/%s%s%s",.
4820: 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d 3e 68  ....buffer_st->h
4830: 65 61 64 65 72 73 2e 68 6f 73 74 2e 68 6f 73 74  eaders.host.host
4840: 2c 0a 09 09 09 09 62 75 66 66 65 72 5f 73 74 2d  ,.....buffer_st-
4850: 3e 70 61 74 68 5b 30 5d 20 3d 3d 20 27 2f 27 20  >path[0] == '/' 
4860: 3f 20 22 22 20 3a 20 22 2f 22 2c 0a 09 09 09 09  ? "" : "/",.....
4870: 62 75 66 66 65 72 5f 73 74 2d 3e 70 61 74 68 0a  buffer_st->path.
4880: 09 09 09 29 3b 0a 09 09 09 69 66 20 28 73 6e 70  ...);....if (snp
4890: 72 69 6e 74 66 5f 72 65 74 20 3e 3d 20 30 29 20  rintf_ret >= 0) 
48a0: 7b 0a 09 09 09 09 69 66 20 28 28 28 75 6e 73 69  {.....if (((unsi
48b0: 67 6e 65 64 20 69 6e 74 29 20 73 6e 70 72 69 6e  gned int) snprin
48c0: 74 66 5f 72 65 74 29 20 3c 20 62 75 66 66 65 72  tf_ret) < buffer
48d0: 5f 6c 65 6e 29 20 7b 0a 09 09 09 09 09 73 74 72  _len) {......str
48e0: 63 70 79 28 62 75 66 66 65 72 5f 73 74 2d 3e 70  cpy(buffer_st->p
48f0: 61 74 68 2c 20 62 75 66 66 65 72 29 3b 0a 09 09  ath, buffer);...
4900: 09 09 7d 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a  ..}....}...}..}.
4910: 0a 09 72 65 74 75 72 6e 28 62 75 66 66 65 72 5f  ..return(buffer_
4920: 73 74 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72  st);.}../* Retur
4930: 6e 20 61 6e 20 65 72 72 6f 72 20 70 61 67 65 20  n an error page 
4940: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
4950: 69 6c 65 64 5f 65 72 72 6f 72 5f 70 61 67 65 28  iled_error_page(
4960: 46 49 4c 45 20 2a 66 70 2c 20 63 6f 6e 73 74 20  FILE *fp, const 
4970: 63 68 61 72 20 2a 64 61 74 65 5f 63 75 72 72 65  char *date_curre
4980: 6e 74 2c 20 69 6e 74 20 65 72 72 6f 72 5f 6e 75  nt, int error_nu
4990: 6d 62 65 72 2c 20 69 6e 74 20 6d 65 74 68 6f 64  mber, int method
49a0: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 72 65  , const char *re
49b0: 61 73 6f 6e 2c 20 73 74 72 75 63 74 20 66 69 6c  ason, struct fil
49c0: 65 64 5f 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f  ed_log_entry *lo
49d0: 67 29 20 7b 0a 09 63 68 61 72 20 2a 65 72 72 6f  g) {..char *erro
49e0: 72 5f 73 74 72 69 6e 67 20 3d 20 22 3c 68 74 6d  r_string = "<htm
49f0: 6c 3e 3c 68 65 61 64 3e 3c 74 69 74 6c 65 3e 45  l><head><title>E
4a00: 52 52 4f 52 3c 2f 74 69 74 6c 65 3e 3c 2f 68 65  RROR</title></he
4a10: 61 64 3e 3c 62 6f 64 79 3e 55 6e 61 62 6c 65 20  ad><body>Unable 
4a20: 74 6f 20 70 72 6f 63 65 73 73 20 72 65 71 75 65  to process reque
4a30: 73 74 3c 2f 62 6f 64 79 3e 3c 2f 68 74 6d 6c 3e  st</body></html>
4a40: 22 3b 0a 0a 09 66 70 72 69 6e 74 66 28 66 70 2c  ";...fprintf(fp,
4a50: 20 22 48 54 54 50 2f 31 2e 31 20 25 69 20 4e 6f   "HTTP/1.1 %i No
4a60: 74 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73  t OK\r\nDate: %s
4a70: 5c 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65  \r\nServer: file
4a80: 64 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69  d\r\nLast-Modifi
4a90: 65 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e  ed: %s\r\nConten
4aa0: 74 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c 72  t-Length: %llu\r
4ab0: 5c 6e 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20  \nContent-Type: 
4ac0: 25 73 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69 6f 6e  %s\r\nConnection
4ad0: 3a 20 63 6c 6f 73 65 5c 72 5c 6e 5c 72 5c 6e 22  : close\r\n\r\n"
4ae0: 2c 0a 09 09 65 72 72 6f 72 5f 6e 75 6d 62 65 72  ,...error_number
4af0: 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74  ,...date_current
4b00: 2c 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74  ,...date_current
4b10: 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f  ,...(unsigned lo
4b20: 6e 67 20 6c 6f 6e 67 29 20 73 74 72 6c 65 6e 28  ng long) strlen(
4b30: 65 72 72 6f 72 5f 73 74 72 69 6e 67 29 2c 0a 09  error_string),..
4b40: 09 22 74 65 78 74 2f 68 74 6d 6c 22 0a 09 29 3b  ."text/html"..);
4b50: 0a 0a 09 2f 2a 20 73 69 6c 65 6e 63 65 20 65 72  .../* silence er
4b60: 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20 48  ror string for H
4b70: 45 41 44 20 72 65 71 75 65 73 74 73 20 2a 2f 0a  EAD requests */.
4b80: 09 69 66 20 28 6d 65 74 68 6f 64 20 21 3d 20 46  .if (method != F
4b90: 49 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54  ILED_REQUEST_MET
4ba0: 48 4f 44 5f 48 45 41 44 29 20 7b 0a 09 09 66 70  HOD_HEAD) {...fp
4bb0: 72 69 6e 74 66 28 66 70 2c 20 22 25 73 22 2c 20  rintf(fp, "%s", 
4bc0: 65 72 72 6f 72 5f 73 74 72 69 6e 67 29 3b 0a 09  error_string);..
4bd0: 7d 0a 0a 09 2f 2a 20 4c 6f 67 20 65 72 72 6f 72  }.../* Log error
4be0: 20 2a 2f 0a 09 2f 2a 2a 20 72 65 61 73 6f 6e 20   */../** reason 
4bf0: 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 61 20  must point to a 
4c00: 67 6c 6f 62 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  globally allocat
4c10: 65 64 20 76 61 6c 75 65 20 2a 2a 2f 0a 09 6c 6f  ed value **/..lo
4c20: 67 2d 3e 72 65 61 73 6f 6e 20 3d 20 72 65 61 73  g->reason = reas
4c30: 6f 6e 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63  on;..log->http_c
4c40: 6f 64 65 20 3d 20 65 72 72 6f 72 5f 6e 75 6d 62  ode = error_numb
4c50: 65 72 3b 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f  er;...filed_log_
4c60: 65 6e 74 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a  entry(log);.../*
4c70: 20 43 6c 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f   Close connectio
4c80: 6e 20 2a 2f 0a 09 66 63 6c 6f 73 65 28 66 70 29  n */..fclose(fp)
4c90: 3b 0a 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;...return;.}../
4ca0: 2a 20 52 65 74 75 72 6e 20 61 20 72 65 64 69 72  * Return a redir
4cb0: 65 63 74 20 74 6f 20 69 6e 64 65 78 2e 68 74 6d  ect to index.htm
4cc0: 6c 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  l */.static void
4cd0: 20 66 69 6c 65 64 5f 72 65 64 69 72 65 63 74 5f   filed_redirect_
4ce0: 69 6e 64 65 78 28 46 49 4c 45 20 2a 66 70 2c 20  index(FILE *fp, 
4cf0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 61 74 65  const char *date
4d00: 5f 63 75 72 72 65 6e 74 2c 20 63 6f 6e 73 74 20  _current, const 
4d10: 63 68 61 72 20 2a 70 61 74 68 2c 20 73 74 72 75  char *path, stru
4d20: 63 74 20 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ct filed_log_ent
4d30: 72 79 20 2a 6c 6f 67 29 20 7b 0a 09 69 6e 74 20  ry *log) {..int 
4d40: 68 74 74 70 5f 63 6f 64 65 20 3d 20 33 30 31 3b  http_code = 301;
4d50: 0a 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 48  ..fprintf(fp, "H
4d60: 54 54 50 2f 31 2e 31 20 25 69 20 4f 4b 5c 72 5c  TTP/1.1 %i OK\r\
4d70: 6e 44 61 74 65 3a 20 25 73 5c 72 5c 6e 53 65 72  nDate: %s\r\nSer
4d80: 76 65 72 3a 20 66 69 6c 65 64 5c 72 5c 6e 4c 61  ver: filed\r\nLa
4d90: 73 74 2d 4d 6f 64 69 66 69 65 64 3a 20 25 73 5c  st-Modified: %s\
4da0: 72 5c 6e 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74  r\nContent-Lengt
4db0: 68 3a 20 30 5c 72 5c 6e 43 6f 6e 6e 65 63 74 69  h: 0\r\nConnecti
4dc0: 6f 6e 3a 20 63 6c 6f 73 65 5c 72 5c 6e 4c 6f 63  on: close\r\nLoc
4dd0: 61 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 5c 72 5c  ation: %s\r\n\r\
4de0: 6e 22 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c  n",...http_code,
4df0: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c  ...date_current,
4e00: 0a 09 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c  ...date_current,
4e10: 0a 09 09 22 69 6e 64 65 78 2e 68 74 6d 6c 22 0a  ..."index.html".
4e20: 09 29 3b 0a 0a 09 2f 2a 20 4c 6f 67 20 72 65 64  .);.../* Log red
4e30: 69 72 65 63 74 20 2a 2f 0a 09 6c 6f 67 2d 3e 72  irect */..log->r
4e40: 65 61 73 6f 6e 20 3d 20 22 72 65 64 69 72 65 63  eason = "redirec
4e50: 74 22 3b 0a 09 6c 6f 67 2d 3e 68 74 74 70 5f 63  t";..log->http_c
4e60: 6f 64 65 20 3d 20 68 74 74 70 5f 63 6f 64 65 3b  ode = http_code;
4e70: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ...filed_log_ent
4e80: 72 79 28 6c 6f 67 29 3b 0a 0a 09 2f 2a 20 43 6c  ry(log);.../* Cl
4e90: 6f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ose connection *
4ea0: 2f 0a 09 66 63 6c 6f 73 65 28 66 70 29 3b 0a 0a  /..fclose(fp);..
4eb0: 09 72 65 74 75 72 6e 3b 0a 0a 09 2f 2a 20 43 75  .return;.../* Cu
4ec0: 72 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 3a 20  rrently unused: 
4ed0: 70 61 74 68 20 2a 2f 0a 09 70 61 74 68 20 3d 20  path */..path = 
4ee0: 70 61 74 68 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76  path;.}../* Conv
4ef0: 65 72 74 20 61 6e 20 65 6e 75 6d 20 72 65 70 72  ert an enum repr
4f00: 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22 43 6f  esenting the "Co
4f10: 6e 6e 65 63 74 69 6f 6e 22 20 68 65 61 64 65 72  nnection" header
4f20: 20 76 61 6c 75 65 20 74 6f 20 61 20 73 74 72 69   value to a stri
4f30: 6e 67 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  ng */.static con
4f40: 73 74 20 63 68 61 72 20 2a 66 69 6c 65 64 5f 63  st char *filed_c
4f50: 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 72 28 69 6e  onnection_str(in
4f60: 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c  t connection_val
4f70: 75 65 29 20 7b 0a 09 73 77 69 74 63 68 20 28 63  ue) {..switch (c
4f80: 6f 6e 6e 65 63 74 69 6f 6e 5f 76 61 6c 75 65 29  onnection_value)
4f90: 20 7b 0a 09 09 63 61 73 65 20 46 49 4c 45 44 5f   {...case FILED_
4fa0: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45  CONNECTION_CLOSE
4fb0: 3a 0a 09 09 09 72 65 74 75 72 6e 28 22 63 6c 6f  :....return("clo
4fc0: 73 65 22 29 3b 0a 09 09 63 61 73 65 20 46 49 4c  se");...case FIL
4fd0: 45 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 4b 45  ED_CONNECTION_KE
4fe0: 45 50 5f 41 4c 49 56 45 3a 0a 09 09 09 72 65 74  EP_ALIVE:....ret
4ff0: 75 72 6e 28 22 6b 65 65 70 2d 61 6c 69 76 65 22  urn("keep-alive"
5000: 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 28 22  );..}...return("
5010: 63 6c 6f 73 65 22 29 3b 0a 7d 0a 0a 2f 2a 20 48  close");.}../* H
5020: 61 6e 64 6c 65 20 61 20 73 69 6e 67 6c 65 20 72  andle a single r
5030: 65 71 75 65 73 74 20 66 72 6f 6d 20 61 20 63 6c  equest from a cl
5040: 69 65 6e 74 20 2a 2f 0a 73 74 61 74 69 63 20 69  ient */.static i
5050: 6e 74 20 66 69 6c 65 64 5f 68 61 6e 64 6c 65 5f  nt filed_handle_
5060: 63 6c 69 65 6e 74 28 69 6e 74 20 66 64 2c 20 73  client(int fd, s
5070: 74 72 75 63 74 20 66 69 6c 65 64 5f 68 74 74 70  truct filed_http
5080: 5f 72 65 71 75 65 73 74 20 2a 72 65 71 75 65 73  _request *reques
5090: 74 2c 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f  t, struct filed_
50a0: 6c 6f 67 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c 20  log_entry *log, 
50b0: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74  struct filed_opt
50c0: 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20 7b  ions *options) {
50d0: 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f 66  ..struct filed_f
50e0: 69 6c 65 69 6e 66 6f 20 2a 66 69 6c 65 69 6e 66  ileinfo *fileinf
50f0: 6f 3b 0a 09 73 73 69 7a 65 5f 74 20 73 65 6e 64  o;..ssize_t send
5100: 66 69 6c 65 5f 72 65 74 3b 0a 09 73 69 7a 65 5f  file_ret;..size_
5110: 74 20 73 65 6e 64 66 69 6c 65 5f 73 69 7a 65 3b  t sendfile_size;
5120: 0a 09 6f 66 66 5f 74 20 73 65 6e 64 66 69 6c 65  ..off_t sendfile
5130: 5f 6f 66 66 73 65 74 2c 20 73 65 6e 64 66 69 6c  _offset, sendfil
5140: 65 5f 73 65 6e 74 2c 20 73 65 6e 64 66 69 6c 65  e_sent, sendfile
5150: 5f 6c 65 6e 3b 0a 09 63 68 61 72 20 2a 70 61 74  _len;..char *pat
5160: 68 3b 0a 09 63 68 61 72 20 2a 64 61 74 65 5f 63  h;..char *date_c
5170: 75 72 72 65 6e 74 2c 20 64 61 74 65 5f 63 75 72  urrent, date_cur
5180: 72 65 6e 74 5f 62 5b 36 34 5d 3b 0a 09 69 6e 74  rent_b[64];..int
5190: 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 46 49 4c   http_code;..FIL
51a0: 45 20 2a 66 70 3b 0a 0a 09 2f 2a 20 44 65 74 65  E *fp;.../* Dete
51b0: 72 6d 69 6e 65 20 63 75 72 72 65 6e 74 20 74 69  rmine current ti
51c0: 6d 65 20 2a 2f 0a 09 64 61 74 65 5f 63 75 72 72  me */..date_curr
51d0: 65 6e 74 20 3d 20 66 69 6c 65 64 5f 66 6f 72 6d  ent = filed_form
51e0: 61 74 5f 74 69 6d 65 28 64 61 74 65 5f 63 75 72  at_time(date_cur
51f0: 72 65 6e 74 5f 62 2c 20 73 69 7a 65 6f 66 28 64  rent_b, sizeof(d
5200: 61 74 65 5f 63 75 72 72 65 6e 74 5f 62 29 2c 20  ate_current_b), 
5210: 74 69 6d 65 28 4e 55 4c 4c 29 29 3b 0a 0a 09 2f  time(NULL));.../
5220: 2a 20 4f 70 65 6e 20 73 6f 63 6b 65 74 20 61 73  * Open socket as
5230: 20 41 4e 53 49 20 49 2f 4f 20 66 6f 72 20 65 61   ANSI I/O for ea
5240: 73 65 20 6f 66 20 75 73 65 20 2a 2f 0a 09 66 70  se of use */..fp
5250: 20 3d 20 66 64 6f 70 65 6e 28 66 64 2c 20 22 77   = fdopen(fd, "w
5260: 2b 62 22 29 3b 0a 09 69 66 20 28 66 70 20 3d 3d  +b");..if (fp ==
5270: 20 4e 55 4c 4c 29 20 7b 0a 09 09 63 6c 6f 73 65   NULL) {...close
5280: 28 66 64 29 3b 0a 0a 09 09 6c 6f 67 2d 3e 62 75  (fd);....log->bu
5290: 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a  ffer[0] = '\0';.
52a0: 09 09 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65  ..log->http_code
52b0: 20 3d 20 2d 31 3b 0a 09 09 6c 6f 67 2d 3e 72 65   = -1;...log->re
52c0: 61 73 6f 6e 20 3d 20 22 66 64 6f 70 65 6e 5f 66  ason = "fdopen_f
52d0: 61 69 6c 65 64 22 3b 0a 0a 09 09 66 69 6c 65 64  ailed";....filed
52e0: 5f 6c 6f 67 5f 65 6e 74 72 79 28 6c 6f 67 29 3b  _log_entry(log);
52f0: 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45 44  ....return(FILED
5300: 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53  _CONNECTION_CLOS
5310: 45 29 3b 0a 09 7d 0a 0a 09 72 65 71 75 65 73 74  E);..}...request
5320: 20 3d 20 66 69 6c 65 64 5f 67 65 74 5f 68 74 74   = filed_get_htt
5330: 70 5f 72 65 71 75 65 73 74 28 66 70 2c 20 72 65  p_request(fp, re
5340: 71 75 65 73 74 2c 20 6f 70 74 69 6f 6e 73 29 3b  quest, options);
5350: 0a 0a 09 69 66 20 28 72 65 71 75 65 73 74 20 3d  ...if (request =
5360: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 6c 6f 67 2d  = NULL) {...log-
5370: 3e 62 75 66 66 65 72 5b 30 5d 20 3d 20 27 5c 30  >buffer[0] = '\0
5380: 27 3b 0a 0a 09 09 66 69 6c 65 64 5f 65 72 72 6f  ';....filed_erro
5390: 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f  r_page(fp, date_
53a0: 63 75 72 72 65 6e 74 2c 20 35 30 30 2c 20 46 49  current, 500, FI
53b0: 4c 45 44 5f 52 45 51 55 45 53 54 5f 4d 45 54 48  LED_REQUEST_METH
53c0: 4f 44 5f 47 45 54 2c 20 22 66 6f 72 6d 61 74 22  OD_GET, "format"
53d0: 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75 72  , log);....retur
53e0: 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49  n(FILED_CONNECTI
53f0: 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a 09  ON_CLOSE);..}...
5400: 70 61 74 68 20 3d 20 72 65 71 75 65 73 74 2d 3e  path = request->
5410: 70 61 74 68 3b 0a 09 73 74 72 63 70 79 28 6c 6f  path;..strcpy(lo
5420: 67 2d 3e 62 75 66 66 65 72 2c 20 70 61 74 68 29  g->buffer, path)
5430: 3b 0a 09 6c 6f 67 2d 3e 6d 65 74 68 6f 64 20 3d  ;..log->method =
5440: 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64   request->method
5450: 3b 0a 0a 09 2f 2a 20 49 66 20 74 68 65 20 72 65  ;.../* If the re
5460: 71 75 65 73 74 65 64 20 70 61 74 68 20 69 73 20  quested path is 
5470: 61 20 64 69 72 65 63 74 6f 72 79 2c 20 72 65 64  a directory, red
5480: 69 72 65 63 74 20 74 6f 20 69 6e 64 65 78 20 70  irect to index p
5490: 61 67 65 20 2a 2f 0a 09 69 66 20 28 72 65 71 75  age */..if (requ
54a0: 65 73 74 2d 3e 74 79 70 65 20 3d 3d 20 46 49 4c  est->type == FIL
54b0: 45 44 5f 52 45 51 55 45 53 54 5f 54 59 50 45 5f  ED_REQUEST_TYPE_
54c0: 44 49 52 45 43 54 4f 52 59 29 20 7b 0a 09 09 66  DIRECTORY) {...f
54d0: 69 6c 65 64 5f 72 65 64 69 72 65 63 74 5f 69 6e  iled_redirect_in
54e0: 64 65 78 28 66 70 2c 20 64 61 74 65 5f 63 75 72  dex(fp, date_cur
54f0: 72 65 6e 74 2c 20 70 61 74 68 2c 20 6c 6f 67 29  rent, path, log)
5500: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45  ;....return(FILE
5510: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f  D_CONNECTION_CLO
5520: 53 45 29 3b 0a 09 7d 0a 0a 09 66 69 6c 65 69 6e  SE);..}...filein
5530: 66 6f 20 3d 20 66 69 6c 65 64 5f 6f 70 65 6e 5f  fo = filed_open_
5540: 66 69 6c 65 28 70 61 74 68 2c 20 26 72 65 71 75  file(path, &requ
5550: 65 73 74 2d 3e 66 69 6c 65 69 6e 66 6f 29 3b 0a  est->fileinfo);.
5560: 09 69 66 20 28 66 69 6c 65 69 6e 66 6f 20 3d 3d  .if (fileinfo ==
5570: 20 4e 55 4c 4c 29 20 7b 0a 09 09 66 69 6c 65 64   NULL) {...filed
5580: 5f 65 72 72 6f 72 5f 70 61 67 65 28 66 70 2c 20  _error_page(fp, 
5590: 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 20 34 30  date_current, 40
55a0: 34 2c 20 72 65 71 75 65 73 74 2d 3e 6d 65 74 68  4, request->meth
55b0: 6f 64 2c 20 22 6f 70 65 6e 5f 66 61 69 6c 65 64  od, "open_failed
55c0: 22 2c 20 6c 6f 67 29 3b 0a 0a 09 09 72 65 74 75  ", log);....retu
55d0: 72 6e 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54  rn(FILED_CONNECT
55e0: 49 4f 4e 5f 43 4c 4f 53 45 29 3b 0a 09 7d 0a 0a  ION_CLOSE);..}..
55f0: 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68 65  .if (request->he
5600: 61 64 65 72 73 2e 72 61 6e 67 65 2e 70 72 65 73  aders.range.pres
5610: 65 6e 74 29 20 7b 0a 09 09 69 66 20 28 72 65 71  ent) {...if (req
5620: 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61  uest->headers.ra
5630: 6e 67 65 2e 6f 66 66 73 65 74 20 21 3d 20 30 20  nge.offset != 0 
5640: 7c 7c 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64  || request->head
5650: 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68  ers.range.length
5660: 20 3e 3d 20 30 29 20 7b 0a 09 09 09 69 66 20 28   >= 0) {....if (
5670: 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73  request->headers
5680: 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3e 3d  .range.offset >=
5690: 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 29 20   fileinfo->len) 
56a0: 7b 0a 09 09 09 09 66 69 6c 65 64 5f 65 72 72 6f  {.....filed_erro
56b0: 72 5f 70 61 67 65 28 66 70 2c 20 64 61 74 65 5f  r_page(fp, date_
56c0: 63 75 72 72 65 6e 74 2c 20 34 31 36 2c 20 72 65  current, 416, re
56d0: 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 2c 20 22  quest->method, "
56e0: 72 61 6e 67 65 5f 69 6e 76 61 6c 69 64 22 2c 20  range_invalid", 
56f0: 6c 6f 67 29 3b 0a 0a 09 09 09 09 63 6c 6f 73 65  log);......close
5700: 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a  (fileinfo->fd);.
5710: 0a 09 09 09 09 72 65 74 75 72 6e 28 46 49 4c 45  .....return(FILE
5720: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f  D_CONNECTION_CLO
5730: 53 45 29 3b 0a 09 09 09 7d 0a 0a 09 09 09 69 66  SE);....}.....if
5740: 20 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65   (request->heade
5750: 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74 68 20  rs.range.length 
5760: 3d 3d 20 28 28 6f 66 66 5f 74 29 20 2d 31 29 29  == ((off_t) -1))
5770: 20 7b 0a 09 09 09 09 66 69 6c 65 64 5f 6c 6f 67   {.....filed_log
5780: 5f 6d 73 67 5f 64 65 62 75 67 28 22 43 6f 6d 70  _msg_debug("Comp
5790: 75 74 69 6e 67 20 6c 65 6e 67 74 68 20 74 6f 20  uting length to 
57a0: 66 69 74 20 69 6e 20 62 6f 75 6e 64 73 3a 20 66  fit in bounds: f
57b0: 69 6c 65 69 6e 66 6f 2d 3e 6c 65 6e 20 3d 20 25  ileinfo->len = %
57c0: 6c 6c 75 2c 20 72 65 71 75 65 73 74 2d 3e 68 65  llu, 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 20 3d 20 25 6c 6c 75 22 2c 0a 09 09 09 09  et = %llu",.....
57f0: 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20  .(unsigned long 
5800: 6c 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e  long) fileinfo->
5810: 6c 65 6e 2c 0a 09 09 09 09 09 28 75 6e 73 69 67  len,......(unsig
5820: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72  ned long long) r
5830: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
5840: 72 61 6e 67 65 2e 6f 66 66 73 65 74 0a 09 09 09  range.offset....
5850: 09 29 3b 0a 0a 09 09 09 09 72 65 71 75 65 73 74  .);......request
5860: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
5870: 6c 65 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66  length = fileinf
5880: 6f 2d 3e 6c 65 6e 20 2d 20 72 65 71 75 65 73 74  o->len - request
5890: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
58a0: 6f 66 66 73 65 74 3b 0a 09 09 09 7d 0a 0a 09 09  offset;....}....
58b0: 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 5f 64  .filed_log_msg_d
58c0: 65 62 75 67 28 22 50 61 72 74 69 61 6c 20 72 65  ebug("Partial re
58d0: 71 75 65 73 74 2c 20 73 74 61 72 74 69 6e 67 20  quest, starting 
58e0: 61 74 3a 20 25 6c 6c 75 20 61 6e 64 20 72 75 6e  at: %llu and run
58f0: 6e 69 6e 67 20 66 6f 72 20 25 6c 6c 69 20 62 79  ning for %lli by
5900: 74 65 73 22 2c 0a 09 09 09 09 28 75 6e 73 69 67  tes",.....(unsig
5910: 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72  ned long long) r
5920: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
5930: 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a 09 09  range.offset,...
5940: 09 09 28 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65  ..(long long) re
5950: 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e 72  quest->headers.r
5960: 61 6e 67 65 2e 6c 65 6e 67 74 68 0a 09 09 09 29  ange.length....)
5970: 3b 0a 0a 09 09 7d 0a 0a 09 09 68 74 74 70 5f 63  ;....}....http_c
5980: 6f 64 65 20 3d 20 32 30 36 3b 0a 09 7d 20 65 6c  ode = 206;..} el
5990: 73 65 20 7b 0a 09 09 68 74 74 70 5f 63 6f 64 65  se {...http_code
59a0: 20 3d 20 32 30 30 3b 0a 0a 09 09 2f 2a 20 43 6f   = 200;..../* Co
59b0: 6d 70 75 74 65 20 66 61 6b 65 20 72 61 6e 67 65  mpute fake range
59c0: 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74   parameters that
59d0: 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20 65 6e   includes the en
59e0: 74 69 72 65 20 66 69 6c 65 20 2a 2f 0a 09 09 72  tire file */...r
59f0: 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72 73 2e  equest->headers.
5a00: 72 61 6e 67 65 2e 6f 66 66 73 65 74 20 3d 20 30  range.offset = 0
5a10: 3b 0a 09 09 72 65 71 75 65 73 74 2d 3e 68 65 61  ;...request->hea
5a20: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74  ders.range.lengt
5a30: 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c 65  h = fileinfo->le
5a40: 6e 3b 0a 09 7d 0a 0a 09 66 70 72 69 6e 74 66 28  n;..}...fprintf(
5a50: 66 70 2c 20 22 48 54 54 50 2f 31 2e 31 20 25 69  fp, "HTTP/1.1 %i
5a60: 20 4f 4b 5c 72 5c 6e 44 61 74 65 3a 20 25 73 5c   OK\r\nDate: %s\
5a70: 72 5c 6e 53 65 72 76 65 72 3a 20 66 69 6c 65 64  r\nServer: filed
5a80: 5c 72 5c 6e 4c 61 73 74 2d 4d 6f 64 69 66 69 65  \r\nLast-Modifie
5a90: 64 3a 20 25 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74  d: %s\r\nContent
5aa0: 2d 4c 65 6e 67 74 68 3a 20 25 6c 6c 75 5c 72 5c  -Length: %llu\r\
5ab0: 6e 41 63 63 65 70 74 2d 52 61 6e 67 65 73 3a 20  nAccept-Ranges: 
5ac0: 62 79 74 65 73 5c 72 5c 6e 43 6f 6e 74 65 6e 74  bytes\r\nContent
5ad0: 2d 54 79 70 65 3a 20 25 73 5c 72 5c 6e 43 6f 6e  -Type: %s\r\nCon
5ae0: 6e 65 63 74 69 6f 6e 3a 20 25 73 5c 72 5c 6e 45  nection: %s\r\nE
5af0: 54 61 67 3a 20 5c 22 25 73 5c 22 5c 72 5c 6e 22  Tag: \"%s\"\r\n"
5b00: 2c 0a 09 09 68 74 74 70 5f 63 6f 64 65 2c 0a 09  ,...http_code,..
5b10: 09 64 61 74 65 5f 63 75 72 72 65 6e 74 2c 0a 09  .date_current,..
5b20: 09 66 69 6c 65 69 6e 66 6f 2d 3e 6c 61 73 74 6d  .fileinfo->lastm
5b30: 6f 64 2c 0a 09 09 28 75 6e 73 69 67 6e 65 64 20  od,...(unsigned 
5b40: 6c 6f 6e 67 20 6c 6f 6e 67 29 20 72 65 71 75 65  long long) reque
5b50: 73 74 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67  st->headers.rang
5b60: 65 2e 6c 65 6e 67 74 68 2c 0a 09 09 66 69 6c 65  e.length,...file
5b70: 69 6e 66 6f 2d 3e 74 79 70 65 2c 0a 09 09 66 69  info->type,...fi
5b80: 6c 65 64 5f 63 6f 6e 6e 65 63 74 69 6f 6e 5f 73  led_connection_s
5b90: 74 72 28 72 65 71 75 65 73 74 2d 3e 68 65 61 64  tr(request->head
5ba0: 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f 6e 29 2c  ers.connection),
5bb0: 0a 09 09 66 69 6c 65 69 6e 66 6f 2d 3e 65 74 61  ...fileinfo->eta
5bc0: 67 0a 09 29 3b 0a 0a 09 69 66 20 28 68 74 74 70  g..);...if (http
5bd0: 5f 63 6f 64 65 20 3d 3d 20 32 30 36 29 20 7b 0a  _code == 206) {.
5be0: 09 09 66 70 72 69 6e 74 66 28 66 70 2c 20 22 43  ..fprintf(fp, "C
5bf0: 6f 6e 74 65 6e 74 2d 52 61 6e 67 65 3a 20 62 79  ontent-Range: by
5c00: 74 65 73 20 25 6c 6c 75 2d 25 6c 6c 75 2f 25 6c  tes %llu-%llu/%l
5c10: 6c 75 5c 72 5c 6e 22 2c 0a 09 09 09 28 75 6e 73  lu\r\n",....(uns
5c20: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 29  igned long long)
5c30: 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65 72   request->header
5c40: 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 2c 0a  s.range.offset,.
5c50: 09 09 09 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  ...(unsigned lon
5c60: 67 20 6c 6f 6e 67 29 20 28 72 65 71 75 65 73 74  g long) (request
5c70: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
5c80: 6f 66 66 73 65 74 20 2b 20 72 65 71 75 65 73 74  offset + request
5c90: 2d 3e 68 65 61 64 65 72 73 2e 72 61 6e 67 65 2e  ->headers.range.
5ca0: 6c 65 6e 67 74 68 20 2d 20 31 29 2c 0a 09 09 09  length - 1),....
5cb0: 28 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c  (unsigned long l
5cc0: 6f 6e 67 29 20 66 69 6c 65 69 6e 66 6f 2d 3e 6c  ong) fileinfo->l
5cd0: 65 6e 0a 09 09 29 3b 0a 09 7d 0a 09 66 70 72 69  en...);..}..fpri
5ce0: 6e 74 66 28 66 70 2c 20 22 5c 72 5c 6e 22 29 3b  ntf(fp, "\r\n");
5cf0: 0a 09 66 66 6c 75 73 68 28 66 70 29 3b 0a 0a 09  ..fflush(fp);...
5d00: 6c 6f 67 2d 3e 68 74 74 70 5f 63 6f 64 65 20 3d  log->http_code =
5d10: 20 68 74 74 70 5f 63 6f 64 65 3b 0a 09 6c 6f 67   http_code;..log
5d20: 2d 3e 72 65 61 73 6f 6e 20 3d 20 22 4f 4b 22 3b  ->reason = "OK";
5d30: 0a 09 6c 6f 67 2d 3e 73 74 61 72 74 74 69 6d 65  ..log->starttime
5d40: 20 3d 20 74 69 6d 65 28 4e 55 4c 4c 29 3b 0a 09   = time(NULL);..
5d50: 6c 6f 67 2d 3e 72 65 71 5f 6f 66 66 73 65 74 20  log->req_offset 
5d60: 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61 64 65  = request->heade
5d70: 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73 65 74 3b  rs.range.offset;
5d80: 0a 09 6c 6f 67 2d 3e 72 65 71 5f 6c 65 6e 67 74  ..log->req_lengt
5d90: 68 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61  h = request->hea
5da0: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74  ders.range.lengt
5db0: 68 3b 0a 09 6c 6f 67 2d 3e 66 69 6c 65 5f 6c 65  h;..log->file_le
5dc0: 6e 67 74 68 20 3d 20 66 69 6c 65 69 6e 66 6f 2d  ngth = fileinfo-
5dd0: 3e 6c 65 6e 3b 0a 0a 23 69 66 64 65 66 20 46 49  >len;..#ifdef FI
5de0: 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f 48 54 54  LED_NONBLOCK_HTT
5df0: 50 0a 09 69 6e 74 20 73 6f 63 6b 65 74 5f 66 6c  P..int socket_fl
5e00: 61 67 73 3b 0a 09 66 64 5f 73 65 74 20 72 66 64  ags;..fd_set rfd
5e10: 2c 20 77 66 64 3b 0a 09 63 68 61 72 20 73 69 6e  , wfd;..char sin
5e20: 6b 62 75 66 5b 38 31 39 32 5d 3b 0a 09 73 73 69  kbuf[8192];..ssi
5e30: 7a 65 5f 74 20 72 65 61 64 5f 72 65 74 3b 0a 0a  ze_t read_ret;..
5e40: 09 46 44 5f 5a 45 52 4f 28 26 72 66 64 29 3b 0a  .FD_ZERO(&rfd);.
5e50: 09 46 44 5f 5a 45 52 4f 28 26 77 66 64 29 3b 0a  .FD_ZERO(&wfd);.
5e60: 09 46 44 5f 53 45 54 28 66 64 2c 20 26 72 66 64  .FD_SET(fd, &rfd
5e70: 29 3b 0a 09 46 44 5f 53 45 54 28 66 64 2c 20 26  );..FD_SET(fd, &
5e80: 77 66 64 29 3b 0a 0a 09 73 6f 63 6b 65 74 5f 66  wfd);...socket_f
5e90: 6c 61 67 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c  lags = fcntl(fd,
5ea0: 20 46 5f 47 45 54 46 4c 29 3b 0a 09 66 63 6e 74   F_GETFL);..fcnt
5eb0: 6c 28 66 64 2c 20 46 5f 53 45 54 46 4c 2c 20 73  l(fd, F_SETFL, s
5ec0: 6f 63 6b 65 74 5f 66 6c 61 67 73 20 7c 20 4f 5f  ocket_flags | O_
5ed0: 4e 4f 4e 42 4c 4f 43 4b 29 3b 0a 23 65 6e 64 69  NONBLOCK);.#endi
5ee0: 66 0a 09 73 65 6e 64 66 69 6c 65 5f 6f 66 66 73  f..sendfile_offs
5ef0: 65 74 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65  et = request->he
5f00: 61 64 65 72 73 2e 72 61 6e 67 65 2e 6f 66 66 73  aders.range.offs
5f10: 65 74 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 6c 65  et;..sendfile_le
5f20: 6e 20 3d 20 72 65 71 75 65 73 74 2d 3e 68 65 61  n = request->hea
5f30: 64 65 72 73 2e 72 61 6e 67 65 2e 6c 65 6e 67 74  ders.range.lengt
5f40: 68 3b 0a 09 73 65 6e 64 66 69 6c 65 5f 73 65 6e  h;..sendfile_sen
5f50: 74 20 3d 20 30 3b 0a 09 77 68 69 6c 65 20 28 72  t = 0;..while (r
5f60: 65 71 75 65 73 74 2d 3e 6d 65 74 68 6f 64 20 3d  equest->method =
5f70: 3d 20 46 49 4c 45 44 5f 52 45 51 55 45 53 54 5f  = FILED_REQUEST_
5f80: 4d 45 54 48 4f 44 5f 47 45 54 29 20 7b 0a 09 09  METHOD_GET) {...
5f90: 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 6c 65 6e  if (sendfile_len
5fa0: 20 3e 20 46 49 4c 45 44 5f 53 45 4e 44 46 49 4c   > FILED_SENDFIL
5fb0: 45 5f 4d 41 58 29 20 7b 0a 09 09 09 73 65 6e 64  E_MAX) {....send
5fc0: 66 69 6c 65 5f 73 69 7a 65 20 3d 20 46 49 4c 45  file_size = FILE
5fd0: 44 5f 53 45 4e 44 46 49 4c 45 5f 4d 41 58 3b 0a  D_SENDFILE_MAX;.
5fe0: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 73 65  ..} else {....se
5ff0: 6e 64 66 69 6c 65 5f 73 69 7a 65 20 3d 20 73 65  ndfile_size = se
6000: 6e 64 66 69 6c 65 5f 6c 65 6e 3b 0a 09 09 7d 0a  ndfile_len;...}.
6010: 0a 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20  ...sendfile_ret 
6020: 3d 20 73 65 6e 64 66 69 6c 65 28 66 64 2c 20 66  = sendfile(fd, f
6030: 69 6c 65 69 6e 66 6f 2d 3e 66 64 2c 20 26 73 65  ileinfo->fd, &se
6040: 6e 64 66 69 6c 65 5f 6f 66 66 73 65 74 2c 20 73  ndfile_offset, s
6050: 65 6e 64 66 69 6c 65 5f 73 69 7a 65 29 3b 0a 09  endfile_size);..
6060: 09 69 66 20 28 73 65 6e 64 66 69 6c 65 5f 72 65  .if (sendfile_re
6070: 74 20 3c 3d 20 30 29 20 7b 0a 23 69 66 64 65 66  t <= 0) {.#ifdef
6080: 20 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b 5f   FILED_NONBLOCK_
6090: 48 54 54 50 0a 09 09 09 69 66 20 28 65 72 72 6e  HTTP....if (errn
60a0: 6f 20 3d 3d 20 45 41 47 41 49 4e 29 20 7b 0a 09  o == EAGAIN) {..
60b0: 09 09 09 73 65 6e 64 66 69 6c 65 5f 72 65 74 20  ...sendfile_ret 
60c0: 3d 20 30 3b 0a 0a 09 09 09 09 77 68 69 6c 65 20  = 0;......while 
60d0: 28 31 29 20 7b 0a 09 09 09 09 09 73 65 6c 65 63  (1) {......selec
60e0: 74 28 66 64 20 2b 20 31 2c 20 26 72 66 64 2c 20  t(fd + 1, &rfd, 
60f0: 26 77 66 64 2c 20 4e 55 4c 4c 2c 20 4e 55 4c 4c  &wfd, NULL, NULL
6100: 29 3b 0a 09 09 09 09 09 69 66 20 28 46 44 5f 49  );......if (FD_I
6110: 53 53 45 54 28 66 64 2c 20 26 72 66 64 29 29 20  SSET(fd, &rfd)) 
6120: 7b 0a 09 09 09 09 09 09 72 65 61 64 5f 72 65 74  {.......read_ret
6130: 20 3d 20 72 65 61 64 28 66 64 2c 20 73 69 6e 6b   = read(fd, sink
6140: 62 75 66 2c 20 73 69 7a 65 6f 66 28 73 69 6e 6b  buf, sizeof(sink
6150: 62 75 66 29 29 3b 0a 0a 09 09 09 09 09 09 69 66  buf));........if
6160: 20 28 72 65 61 64 5f 72 65 74 20 3c 3d 20 30 29   (read_ret <= 0)
6170: 20 7b 0a 09 09 09 09 09 09 09 62 72 65 61 6b 3b   {........break;
6180: 0a 09 09 09 09 09 09 7d 0a 09 09 09 09 09 7d 0a  .......}......}.
6190: 0a 09 09 09 09 09 69 66 20 28 46 44 5f 49 53 53  ......if (FD_ISS
61a0: 45 54 28 66 64 2c 20 26 77 66 64 29 29 20 7b 0a  ET(fd, &wfd)) {.
61b0: 09 09 09 09 09 09 72 65 61 64 5f 72 65 74 20 3d  ......read_ret =
61c0: 20 31 3b 0a 0a 09 09 09 09 09 09 62 72 65 61 6b   1;........break
61d0: 3b 0a 09 09 09 09 09 7d 0a 09 09 09 09 7d 0a 0a  ;......}.....}..
61e0: 09 09 09 09 69 66 20 28 72 65 61 64 5f 72 65 74  ....if (read_ret
61f0: 20 3c 3d 20 30 29 20 7b 0a 09 09 09 09 09 62 72   <= 0) {......br
6200: 65 61 6b 3b 0a 09 09 09 09 7d 0a 09 09 09 7d 20  eak;.....}....} 
6210: 65 6c 73 65 20 7b 0a 09 09 09 09 62 72 65 61 6b  else {.....break
6220: 3b 0a 09 09 09 7d 0a 23 65 6c 73 65 0a 09 09 09  ;....}.#else....
6230: 62 72 65 61 6b 3b 0a 23 65 6e 64 69 66 0a 09 09  break;.#endif...
6240: 7d 0a 0a 09 09 73 65 6e 64 66 69 6c 65 5f 6c 65  }....sendfile_le
6250: 6e 20 2d 3d 20 73 65 6e 64 66 69 6c 65 5f 72 65  n -= sendfile_re
6260: 74 3b 0a 09 09 73 65 6e 64 66 69 6c 65 5f 73 65  t;...sendfile_se
6270: 6e 74 20 2b 3d 20 73 65 6e 64 66 69 6c 65 5f 72  nt += sendfile_r
6280: 65 74 3b 0a 09 09 69 66 20 28 73 65 6e 64 66 69  et;...if (sendfi
6290: 6c 65 5f 6c 65 6e 20 3d 3d 20 30 29 20 7b 0a 09  le_len == 0) {..
62a0: 09 09 62 72 65 61 6b 3b 0a 09 09 7d 0a 09 7d 0a  ..break;...}..}.
62b0: 0a 09 6c 6f 67 2d 3e 65 6e 64 74 69 6d 65 20 3d  ..log->endtime =
62c0: 20 28 74 69 6d 65 5f 74 29 20 2d 31 3b 0a 09 6c   (time_t) -1;..l
62d0: 6f 67 2d 3e 73 65 6e 74 5f 6c 65 6e 67 74 68 20  og->sent_length 
62e0: 3d 20 73 65 6e 64 66 69 6c 65 5f 73 65 6e 74 3b  = sendfile_sent;
62f0: 0a 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 65 6e 74  ...filed_log_ent
6300: 72 79 28 6c 6f 67 29 3b 0a 0a 09 63 6c 6f 73 65  ry(log);...close
6310: 28 66 69 6c 65 69 6e 66 6f 2d 3e 66 64 29 3b 0a  (fileinfo->fd);.
6320: 0a 09 69 66 20 28 72 65 71 75 65 73 74 2d 3e 68  ..if (request->h
6330: 65 61 64 65 72 73 2e 63 6f 6e 6e 65 63 74 69 6f  eaders.connectio
6340: 6e 20 21 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45  n != FILED_CONNE
6350: 43 54 49 4f 4e 5f 4b 45 45 50 5f 41 4c 49 56 45  CTION_KEEP_ALIVE
6360: 29 20 7b 0a 09 09 66 63 6c 6f 73 65 28 66 70 29  ) {...fclose(fp)
6370: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 46 49 4c 45  ;....return(FILE
6380: 44 5f 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f  D_CONNECTION_CLO
6390: 53 45 29 3b 0a 09 7d 0a 0a 09 72 65 74 75 72 6e  SE);..}...return
63a0: 28 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54 49 4f  (FILED_CONNECTIO
63b0: 4e 5f 4b 45 45 50 5f 41 4c 49 56 45 29 3b 0a 7d  N_KEEP_ALIVE);.}
63c0: 0a 0a 2f 2a 20 48 61 6e 64 6c 65 20 69 6e 63 6f  ../* Handle inco
63d0: 6d 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  ming connections
63e0: 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
63f0: 2a 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f 74 68  *filed_worker_th
6400: 72 65 61 64 28 76 6f 69 64 20 2a 61 72 67 5f 76  read(void *arg_v
6410: 29 20 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65  ) {..struct file
6420: 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f  d_worker_thread_
6430: 61 72 67 73 20 2a 61 72 67 3b 0a 09 73 74 72 75  args *arg;..stru
6440: 63 74 20 66 69 6c 65 64 5f 68 74 74 70 5f 72 65  ct filed_http_re
6450: 71 75 65 73 74 20 72 65 71 75 65 73 74 3b 0a 09  quest request;..
6460: 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6c 6f 67  struct filed_log
6470: 5f 65 6e 74 72 79 20 2a 6c 6f 67 2c 20 6c 6f 63  _entry *log, loc
6480: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 3b 0a 09 73  al_dummy_log;..s
6490: 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69  truct filed_opti
64a0: 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 3b 0a 09 73  ons *options;..s
64b0: 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69  truct sockaddr_i
64c0: 6e 36 20 61 64 64 72 3b 0a 09 73 6f 63 6b 6c 65  n6 addr;..sockle
64d0: 6e 5f 74 20 61 64 64 72 6c 65 6e 3b 0a 09 69 6e  n_t addrlen;..in
64e0: 74 20 66 61 69 6c 75 72 65 5f 63 6f 75 6e 74 20  t failure_count 
64f0: 3d 20 30 2c 20 6d 61 78 5f 66 61 69 6c 75 72 65  = 0, max_failure
6500: 5f 63 6f 75 6e 74 20 3d 20 46 49 4c 45 44 5f 4d  _count = FILED_M
6510: 41 58 5f 46 41 49 4c 55 52 45 5f 43 4f 55 4e 54  AX_FAILURE_COUNT
6520: 3b 0a 09 69 6e 74 20 63 6f 6e 6e 65 63 74 69 6f  ;..int connectio
6530: 6e 5f 73 74 61 74 65 20 3d 20 46 49 4c 45 44 5f  n_state = FILED_
6540: 43 4f 4e 4e 45 43 54 49 4f 4e 5f 43 4c 4f 53 45  CONNECTION_CLOSE
6550: 3b 0a 09 69 6e 74 20 6d 61 73 74 65 72 5f 66 64  ;..int master_fd
6560: 2c 20 66 64 20 3d 20 2d 31 3b 0a 0a 09 2f 2a 20  , fd = -1;.../* 
6570: 52 65 61 64 20 61 72 67 75 6d 65 6e 74 73 20 2a  Read arguments *
6580: 2f 0a 09 61 72 67 20 3d 20 61 72 67 5f 76 3b 0a  /..arg = arg_v;.
6590: 0a 09 6d 61 73 74 65 72 5f 66 64 20 3d 20 61 72  ..master_fd = ar
65a0: 67 2d 3e 66 64 3b 0a 09 6f 70 74 69 6f 6e 73 20  g->fd;..options 
65b0: 3d 20 26 61 72 67 2d 3e 6f 70 74 69 6f 6e 73 3b  = &arg->options;
65c0: 0a 0a 09 77 68 69 6c 65 20 28 31 29 20 7b 0a 09  ...while (1) {..
65d0: 09 2f 2a 20 46 61 69 6c 75 72 65 20 6c 6f 6f 70  ./* Failure loop
65e0: 20 70 72 65 76 65 6e 74 69 6f 6e 20 2a 2f 0a 09   prevention */..
65f0: 09 69 66 20 28 66 61 69 6c 75 72 65 5f 63 6f 75  .if (failure_cou
6600: 6e 74 20 3e 20 6d 61 78 5f 66 61 69 6c 75 72 65  nt > max_failure
6610: 5f 63 6f 75 6e 74 29 20 7b 0a 09 09 09 62 72 65  _count) {....bre
6620: 61 6b 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 41 6c  ak;...}..../* Al
6630: 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 6c 6f 67  locate a new log
6640: 20 62 75 66 66 65 72 20 2a 2f 0a 09 09 6c 6f 67   buffer */...log
6650: 20 3d 20 66 69 6c 65 64 5f 6c 6f 67 5f 6e 65 77   = filed_log_new
6660: 28 31 29 3b 0a 09 09 69 66 20 28 6c 6f 67 20 3d  (1);...if (log =
6670: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 09 66 69 6c  = NULL) {....fil
6680: 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41 4c 4c 4f  ed_log_msg("ALLO
6690: 43 41 54 45 5f 4c 4f 47 5f 4d 53 47 5f 46 41 49  CATE_LOG_MSG_FAI
66a0: 4c 45 44 22 29 3b 0a 0a 09 09 09 62 72 65 61 6b  LED");.....break
66b0: 3b 0a 09 09 7d 0a 0a 09 09 6c 6f 67 2d 3e 74 79  ;...}....log->ty
66c0: 70 65 20 3d 20 46 49 4c 45 44 5f 4c 4f 47 5f 54  pe = FILED_LOG_T
66d0: 59 50 45 5f 54 52 41 4e 53 46 45 52 3b 0a 0a 09  YPE_TRANSFER;...
66e0: 09 2f 2a 20 49 66 20 77 65 20 63 6c 6f 73 65 64  ./* If we closed
66f0: 20 74 68 65 20 6f 6c 64 20 63 6f 6e 6e 65 63 74   the old connect
6700: 69 6f 6e 2c 20 61 63 63 65 70 74 20 61 20 6e 65  ion, accept a ne
6710: 77 20 6f 6e 65 20 2a 2f 0a 09 09 69 66 20 28 63  w one */...if (c
6720: 6f 6e 6e 65 63 74 69 6f 6e 5f 73 74 61 74 65 20  onnection_state 
6730: 3d 3d 20 46 49 4c 45 44 5f 43 4f 4e 4e 45 43 54  == FILED_CONNECT
6740: 49 4f 4e 5f 43 4c 4f 53 45 29 20 7b 0a 09 09 09  ION_CLOSE) {....
6750: 2f 2a 20 41 63 63 65 70 74 20 61 20 6e 65 77 20  /* Accept a new 
6760: 63 6c 69 65 6e 74 20 2a 2f 0a 09 09 09 61 64 64  client */....add
6770: 72 6c 65 6e 20 3d 20 73 69 7a 65 6f 66 28 61 64  rlen = sizeof(ad
6780: 64 72 29 3b 0a 0a 09 09 09 66 64 20 3d 20 61 63  dr);.....fd = ac
6790: 63 65 70 74 28 6d 61 73 74 65 72 5f 66 64 2c 20  cept(master_fd, 
67a0: 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72  (struct sockaddr
67b0: 20 2a 29 20 26 61 64 64 72 2c 20 26 61 64 64 72   *) &addr, &addr
67c0: 6c 65 6e 29 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 0a  len);...}..../*.
67d0: 09 09 20 2a 20 49 66 20 77 65 20 66 61 69 6c 2c  .. * If we fail,
67e0: 20 6d 61 6b 65 20 61 20 6e 6f 74 65 20 6f 66 20   make a note of 
67f0: 69 74 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 67  it so we don't g
6800: 6f 20 69 6e 74 6f 20 61 20 6c 6f 6f 70 20 6f 66  o into a loop of
6810: 0a 09 09 20 2a 20 61 63 63 65 70 74 28 29 20 66  ... * accept() f
6820: 61 69 6c 69 6e 67 0a 09 09 20 2a 2f 0a 09 09 69  ailing... */...i
6830: 66 20 28 66 64 20 3c 20 30 29 20 7b 0a 09 09 09  f (fd < 0) {....
6840: 2f 2a 20 4c 6f 67 20 74 68 65 20 6e 65 77 20 63  /* Log the new c
6850: 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 09 09 09  onnection */....
6860: 66 69 6c 65 64 5f 6c 6f 67 5f 6d 73 67 28 22 41  filed_log_msg("A
6870: 43 43 45 50 54 5f 46 41 49 4c 45 44 22 29 3b 0a  CCEPT_FAILED");.
6880: 0a 09 09 09 66 61 69 6c 75 72 65 5f 63 6f 75 6e  ....failure_coun
6890: 74 2b 2b 3b 0a 0a 09 09 09 66 72 65 65 28 6c 6f  t++;.....free(lo
68a0: 67 29 3b 0a 0a 09 09 09 63 6f 6e 74 69 6e 75 65  g);.....continue
68b0: 3b 0a 09 09 7d 0a 0a 09 09 2f 2a 20 46 69 6c 6c  ;...}..../* Fill
68c0: 20 69 6e 20 6c 6f 67 20 73 74 72 75 63 74 75 72   in log structur
68d0: 65 20 2a 2f 0a 09 09 69 66 20 28 66 69 6c 65 64  e */...if (filed
68e0: 5f 6c 6f 67 5f 69 70 28 28 73 74 72 75 63 74 20  _log_ip((struct 
68f0: 73 6f 63 6b 61 64 64 72 20 2a 29 20 26 61 64 64  sockaddr *) &add
6900: 72 2c 20 6c 6f 67 2d 3e 69 70 2c 20 73 69 7a 65  r, log->ip, size
6910: 6f 66 28 6c 6f 67 2d 3e 69 70 29 29 20 3d 3d 20  of(log->ip)) == 
6920: 4e 55 4c 4c 29 20 7b 0a 09 09 09 6c 6f 67 2d 3e  NULL) {....log->
6930: 69 70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 09 09  ip[0] = '\0';...
6940: 09 6c 6f 67 2d 3e 70 6f 72 74 20 3d 20 30 3b 0a  .log->port = 0;.
6950: 09 09 7d 20 65 6c 73 65 20 7b 0a 09 09 09 6c 6f  ..} else {....lo
6960: 67 2d 3e 70 6f 72 74 20 3d 20 61 64 64 72 2e 73  g->port = addr.s
6970: 69 6e 36 5f 70 6f 72 74 3b 0a 09 09 7d 0a 0a 09  in6_port;...}...
6980: 09 2f 2a 20 52 65 73 65 74 20 66 61 69 6c 75 72  ./* Reset failur
6990: 65 20 63 6f 75 6e 74 2a 2f 0a 09 09 66 61 69 6c  e count*/...fail
69a0: 75 72 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 0a  ure_count = 0;..
69b0: 09 09 2f 2a 20 48 61 6e 64 6c 65 20 73 6f 63 6b  ../* Handle sock
69c0: 65 74 20 2a 2f 0a 09 09 63 6f 6e 6e 65 63 74 69  et */...connecti
69d0: 6f 6e 5f 73 74 61 74 65 20 3d 20 66 69 6c 65 64  on_state = filed
69e0: 5f 68 61 6e 64 6c 65 5f 63 6c 69 65 6e 74 28 66  _handle_client(f
69f0: 64 2c 20 26 72 65 71 75 65 73 74 2c 20 6c 6f 67  d, &request, log
6a00: 2c 20 6f 70 74 69 6f 6e 73 29 3b 0a 09 7d 0a 0a  , options);..}..
6a10: 09 2f 2a 20 52 65 70 6f 72 74 20 65 72 72 6f 72  ./* Report error
6a20: 20 2a 2f 0a 09 66 69 6c 65 64 5f 6c 6f 67 5f 6d   */..filed_log_m
6a30: 73 67 28 22 54 48 52 45 41 44 5f 44 49 45 44 20  sg("THREAD_DIED 
6a40: 41 42 4e 4f 52 4d 41 4c 22 29 3b 0a 0a 09 72 65  ABNORMAL");...re
6a50: 74 75 72 6e 28 4e 55 4c 4c 29 3b 0a 0a 09 2f 2a  turn(NULL);.../*
6a60: 20 6c 6f 63 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67   local_dummy_log
6a70: 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66   is only used if
6a80: 20 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47 20   FILED_DONT_LOG 
6a90: 69 73 20 65 6e 61 62 6c 65 64 2c 20 6f 74 68 65  is enabled, othe
6aa0: 72 77 69 73 65 20 69 74 27 73 20 6e 6f 74 20 75  rwise it's not u
6ab0: 73 65 64 2c 20 62 75 74 20 74 68 65 20 63 6f 6d  sed, but the com
6ac0: 70 69 6c 65 72 20 68 61 74 65 73 20 74 68 61 74  piler hates that
6ad0: 20 69 64 65 61 2e 20 2a 2f 0a 09 6c 6f 63 61 6c   idea. */..local
6ae0: 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70 65 20  _dummy_log.type 
6af0: 3d 20 30 3b 0a 09 6c 6f 63 61 6c 5f 64 75 6d 6d  = 0;..local_dumm
6b00: 79 5f 6c 6f 67 2e 74 79 70 65 20 3d 20 6c 6f 63  y_log.type = loc
6b10: 61 6c 5f 64 75 6d 6d 79 5f 6c 6f 67 2e 74 79 70  al_dummy_log.typ
6b20: 65 3b 0a 7d 0a 0a 2f 2a 20 43 72 65 61 74 65 20  e;.}../* Create 
6b30: 77 6f 72 6b 65 72 20 74 68 72 65 61 64 73 20 2a  worker threads *
6b40: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c  /.static int fil
6b50: 65 64 5f 77 6f 72 6b 65 72 5f 74 68 72 65 61 64  ed_worker_thread
6b60: 73 5f 69 6e 69 74 28 69 6e 74 20 66 64 2c 20 69  s_init(int fd, i
6b70: 6e 74 20 74 68 72 65 61 64 5f 63 6f 75 6e 74 2c  nt thread_count,
6b80: 20 73 74 72 75 63 74 20 66 69 6c 65 64 5f 6f 70   struct filed_op
6b90: 74 69 6f 6e 73 20 2a 6f 70 74 69 6f 6e 73 29 20  tions *options) 
6ba0: 7b 0a 09 73 74 72 75 63 74 20 66 69 6c 65 64 5f  {..struct filed_
6bb0: 77 6f 72 6b 65 72 5f 74 68 72 65 61 64 5f 61 72  worker_thread_ar
6bc0: 67 73 20 2a 61 72 67 3b 0a 09 70 74 68 72 65 61  gs *arg;..pthrea
6bd0: 64 5f 74 20 74 68 72 65 61 64 69 64 3b 0a 09 69  d_t threadid;..i
6be0: 6e 74 20 70 74 68 72 65 61 64 5f 72 65 74 3b 0a  nt pthread_ret;.
6bf0: 09 69 6e 74 20 69 3b 0a 0a 09 66 6f 72 20 28 69  .int i;...for (i
6c00: 20 3d 20 30 3b 20 69 20 3c 20 74 68 72 65 61 64   = 0; i < thread
6c10: 5f 63 6f 75 6e 74 3b 20 69 2b 2b 29 20 7b 0a 09  _count; i++) {..
6c20: 09 61 72 67 20 3d 20 6d 61 6c 6c 6f 63 28 73 69  .arg = malloc(si
6c30: 7a 65 6f 66 28 2a 61 72 67 29 29 3b 0a 0a 09 09  zeof(*arg));....
6c40: 61 72 67 2d 3e 66 64 20 3d 20 66 64 3b 0a 09 09  arg->fd = fd;...
6c50: 6d 65 6d 63 70 79 28 26 61 72 67 2d 3e 6f 70 74  memcpy(&arg->opt
6c60: 69 6f 6e 73 2c 20 6f 70 74 69 6f 6e 73 2c 20 73  ions, options, s
6c70: 69 7a 65 6f 66 28 2a 6f 70 74 69 6f 6e 73 29 29  izeof(*options))
6c80: 3b 0a 0a 09 09 70 74 68 72 65 61 64 5f 72 65 74  ;....pthread_ret
6c90: 20 3d 20 70 74 68 72 65 61 64 5f 63 72 65 61 74   = pthread_creat
6ca0: 65 28 26 74 68 72 65 61 64 69 64 2c 20 4e 55 4c  e(&threadid, NUL
6cb0: 4c 2c 20 66 69 6c 65 64 5f 77 6f 72 6b 65 72 5f  L, filed_worker_
6cc0: 74 68 72 65 61 64 2c 20 61 72 67 29 3b 0a 09 09  thread, arg);...
6cd0: 69 66 20 28 70 74 68 72 65 61 64 5f 72 65 74 20  if (pthread_ret 
6ce0: 21 3d 20 30 29 20 7b 0a 09 09 09 72 65 74 75 72  != 0) {....retur
6cf0: 6e 28 2d 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09  n(-1);...}..}...
6d00: 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a  return(0);.}../*
6d10: 20 44 69 73 70 6c 61 79 20 68 65 6c 70 20 2a 2f   Display help */
6d20: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 6c  .static void fil
6d30: 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 46 49  ed_print_help(FI
6d40: 4c 45 20 2a 6f 75 74 70 75 74 2c 20 69 6e 74 20  LE *output, int 
6d50: 6c 6f 6e 67 5f 68 65 6c 70 2c 20 63 6f 6e 73 74  long_help, const
6d60: 20 63 68 61 72 20 2a 65 78 74 72 61 29 20 7b 0a   char *extra) {.
6d70: 09 69 66 20 28 65 78 74 72 61 29 20 7b 0a 09 09  .if (extra) {...
6d80: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
6d90: 22 25 73 5c 6e 22 2c 20 65 78 74 72 61 29 3b 0a  "%s\n", extra);.
6da0: 09 7d 0a 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  .}...fprintf(out
6db0: 70 75 74 2c 20 22 55 73 61 67 65 3a 20 66 69 6c  put, "Usage: fil
6dc0: 65 64 20 5b 3c 6f 70 74 69 6f 6e 73 3e 5d 5c 6e  ed [<options>]\n
6dd0: 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74  ");..fprintf(out
6de0: 70 75 74 2c 20 22 20 20 4f 70 74 69 6f 6e 73 3a  put, "  Options:
6df0: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
6e00: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 68  utput, "      -h
6e10: 2c 20 2d 2d 68 65 6c 70 5c 6e 22 29 3b 0a 09 66  , --help\n");..f
6e20: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
6e30: 20 20 20 20 20 20 2d 64 2c 20 2d 2d 64 61 65 6d        -d, --daem
6e40: 6f 6e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66  on\n");..fprintf
6e50: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
6e60: 2d 76 2c 20 2d 2d 76 65 72 73 69 6f 6e 5c 6e 22  -v, --version\n"
6e70: 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70  );..fprintf(outp
6e80: 75 74 2c 20 22 20 20 20 20 20 20 2d 56 2c 20 2d  ut, "      -V, -
6e90: 2d 76 68 6f 73 74 5c 6e 22 29 3b 0a 09 66 70 72  -vhost\n");..fpr
6ea0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
6eb0: 20 20 20 20 2d 62 20 3c 61 64 64 72 65 73 73 3e      -b <address>
6ec0: 2c 20 2d 2d 62 69 6e 64 20 3c 61 64 64 72 65 73  , --bind <addres
6ed0: 73 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66  s>\n");..fprintf
6ee0: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
6ef0: 2d 70 20 3c 70 6f 72 74 3e 2c 20 2d 2d 70 6f 72  -p <port>, --por
6f00: 74 20 3c 70 6f 72 74 3e 5c 6e 22 29 3b 0a 09 66  t <port>\n");..f
6f10: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
6f20: 20 20 20 20 20 20 2d 74 20 3c 63 6f 75 6e 74 3e        -t <count>
6f30: 2c 20 2d 2d 74 68 72 65 61 64 73 20 3c 63 6f 75  , --threads <cou
6f40: 6e 74 3e 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74  nt>\n");..fprint
6f50: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
6f60: 20 2d 63 20 3c 65 6e 74 72 69 65 73 3e 2c 20 2d   -c <entries>, -
6f70: 2d 63 61 63 68 65 20 3c 65 6e 74 72 69 65 73 3e  -cache <entries>
6f80: 5c 6e 22 29 3b 0a 09 66 70 72 69 6e 74 66 28 6f  \n");..fprintf(o
6f90: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 6c  utput, "      -l
6fa0: 20 3c 66 69 6c 65 3e 2c 20 2d 2d 6c 6f 67 20 3c   <file>, --log <
6fb0: 66 69 6c 65 3e 5c 6e 22 29 3b 0a 09 66 70 72 69  file>\n");..fpri
6fc0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20  ntf(output, "   
6fd0: 20 20 20 2d 75 20 3c 75 73 65 72 3e 2c 20 2d 2d     -u <user>, --
6fe0: 75 73 65 72 20 3c 75 73 65 72 3e 5c 6e 22 29 3b  user <user>\n");
6ff0: 0a 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
7000: 2c 20 22 20 20 20 20 20 20 2d 72 20 3c 64 69 72  , "      -r <dir
7010: 65 63 74 6f 72 79 3e 2c 20 2d 2d 72 6f 6f 74 20  ectory>, --root 
7020: 3c 64 69 72 65 63 74 6f 72 79 3e 5c 6e 22 29 3b  <directory>\n");
7030: 0a 0a 09 69 66 20 28 6c 6f 6e 67 5f 68 65 6c 70  ...if (long_help
7040: 29 20 7b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ) {...fprintf(ou
7050: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
7060: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
7070: 20 20 55 73 61 67 65 3a 5c 6e 22 29 3b 0a 09 09    Usage:\n");...
7080: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
7090: 22 20 20 20 20 20 20 2d 68 20 28 6f 72 20 2d 2d  "      -h (or --
70a0: 68 65 6c 70 29 20 70 72 69 6e 74 73 20 74 68 69  help) prints thi
70b0: 73 20 75 73 61 67 65 20 69 6e 66 6f 72 6d 61 74  s usage informat
70c0: 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69  ion.\n");...fpri
70d0: 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22  ntf(output, "\n"
70e0: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
70f0: 70 75 74 2c 20 22 20 20 20 20 20 20 2d 64 20 28  put, "      -d (
7100: 6f 72 20 2d 2d 64 61 65 6d 6f 6e 29 20 69 6e 73  or --daemon) ins
7110: 74 72 75 63 74 73 20 66 69 6c 65 64 20 74 6f 20  tructs filed to 
7120: 62 65 63 6f 6d 65 20 61 20 64 61 65 6d 6f 6e 20  become a daemon 
7130: 61 66 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 69  after initializi
7140: 6e 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  ng\n");...fprint
7150: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
7160: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7170: 20 20 74 68 65 20 6c 69 73 74 65 6e 69 6e 67 20    the listening 
7180: 54 43 50 20 73 6f 63 6b 65 74 20 61 6e 64 20 6c  TCP socket and l
7190: 6f 67 20 66 69 6c 65 73 2e 5c 6e 22 29 3b 0a 09  og files.\n");..
71a0: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
71b0: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
71c0: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
71d0: 20 2d 76 20 28 6f 72 20 2d 2d 76 65 72 73 69 6f   -v (or --versio
71e0: 6e 29 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c  n) instructs fil
71f0: 65 64 20 70 72 69 6e 74 20 6f 75 74 20 74 68 65  ed print out the
7200: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20   version number 
7210: 61 6e 64 20 65 78 69 74 2e 5c 6e 22 29 3b 0a 09  and exit.\n");..
7220: 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c  .fprintf(output,
7230: 20 22 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74   "\n");...fprint
7240: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
7250: 20 2d 56 20 28 6f 72 20 2d 2d 76 68 6f 73 74 29   -V (or --vhost)
7260: 20 69 6e 73 74 72 75 63 74 73 20 66 69 6c 65 64   instructs filed
7270: 20 74 6f 20 70 72 65 70 65 6e 64 20 61 6c 6c 20   to prepend all 
7280: 72 65 71 75 65 73 74 73 20 77 69 74 68 20 74 68  requests with th
7290: 65 69 72 20 48 54 54 50 5c 6e 22 29 3b 0a 09 09  eir HTTP\n");...
72a0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
72b0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
72c0: 20 20 20 20 20 20 20 48 6f 73 74 20 68 65 61 64         Host head
72d0: 65 72 2e 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e  er.\n");...fprin
72e0: 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29  tf(output, "\n")
72f0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
7300: 75 74 2c 20 22 20 20 20 20 20 20 2d 62 20 28 6f  ut, "      -b (o
7310: 72 20 2d 2d 62 69 6e 64 29 20 73 70 65 63 69 66  r --bind) specif
7320: 69 65 73 20 74 68 65 20 61 64 64 72 65 73 73 20  ies the address 
7330: 74 6f 20 6c 69 73 74 65 6e 20 66 6f 72 20 69 6e  to listen for in
7340: 63 6f 6d 69 6e 67 20 48 54 54 50 5c 6e 22 29 3b  coming HTTP\n");
7350: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
7360: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
7370: 20 20 20 20 20 20 20 20 20 72 65 71 75 65 73 74           request
7380: 73 20 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75  s on.  The defau
7390: 6c 74 20 76 61 6c 75 65 20 69 73 20 5c 22 25 73  lt value is \"%s
73a0: 5c 22 2e 5c 6e 22 2c 20 42 49 4e 44 5f 41 44 44  \".\n", BIND_ADD
73b0: 52 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  R);...fprintf(ou
73c0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
73d0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
73e0: 20 20 20 20 20 20 2d 70 20 28 6f 72 20 2d 2d 70        -p (or --p
73f0: 6f 72 74 29 20 73 70 65 63 69 66 69 65 73 20 74  ort) specifies t
7400: 68 65 20 54 43 50 20 70 6f 72 74 20 6e 75 6d 62  he TCP port numb
7410: 65 72 20 74 6f 20 6c 69 73 74 65 6e 20 66 6f 72  er to listen for
7420: 20 69 6e 63 6f 6d 69 6e 67 20 48 54 54 50 5c 6e   incoming HTTP\n
7430: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
7440: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
7450: 20 20 20 20 20 20 20 20 20 20 20 20 72 65 71 75              requ
7460: 65 73 74 73 20 6f 6e 2e 20 20 54 68 65 20 64 65  ests on.  The de
7470: 66 61 75 6c 74 20 69 73 20 25 75 2e 5c 6e 22 2c  fault is %u.\n",
7480: 20 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 20   (unsigned int) 
7490: 50 4f 52 54 29 3b 0a 09 09 66 70 72 69 6e 74 66  PORT);...fprintf
74a0: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
74b0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
74c0: 2c 20 22 20 20 20 20 20 20 2d 74 20 28 6f 72 20  , "      -t (or 
74d0: 2d 2d 74 68 72 65 61 64 73 29 20 73 70 65 63 69  --threads) speci
74e0: 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  fies the number 
74f0: 6f 66 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64  of worker thread
7500: 73 20 74 6f 20 63 72 65 61 74 65 2e 20 45 61 63  s to create. Eac
7510: 68 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  h\n");...fprintf
7520: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
7530: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7540: 20 20 77 6f 72 6b 65 72 20 74 68 72 65 61 64 20    worker thread 
7550: 63 61 6e 20 73 65 72 76 69 63 65 20 6f 6e 65 20  can service one 
7560: 63 6f 6e 63 75 72 72 65 6e 74 20 48 54 54 50 20  concurrent HTTP 
7570: 73 65 73 73 69 6f 6e 2e 5c 6e 22 29 3b 0a 09 09  session.\n");...
7580: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
7590: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
75a0: 20 20 20 20 20 20 20 20 20 54 68 75 73 20 74 68           Thus th
75b0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65  e number of thre
75c0: 61 64 73 20 63 72 65 61 74 65 64 20 77 69 6c 6c  ads created will
75d0: 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 5c 6e   determine how\n
75e0: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
75f0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
7600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
7610: 61 6e 79 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73  any simultaneous
7620: 20 74 72 61 6e 73 66 65 72 73 20 77 69 6c 6c 20   transfers will 
7630: 62 65 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 65  be possible. The
7640: 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28  \n");...fprintf(
7650: 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20  output, "       
7660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7670: 20 64 65 66 61 75 6c 74 20 69 73 20 25 6c 75 2e   default is %lu.
7680: 5c 6e 22 2c 20 28 75 6e 73 69 67 6e 65 64 20 6c  \n", (unsigned l
7690: 6f 6e 67 29 20 54 48 52 45 41 44 5f 43 4f 55 4e  ong) THREAD_COUN
76a0: 54 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  T);...fprintf(ou
76b0: 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66  tput, "\n");...f
76c0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
76d0: 20 20 20 20 20 20 2d 63 20 28 6f 72 20 2d 2d 63        -c (or --c
76e0: 61 63 68 65 29 20 73 70 65 63 69 66 69 65 73 20  ache) specifies 
76f0: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69  the number of fi
7700: 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 63  le information c
7710: 61 63 68 65 20 65 6e 74 72 69 65 73 5c 6e 22 29  ache entries\n")
7720: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
7730: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
7740: 20 20 20 20 20 20 20 20 20 20 20 74 6f 20 61 6c             to al
7750: 6c 6f 63 61 74 65 2e 20 20 45 61 63 68 20 63 61  locate.  Each ca
7760: 63 68 65 20 65 6e 74 72 79 20 68 6f 6c 64 73 20  che entry holds 
7770: 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  file information
7780: 20 61 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e   as\n");...fprin
7790: 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20  tf(output, "    
77a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77b0: 20 20 77 65 6c 6c 20 61 73 20 61 6e 20 6f 70 65    well as an ope
77c0: 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  n file descripto
77d0: 72 20 74 6f 20 74 68 65 20 66 69 6c 65 2c 20 73  r to the file, s
77e0: 6f 20 72 65 73 6f 75 72 63 65 5c 6e 22 29 3b 0a  o resource\n");.
77f0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
7800: 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20 20  , "             
7810: 20 20 20 20 20 20 20 20 20 6c 69 6d 69 74 73 20           limits 
7820: 28 69 2e 65 2e 2c 20 75 6c 69 6d 69 74 29 20 73  (i.e., ulimit) s
7830: 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64 65  hould be conside
7840: 72 65 64 2e 20 20 54 68 69 73 20 73 68 6f 75 6c  red.  This shoul
7850: 64 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  d\n");...fprintf
7860: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
7870: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7880: 62 65 20 61 20 70 72 69 6d 65 20 6e 75 6d 62 65  be a prime numbe
7890: 72 20 66 6f 72 20 69 64 65 61 6c 20 75 73 65 20  r for ideal use 
78a0: 77 69 74 68 20 74 68 65 20 6c 6f 6f 6b 75 70 20  with the lookup 
78b0: 6d 65 74 68 6f 64 2e 5c 6e 22 29 3b 0a 09 09 66  method.\n");...f
78c0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
78d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78e0: 20 20 20 20 20 20 54 68 65 20 64 65 66 61 75 6c        The defaul
78f0: 74 20 69 73 20 25 6c 75 2e 5c 6e 22 2c 20 28 75  t is %lu.\n", (u
7900: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 29 20 43 41  nsigned long) CA
7910: 43 48 45 5f 53 49 5a 45 29 3b 0a 09 09 66 70 72  CHE_SIZE);...fpr
7920: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 5c 6e  intf(output, "\n
7930: 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  ");...fprintf(ou
7940: 74 70 75 74 2c 20 22 20 20 20 20 20 20 2d 6c 20  tput, "      -l 
7950: 28 6f 72 20 2d 2d 6c 6f 67 29 20 73 70 65 63 69  (or --log) speci
7960: 66 69 65 73 20 61 20 66 69 6c 65 6e 61 6d 65 20  fies a filename 
7970: 74 6f 20 6f 70 65 6e 20 66 6f 72 20 77 72 69 74  to open for writ
7980: 69 6e 67 20 6c 6f 67 20 65 6e 74 72 69 65 73 2e  ing log entries.
7990: 20 20 4c 6f 67 5c 6e 22 29 3b 0a 09 09 66 70 72    Log\n");...fpr
79a0: 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20 20  intf(output, "  
79b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
79c0: 20 20 65 6e 74 72 69 65 73 20 61 72 65 20 6d 61    entries are ma
79d0: 64 65 20 66 6f 72 20 76 61 72 69 6f 75 73 20 73  de for various s
79e0: 74 61 67 65 73 20 69 6e 20 74 72 61 6e 73 66 65  tages in transfe
79f0: 72 69 6e 67 20 66 69 6c 65 73 2e 5c 6e 22 29 3b  ring files.\n");
7a00: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
7a10: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
7a20: 20 20 20 20 20 20 20 20 54 68 65 20 6c 6f 67 20          The log 
7a30: 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 20 62  file is opened b
7a40: 65 66 6f 72 65 20 73 77 69 74 63 68 69 6e 67 20  efore switching 
7a50: 75 73 65 72 73 20 28 73 65 65 20 5c 22 2d 75 5c  users (see \"-u\
7a60: 22 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  ")\n");...fprint
7a70: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
7a80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
7a90: 6e 64 20 72 6f 6f 74 20 64 69 72 65 63 74 6f 72  nd root director
7aa0: 69 65 73 20 28 73 65 65 20 5c 22 2d 72 5c 22 29  ies (see \"-r\")
7ab0: 2e 20 20 54 68 65 20 6c 6f 67 20 66 69 6c 65 20  .  The log file 
7ac0: 69 73 20 6e 65 76 65 72 5c 6e 22 29 3b 0a 09 09  is never\n");...
7ad0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
7ae0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
7af0: 20 20 20 20 20 63 6c 6f 73 65 64 20 73 6f 20 6c       closed so l
7b00: 6f 67 20 72 6f 74 61 74 69 6f 6e 20 77 69 74 68  og rotation with
7b10: 6f 75 74 20 73 74 6f 70 70 69 6e 67 20 74 68 65  out stopping the
7b20: 20 64 61 65 6d 6f 6e 20 69 73 20 77 69 6c 6c 5c   daemon is will\
7b30: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
7b40: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
7b50: 20 20 20 20 20 20 20 20 20 20 20 20 6e 6f 74 20              not 
7b60: 77 6f 72 6b 2e 20 20 54 68 65 20 76 61 6c 75 65  work.  The value
7b70: 20 6f 66 20 5c 22 2d 5c 22 20 69 6e 64 69 63 61   of \"-\" indica
7b80: 74 65 73 20 74 68 61 74 20 73 74 61 6e 64 61 72  tes that standar
7b90: 64 20 6f 75 74 70 75 74 5c 6e 22 29 3b 0a 09 09  d output\n");...
7ba0: 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20  fprintf(output, 
7bb0: 22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  "               
7bc0: 20 20 20 20 20 73 68 6f 75 6c 64 20 62 65 20 75       should be u
7bd0: 73 65 64 20 66 6f 72 20 6c 6f 67 67 69 6e 67 2e  sed for logging.
7be0: 20 20 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d    If the filenam
7bf0: 65 20 62 65 67 69 6e 73 20 77 69 74 68 20 61 5c  e begins with a\
7c00: 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f  n");...fprintf(o
7c10: 75 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20  utput, "        
7c20: 20 20 20 20 20 20 20 20 20 20 20 20 70 69 70 65              pipe
7c30: 20 28 5c 22 7c 5c 22 29 20 74 68 65 6e 20 61 20   (\"|\") then a 
7c40: 70 72 6f 63 65 73 73 20 69 73 20 73 74 61 72 74  process is start
7c50: 65 64 20 61 6e 64 20 75 73 65 64 20 66 6f 72 20  ed and used for 
7c60: 6c 6f 67 67 69 6e 67 5c 6e 22 29 3b 0a 09 09 66  logging\n");...f
7c70: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
7c80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7c90: 20 20 20 20 69 6e 73 74 65 61 64 20 6f 66 20 61      instead of a
7ca0: 20 66 69 6c 65 2e 20 20 54 68 65 20 64 65 66 61   file.  The defa
7cb0: 75 6c 74 20 69 73 20 5c 22 25 73 5c 22 2e 5c 6e  ult is \"%s\".\n
7cc0: 22 2c 20 4c 4f 47 5f 46 49 4c 45 29 3b 0a 23 69  ", LOG_FILE);.#i
7cd0: 66 64 65 66 20 46 49 4c 45 44 5f 44 4f 4e 54 5f  fdef FILED_DONT_
7ce0: 4c 4f 47 0a 09 09 66 70 72 69 6e 74 66 28 6f 75  LOG...fprintf(ou
7cf0: 74 70 75 74 2c 20 22 20 20 20 20 20 20 20 20 20  tput, "         
7d00: 20 20 20 20 20 20 20 20 20 20 20 4e 6f 74 65 20             Note 
7d10: 74 68 61 74 20 6c 6f 67 67 69 6e 67 20 69 73 20  that logging is 
7d20: 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 61 62  completely disab
7d30: 6c 65 64 20 73 6f 20 74 68 69 73 20 6f 70 74 69  led so this opti
7d40: 6f 6e 20 64 6f 65 73 5c 6e 22 29 3b 0a 09 09 66  on does\n");...f
7d50: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
7d60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d70: 20 20 20 20 6e 6f 74 68 69 6e 67 20 69 6e 20 74      nothing in t
7d80: 68 69 73 20 62 75 69 6c 64 2e 5c 6e 22 29 3b 0a  his build.\n");.
7d90: 23 65 6e 64 69 66 0a 09 09 66 70 72 69 6e 74 66  #endif...fprintf
7da0: 28 6f 75 74 70 75 74 2c 20 22 5c 6e 22 29 3b 0a  (output, "\n");.
7db0: 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75 74  ..fprintf(output
7dc0: 2c 20 22 20 20 20 20 20 20 2d 75 20 28 6f 72 20  , "      -u (or 
7dd0: 2d 2d 75 73 65 72 29 20 73 70 65 63 69 66 69 65  --user) specifie
7de0: 73 20 74 68 65 20 75 73 65 72 20 74 6f 20 73 77  s the user to sw
7df0: 69 74 63 68 20 75 73 65 72 20 49 44 73 20 74 6f  itch user IDs to
7e00: 20 62 65 66 6f 72 65 20 73 65 72 76 69 63 69 6e   before servicin
7e10: 67 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  g\n");...fprintf
7e20: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
7e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
7e40: 65 71 75 65 73 74 73 2e 20 20 54 68 65 20 64 65  equests.  The de
7e50: 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 63 68 61  fault is not cha
7e60: 6e 67 65 20 75 73 65 72 20 49 44 73 2e 5c 6e 22  nge user IDs.\n"
7e70: 29 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74  );...fprintf(out
7e80: 70 75 74 2c 20 22 5c 6e 22 29 3b 0a 09 09 66 70  put, "\n");...fp
7e90: 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22 20  rintf(output, " 
7ea0: 20 20 20 20 20 2d 72 20 28 6f 72 20 2d 2d 72 6f       -r (or --ro
7eb0: 6f 74 29 20 73 70 65 63 69 66 69 65 73 20 74 68  ot) specifies th
7ec0: 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20 61  e directory to a
7ed0: 63 74 20 61 73 20 74 68 65 20 72 6f 6f 74 20 64  ct as the root d
7ee0: 69 72 65 63 74 6f 72 79 20 66 6f 72 5c 6e 22 29  irectory for\n")
7ef0: 3b 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70  ;...fprintf(outp
7f00: 75 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20  ut, "           
7f10: 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 69            the fi
7f20: 6c 65 20 73 65 72 76 65 72 2e 20 20 49 66 20 74  le server.  If t
7f30: 68 69 73 20 6f 70 74 69 6f 6e 20 69 73 20 73 70  his option is sp
7f40: 65 63 69 66 69 65 64 2c 20 63 68 72 6f 6f 74 28  ecified, chroot(
7f50: 32 29 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74  2)\n");...fprint
7f60: 66 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20  f(output, "     
7f70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f80: 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20  is called.  The 
7f90: 64 65 66 61 75 6c 74 20 69 73 20 6e 6f 74 20 63  default is not c
7fa0: 68 61 6e 67 65 20 72 6f 6f 74 20 64 69 72 65 63  hange root direc
7fb0: 74 6f 72 69 65 73 2c 5c 6e 22 29 3b 0a 09 09 66  tories,\n");...f
7fc0: 70 72 69 6e 74 66 28 6f 75 74 70 75 74 2c 20 22  printf(output, "
7fd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7fe0: 20 20 20 20 20 74 68 61 74 20 69 73 2c 20 74 68       that is, th
7ff0: 65 20 5c 22 2f 5c 22 20 64 69 72 65 63 74 6f 72  e \"/\" director
8000: 79 20 69 73 20 73 68 61 72 65 64 20 6f 75 74 2e  y is shared out.
8010: 20 20 54 68 69 73 20 77 69 6c 6c 5c 6e 22 29 3b    This will\n");
8020: 0a 09 09 66 70 72 69 6e 74 66 28 6f 75 74 70 75  ...fprintf(outpu
8030: 74 2c 20 22 20 20 20 20 20 20 20 20 20 20 20 20  t, "            
8040: 20 20 20 20 20 20 20 20 20 6c 69 6b 65 6c 79 20           likely 
8050: 62 65 20 61 20 73 65 63 75 72 69 74 79 20 69 73  be a security is
8060: 73 75 65 2c 20 73 6f 20 74 68 69 73 20 6f 70 74  sue, so this opt
8070: 69 6f 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ion should alway
8080: 73 5c 6e 22 29 3b 0a 09 09 66 70 72 69 6e 74 66  s\n");...fprintf
8090: 28 6f 75 74 70 75 74 2c 20 22 20 20 20 20 20 20  (output, "      
80a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
80b0: 65 20 75 73 65 64 2e 5c 6e 22 29 3b 0a 09 7d 0a  e used.\n");..}.
80c0: 0a 09 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20  ..return;.}../* 
80d0: 41 64 64 20 61 20 67 65 74 6f 70 74 20 6f 70 74  Add a getopt opt
80e0: 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ion */.static vo
80f0: 69 64 20 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f  id filed_getopt_
8100: 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 73 74 72 75  long_setopt(stru
8110: 63 74 20 6f 70 74 69 6f 6e 20 2a 6f 70 74 2c 20  ct option *opt, 
8120: 63 6f 6e 73 74 20 63 68 61 72 20 2a 6e 61 6d 65  const char *name
8130: 2c 20 69 6e 74 20 68 61 73 5f 61 72 67 2c 20 69  , int has_arg, i
8140: 6e 74 20 76 61 6c 29 20 7b 0a 09 6f 70 74 2d 3e  nt val) {..opt->
8150: 6e 61 6d 65 20 20 20 20 20 3d 20 6e 61 6d 65 3b  name     = name;
8160: 0a 09 6f 70 74 2d 3e 68 61 73 5f 61 72 67 20 20  ..opt->has_arg  
8170: 3d 20 68 61 73 5f 61 72 67 3b 0a 09 6f 70 74 2d  = has_arg;..opt-
8180: 3e 66 6c 61 67 20 20 20 20 20 3d 20 4e 55 4c 4c  >flag     = NULL
8190: 3b 0a 09 6f 70 74 2d 3e 76 61 6c 20 20 20 20 20  ;..opt->val     
81a0: 20 3d 20 76 61 6c 3b 0a 0a 09 72 65 74 75 72 6e   = val;...return
81b0: 3b 0a 7d 0a 0a 2f 2a 20 52 65 73 6f 6c 76 65 20  ;.}../* Resolve 
81c0: 61 20 75 73 65 72 6e 61 6d 65 20 74 6f 20 61 20  a username to a 
81d0: 55 49 44 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  UID */.static in
81e0: 74 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f  t filed_user_loo
81f0: 6b 75 70 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  kup(const char *
8200: 75 73 65 72 2c 20 75 69 64 5f 74 20 2a 75 73 65  user, uid_t *use
8210: 72 5f 69 64 29 20 7b 0a 09 63 68 61 72 20 2a 6e  r_id) {..char *n
8220: 65 78 74 3b 0a 09 75 69 64 5f 74 20 75 73 65 72  ext;..uid_t user
8230: 5f 69 64 5f 63 68 65 63 6b 3b 0a 23 69 66 6e 64  _id_check;.#ifnd
8240: 65 66 20 46 49 4c 45 44 5f 4e 4f 5f 47 45 54 50  ef FILED_NO_GETP
8250: 57 4e 41 4d 0a 09 73 74 72 75 63 74 20 70 61 73  WNAM..struct pas
8260: 73 77 64 20 2a 65 6e 74 3b 0a 0a 09 65 6e 74 20  swd *ent;...ent 
8270: 3d 20 67 65 74 70 77 6e 61 6d 28 75 73 65 72 29  = getpwnam(user)
8280: 3b 0a 09 69 66 20 28 65 6e 74 20 21 3d 20 4e 55  ;..if (ent != NU
8290: 4c 4c 29 20 7b 0a 09 09 2a 75 73 65 72 5f 69 64  LL) {...*user_id
82a0: 20 3d 20 65 6e 74 2d 3e 70 77 5f 75 69 64 3b 0a   = ent->pw_uid;.
82b0: 0a 09 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 7d  ...return(0);..}
82c0: 0a 23 65 6e 64 69 66 0a 0a 09 75 73 65 72 5f 69  .#endif...user_i
82d0: 64 5f 63 68 65 63 6b 20 3d 20 73 74 72 74 6f 75  d_check = strtou
82e0: 6c 6c 28 75 73 65 72 2c 20 26 6e 65 78 74 2c 20  ll(user, &next, 
82f0: 31 30 29 3b 0a 09 69 66 20 28 6e 65 78 74 20 3d  10);..if (next =
8300: 3d 20 4e 55 4c 4c 29 20 7b 0a 09 09 72 65 74 75  = NULL) {...retu
8310: 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28  rn(1);..}...if (
8320: 6e 65 78 74 5b 30 5d 20 21 3d 20 27 5c 30 27 29  next[0] != '\0')
8330: 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b 0a   {...return(1);.
8340: 09 7d 0a 0a 09 2a 75 73 65 72 5f 69 64 20 3d 20  .}...*user_id = 
8350: 75 73 65 72 5f 69 64 5f 63 68 65 63 6b 3b 0a 0a  user_id_check;..
8360: 09 72 65 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f  .return(0);.}../
8370: 2a 20 44 61 65 6d 6f 6e 69 7a 65 20 2a 2f 0a 73  * Daemonize */.s
8380: 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 64 5f  tatic int filed_
8390: 64 61 65 6d 6f 6e 69 7a 65 28 76 6f 69 64 29 20  daemonize(void) 
83a0: 7b 0a 09 70 69 64 5f 74 20 73 65 74 73 69 64 5f  {..pid_t setsid_
83b0: 72 65 74 2c 20 66 6f 72 6b 5f 72 65 74 3b 0a 09  ret, fork_ret;..
83c0: 69 6e 74 20 63 68 64 69 72 5f 72 65 74 2c 20 64  int chdir_ret, d
83d0: 75 70 32 5f 72 65 74 3b 0a 09 69 6e 74 20 66 64  up2_ret;..int fd
83e0: 5f 69 6e 2c 20 66 64 5f 6f 75 74 3b 0a 0a 09 63  _in, fd_out;...c
83f0: 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64 69 72  hdir_ret = chdir
8400: 28 22 2f 22 29 3b 0a 09 69 66 20 28 63 68 64 69  ("/");..if (chdi
8410: 72 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09 09  r_ret != 0) {...
8420: 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09  return(1);..}...
8430: 66 6f 72 6b 5f 72 65 74 20 3d 20 66 6f 72 6b 28  fork_ret = fork(
8440: 29 3b 0a 09 69 66 20 28 66 6f 72 6b 5f 72 65 74  );..if (fork_ret
8450: 20 3c 20 30 29 20 7b 0a 09 09 72 65 74 75 72 6e   < 0) {...return
8460: 28 31 29 3b 0a 09 7d 0a 0a 09 69 66 20 28 66 6f  (1);..}...if (fo
8470: 72 6b 5f 72 65 74 20 3e 20 30 29 20 7b 0a 09 09  rk_ret > 0) {...
8480: 2f 2a 20 50 61 72 65 6e 74 20 2a 2f 0a 09 09 77  /* Parent */...w
8490: 61 69 74 70 69 64 28 66 6f 72 6b 5f 72 65 74 2c  aitpid(fork_ret,
84a0: 20 4e 55 4c 4c 2c 20 30 29 3b 0a 0a 09 09 65 78   NULL, 0);....ex
84b0: 69 74 28 45 58 49 54 5f 53 55 43 43 45 53 53 29  it(EXIT_SUCCESS)
84c0: 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 68 69 6c 64 20  ;..}.../* Child 
84d0: 2a 2f 0a 09 69 66 20 28 66 6f 72 6b 28 29 20 21  */..if (fork() !
84e0: 3d 20 30 29 20 7b 0a 09 09 2f 2a 20 43 68 69 6c  = 0) {.../* Chil
84f0: 64 20 2a 2f 0a 09 09 65 78 69 74 28 45 58 49 54  d */...exit(EXIT
8500: 5f 53 55 43 43 45 53 53 29 3b 0a 09 7d 0a 0a 09  _SUCCESS);..}...
8510: 2f 2a 20 47 72 61 6e 64 20 63 68 69 6c 64 20 2a  /* Grand child *
8520: 2f 0a 09 73 65 74 73 69 64 5f 72 65 74 20 3d 20  /..setsid_ret = 
8530: 73 65 74 73 69 64 28 29 3b 0a 09 69 66 20 28 73  setsid();..if (s
8540: 65 74 73 69 64 5f 72 65 74 20 3d 3d 20 28 28 70  etsid_ret == ((p
8550: 69 64 5f 74 29 20 2d 31 29 29 20 7b 0a 09 09 72  id_t) -1)) {...r
8560: 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 66  eturn(1);..}...f
8570: 64 5f 69 6e 20 3d 20 6f 70 65 6e 28 22 2f 64 65  d_in = open("/de
8580: 76 2f 6e 75 6c 6c 22 2c 20 4f 5f 52 44 4f 4e 4c  v/null", O_RDONL
8590: 59 29 3b 0a 09 66 64 5f 6f 75 74 20 3d 20 6f 70  Y);..fd_out = op
85a0: 65 6e 28 22 2f 64 65 76 2f 6e 75 6c 6c 22 2c 20  en("/dev/null", 
85b0: 4f 5f 57 52 4f 4e 4c 59 29 3b 0a 09 69 66 20 28  O_WRONLY);..if (
85c0: 66 64 5f 69 6e 20 3c 20 30 20 7c 7c 20 66 64 5f  fd_in < 0 || fd_
85d0: 6f 75 74 20 3c 20 30 29 20 7b 0a 09 09 72 65 74  out < 0) {...ret
85e0: 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 64 75 70  urn(1);..}...dup
85f0: 32 5f 72 65 74 20 3d 20 64 75 70 32 28 66 64 5f  2_ret = dup2(fd_
8600: 69 6e 2c 20 53 54 44 49 4e 5f 46 49 4c 45 4e 4f  in, STDIN_FILENO
8610: 29 3b 0a 09 69 66 20 28 64 75 70 32 5f 72 65 74  );..if (dup2_ret
8620: 20 21 3d 20 53 54 44 49 4e 5f 46 49 4c 45 4e 4f   != STDIN_FILENO
8630: 29 20 7b 0a 09 09 72 65 74 75 72 6e 28 31 29 3b  ) {...return(1);
8640: 0a 09 7d 0a 0a 09 64 75 70 32 5f 72 65 74 20 3d  ..}...dup2_ret =
8650: 20 64 75 70 32 28 66 64 5f 6f 75 74 2c 20 53 54   dup2(fd_out, ST
8660: 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69  DOUT_FILENO);..i
8670: 66 20 28 64 75 70 32 5f 72 65 74 20 21 3d 20 53  f (dup2_ret != S
8680: 54 44 4f 55 54 5f 46 49 4c 45 4e 4f 29 20 7b 0a  TDOUT_FILENO) {.
8690: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 7d 0a  ..return(1);..}.
86a0: 0a 09 64 75 70 32 5f 72 65 74 20 3d 20 64 75 70  ..dup2_ret = dup
86b0: 32 28 66 64 5f 6f 75 74 2c 20 53 54 44 45 52 52  2(fd_out, STDERR
86c0: 5f 46 49 4c 45 4e 4f 29 3b 0a 09 69 66 20 28 64  _FILENO);..if (d
86d0: 75 70 32 5f 72 65 74 20 21 3d 20 53 54 44 45 52  up2_ret != STDER
86e0: 52 5f 46 49 4c 45 4e 4f 29 20 7b 0a 09 09 72 65  R_FILENO) {...re
86f0: 74 75 72 6e 28 31 29 3b 0a 09 7d 0a 0a 09 63 6c  turn(1);..}...cl
8700: 6f 73 65 28 66 64 5f 69 6e 29 3b 0a 09 63 6c 6f  ose(fd_in);..clo
8710: 73 65 28 66 64 5f 6f 75 74 29 3b 0a 0a 09 72 65  se(fd_out);...re
8720: 74 75 72 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 20 52  turn(0);.}../* R
8730: 75 6e 20 70 72 6f 63 65 73 73 20 2a 2f 0a 69 6e  un process */.in
8740: 74 20 6d 61 69 6e 28 69 6e 74 20 61 72 67 63 2c  t main(int argc,
8750: 20 63 68 61 72 20 2a 2a 61 72 67 76 29 20 7b 0a   char **argv) {.
8760: 09 73 74 72 75 63 74 20 6f 70 74 69 6f 6e 20 6f  .struct option o
8770: 70 74 69 6f 6e 73 5b 31 32 5d 3b 0a 09 73 74 72  ptions[12];..str
8780: 75 63 74 20 66 69 6c 65 64 5f 6f 70 74 69 6f 6e  uct filed_option
8790: 73 20 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73  s thread_options
87a0: 3b 0a 09 63 6f 6e 73 74 20 63 68 61 72 20 2a 62  ;..const char *b
87b0: 69 6e 64 5f 61 64 64 72 20 3d 20 42 49 4e 44 5f  ind_addr = BIND_
87c0: 41 44 44 52 2c 20 2a 6e 65 77 72 6f 6f 74 20 3d  ADDR, *newroot =
87d0: 20 4e 55 4c 4c 2c 20 2a 6c 6f 67 5f 66 69 6c 65   NULL, *log_file
87e0: 20 3d 20 4c 4f 47 5f 46 49 4c 45 3b 0a 09 46 49   = LOG_FILE;..FI
87f0: 4c 45 20 2a 6c 6f 67 5f 66 70 3b 0a 09 75 69 64  LE *log_fp;..uid
8800: 5f 74 20 75 73 65 72 20 3d 20 30 3b 0a 09 69 6e  _t user = 0;..in
8810: 74 20 70 6f 72 74 20 3d 20 50 4f 52 54 2c 20 74  t port = PORT, t
8820: 68 72 65 61 64 5f 63 6f 75 6e 74 20 3d 20 54 48  hread_count = TH
8830: 52 45 41 44 5f 43 4f 55 4e 54 3b 0a 09 69 6e 74  READ_COUNT;..int
8840: 20 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 43 41   cache_size = CA
8850: 43 48 45 5f 53 49 5a 45 3b 0a 09 69 6e 74 20 69  CHE_SIZE;..int i
8860: 6e 69 74 5f 72 65 74 2c 20 63 68 72 6f 6f 74 5f  nit_ret, chroot_
8870: 72 65 74 2c 20 73 65 74 75 69 64 5f 72 65 74 2c  ret, setuid_ret,
8880: 20 6c 6f 6f 6b 75 70 5f 72 65 74 2c 20 63 68 64   lookup_ret, chd
8890: 69 72 5f 72 65 74 3b 0a 09 69 6e 74 20 73 65 74  ir_ret;..int set
88a0: 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d 20 30 2c  uid_enabled = 0,
88b0: 20 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20   daemon_enabled 
88c0: 3d 20 30 3b 0a 09 69 6e 74 20 63 68 3b 0a 09 69  = 0;..int ch;..i
88d0: 6e 74 20 66 64 3b 0a 0a 09 2f 2a 20 53 65 74 20  nt fd;.../* Set 
88e0: 64 65 66 61 75 6c 74 20 76 61 6c 75 65 73 20 2a  default values *
88f0: 2f 0a 09 74 68 72 65 61 64 5f 6f 70 74 69 6f 6e  /..thread_option
8900: 73 2e 76 68 6f 73 74 73 5f 65 6e 61 62 6c 65 64  s.vhosts_enabled
8910: 20 3d 20 30 3b 0a 0a 09 2f 2a 20 50 72 6f 63 65   = 0;.../* Proce
8920: 73 73 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  ss arguments */.
8930: 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c 6f  .filed_getopt_lo
8940: 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69 6f  ng_setopt(&optio
8950: 6e 73 5b 30 5d 2c 20 22 70 6f 72 74 22 2c 20 72  ns[0], "port", r
8960: 65 71 75 69 72 65 64 5f 61 72 67 75 6d 65 6e 74  equired_argument
8970: 2c 20 27 70 27 29 3b 0a 09 66 69 6c 65 64 5f 67  , 'p');..filed_g
8980: 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70  etopt_long_setop
8990: 74 28 26 6f 70 74 69 6f 6e 73 5b 31 5d 2c 20 22  t(&options[1], "
89a0: 74 68 72 65 61 64 73 22 2c 20 72 65 71 75 69 72  threads", requir
89b0: 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 74 27  ed_argument, 't'
89c0: 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74  );..filed_getopt
89d0: 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70  _long_setopt(&op
89e0: 74 69 6f 6e 73 5b 32 5d 2c 20 22 63 61 63 68 65  tions[2], "cache
89f0: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75  ", required_argu
8a00: 6d 65 6e 74 2c 20 27 63 27 29 3b 0a 09 66 69 6c  ment, 'c');..fil
8a10: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
8a20: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 33  etopt(&options[3
8a30: 5d 2c 20 22 62 69 6e 64 22 2c 20 72 65 71 75 69  ], "bind", requi
8a40: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 62  red_argument, 'b
8a50: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70  ');..filed_getop
8a60: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
8a70: 70 74 69 6f 6e 73 5b 34 5d 2c 20 22 75 73 65 72  ptions[4], "user
8a80: 22 2c 20 72 65 71 75 69 72 65 64 5f 61 72 67 75  ", required_argu
8a90: 6d 65 6e 74 2c 20 27 75 27 29 3b 0a 09 66 69 6c  ment, 'u');..fil
8aa0: 65 64 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73  ed_getopt_long_s
8ab0: 65 74 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 35  etopt(&options[5
8ac0: 5d 2c 20 22 72 6f 6f 74 22 2c 20 72 65 71 75 69  ], "root", requi
8ad0: 72 65 64 5f 61 72 67 75 6d 65 6e 74 2c 20 27 72  red_argument, 'r
8ae0: 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70  ');..filed_getop
8af0: 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f  t_long_setopt(&o
8b00: 70 74 69 6f 6e 73 5b 36 5d 2c 20 22 68 65 6c 70  ptions[6], "help
8b10: 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20  ", no_argument, 
8b20: 27 68 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74  'h');..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 37 5d 2c 20 22 64 61  &options[7], "da
8b50: 65 6d 6f 6e 22 2c 20 6e 6f 5f 61 72 67 75 6d 65  emon", no_argume
8b60: 6e 74 2c 20 27 64 27 29 3b 0a 09 66 69 6c 65 64  nt, 'd');..filed
8b70: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74  _getopt_long_set
8b80: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 38 5d 2c  opt(&options[8],
8b90: 20 22 6c 6f 67 22 2c 20 72 65 71 75 69 72 65 64   "log", required
8ba0: 5f 61 72 67 75 6d 65 6e 74 2c 20 27 6c 27 29 3b  _argument, 'l');
8bb0: 0a 09 66 69 6c 65 64 5f 67 65 74 6f 70 74 5f 6c  ..filed_getopt_l
8bc0: 6f 6e 67 5f 73 65 74 6f 70 74 28 26 6f 70 74 69  ong_setopt(&opti
8bd0: 6f 6e 73 5b 39 5d 2c 20 22 76 65 72 73 69 6f 6e  ons[9], "version
8be0: 22 2c 20 6e 6f 5f 61 72 67 75 6d 65 6e 74 2c 20  ", no_argument, 
8bf0: 27 76 27 29 3b 0a 09 66 69 6c 65 64 5f 67 65 74  'v');..filed_get
8c00: 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74 6f 70 74 28  opt_long_setopt(
8c10: 26 6f 70 74 69 6f 6e 73 5b 31 30 5d 2c 20 22 76  &options[10], "v
8c20: 68 6f 73 74 22 2c 20 6e 6f 5f 61 72 67 75 6d 65  host", no_argume
8c30: 6e 74 2c 20 27 56 27 29 3b 0a 09 66 69 6c 65 64  nt, 'V');..filed
8c40: 5f 67 65 74 6f 70 74 5f 6c 6f 6e 67 5f 73 65 74  _getopt_long_set
8c50: 6f 70 74 28 26 6f 70 74 69 6f 6e 73 5b 31 31 5d  opt(&options[11]
8c60: 2c 20 4e 55 4c 4c 2c 20 30 2c 20 30 29 3b 0a 09  , NULL, 0, 0);..
8c70: 77 68 69 6c 65 20 28 28 63 68 20 3d 20 67 65 74  while ((ch = get
8c80: 6f 70 74 5f 6c 6f 6e 67 28 61 72 67 63 2c 20 61  opt_long(argc, a
8c90: 72 67 76 2c 20 22 70 3a 74 3a 63 3a 62 3a 75 3a  rgv, "p:t:c:b:u:
8ca0: 72 3a 6c 3a 68 64 76 56 22 2c 20 6f 70 74 69 6f  r:l:hdvV", optio
8cb0: 6e 73 2c 20 4e 55 4c 4c 29 29 20 21 3d 20 2d 31  ns, NULL)) != -1
8cc0: 29 20 7b 0a 09 09 73 77 69 74 63 68 28 63 68 29  ) {...switch(ch)
8cd0: 20 7b 0a 09 09 09 63 61 73 65 20 27 70 27 3a 0a   {....case 'p':.
8ce0: 09 09 09 09 70 6f 72 74 20 3d 20 61 74 6f 69 28  ....port = atoi(
8cf0: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65  optarg);.....bre
8d00: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 74 27 3a  ak;....case 't':
8d10: 0a 09 09 09 09 74 68 72 65 61 64 5f 63 6f 75 6e  .....thread_coun
8d20: 74 20 3d 20 61 74 6f 69 28 6f 70 74 61 72 67 29  t = atoi(optarg)
8d30: 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09  ;.....break;....
8d40: 63 61 73 65 20 27 63 27 3a 0a 09 09 09 09 63 61  case 'c':.....ca
8d50: 63 68 65 5f 73 69 7a 65 20 3d 20 61 74 6f 69 28  che_size = atoi(
8d60: 6f 70 74 61 72 67 29 3b 0a 09 09 09 09 62 72 65  optarg);.....bre
8d70: 61 6b 3b 0a 09 09 09 63 61 73 65 20 27 62 27 3a  ak;....case 'b':
8d80: 0a 09 09 09 09 62 69 6e 64 5f 61 64 64 72 20 3d  .....bind_addr =
8d90: 20 73 74 72 64 75 70 28 6f 70 74 61 72 67 29 3b   strdup(optarg);
8da0: 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09 09 63  .....break;....c
8db0: 61 73 65 20 27 75 27 3a 0a 09 09 09 09 73 65 74  ase 'u':.....set
8dc0: 75 69 64 5f 65 6e 61 62 6c 65 64 20 3d 20 31 3b  uid_enabled = 1;
8dd0: 0a 09 09 09 09 6c 6f 6f 6b 75 70 5f 72 65 74 20  .....lookup_ret 
8de0: 3d 20 66 69 6c 65 64 5f 75 73 65 72 5f 6c 6f 6f  = filed_user_loo
8df0: 6b 75 70 28 6f 70 74 61 72 67 2c 20 26 75 73 65  kup(optarg, &use
8e00: 72 29 3b 0a 09 09 09 09 69 66 20 28 6c 6f 6f 6b  r);.....if (look
8e10: 75 70 5f 72 65 74 20 21 3d 20 30 29 20 7b 0a 09  up_ret != 0) {..
8e20: 09 09 09 09 66 69 6c 65 64 5f 70 72 69 6e 74 5f  ....filed_print_
8e30: 68 65 6c 70 28 73 74 64 65 72 72 2c 20 30 2c 20  help(stderr, 0, 
8e40: 22 49 6e 76 61 6c 69 64 20 75 73 65 72 6e 61 6d  "Invalid usernam
8e50: 65 20 73 70 65 63 69 66 69 65 64 22 29 3b 0a 0a  e specified");..
8e60: 09 09 09 09 09 72 65 74 75 72 6e 28 31 29 3b 0a  .....return(1);.
8e70: 09 09 09 09 7d 0a 09 09 09 09 62 72 65 61 6b 3b  ....}.....break;
8e80: 0a 09 09 09 63 61 73 65 20 27 72 27 3a 0a 09 09  ....case 'r':...
8e90: 09 09 6e 65 77 72 6f 6f 74 20 3d 20 73 74 72 64  ..newroot = strd
8ea0: 75 70 28 6f 70 74 61 72 67 29 3b 0a 09 09 09 09  up(optarg);.....
8eb0: 62 72 65 61 6b 3b 0a 09 09 09 63 61 73 65 20 27  break;....case '
8ec0: 6c 27 3a 0a 09 09 09 09 6c 6f 67 5f 66 69 6c 65  l':.....log_file
8ed0: 20 3d 20 73 74 72 64 75 70 28 6f 70 74 61 72 67   = strdup(optarg
8ee0: 29 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  );.....break;...
8ef0: 09 63 61 73 65 20 27 64 27 3a 0a 09 09 09 09 64  .case 'd':.....d
8f00: 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64 20 3d 20  aemon_enabled = 
8f10: 31 3b 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  1;.....break;...
8f20: 09 63 61 73 65 20 27 56 27 3a 0a 09 09 09 09 74  .case 'V':.....t
8f30: 68 72 65 61 64 5f 6f 70 74 69 6f 6e 73 2e 76 68  hread_options.vh
8f40: 6f 73 74 73 5f 65 6e 61 62 6c 65 64 20 3d 20 31  osts_enabled = 1
8f50: 3b 0a 0a 09 09 09 09 62 72 65 61 6b 3b 0a 09 09  ;......break;...
8f60: 09 63 61 73 65 20 27 76 27 3a 0a 09 09 09 09 70  .case 'v':.....p
8f70: 72 69 6e 74 66 28 22 66 69 6c 65 64 20 76 65 72  rintf("filed ver
8f80: 73 69 6f 6e 20 25 73 5c 6e 22 2c 20 46 49 4c 45  sion %s\n", FILE
8f90: 44 5f 56 45 52 53 49 4f 4e 29 3b 0a 0a 09 09 09  D_VERSION);.....
8fa0: 09 72 65 74 75 72 6e 28 30 29 3b 0a 09 09 09 63  .return(0);....c
8fb0: 61 73 65 20 27 3f 27 3a 0a 09 09 09 63 61 73 65  ase '?':....case
8fc0: 20 27 3a 27 3a 0a 09 09 09 09 66 69 6c 65 64 5f   ':':.....filed_
8fd0: 70 72 69 6e 74 5f 68 65 6c 70 28 73 74 64 65 72  print_help(stder
8fe0: 72 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 0a 09 09  r, 0, NULL);....
8ff0: 09 09 72 65 74 75 72 6e 28 31 29 3b 0a 09 09 09  ..return(1);....
9000: 63 61 73 65 20 27 68 27 3a 0a 09 09 09 09 66 69  case 'h':.....fi
9010: 6c 65 64 5f 70 72 69 6e 74 5f 68 65 6c 70 28 73  led_print_help(s
9020: 74 64 6f 75 74 2c 20 31 2c 20 4e 55 4c 4c 29 3b  tdout, 1, NULL);
9030: 0a 0a 09 09 09 09 72 65 74 75 72 6e 28 30 29 3b  ......return(0);
9040: 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a 20 4f 70 65  ...}..}.../* Ope
9050: 6e 20 6c 6f 67 20 66 69 6c 65 20 2a 2f 0a 09 6c  n log file */..l
9060: 6f 67 5f 66 70 20 3d 20 66 69 6c 65 64 5f 6c 6f  og_fp = filed_lo
9070: 67 5f 6f 70 65 6e 28 6c 6f 67 5f 66 69 6c 65 29  g_open(log_file)
9080: 3b 0a 09 69 66 20 28 6c 6f 67 5f 66 70 20 3d 3d  ;..if (log_fp ==
9090: 20 4e 55 4c 4c 29 20 7b 0a 09 09 70 65 72 72 6f   NULL) {...perro
90a0: 72 28 22 66 69 6c 65 64 5f 6c 6f 67 5f 6f 70 65  r("filed_log_ope
90b0: 6e 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 34  n");....return(4
90c0: 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 43 72 65 61 74  );..}.../* Creat
90d0: 65 20 6c 69 73 74 65 6e 69 6e 67 20 73 6f 63 6b  e listening sock
90e0: 65 74 20 2a 2f 0a 09 66 64 20 3d 20 66 69 6c 65  et */..fd = file
90f0: 64 5f 6c 69 73 74 65 6e 28 62 69 6e 64 5f 61 64  d_listen(bind_ad
9100: 64 72 2c 20 70 6f 72 74 29 3b 0a 09 69 66 20 28  dr, port);..if (
9110: 66 64 20 3c 20 30 29 20 7b 0a 09 09 70 65 72 72  fd < 0) {...perr
9120: 6f 72 28 22 66 69 6c 65 64 5f 6c 69 73 74 65 6e  or("filed_listen
9130: 22 29 3b 0a 0a 09 09 72 65 74 75 72 6e 28 31 29  ");....return(1)
9140: 3b 0a 09 7d 0a 0a 09 2f 2a 20 42 65 63 6f 6d 65  ;..}.../* Become
9150: 20 61 20 64 61 65 6d 6f 6e 20 2a 2f 0a 09 69 66   a daemon */..if
9160: 20 28 64 61 65 6d 6f 6e 5f 65 6e 61 62 6c 65 64   (daemon_enabled
9170: 29 20 7b 0a 09 09 69 6e 69 74 5f 72 65 74 20 3d  ) {...init_ret =
9180: 20 66 69 6c 65 64 5f 64 61 65 6d 6f 6e 69 7a 65   filed_daemonize
9190: 28 29 3b 0a 09 09 69 66 20 28 69 6e 69 74 5f 72  ();...if (init_r
91a0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 09 70 65  et != 0) {....pe
91b0: 72 72 6f 72 28 22 66 69 6c 65 64 5f 64 61 65 6d  rror("filed_daem
91c0: 6f 6e 69 7a 65 22 29 3b 0a 0a 09 09 09 72 65 74  onize");.....ret
91d0: 75 72 6e 28 36 29 3b 0a 09 09 7d 0a 09 7d 0a 0a  urn(6);...}..}..
91e0: 09 2f 2a 20 43 68 72 6f 6f 74 2c 20 69 66 20 61  ./* Chroot, if a
91f0: 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 09 69  ppropriate */..i
9200: 66 20 28 6e 65 77 72 6f 6f 74 29 20 7b 0a 09 09  f (newroot) {...
9210: 63 68 64 69 72 5f 72 65 74 20 3d 20 63 68 64 69  chdir_ret = chdi
9220: 72 28 6e 65 77 72 6f 6f 74 29 3b 0a 09 09 69 66  r(newroot);...if
9230: 20 28 63 68 64 69 72 5f 72 65 74 20 21 3d 20 30   (chdir_ret != 0
9240: 29 20 7b 0a 09 09 09 70 65 72 72 6f 72 28 22 63  ) {....perror("c
9250: 68 64 69 72 22 29 3b 0a 0a 09 09 09 72 65 74 75  hdir");.....retu
9260: 72 6e 28 31 29 3b 0a 09 09 7d 0a 0a 09 09 63 68  rn(1);...}....ch
9270: 72 6f 6f 74 5f 72 65 74 20 3d 20 63 68 72 6f 6f  root_ret = chroo
9280: 74 28 22 2e 22 29 3b 0a 09 09 69 66 20 28 63 68  t(".");...if (ch
9290: 72 6f 6f 74 5f 72 65 74 20 21 3d 20 30 29 20 7b  root_ret != 0) {
92a0: 0a 09 09 09 70 65 72 72 6f 72 28 22 63 68 72 6f  ....perror("chro
92b0: 6f 74 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e  ot");.....return
92c0: 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a  (1);...}..}.../*
92d0: 20 44 72 6f 70 20 70 72 69 76 69 6c 65 67 65 73   Drop privileges
92e0: 2c 20 69 66 20 61 70 70 72 6f 70 72 69 61 74 65  , if appropriate
92f0: 20 2a 2f 0a 09 69 66 20 28 73 65 74 75 69 64 5f   */..if (setuid_
9300: 65 6e 61 62 6c 65 64 29 20 7b 0a 09 09 73 65 74  enabled) {...set
9310: 75 69 64 5f 72 65 74 20 3d 20 73 65 74 75 69 64  uid_ret = setuid
9320: 28 75 73 65 72 29 3b 0a 09 09 69 66 20 28 73 65  (user);...if (se
9330: 74 75 69 64 5f 72 65 74 20 21 3d 20 30 29 20 7b  tuid_ret != 0) {
9340: 0a 09 09 09 70 65 72 72 6f 72 28 22 73 65 74 75  ....perror("setu
9350: 69 64 22 29 3b 0a 0a 09 09 09 72 65 74 75 72 6e  id");.....return
9360: 28 31 29 3b 0a 09 09 7d 0a 09 7d 0a 0a 09 2f 2a  (1);...}..}.../*
9370: 20 49 6e 69 74 69 61 6c 69 7a 65 20 2a 2f 0a 09   Initialize */..
9380: 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65 64  init_ret = filed
9390: 5f 69 6e 69 74 28 63 61 63 68 65 5f 73 69 7a 65  _init(cache_size
93a0: 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72 65 74  );..if (init_ret
93b0: 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72 72 6f   != 0) {...perro
93c0: 72 28 22 66 69 6c 65 64 5f 69 6e 69 74 22 29 3b  r("filed_init");
93d0: 0a 0a 09 09 72 65 74 75 72 6e 28 33 29 3b 0a 09  ....return(3);..
93e0: 7d 0a 0a 09 2f 2a 20 43 72 65 61 74 65 20 6c 6f  }.../* Create lo
93f0: 67 67 69 6e 67 20 74 68 72 65 61 64 20 2a 2f 0a  gging thread */.
9400: 09 69 6e 69 74 5f 72 65 74 20 3d 20 66 69 6c 65  .init_ret = file
9410: 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72 65 61 64  d_logging_thread
9420: 5f 69 6e 69 74 28 6c 6f 67 5f 66 70 29 3b 0a 09  _init(log_fp);..
9430: 69 66 20 28 69 6e 69 74 5f 72 65 74 20 21 3d 20  if (init_ret != 
9440: 30 29 20 7b 0a 09 09 70 65 72 72 6f 72 28 22 66  0) {...perror("f
9450: 69 6c 65 64 5f 6c 6f 67 67 69 6e 67 5f 74 68 72  iled_logging_thr
9460: 65 61 64 5f 69 6e 69 74 22 29 3b 0a 0a 09 09 72  ead_init");....r
9470: 65 74 75 72 6e 28 34 29 3b 0a 09 7d 0a 0a 09 2f  eturn(4);..}.../
9480: 2a 20 43 72 65 61 74 65 20 77 6f 72 6b 65 72 20  * Create worker 
9490: 74 68 72 65 61 64 73 20 2a 2f 0a 09 69 6e 69 74  threads */..init
94a0: 5f 72 65 74 20 3d 20 66 69 6c 65 64 5f 77 6f 72  _ret = filed_wor
94b0: 6b 65 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74  ker_threads_init
94c0: 28 66 64 2c 20 74 68 72 65 61 64 5f 63 6f 75 6e  (fd, thread_coun
94d0: 74 2c 20 26 74 68 72 65 61 64 5f 6f 70 74 69 6f  t, &thread_optio
94e0: 6e 73 29 3b 0a 09 69 66 20 28 69 6e 69 74 5f 72  ns);..if (init_r
94f0: 65 74 20 21 3d 20 30 29 20 7b 0a 09 09 70 65 72  et != 0) {...per
9500: 72 6f 72 28 22 66 69 6c 65 64 5f 77 6f 72 6b 65  ror("filed_worke
9510: 72 5f 74 68 72 65 61 64 73 5f 69 6e 69 74 22 29  r_threads_init")
9520: 3b 0a 0a 09 09 72 65 74 75 72 6e 28 35 29 3b 0a  ;....return(5);.
9530: 09 7d 0a 0a 09 2f 2a 20 57 61 69 74 20 66 6f 72  .}.../* Wait for
9540: 20 74 68 72 65 61 64 73 20 74 6f 20 65 78 69 74   threads to exit
9550: 20 2a 2f 0a 09 2f 2a 20 58 58 58 3a 54 4f 44 4f   */../* XXX:TODO
9560: 3a 20 4d 6f 6e 69 74 6f 72 20 74 68 72 65 61 64  : Monitor thread
9570: 20 75 73 61 67 65 20 2a 2f 0a 09 77 68 69 6c 65   usage */..while
9580: 20 28 31 29 20 7b 0a 09 09 73 6c 65 65 70 28 36   (1) {...sleep(6
9590: 30 29 3b 0a 09 7d 0a 0a 09 2f 2a 20 52 65 74 75  0);..}.../* Retu
95a0: 72 6e 20 69 6e 20 66 61 69 6c 75 72 65 20 2a 2f  rn in failure */
95b0: 0a 09 72 65 74 75 72 6e 28 32 29 3b 0a 7d 0a     ..return(2);.}.