PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mochiweb_mime.erl

http://github.com/basho/mochiweb
Erlang | 433 lines | 397 code | 8 blank | 28 comment | 1 complexity | 0d3d54582eb969d815729e31401b5809 MD5 | raw file
Possible License(s): MIT
  1. %% @author Bob Ippolito <bob@mochimedia.com>
  2. %% @copyright 2007 Mochi Media, Inc.
  3. %%
  4. %% Permission is hereby granted, free of charge, to any person obtaining a
  5. %% copy of this software and associated documentation files (the "Software"),
  6. %% to deal in the Software without restriction, including without limitation
  7. %% the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. %% and/or sell copies of the Software, and to permit persons to whom the
  9. %% Software is furnished to do so, subject to the following conditions:
  10. %%
  11. %% The above copyright notice and this permission notice shall be included in
  12. %% all copies or substantial portions of the Software.
  13. %%
  14. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. %% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. %% DEALINGS IN THE SOFTWARE.
  21. %% @doc Gives a good MIME type guess based on file extension.
  22. -module(mochiweb_mime).
  23. -author('bob@mochimedia.com').
  24. -export([from_extension/1]).
  25. %% @spec from_extension(S::string()) -> string() | undefined
  26. %% @doc Given a filename extension (e.g. ".html") return a guess for the MIME
  27. %% type such as "text/html". Will return the atom undefined if no good
  28. %% guess is available.
  29. from_extension(".stl") ->
  30. "application/SLA";
  31. from_extension(".stp") ->
  32. "application/STEP";
  33. from_extension(".step") ->
  34. "application/STEP";
  35. from_extension(".dwg") ->
  36. "application/acad";
  37. from_extension(".ez") ->
  38. "application/andrew-inset";
  39. from_extension(".ccad") ->
  40. "application/clariscad";
  41. from_extension(".drw") ->
  42. "application/drafting";
  43. from_extension(".tsp") ->
  44. "application/dsptype";
  45. from_extension(".dxf") ->
  46. "application/dxf";
  47. from_extension(".xls") ->
  48. "application/excel";
  49. from_extension(".unv") ->
  50. "application/i-deas";
  51. from_extension(".jar") ->
  52. "application/java-archive";
  53. from_extension(".hqx") ->
  54. "application/mac-binhex40";
  55. from_extension(".cpt") ->
  56. "application/mac-compactpro";
  57. from_extension(".pot") ->
  58. "application/vnd.ms-powerpoint";
  59. from_extension(".ppt") ->
  60. "application/vnd.ms-powerpoint";
  61. from_extension(".dms") ->
  62. "application/octet-stream";
  63. from_extension(".lha") ->
  64. "application/octet-stream";
  65. from_extension(".lzh") ->
  66. "application/octet-stream";
  67. from_extension(".oda") ->
  68. "application/oda";
  69. from_extension(".ogg") ->
  70. "application/ogg";
  71. from_extension(".ogm") ->
  72. "application/ogg";
  73. from_extension(".pdf") ->
  74. "application/pdf";
  75. from_extension(".pgp") ->
  76. "application/pgp";
  77. from_extension(".ai") ->
  78. "application/postscript";
  79. from_extension(".eps") ->
  80. "application/postscript";
  81. from_extension(".ps") ->
  82. "application/postscript";
  83. from_extension(".prt") ->
  84. "application/pro_eng";
  85. from_extension(".rtf") ->
  86. "application/rtf";
  87. from_extension(".smi") ->
  88. "application/smil";
  89. from_extension(".smil") ->
  90. "application/smil";
  91. from_extension(".sol") ->
  92. "application/solids";
  93. from_extension(".vda") ->
  94. "application/vda";
  95. from_extension(".xlm") ->
  96. "application/vnd.ms-excel";
  97. from_extension(".cod") ->
  98. "application/vnd.rim.cod";
  99. from_extension(".pgn") ->
  100. "application/x-chess-pgn";
  101. from_extension(".cpio") ->
  102. "application/x-cpio";
  103. from_extension(".csh") ->
  104. "application/x-csh";
  105. from_extension(".deb") ->
  106. "application/x-debian-package";
  107. from_extension(".dcr") ->
  108. "application/x-director";
  109. from_extension(".dir") ->
  110. "application/x-director";
  111. from_extension(".dxr") ->
  112. "application/x-director";
  113. from_extension(".gz") ->
  114. "application/x-gzip";
  115. from_extension(".hdf") ->
  116. "application/x-hdf";
  117. from_extension(".ipx") ->
  118. "application/x-ipix";
  119. from_extension(".ips") ->
  120. "application/x-ipscript";
  121. from_extension(".js") ->
  122. "application/x-javascript";
  123. from_extension(".skd") ->
  124. "application/x-koan";
  125. from_extension(".skm") ->
  126. "application/x-koan";
  127. from_extension(".skp") ->
  128. "application/x-koan";
  129. from_extension(".skt") ->
  130. "application/x-koan";
  131. from_extension(".latex") ->
  132. "application/x-latex";
  133. from_extension(".lsp") ->
  134. "application/x-lisp";
  135. from_extension(".scm") ->
  136. "application/x-lotusscreencam";
  137. from_extension(".mif") ->
  138. "application/x-mif";
  139. from_extension(".com") ->
  140. "application/x-msdos-program";
  141. from_extension(".exe") ->
  142. "application/octet-stream";
  143. from_extension(".cdf") ->
  144. "application/x-netcdf";
  145. from_extension(".nc") ->
  146. "application/x-netcdf";
  147. from_extension(".pl") ->
  148. "application/x-perl";
  149. from_extension(".pm") ->
  150. "application/x-perl";
  151. from_extension(".rar") ->
  152. "application/x-rar-compressed";
  153. from_extension(".sh") ->
  154. "application/x-sh";
  155. from_extension(".shar") ->
  156. "application/x-shar";
  157. from_extension(".swf") ->
  158. "application/x-shockwave-flash";
  159. from_extension(".sit") ->
  160. "application/x-stuffit";
  161. from_extension(".sv4cpio") ->
  162. "application/x-sv4cpio";
  163. from_extension(".sv4crc") ->
  164. "application/x-sv4crc";
  165. from_extension(".tar.gz") ->
  166. "application/x-tar-gz";
  167. from_extension(".tgz") ->
  168. "application/x-tar-gz";
  169. from_extension(".tar") ->
  170. "application/x-tar";
  171. from_extension(".tcl") ->
  172. "application/x-tcl";
  173. from_extension(".texi") ->
  174. "application/x-texinfo";
  175. from_extension(".texinfo") ->
  176. "application/x-texinfo";
  177. from_extension(".man") ->
  178. "application/x-troff-man";
  179. from_extension(".me") ->
  180. "application/x-troff-me";
  181. from_extension(".ms") ->
  182. "application/x-troff-ms";
  183. from_extension(".roff") ->
  184. "application/x-troff";
  185. from_extension(".t") ->
  186. "application/x-troff";
  187. from_extension(".tr") ->
  188. "application/x-troff";
  189. from_extension(".ustar") ->
  190. "application/x-ustar";
  191. from_extension(".src") ->
  192. "application/x-wais-source";
  193. from_extension(".zip") ->
  194. "application/zip";
  195. from_extension(".tsi") ->
  196. "audio/TSP-audio";
  197. from_extension(".au") ->
  198. "audio/basic";
  199. from_extension(".snd") ->
  200. "audio/basic";
  201. from_extension(".kar") ->
  202. "audio/midi";
  203. from_extension(".mid") ->
  204. "audio/midi";
  205. from_extension(".midi") ->
  206. "audio/midi";
  207. from_extension(".mp2") ->
  208. "audio/mpeg";
  209. from_extension(".mp3") ->
  210. "audio/mpeg";
  211. from_extension(".mpga") ->
  212. "audio/mpeg";
  213. from_extension(".aif") ->
  214. "audio/x-aiff";
  215. from_extension(".aifc") ->
  216. "audio/x-aiff";
  217. from_extension(".aiff") ->
  218. "audio/x-aiff";
  219. from_extension(".m3u") ->
  220. "audio/x-mpegurl";
  221. from_extension(".wax") ->
  222. "audio/x-ms-wax";
  223. from_extension(".wma") ->
  224. "audio/x-ms-wma";
  225. from_extension(".rpm") ->
  226. "audio/x-pn-realaudio-plugin";
  227. from_extension(".ram") ->
  228. "audio/x-pn-realaudio";
  229. from_extension(".rm") ->
  230. "audio/x-pn-realaudio";
  231. from_extension(".ra") ->
  232. "audio/x-realaudio";
  233. from_extension(".wav") ->
  234. "audio/x-wav";
  235. from_extension(".pdb") ->
  236. "chemical/x-pdb";
  237. from_extension(".ras") ->
  238. "image/cmu-raster";
  239. from_extension(".gif") ->
  240. "image/gif";
  241. from_extension(".ief") ->
  242. "image/ief";
  243. from_extension(".jpe") ->
  244. "image/jpeg";
  245. from_extension(".jpeg") ->
  246. "image/jpeg";
  247. from_extension(".jpg") ->
  248. "image/jpeg";
  249. from_extension(".jp2") ->
  250. "image/jp2";
  251. from_extension(".png") ->
  252. "image/png";
  253. from_extension(".tif") ->
  254. "image/tiff";
  255. from_extension(".tiff") ->
  256. "image/tiff";
  257. from_extension(".pnm") ->
  258. "image/x-portable-anymap";
  259. from_extension(".pbm") ->
  260. "image/x-portable-bitmap";
  261. from_extension(".pgm") ->
  262. "image/x-portable-graymap";
  263. from_extension(".ppm") ->
  264. "image/x-portable-pixmap";
  265. from_extension(".rgb") ->
  266. "image/x-rgb";
  267. from_extension(".xbm") ->
  268. "image/x-xbitmap";
  269. from_extension(".xwd") ->
  270. "image/x-xwindowdump";
  271. from_extension(".iges") ->
  272. "model/iges";
  273. from_extension(".igs") ->
  274. "model/iges";
  275. from_extension(".mesh") ->
  276. "model/mesh";
  277. from_extension(".") ->
  278. "";
  279. from_extension(".msh") ->
  280. "model/mesh";
  281. from_extension(".silo") ->
  282. "model/mesh";
  283. from_extension(".vrml") ->
  284. "model/vrml";
  285. from_extension(".wrl") ->
  286. "model/vrml";
  287. from_extension(".css") ->
  288. "text/css";
  289. from_extension(".htm") ->
  290. "text/html";
  291. from_extension(".html") ->
  292. "text/html";
  293. from_extension(".asc") ->
  294. "text/plain";
  295. from_extension(".c") ->
  296. "text/plain";
  297. from_extension(".cc") ->
  298. "text/plain";
  299. from_extension(".f90") ->
  300. "text/plain";
  301. from_extension(".f") ->
  302. "text/plain";
  303. from_extension(".hh") ->
  304. "text/plain";
  305. from_extension(".m") ->
  306. "text/plain";
  307. from_extension(".txt") ->
  308. "text/plain";
  309. from_extension(".rtx") ->
  310. "text/richtext";
  311. from_extension(".sgm") ->
  312. "text/sgml";
  313. from_extension(".sgml") ->
  314. "text/sgml";
  315. from_extension(".tsv") ->
  316. "text/tab-separated-values";
  317. from_extension(".jad") ->
  318. "text/vnd.sun.j2me.app-descriptor";
  319. from_extension(".etx") ->
  320. "text/x-setext";
  321. from_extension(".xml") ->
  322. "application/xml";
  323. from_extension(".dl") ->
  324. "video/dl";
  325. from_extension(".fli") ->
  326. "video/fli";
  327. from_extension(".flv") ->
  328. "video/x-flv";
  329. from_extension(".gl") ->
  330. "video/gl";
  331. from_extension(".mp4") ->
  332. "video/mp4";
  333. from_extension(".mpe") ->
  334. "video/mpeg";
  335. from_extension(".mpeg") ->
  336. "video/mpeg";
  337. from_extension(".mpg") ->
  338. "video/mpeg";
  339. from_extension(".mov") ->
  340. "video/quicktime";
  341. from_extension(".qt") ->
  342. "video/quicktime";
  343. from_extension(".viv") ->
  344. "video/vnd.vivo";
  345. from_extension(".vivo") ->
  346. "video/vnd.vivo";
  347. from_extension(".asf") ->
  348. "video/x-ms-asf";
  349. from_extension(".asx") ->
  350. "video/x-ms-asx";
  351. from_extension(".wmv") ->
  352. "video/x-ms-wmv";
  353. from_extension(".wmx") ->
  354. "video/x-ms-wmx";
  355. from_extension(".wvx") ->
  356. "video/x-ms-wvx";
  357. from_extension(".avi") ->
  358. "video/x-msvideo";
  359. from_extension(".movie") ->
  360. "video/x-sgi-movie";
  361. from_extension(".mime") ->
  362. "www/mime";
  363. from_extension(".ice") ->
  364. "x-conference/x-cooltalk";
  365. from_extension(".vrm") ->
  366. "x-world/x-vrml";
  367. from_extension(".spx") ->
  368. "audio/ogg";
  369. from_extension(".xhtml") ->
  370. "application/xhtml+xml";
  371. from_extension(".bz2") ->
  372. "application/x-bzip2";
  373. from_extension(".doc") ->
  374. "application/msword";
  375. from_extension(".z") ->
  376. "application/x-compress";
  377. from_extension(".ico") ->
  378. "image/x-icon";
  379. from_extension(".bmp") ->
  380. "image/bmp";
  381. from_extension(".m4a") ->
  382. "audio/mpeg";
  383. from_extension(".csv") ->
  384. "text/csv";
  385. from_extension(".eot") ->
  386. "application/vnd.ms-fontobject";
  387. from_extension(".m4v") ->
  388. "video/mp4";
  389. from_extension(".svg") ->
  390. "image/svg+xml";
  391. from_extension(".svgz") ->
  392. "image/svg+xml";
  393. from_extension(".ttc") ->
  394. "application/x-font-ttf";
  395. from_extension(".ttf") ->
  396. "application/x-font-ttf";
  397. from_extension(".vcf") ->
  398. "text/x-vcard";
  399. from_extension(".webm") ->
  400. "video/web";
  401. from_extension(".webp") ->
  402. "image/web";
  403. from_extension(".woff") ->
  404. "application/x-font-woff";
  405. from_extension(".otf") ->
  406. "font/opentype";
  407. from_extension(_) ->
  408. undefined.
  409. %%
  410. %% Tests
  411. %%
  412. -ifdef(TEST).
  413. -include_lib("eunit/include/eunit.hrl").
  414. exhaustive_from_extension_test() ->
  415. T = mochiweb_cover:clause_lookup_table(?MODULE, from_extension),
  416. [?assertEqual(V, from_extension(K)) || {K, V} <- T].
  417. from_extension_test() ->
  418. ?assertEqual("text/html",
  419. from_extension(".html")),
  420. ?assertEqual(undefined,
  421. from_extension("")),
  422. ?assertEqual(undefined,
  423. from_extension(".wtf")),
  424. ok.
  425. -endif.