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