1
0

FileServer.st 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. Object subclass: #FileServer
  2. instanceVariableNames: 'path http fs url port basePath util'
  3. package: 'FileServer'!
  4. !FileServer methodsFor: 'accessing'!
  5. basePath
  6. ^basePath ifNil: ['./']
  7. !
  8. basePath: aString
  9. basePath := aString
  10. !
  11. port
  12. ^self class port
  13. ! !
  14. !FileServer methodsFor: 'initialization'!
  15. initialize
  16. super initialize.
  17. path := self require: 'path'.
  18. http := self require: 'http'.
  19. fs := self require: 'fs'.
  20. util := self require: 'util'.
  21. url := self require: 'url'
  22. !
  23. checkDirectoryLayout
  24. (path existsSync: self basePath, 'index.html') ifFalse: [
  25. console warn: 'Warning: project directory does not contain index.html'].
  26. (path existsSync: self basePath, 'st') ifFalse: [
  27. console warn: 'Warning: project directory is missing an "st" directory'].
  28. (path existsSync: self basePath, 'js') ifFalse: [
  29. console warn: 'Warning: roject directory is missing a "js" directory'].
  30. ! !
  31. !FileServer methodsFor: 'private'!
  32. require: aModuleString
  33. "call to the require function"
  34. ^require value: aModuleString
  35. !
  36. writeData: data toFileNamed: aFilename
  37. console log: aFilename
  38. ! !
  39. !FileServer methodsFor: 'request handling'!
  40. respondNotFoundTo: aResponse
  41. aResponse
  42. writeHead: 404 options: #{'Content-Type' -> 'text/plain'};
  43. write: '404 Not found';
  44. end
  45. !
  46. handleRequest: aRequest respondTo: aResponse
  47. aRequest method = 'PUT'
  48. ifTrue: [self handlePUTRequest: aRequest respondTo: aResponse].
  49. aRequest method = 'GET'
  50. ifTrue:[self handleGETRequest: aRequest respondTo: aResponse].
  51. aRequest method = 'OPTIONS'
  52. ifTrue:[self handleOPTIONSRequest: aRequest respondTo: aResponse]
  53. !
  54. handleGETRequest: aRequest respondTo: aResponse
  55. | uri filename |
  56. uri := (url parse: aRequest url) pathname.
  57. filename := path join: self basePath with: uri.
  58. path exists: filename do: [:boolean |
  59. boolean
  60. ifFalse: [self respondNotFoundTo: aResponse]
  61. ifTrue: [self respondFileNamed: filename to: aResponse]]
  62. !
  63. handlePUTRequest: aRequest respondTo: aResponse
  64. | file stream |
  65. file := '.', aRequest url.
  66. stream := fs createWriteStream: file.
  67. stream on: 'error' do: [:error |
  68. "TODO: notify Amber about the error, otherwise the user might not notice and lose his work."
  69. console warn: 'Error creating WriteStream for file ', file.
  70. console warn: ' Did you forget to create the necessary js/ or st/ directory in your project?'.
  71. console warn: ' The exact error is: ', error].
  72. stream writable ifFalse: [
  73. console log: 'Could not write to ', file.
  74. ^nil].
  75. aRequest setEncoding: 'utf8'.
  76. aRequest on: 'data' do: [:data | stream write: data].
  77. aRequest on: 'end' do: [
  78. stream end.
  79. self respondOKTo: aResponse]
  80. !
  81. handleOPTIONSRequest: aRequest respondTo: aResponse
  82. aResponse writeHead: 200 options: #{'Access-Control-Allow-Origin' -> '*'.
  83. 'Access-Control-Allow-Methods' -> 'GET, PUT, POST, DELETE, OPTIONS'.
  84. 'Access-Control-Allow-Headers' -> 'Content-Type, Accept'.
  85. 'Content-Length' -> 0.
  86. 'Access-Control-Max-Age' -> 10}.
  87. aResponse end
  88. !
  89. respondFileNamed: aFilename to: aResponse
  90. | type filename |
  91. filename := aFilename.
  92. (fs statSync: aFilename) isDirectory ifTrue: [
  93. filename := filename, 'index.html'].
  94. fs readFile: filename do: [:ex :file |
  95. ex notNil
  96. ifTrue: [
  97. console log: filename, ' does not exist'.
  98. self respondInternalErrorTo: aResponse]
  99. ifFalse: [
  100. type := self class mimeTypeFor: filename.
  101. type = 'application/javascript'
  102. ifTrue: [ type:=type,';charset=utf-8' ].
  103. aResponse
  104. writeHead: 200 options: #{'Content-Type' -> type};
  105. write: file encoding: 'binary';
  106. end]]
  107. !
  108. respondInternalErrorTo: aResponse
  109. aResponse
  110. writeHead: 500 options: #{'Content-Type' -> 'text/plain'};
  111. write: '500 Internal server error';
  112. end
  113. !
  114. respondOKTo: aResponse
  115. aResponse
  116. writeHead: 200 options: #{'Content-Type' -> 'text/plain'. 'Access-Control-Allow-Origin' -> '*'}.
  117. aResponse end.
  118. ! !
  119. !FileServer methodsFor: 'starting'!
  120. startOn: aPort
  121. port := aPort.
  122. self start
  123. !
  124. start
  125. (http createServer: [:request :response |
  126. self handleRequest: request respondTo: response])
  127. on: 'error' do: [:error | console log: 'Error starting server: ', error];
  128. on: 'listening' do: [console log: 'Starting file server on port ', self port asString];
  129. listen: self port.
  130. ! !
  131. FileServer class instanceVariableNames: 'port mimeTypes'!
  132. !FileServer class methodsFor: 'accessing'!
  133. port
  134. ^port ifNil: [4000]
  135. !
  136. port: aNumber
  137. port := aNumber
  138. !
  139. defaultMimeTypes
  140. ^ #{
  141. '%' -> 'application/x-trash'.
  142. '323' -> 'text/h323'.
  143. 'abw' -> 'application/x-abiword'.
  144. 'ai' -> 'application/postscript'.
  145. 'aif' -> 'audio/x-aiff'.
  146. 'aifc' -> 'audio/x-aiff'.
  147. 'aiff' -> 'audio/x-aiff'.
  148. 'alc' -> 'chemical/x-alchemy'.
  149. 'art' -> 'image/x-jg'.
  150. 'asc' -> 'text/plain'.
  151. 'asf' -> 'video/x-ms-asf'.
  152. 'asn' -> 'chemical/x-ncbi-asn1-spec'.
  153. 'aso' -> 'chemical/x-ncbi-asn1-binary'.
  154. 'asx' -> 'video/x-ms-asf'.
  155. 'au' -> 'audio/basic'.
  156. 'avi' -> 'video/x-msvideo'.
  157. 'b' -> 'chemical/x-molconn-Z'.
  158. 'bak' -> 'application/x-trash'.
  159. 'bat' -> 'application/x-msdos-program'.
  160. 'bcpio' -> 'application/x-bcpio'.
  161. 'bib' -> 'text/x-bibtex'.
  162. 'bin' -> 'application/octet-stream'.
  163. 'bmp' -> 'image/x-ms-bmp'.
  164. 'book' -> 'application/x-maker'.
  165. 'bsd' -> 'chemical/x-crossfire'.
  166. 'c' -> 'text/x-csrc'.
  167. 'c++' -> 'text/x-c++src'.
  168. 'c3d' -> 'chemical/x-chem3d'.
  169. 'cac' -> 'chemical/x-cache'.
  170. 'cache' -> 'chemical/x-cache'.
  171. 'cascii' -> 'chemical/x-cactvs-binary'.
  172. 'cat' -> 'application/vnd.ms-pki.seccat'.
  173. 'cbin' -> 'chemical/x-cactvs-binary'.
  174. 'cc' -> 'text/x-c++src'.
  175. 'cdf' -> 'application/x-cdf'.
  176. 'cdr' -> 'image/x-coreldraw'.
  177. 'cdt' -> 'image/x-coreldrawtemplate'.
  178. 'cdx' -> 'chemical/x-cdx'.
  179. 'cdy' -> 'application/vnd.cinderella'.
  180. 'cef' -> 'chemical/x-cxf'.
  181. 'cer' -> 'chemical/x-cerius'.
  182. 'chm' -> 'chemical/x-chemdraw'.
  183. 'chrt' -> 'application/x-kchart'.
  184. 'cif' -> 'chemical/x-cif'.
  185. 'class' -> 'application/java-vm'.
  186. 'cls' -> 'text/x-tex'.
  187. 'cmdf' -> 'chemical/x-cmdf'.
  188. 'cml' -> 'chemical/x-cml'.
  189. 'cod' -> 'application/vnd.rim.cod'.
  190. 'com' -> 'application/x-msdos-program'.
  191. 'cpa' -> 'chemical/x-compass'.
  192. 'cpio' -> 'application/x-cpio'.
  193. 'cpp' -> 'text/x-c++src'.
  194. 'cpt' -> 'image/x-corelphotopaint'.
  195. 'crl' -> 'application/x-pkcs7-crl'.
  196. 'crt' -> 'application/x-x509-ca-cert'.
  197. 'csf' -> 'chemical/x-cache-csf'.
  198. 'csh' -> 'text/x-csh'.
  199. 'csm' -> 'chemical/x-csml'.
  200. 'csml' -> 'chemical/x-csml'.
  201. 'css' -> 'text/css'.
  202. 'csv' -> 'text/comma-separated-values'.
  203. 'ctab' -> 'chemical/x-cactvs-binary'.
  204. 'ctx' -> 'chemical/x-ctx'.
  205. 'cu' -> 'application/cu-seeme'.
  206. 'cub' -> 'chemical/x-gaussian-cube'.
  207. 'cxf' -> 'chemical/x-cxf'.
  208. 'cxx' -> 'text/x-c++src'.
  209. 'dat' -> 'chemical/x-mopac-input'.
  210. 'dcr' -> 'application/x-director'.
  211. 'deb' -> 'application/x-debian-package'.
  212. 'dif' -> 'video/dv'.
  213. 'diff' -> 'text/plain'.
  214. 'dir' -> 'application/x-director'.
  215. 'djv' -> 'image/vnd.djvu'.
  216. 'djvu' -> 'image/vnd.djvu'.
  217. 'dl' -> 'video/dl'.
  218. 'dll' -> 'application/x-msdos-program'.
  219. 'dmg' -> 'application/x-apple-diskimage'.
  220. 'dms' -> 'application/x-dms'.
  221. 'doc' -> 'application/msword'.
  222. 'dot' -> 'application/msword'.
  223. 'dv' -> 'video/dv'.
  224. 'dvi' -> 'application/x-dvi'.
  225. 'dx' -> 'chemical/x-jcamp-dx'.
  226. 'dxr' -> 'application/x-director'.
  227. 'emb' -> 'chemical/x-embl-dl-nucleotide'.
  228. 'embl' -> 'chemical/x-embl-dl-nucleotide'.
  229. 'ent' -> 'chemical/x-pdb'.
  230. 'eps' -> 'application/postscript'.
  231. 'etx' -> 'text/x-setext'.
  232. 'exe' -> 'application/x-msdos-program'.
  233. 'ez' -> 'application/andrew-inset'.
  234. 'fb' -> 'application/x-maker'.
  235. 'fbdoc' -> 'application/x-maker'.
  236. 'fch' -> 'chemical/x-gaussian-checkpoint'.
  237. 'fchk' -> 'chemical/x-gaussian-checkpoint'.
  238. 'fig' -> 'application/x-xfig'.
  239. 'flac' -> 'application/x-flac'.
  240. 'fli' -> 'video/fli'.
  241. 'fm' -> 'application/x-maker'.
  242. 'frame' -> 'application/x-maker'.
  243. 'frm' -> 'application/x-maker'.
  244. 'gal' -> 'chemical/x-gaussian-log'.
  245. 'gam' -> 'chemical/x-gamess-input'.
  246. 'gamin' -> 'chemical/x-gamess-input'.
  247. 'gau' -> 'chemical/x-gaussian-input'.
  248. 'gcd' -> 'text/x-pcs-gcd'.
  249. 'gcf' -> 'application/x-graphing-calculator'.
  250. 'gcg' -> 'chemical/x-gcg8-sequence'.
  251. 'gen' -> 'chemical/x-genbank'.
  252. 'gf' -> 'application/x-tex-gf'.
  253. 'gif' -> 'image/gif'.
  254. 'gjc' -> 'chemical/x-gaussian-input'.
  255. 'gjf' -> 'chemical/x-gaussian-input'.
  256. 'gl' -> 'video/gl'.
  257. 'gnumeric' -> 'application/x-gnumeric'.
  258. 'gpt' -> 'chemical/x-mopac-graph'.
  259. 'gsf' -> 'application/x-font'.
  260. 'gsm' -> 'audio/x-gsm'.
  261. 'gtar' -> 'application/x-gtar'.
  262. 'h' -> 'text/x-chdr'.
  263. 'h++' -> 'text/x-c++hdr'.
  264. 'hdf' -> 'application/x-hdf'.
  265. 'hh' -> 'text/x-c++hdr'.
  266. 'hin' -> 'chemical/x-hin'.
  267. 'hpp' -> 'text/x-c++hdr'.
  268. 'hqx' -> 'application/mac-binhex40'.
  269. 'hs' -> 'text/x-haskell'.
  270. 'hta' -> 'application/hta'.
  271. 'htc' -> 'text/x-component'.
  272. 'htm' -> 'text/html'.
  273. 'html' -> 'text/html'.
  274. 'hxx' -> 'text/x-c++hdr'.
  275. 'ica' -> 'application/x-ica'.
  276. 'ice' -> 'x-conference/x-cooltalk'.
  277. 'ico' -> 'image/x-icon'.
  278. 'ics' -> 'text/calendar'.
  279. 'icz' -> 'text/calendar'.
  280. 'ief' -> 'image/ief'.
  281. 'iges' -> 'model/iges'.
  282. 'igs' -> 'model/iges'.
  283. 'iii' -> 'application/x-iphone'.
  284. 'inp' -> 'chemical/x-gamess-input'.
  285. 'ins' -> 'application/x-internet-signup'.
  286. 'iso' -> 'application/x-iso9660-image'.
  287. 'isp' -> 'application/x-internet-signup'.
  288. 'ist' -> 'chemical/x-isostar'.
  289. 'istr' -> 'chemical/x-isostar'.
  290. 'jad' -> 'text/vnd.sun.j2me.app-descriptor'.
  291. 'jar' -> 'application/java-archive'.
  292. 'java' -> 'text/x-java'.
  293. 'jdx' -> 'chemical/x-jcamp-dx'.
  294. 'jmz' -> 'application/x-jmol'.
  295. 'jng' -> 'image/x-jng'.
  296. 'jnlp' -> 'application/x-java-jnlp-file'.
  297. 'jpe' -> 'image/jpeg'.
  298. 'jpeg' -> 'image/jpeg'.
  299. 'jpg' -> 'image/jpeg'.
  300. 'js' -> 'application/javascript'.
  301. 'kar' -> 'audio/midi'.
  302. 'key' -> 'application/pgp-keys'.
  303. 'kil' -> 'application/x-killustrator'.
  304. 'kin' -> 'chemical/x-kinemage'.
  305. 'kpr' -> 'application/x-kpresenter'.
  306. 'kpt' -> 'application/x-kpresenter'.
  307. 'ksp' -> 'application/x-kspread'.
  308. 'kwd' -> 'application/x-kword'.
  309. 'kwt' -> 'application/x-kword'.
  310. 'latex' -> 'application/x-latex'.
  311. 'lha' -> 'application/x-lha'.
  312. 'lhs' -> 'text/x-literate-haskell'.
  313. 'lsf' -> 'video/x-la-asf'.
  314. 'lsx' -> 'video/x-la-asf'.
  315. 'ltx' -> 'text/x-tex'.
  316. 'lzh' -> 'application/x-lzh'.
  317. 'lzx' -> 'application/x-lzx'.
  318. 'm3u' -> 'audio/x-mpegurl'.
  319. 'm4a' -> 'audio/mpeg'.
  320. 'maker' -> 'application/x-maker'.
  321. 'man' -> 'application/x-troff-man'.
  322. 'mcif' -> 'chemical/x-mmcif'.
  323. 'mcm' -> 'chemical/x-macmolecule'.
  324. 'mdb' -> 'application/msaccess'.
  325. 'me' -> 'application/x-troff-me'.
  326. 'mesh' -> 'model/mesh'.
  327. 'mid' -> 'audio/midi'.
  328. 'midi' -> 'audio/midi'.
  329. 'mif' -> 'application/x-mif'.
  330. 'mm' -> 'application/x-freemind'.
  331. 'mmd' -> 'chemical/x-macromodel-input'.
  332. 'mmf' -> 'application/vnd.smaf'.
  333. 'mml' -> 'text/mathml'.
  334. 'mmod' -> 'chemical/x-macromodel-input'.
  335. 'mng' -> 'video/x-mng'.
  336. 'moc' -> 'text/x-moc'.
  337. 'mol' -> 'chemical/x-mdl-molfile'.
  338. 'mol2' -> 'chemical/x-mol2'.
  339. 'moo' -> 'chemical/x-mopac-out'.
  340. 'mop' -> 'chemical/x-mopac-input'.
  341. 'mopcrt' -> 'chemical/x-mopac-input'.
  342. 'mov' -> 'video/quicktime'.
  343. 'movie' -> 'video/x-sgi-movie'.
  344. 'mp2' -> 'audio/mpeg'.
  345. 'mp3' -> 'audio/mpeg'.
  346. 'mp4' -> 'video/mp4'.
  347. 'mpc' -> 'chemical/x-mopac-input'.
  348. 'mpe' -> 'video/mpeg'.
  349. 'mpeg' -> 'video/mpeg'.
  350. 'mpega' -> 'audio/mpeg'.
  351. 'mpg' -> 'video/mpeg'.
  352. 'mpga' -> 'audio/mpeg'.
  353. 'ms' -> 'application/x-troff-ms'.
  354. 'msh' -> 'model/mesh'.
  355. 'msi' -> 'application/x-msi'.
  356. 'mvb' -> 'chemical/x-mopac-vib'.
  357. 'mxu' -> 'video/vnd.mpegurl'.
  358. 'nb' -> 'application/mathematica'.
  359. 'nc' -> 'application/x-netcdf'.
  360. 'nwc' -> 'application/x-nwc'.
  361. 'o' -> 'application/x-object'.
  362. 'oda' -> 'application/oda'.
  363. 'odb' -> 'application/vnd.oasis.opendocument.database'.
  364. 'odc' -> 'application/vnd.oasis.opendocument.chart'.
  365. 'odf' -> 'application/vnd.oasis.opendocument.formula'.
  366. 'odg' -> 'application/vnd.oasis.opendocument.graphics'.
  367. 'odi' -> 'application/vnd.oasis.opendocument.image'.
  368. 'odm' -> 'application/vnd.oasis.opendocument.text-master'.
  369. 'odp' -> 'application/vnd.oasis.opendocument.presentation'.
  370. 'ods' -> 'application/vnd.oasis.opendocument.spreadsheet'.
  371. 'odt' -> 'application/vnd.oasis.opendocument.text'.
  372. 'ogg' -> 'application/ogg'.
  373. 'old' -> 'application/x-trash'.
  374. 'oth' -> 'application/vnd.oasis.opendocument.text-web'.
  375. 'oza' -> 'application/x-oz-application'.
  376. 'p' -> 'text/x-pascal'.
  377. 'p7r' -> 'application/x-pkcs7-certreqresp'.
  378. 'pac' -> 'application/x-ns-proxy-autoconfig'.
  379. 'pas' -> 'text/x-pascal'.
  380. 'pat' -> 'image/x-coreldrawpattern'.
  381. 'pbm' -> 'image/x-portable-bitmap'.
  382. 'pcf' -> 'application/x-font'.
  383. 'pcf.Z' -> 'application/x-font'.
  384. 'pcx' -> 'image/pcx'.
  385. 'pdb' -> 'chemical/x-pdb'.
  386. 'pdf' -> 'application/pdf'.
  387. 'pfa' -> 'application/x-font'.
  388. 'pfb' -> 'application/x-font'.
  389. 'pgm' -> 'image/x-portable-graymap'.
  390. 'pgn' -> 'application/x-chess-pgn'.
  391. 'pgp' -> 'application/pgp-signature'.
  392. 'pk' -> 'application/x-tex-pk'.
  393. 'pl' -> 'text/x-perl'.
  394. 'pls' -> 'audio/x-scpls'.
  395. 'pm' -> 'text/x-perl'.
  396. 'png' -> 'image/png'.
  397. 'pnm' -> 'image/x-portable-anymap'.
  398. 'pot' -> 'text/plain'.
  399. 'ppm' -> 'image/x-portable-pixmap'.
  400. 'pps' -> 'application/vnd.ms-powerpoint'.
  401. 'ppt' -> 'application/vnd.ms-powerpoint'.
  402. 'prf' -> 'application/pics-rules'.
  403. 'prt' -> 'chemical/x-ncbi-asn1-ascii'.
  404. 'ps' -> 'application/postscript'.
  405. 'psd' -> 'image/x-photoshop'.
  406. 'psp' -> 'text/x-psp'.
  407. 'py' -> 'text/x-python'.
  408. 'pyc' -> 'application/x-python-code'.
  409. 'pyo' -> 'application/x-python-code'.
  410. 'qt' -> 'video/quicktime'.
  411. 'qtl' -> 'application/x-quicktimeplayer'.
  412. 'ra' -> 'audio/x-realaudio'.
  413. 'ram' -> 'audio/x-pn-realaudio'.
  414. 'rar' -> 'application/rar'.
  415. 'ras' -> 'image/x-cmu-raster'.
  416. 'rd' -> 'chemical/x-mdl-rdfile'.
  417. 'rdf' -> 'application/rdf+xml'.
  418. 'rgb' -> 'image/x-rgb'.
  419. 'rm' -> 'audio/x-pn-realaudio'.
  420. 'roff' -> 'application/x-troff'.
  421. 'ros' -> 'chemical/x-rosdal'.
  422. 'rpm' -> 'application/x-redhat-package-manager'.
  423. 'rss' -> 'application/rss+xml'.
  424. 'rtf' -> 'text/rtf'.
  425. 'rtx' -> 'text/richtext'.
  426. 'rxn' -> 'chemical/x-mdl-rxnfile'.
  427. 'sct' -> 'text/scriptlet'.
  428. 'sd' -> 'chemical/x-mdl-sdfile'.
  429. 'sd2' -> 'audio/x-sd2'.
  430. 'sda' -> 'application/vnd.stardivision.draw'.
  431. 'sdc' -> 'application/vnd.stardivision.calc'.
  432. 'sdd' -> 'application/vnd.stardivision.impress'.
  433. 'sdf' -> 'chemical/x-mdl-sdfile'.
  434. 'sdp' -> 'application/vnd.stardivision.impress'.
  435. 'sdw' -> 'application/vnd.stardivision.writer'.
  436. 'ser' -> 'application/java-serialized-object'.
  437. 'sgf' -> 'application/x-go-sgf'.
  438. 'sgl' -> 'application/vnd.stardivision.writer-global'.
  439. 'sh' -> 'text/x-sh'.
  440. 'shar' -> 'application/x-shar'.
  441. 'shtml' -> 'text/html'.
  442. 'sid' -> 'audio/prs.sid'.
  443. 'sik' -> 'application/x-trash'.
  444. 'silo' -> 'model/mesh'.
  445. 'sis' -> 'application/vnd.symbian.install'.
  446. 'sit' -> 'application/x-stuffit'.
  447. 'skd' -> 'application/x-koan'.
  448. 'skm' -> 'application/x-koan'.
  449. 'skp' -> 'application/x-koan'.
  450. 'skt' -> 'application/x-koan'.
  451. 'smf' -> 'application/vnd.stardivision.math'.
  452. 'smi' -> 'application/smil'.
  453. 'smil' -> 'application/smil'.
  454. 'snd' -> 'audio/basic'.
  455. 'spc' -> 'chemical/x-galactic-spc'.
  456. 'spl' -> 'application/x-futuresplash'.
  457. 'src' -> 'application/x-wais-source'.
  458. 'stc' -> 'application/vnd.sun.xml.calc.template'.
  459. 'std' -> 'application/vnd.sun.xml.draw.template'.
  460. 'sti' -> 'application/vnd.sun.xml.impress.template'.
  461. 'stl' -> 'application/vnd.ms-pki.stl'.
  462. 'stw' -> 'application/vnd.sun.xml.writer.template'.
  463. 'sty' -> 'text/x-tex'.
  464. 'sv4cpio' -> 'application/x-sv4cpio'.
  465. 'sv4crc' -> 'application/x-sv4crc'.
  466. 'svg' -> 'image/svg+xml'.
  467. 'svgz' -> 'image/svg+xml'.
  468. 'sw' -> 'chemical/x-swissprot'.
  469. 'swf' -> 'application/x-shockwave-flash'.
  470. 'swfl' -> 'application/x-shockwave-flash'.
  471. 'sxc' -> 'application/vnd.sun.xml.calc'.
  472. 'sxd' -> 'application/vnd.sun.xml.draw'.
  473. 'sxg' -> 'application/vnd.sun.xml.writer.global'.
  474. 'sxi' -> 'application/vnd.sun.xml.impress'.
  475. 'sxm' -> 'application/vnd.sun.xml.math'.
  476. 'sxw' -> 'application/vnd.sun.xml.writer'.
  477. 't' -> 'application/x-troff'.
  478. 'tar' -> 'application/x-tar'.
  479. 'taz' -> 'application/x-gtar'.
  480. 'tcl' -> 'text/x-tcl'.
  481. 'tex' -> 'text/x-tex'.
  482. 'texi' -> 'application/x-texinfo'.
  483. 'texinfo' -> 'application/x-texinfo'.
  484. 'text' -> 'text/plain'.
  485. 'tgf' -> 'chemical/x-mdl-tgf'.
  486. 'tgz' -> 'application/x-gtar'.
  487. 'tif' -> 'image/tiff'.
  488. 'tiff' -> 'image/tiff'.
  489. 'tk' -> 'text/x-tcl'.
  490. 'tm' -> 'text/texmacs'.
  491. 'torrent' -> 'application/x-bittorrent'.
  492. 'tr' -> 'application/x-troff'.
  493. 'ts' -> 'text/texmacs'.
  494. 'tsp' -> 'application/dsptype'.
  495. 'tsv' -> 'text/tab-separated-values'.
  496. 'txt' -> 'text/plain'.
  497. 'udeb' -> 'application/x-debian-package'.
  498. 'uls' -> 'text/iuls'.
  499. 'ustar' -> 'application/x-ustar'.
  500. 'val' -> 'chemical/x-ncbi-asn1-binary'.
  501. 'vcd' -> 'application/x-cdlink'.
  502. 'vcf' -> 'text/x-vcard'.
  503. 'vcs' -> 'text/x-vcalendar'.
  504. 'vmd' -> 'chemical/x-vmd'.
  505. 'vms' -> 'chemical/x-vamas-iso14976'.
  506. 'vor' -> 'application/vnd.stardivision.writer'.
  507. 'vrm' -> 'x-world/x-vrml'.
  508. 'vrml' -> 'x-world/x-vrml'.
  509. 'vsd' -> 'application/vnd.visio'.
  510. 'wad' -> 'application/x-doom'.
  511. 'wav' -> 'audio/x-wav'.
  512. 'wax' -> 'audio/x-ms-wax'.
  513. 'wbmp' -> 'image/vnd.wap.wbmp'.
  514. 'wbxml' -> 'application/vnd.wap.wbxml'.
  515. 'wk' -> 'application/x-123'.
  516. 'wm' -> 'video/x-ms-wm'.
  517. 'wma' -> 'audio/x-ms-wma'.
  518. 'wmd' -> 'application/x-ms-wmd'.
  519. 'wml' -> 'text/vnd.wap.wml'.
  520. 'wmlc' -> 'application/vnd.wap.wmlc'.
  521. 'wmls' -> 'text/vnd.wap.wmlscript'.
  522. 'wmlsc' -> 'application/vnd.wap.wmlscriptc'.
  523. 'wmv' -> 'video/x-ms-wmv'.
  524. 'wmx' -> 'video/x-ms-wmx'.
  525. 'wmz' -> 'application/x-ms-wmz'.
  526. 'wp5' -> 'application/wordperfect5.1'.
  527. 'wpd' -> 'application/wordperfect'.
  528. 'wrl' -> 'x-world/x-vrml'.
  529. 'wsc' -> 'text/scriptlet'.
  530. 'wvx' -> 'video/x-ms-wvx'.
  531. 'wz' -> 'application/x-wingz'.
  532. 'xbm' -> 'image/x-xbitmap'.
  533. 'xcf' -> 'application/x-xcf'.
  534. 'xht' -> 'application/xhtml+xml'.
  535. 'xhtml' -> 'application/xhtml+xml'.
  536. 'xlb' -> 'application/vnd.ms-excel'.
  537. 'xls' -> 'application/vnd.ms-excel'.
  538. 'xlt' -> 'application/vnd.ms-excel'.
  539. 'xml' -> 'application/xml'.
  540. 'xpi' -> 'application/x-xpinstall'.
  541. 'xpm' -> 'image/x-xpixmap'.
  542. 'xsl' -> 'application/xml'.
  543. 'xtel' -> 'chemical/x-xtel'.
  544. 'xul' -> 'application/vnd.mozilla.xul+xml'.
  545. 'xwd' -> 'image/x-xwindowdump'.
  546. 'xyz' -> 'chemical/x-xyz'.
  547. 'zip' -> 'application/zip'.
  548. 'zmt' -> 'chemical/x-mopac-input'.
  549. '~' -> 'application/x-trash'
  550. }
  551. !
  552. mimeTypes
  553. ^mimeTypes ifNil: [mimeTypes := self defaultMimeTypes]
  554. !
  555. mimeTypeFor: aString
  556. ^self mimeTypes at: (aString replace: '.*[\.]' with: '') ifAbsent: ['text/plain']
  557. ! !
  558. !FileServer class methodsFor: 'initialization'!
  559. main
  560. | fileServer arguments portOption port|
  561. fileServer := self new.
  562. fileServer checkDirectoryLayout.
  563. arguments := process argv.
  564. portOption := arguments at: 3 ifAbsent: [nil].
  565. port := arguments at: 4 ifAbsent: [nil].
  566. ('-p' = portOption and: [port notNil]) ifTrue: [
  567. fileServer port: port.
  568. ].
  569. ^fileServer start
  570. ! !