FileServer.st 21 KB

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