FileServer.st 21 KB

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