1
0

FileServer.st 20 KB

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