Hex Artifact Content

Artifact 39436c7686689e29110e6452774107a498a61cfc:


0000: 66 69 6c 65 64 0a 3d 3d 3d 3d 3d 0a 0a 49 6e 74  filed.=====..Int
0010: 72 6f 64 75 63 74 69 6f 6e 0a 2d 2d 2d 2d 2d 2d  roduction.------
0020: 2d 2d 2d 2d 2d 2d 0a 22 66 69 6c 65 64 22 20 69  ------."filed" i
0030: 73 20 61 20 73 69 6d 70 6c 65 20 48 54 54 50 20  s a simple HTTP 
0040: 73 65 72 76 65 72 20 66 6f 72 20 73 65 72 76 69  server for servi
0050: 6e 67 20 6c 6f 63 61 6c 20 73 74 61 74 69 63 20  ng local static 
0060: 66 69 6c 65 73 20 6f 76 65 72 20 48 54 54 50 20  files over HTTP 
0070: 66 72 6f 6d 0a 4c 69 6e 75 78 2e 20 49 74 20 64  from.Linux. It d
0080: 6f 65 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d  oes the least am
0090: 6f 75 6e 74 20 6f 66 20 65 66 66 6f 72 74 20 70  ount of effort p
00a0: 6f 73 73 69 62 6c 65 20 74 6f 20 67 65 74 20 74  ossible to get t
00b0: 6f 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 0a 68  o the point of.h
00c0: 61 6e 64 69 6e 67 20 74 68 65 20 61 63 74 75 61  anding the actua
00d0: 6c 20 74 72 61 6e 73 66 65 72 20 6f 76 65 72 20  l transfer over 
00e0: 74 6f 20 74 68 65 20 6b 65 72 6e 65 6c 2e 0a 0a  to the kernel...
00f0: 49 74 20 69 73 20 6d 75 6c 74 69 74 68 72 65 61  It is multithrea
0100: 64 65 64 20 77 68 65 72 65 20 65 76 65 72 79 20  ded where every 
0110: 74 68 72 65 61 64 20 73 65 72 76 69 63 65 73 20  thread services 
0120: 61 20 73 69 6e 67 6c 65 20 63 6f 6e 63 75 72 72  a single concurr
0130: 65 6e 74 20 63 6c 69 65 6e 74 2e 0a 49 74 20 61  ent client..It a
0140: 74 74 65 6d 70 74 73 20 74 6f 20 72 65 64 75 63  ttempts to reduc
0150: 65 20 6c 61 74 65 6e 63 79 20 62 79 20 63 61 63  e latency by cac
0160: 68 69 6e 67 20 6f 70 65 6e 20 66 69 6c 65 20 64  hing open file d
0170: 65 73 63 72 69 70 74 6f 72 73 20 61 73 20 77 65  escriptors as we
0180: 6c 6c 2e 0a 0a 49 74 20 68 61 73 20 6e 6f 20 63  ll...It has no c
0190: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 69 6c  onfiguration fil
01a0: 65 20 61 6e 64 20 73 75 70 70 6f 72 74 73 20 6f  e and supports o
01b0: 6e 6c 79 20 6d 69 6e 69 6d 61 6c 20 63 6f 6e 66  nly minimal conf
01c0: 69 67 75 72 61 74 69 6f 6e 2e 0a 4f 70 65 72 61  iguration..Opera
01d0: 74 69 6f 6e 20 69 73 20 64 65 73 63 72 69 62 65  tion is describe
01e0: 64 20 69 6e 20 74 68 65 20 6d 61 6e 75 61 6c 20  d in the manual 
01f0: 70 61 67 65 2e 0a 0a 55 73 61 67 65 0a 2d 2d 2d  page...Usage.---
0200: 2d 2d 0a 54 68 65 20 73 69 6d 70 6c 65 73 74 20  --.The simplest 
0210: 75 73 61 67 65 20 6f 66 20 22 66 69 6c 65 64 22  usage of "filed"
0220: 20 69 73 20 74 6f 20 72 75 6e 20 69 74 20 77 69   is to run it wi
0230: 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73 2e  th no arguments.
0240: 20 20 54 68 69 73 20 77 69 6c 6c 20 73 74 61 72    This will star
0250: 74 0a 75 70 20 61 6e 20 48 54 54 50 20 73 65 72  t.up an HTTP ser
0260: 76 65 72 20 6c 69 73 74 65 6e 69 6e 67 20 6f 6e  ver listening on
0270: 20 70 6f 72 74 20 38 30 20 61 6e 64 20 73 68 61   port 80 and sha
0280: 72 69 6e 67 20 6f 75 74 20 74 68 65 20 72 6f 6f  ring out the roo
0290: 74 20 66 69 6c 65 73 79 73 74 65 6d 20 66 6f 72  t filesystem for
02a0: 0a 74 68 65 20 73 79 73 74 65 6d 20 61 73 20 74  .the system as t
02b0: 68 65 20 63 75 72 72 65 6e 74 20 75 73 65 72 2e  he current user.
02c0: 20 20 59 6f 75 20 70 72 6f 62 61 62 6c 79 20 64    You probably d
02d0: 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 73 68  o not want to sh
02e0: 61 72 65 20 6f 75 74 20 79 6f 75 72 0a 72 6f 6f  are out your.roo
02f0: 74 20 66 69 6c 65 73 79 73 74 65 6d 20 73 69 6e  t filesystem sin
0300: 63 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78 70  ce that will exp
0310: 6f 73 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ose information 
0320: 73 75 63 68 20 61 73 20 22 2f 65 74 63 2f 70 61  such as "/etc/pa
0330: 73 73 77 64 22 20 74 6f 0a 61 6e 6f 6e 79 6d 6f  sswd" to.anonymo
0340: 75 73 20 61 63 63 65 73 73 2e 20 20 59 6f 75 20  us access.  You 
0350: 70 72 6f 62 61 62 6c 79 20 61 6c 73 6f 20 64 6f  probably also do
0360: 6e 27 74 20 77 61 6e 74 20 74 6f 20 64 6f 20 74  n't want to do t
0370: 68 61 74 20 61 73 20 72 6f 6f 74 20 73 69 6e 63  hat as root sinc
0380: 65 20 74 68 65 6e 0a 73 65 6e 73 69 74 69 76 65  e then.sensitive
0390: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75 63   information suc
03a0: 68 20 61 73 20 22 2f 65 74 63 2f 73 68 61 64 6f  h as "/etc/shado
03b0: 77 22 20 63 6f 75 6c 64 20 62 65 20 75 73 65 64  w" could be used
03c0: 20 74 6f 20 63 6f 6d 70 72 6f 6d 69 73 65 20 74   to compromise t
03d0: 68 65 0a 73 79 73 74 65 6d 2e 0a 0a 54 68 65 72  he.system...Ther
03e0: 65 66 6f 72 65 2c 20 74 68 65 20 22 2d 2d 75 73  efore, the "--us
03f0: 65 72 22 20 61 6e 64 20 22 2d 2d 72 6f 6f 74 22  er" and "--root"
0400: 20 6f 70 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20   options should 
0410: 62 65 20 75 73 65 64 20 74 6f 20 73 70 65 63 69  be used to speci
0420: 66 79 20 61 20 75 73 65 72 0a 66 6f 72 20 22 66  fy a user.for "f
0430: 69 6c 65 64 22 20 74 6f 20 72 75 6e 20 61 73 20  iled" to run as 
0440: 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 61  in addition to a
0450: 20 64 69 72 65 63 74 6f 72 79 20 74 6f 20 63 68   directory to ch
0460: 61 6e 67 65 20 72 6f 6f 74 20 28 63 68 72 6f 6f  ange root (chroo
0470: 74 28 32 29 29 20 74 6f 0a 70 72 69 6f 72 20 74  t(2)) to.prior t
0480: 6f 20 73 65 72 76 69 6e 67 20 6f 75 74 20 66 69  o serving out fi
0490: 6c 65 73 2e 0a 0a 45 78 61 6d 70 6c 65 20 31 2c  les...Example 1,
04a0: 20 69 6e 73 65 63 75 72 65 20 66 69 6c 65 20 73   insecure file s
04b0: 68 61 72 69 6e 67 3a 0a 09 24 20 66 69 6c 65 64  haring:..$ filed
04c0: 0a 0a 45 78 61 6d 70 6c 65 20 32 2c 20 73 68 61  ..Example 2, sha
04d0: 72 69 6e 67 20 73 65 6e 73 69 74 69 76 65 20 69  ring sensitive i
04e0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 09 23 20 66  nformation:..# f
04f0: 69 6c 65 64 0a 0a 45 78 61 6d 70 6c 65 20 33 2c  iled..Example 3,
0500: 20 73 68 61 72 69 6e 67 20 6f 6e 6c 79 20 61 20   sharing only a 
0510: 73 70 65 63 69 66 69 63 20 64 69 72 65 63 74 6f  specific directo
0520: 72 79 3a 0a 09 23 20 66 69 6c 65 64 20 2d 2d 72  ry:..# filed --r
0530: 6f 6f 74 20 2f 77 77 77 20 2d 2d 75 73 65 72 20  oot /www --user 
0540: 6e 6f 62 6f 64 79 0a 0a 45 78 61 6d 70 6c 65 20  nobody..Example 
0550: 34 2c 20 72 75 6e 6e 69 6e 67 20 61 73 20 61 20  4, running as a 
0560: 64 61 65 6d 6f 6e 3a 0a 09 23 20 66 69 6c 65 64  daemon:..# filed
0570: 20 2d 2d 72 6f 6f 74 20 2f 77 77 77 20 2d 2d 75   --root /www --u
0580: 73 65 72 20 6e 6f 62 6f 64 79 20 2d 2d 64 61 65  ser nobody --dae
0590: 6d 6f 6e 0a 0a 42 75 69 6c 64 20 54 69 6d 65 20  mon..Build Time 
05a0: 43 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73 0a 2d  Considerations.-
05b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
05c0: 2d 2d 2d 2d 2d 2d 2d 2d 0a 49 6e 20 67 65 6e 65  --------.In gene
05d0: 72 61 6c 2c 20 69 74 20 69 73 20 62 65 73 74 20  ral, it is best 
05e0: 74 6f 20 6c 65 61 76 65 20 61 6c 6c 20 63 6f 6e  to leave all con
05f0: 66 69 67 75 72 61 74 69 6f 6e 20 74 6f 20 72 75  figuration to ru
0600: 6e 2d 74 69 6d 65 20 73 6f 20 74 68 61 74 20 61  n-time so that a
0610: 0a 63 6f 6d 70 69 6c 65 64 20 62 69 6e 61 72 79  .compiled binary
0620: 20 63 61 6e 20 64 6f 20 61 73 20 6d 75 63 68 20   can do as much 
0630: 61 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68  as possible with
0640: 6f 75 74 20 6e 65 65 64 69 6e 67 20 74 6f 20 62  out needing to b
0650: 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e 0a 48 6f  e recompiled..Ho
0660: 77 65 76 65 72 2c 20 74 68 65 72 65 20 61 72 65  wever, there are
0670: 20 73 6f 6d 65 20 74 68 69 6e 67 73 20 74 68 61   some things tha
0680: 74 20 61 72 65 20 62 65 73 74 20 64 6f 6e 65 20  t are best done 
0690: 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
06a0: 66 6f 72 0a 76 61 72 69 6f 75 73 20 72 65 61 73  for.various reas
06b0: 6f 6e 73 2c 20 65 73 70 65 63 69 61 6c 6c 79 20  ons, especially 
06c0: 69 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 70  in the name of p
06d0: 65 72 66 6f 72 6d 61 6e 63 65 20 6f 72 20 65 78  erformance or ex
06e0: 65 63 75 74 61 62 6c 65 20 73 69 7a 65 2e 0a 0a  ecutable size...
06f0: 66 69 6c 65 64 20 61 64 6d 69 74 73 20 74 68 6f  filed admits tho
0700: 73 65 20 74 72 61 64 65 2d 6f 66 66 73 20 61 72  se trade-offs ar
0710: 65 20 6f 63 63 61 73 69 6f 6e 61 6c 6c 79 20 72  e occasionally r
0720: 65 71 75 69 72 65 64 20 61 6e 64 20 6f 66 66 65  equired and offe
0730: 72 73 20 74 68 65 0a 66 6f 6c 6c 6f 77 69 6e 67  rs the.following
0740: 20 74 75 6e 61 62 6c 65 73 20 74 68 61 74 20 63   tunables that c
0750: 61 6e 20 6f 6e 6c 79 20 62 65 20 74 6f 67 67 6c  an only be toggl
0760: 65 64 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c  ed during compil
0770: 65 2d 74 69 6d 65 3a 0a 0a 20 20 20 31 2e 20 4c  e-time:..   1. L
0780: 6f 67 67 69 6e 67 20 28 43 46 4c 41 47 53 2c 20  ogging (CFLAGS, 
0790: 2d 44 46 49 4c 45 44 5f 44 4f 4e 54 5f 4c 4f 47  -DFILED_DONT_LOG
07a0: 3d 31 29 0a 09 49 74 20 69 73 20 70 6f 73 73 69  =1)..It is possi
07b0: 62 6c 65 20 74 6f 20 64 69 73 61 62 6c 65 20 41  ble to disable A
07c0: 4c 4c 20 6c 6f 67 67 69 6e 67 20 66 72 6f 6d 20  LL logging from 
07d0: 66 69 6c 65 64 2e 20 20 57 68 65 6e 20 6c 6f 67  filed.  When log
07e0: 67 69 6e 67 20 69 73 0a 09 63 6f 6d 70 6c 65 74  ging is..complet
07f0: 65 6c 79 20 64 69 73 61 62 6c 65 64 20 69 6e 74  ely disabled int
0800: 65 72 6c 6f 63 6b 73 20 28 6d 75 74 65 78 65 73  erlocks (mutexes
0810: 29 20 66 6f 72 20 74 68 65 20 6c 6f 67 67 69 6e  ) for the loggin
0820: 67 20 70 6f 69 6e 74 65 72 20 61 72 65 0a 09 6e  g pointer are..n
0830: 6f 74 20 65 6e 67 61 67 65 64 20 61 6e 64 20 74  ot engaged and t
0840: 68 65 20 6c 6f 67 67 69 6e 67 20 66 75 6e 63 74  he logging funct
0850: 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 63 6f 6d  ions are not com
0860: 70 69 6c 65 64 20 61 74 20 61 6c 6c 2e 0a 09 54  piled at all...T
0870: 68 69 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61  his results in a
0880: 20 73 6c 69 67 68 74 6c 79 20 73 6d 61 6c 6c 65   slightly smalle
0890: 72 20 61 6e 64 20 66 61 73 74 65 72 20 62 69 6e  r and faster bin
08a0: 61 72 79 0a 0a 20 20 20 32 2e 20 44 65 62 75 67  ary..   2. Debug
08b0: 67 69 6e 67 20 28 43 46 4c 41 47 53 2c 20 2d 44  ging (CFLAGS, -D
08c0: 46 49 4c 45 44 5f 44 45 42 55 47 3d 31 29 0a 09  FILED_DEBUG=1)..
08d0: 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72  This is an inter
08e0: 6e 61 6c 20 6f 70 74 69 6f 6e 20 61 6e 64 20 73  nal option and s
08f0: 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 75 73  hould only be us
0900: 65 64 20 64 75 72 69 6e 67 20 64 65 76 65 6c 6f  ed during develo
0910: 70 6d 65 6e 74 2e 0a 0a 20 20 20 33 2e 20 44 69  pment...   3. Di
0920: 66 66 65 72 69 6e 67 20 48 54 54 50 20 73 65 6d  ffering HTTP sem
0930: 61 6e 74 69 63 73 20 28 43 46 4c 41 47 53 2c 20  antics (CFLAGS, 
0940: 2d 44 46 49 4c 45 44 5f 4e 4f 4e 42 4c 4f 43 4b  -DFILED_NONBLOCK
0950: 5f 48 54 54 50 3d 31 29 0a 09 49 74 20 69 73 20  _HTTP=1)..It is 
0960: 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20 73 6f  possible that so
0970: 6d 65 20 48 54 54 50 20 63 6c 69 65 6e 74 73 20  me HTTP clients 
0980: 6d 61 79 20 6e 6f 74 20 70 72 6f 63 65 73 73 20  may not process 
0990: 74 68 65 20 48 54 54 50 20 73 74 72 65 61 6d 0a  the HTTP stream.
09a0: 09 62 65 69 6e 67 20 64 65 6c 69 76 65 72 65 64  .being delivered
09b0: 20 69 66 20 74 68 65 79 20 63 61 6e 6e 6f 74 20   if they cannot 
09c0: 77 72 69 74 65 20 74 6f 20 74 68 65 20 48 54 54  write to the HTT
09d0: 50 20 73 74 72 65 61 6d 20 69 74 73 65 6c 66 2e  P stream itself.
09e0: 20 20 54 68 69 73 0a 09 68 61 73 20 6e 6f 74 20    This..has not 
09f0: 62 65 65 6e 20 6f 62 73 65 72 76 65 64 20 79 65  been observed ye
0a00: 74 2c 20 62 75 74 20 69 74 20 69 73 20 70 6f 73  t, but it is pos
0a10: 73 69 62 6c 65 2e 20 20 49 66 20 74 68 65 73 65  sible.  If these
0a20: 20 73 65 6d 61 6e 74 69 63 73 20 61 72 65 0a 09   semantics are..
0a30: 6e 65 65 64 65 64 20 28 61 6e 64 20 74 68 65 79  needed (and they
0a40: 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 29 20   should not be) 
0a50: 74 68 65 6e 20 74 68 65 79 20 63 61 6e 20 62 65  then they can be
0a60: 20 65 6e 61 62 6c 65 64 20 77 69 74 68 20 74 68   enabled with th
0a70: 69 73 0a 09 66 6c 61 67 20 61 74 20 74 68 65 20  is..flag at the 
0a80: 63 6f 73 74 20 6f 66 20 70 65 72 66 6f 72 6d 61  cost of performa
0a90: 6e 63 65 0a 0a 20 20 20 34 2e 20 4d 49 4d 45 20  nce..   4. MIME 
0aa0: 54 79 70 65 73 20 28 4d 49 4d 45 54 59 50 45 53  Types (MIMETYPES
0ab0: 29 0a 09 46 6f 72 20 73 69 6e 67 6c 65 2d 66 69  )..For single-fi
0ac0: 6c 65 20 63 6f 6e 76 69 65 6e 63 65 20 22 66 69  le convience "fi
0ad0: 6c 65 64 22 20 63 6f 6d 70 69 6c 65 73 20 74 68  led" compiles th
0ae0: 65 20 6d 61 70 70 69 6e 67 20 6f 66 20 66 69 6c  e mapping of fil
0af0: 65 0a 09 65 78 74 65 6e 73 69 6f 6e 73 20 28 74  e..extensions (t
0b00: 68 65 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65  he string in the
0b10: 20 66 69 6c 65 6e 61 6d 65 20 66 6f 6c 6c 6f 77   filename follow
0b20: 69 6e 67 20 69 74 73 20 6c 61 73 74 20 64 6f 74  ing its last dot
0b30: 20 28 22 2e 22 29 29 0a 09 69 6e 74 6f 20 74 68   ("."))..into th
0b40: 65 20 65 78 65 63 75 74 61 62 6c 65 2e 20 20 54  e executable.  T
0b50: 68 69 73 20 6d 61 70 70 69 6e 67 20 63 6f 6d 65  his mapping come
0b60: 73 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e  s from a file in
0b70: 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 0a 09   the format of..
0b80: 09 74 79 70 65 31 20 20 20 74 79 70 65 31 5f 65  .type1   type1_e
0b90: 78 74 65 6e 73 69 6f 6e 31 20 74 79 70 65 31 5f  xtension1 type1_
0ba0: 65 78 74 65 6e 73 69 6f 6e 32 2e 2e 2e 0a 09 09  extension2......
0bb0: 74 79 70 65 32 20 20 20 74 79 70 65 32 5f 65 78  type2   type2_ex
0bc0: 74 65 6e 73 69 6f 6e 31 20 74 79 70 65 32 5f 65  tension1 type2_e
0bd0: 78 74 65 6e 73 69 6f 6e 32 2e 2e 2e 0a 09 09 2e  xtension2.......
0be0: 2e 2e 0a 09 48 6f 77 65 76 65 72 20 69 74 20 6d  ....However it m
0bf0: 61 79 20 6e 6f 74 20 62 65 20 64 65 73 69 72 61  ay not be desira
0c00: 62 6c 65 20 74 6f 20 69 6e 63 6c 75 64 65 20 74  ble to include t
0c10: 68 69 73 20 6d 61 70 70 69 6e 67 2c 20 6f 72 20  his mapping, or 
0c20: 69 74 20 6d 61 79 20 62 65 0a 09 64 65 73 69 72  it may be..desir
0c30: 61 62 6c 65 20 74 6f 20 75 73 65 20 79 6f 75 72  able to use your
0c40: 20 6f 77 6e 20 6d 61 70 70 69 6e 67 20 72 61 74   own mapping rat
0c50: 68 65 72 20 74 68 61 6e 20 74 68 65 20 64 65 66  her than the def
0c60: 61 75 6c 74 20 6f 6e 65 2e 20 20 54 68 69 73 0a  ault one.  This.
0c70: 09 63 61 6e 20 62 65 20 64 6f 6e 65 20 62 79 20  .can be done by 
0c80: 73 70 65 63 69 66 79 69 6e 67 20 74 68 65 20 4d  specifying the M
0c90: 49 4d 45 54 59 50 45 53 20 6d 61 63 72 6f 20 74  IMETYPES macro t
0ca0: 6f 20 22 6d 61 6b 65 22 2e 20 20 49 66 20 6e 6f  o "make".  If no
0cb0: 0a 09 6d 61 70 70 69 6e 67 20 69 73 20 64 65 73  ..mapping is des
0cc0: 69 72 65 64 2c 20 22 2f 64 65 76 2f 6e 75 6c 6c  ired, "/dev/null
0cd0: 22 20 6d 61 79 20 62 65 20 73 70 65 63 69 66 69  " may be specifi
0ce0: 65 64 2e 0a                                      ed..