Skip to content
Browse files

Missing mochiweb dependencies

  • Loading branch information...
1 parent 250c9f4 commit 6c51e3e407bfe6f51e749625c1db3583060617ad @gar1t committed Jul 7, 2011
Showing with 833 additions and 0 deletions.
  1. +9 −0 LICENSE_mochiweb
  2. +2 −0 README.rst
  3. +354 −0 src/mochinum.erl
  4. +75 −0 src/mochiweb_cover.erl
  5. +393 −0 src/mochiweb_mime.erl
View
9 LICENSE_mochiweb
@@ -0,0 +1,9 @@
+This is the MIT license.
+
+Copyright (c) 2007 Mochi Media, Inc.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
2 README.rst
@@ -50,6 +50,8 @@ To Do
- If modlib_webapp is declared as a behavior, it breaks the wrapping that's
done by the webapp include.
+- Remove mochiweb util module dependencies, lazy!
+
inets Wish List
===============
View
354 src/mochinum.erl
@@ -0,0 +1,354 @@
+%% @copyright 2007 Mochi Media, Inc.
+%% @author Bob Ippolito <bob@mochimedia.com>
+
+%% @doc Useful numeric algorithms for floats that cover some deficiencies
+%% in the math module. More interesting is digits/1, which implements
+%% the algorithm from:
+%% http://www.cs.indiana.edu/~burger/fp/index.html
+%% See also "Printing Floating-Point Numbers Quickly and Accurately"
+%% in Proceedings of the SIGPLAN '96 Conference on Programming Language
+%% Design and Implementation.
+
+-module(mochinum).
+-author("Bob Ippolito <bob@mochimedia.com>").
+-export([digits/1, frexp/1, int_pow/2, int_ceil/1]).
+
+%% IEEE 754 Float exponent bias
+-define(FLOAT_BIAS, 1022).
+-define(MIN_EXP, -1074).
+-define(BIG_POW, 4503599627370496).
+
+%% External API
+
+%% @spec digits(number()) -> string()
+%% @doc Returns a string that accurately represents the given integer or float
+%% using a conservative amount of digits. Great for generating
+%% human-readable output, or compact ASCII serializations for floats.
+digits(N) when is_integer(N) ->
+ integer_to_list(N);
+digits(0.0) ->
+ "0.0";
+digits(Float) ->
+ {Frac1, Exp1} = frexp_int(Float),
+ [Place0 | Digits0] = digits1(Float, Exp1, Frac1),
+ {Place, Digits} = transform_digits(Place0, Digits0),
+ R = insert_decimal(Place, Digits),
+ case Float < 0 of
+ true ->
+ [$- | R];
+ _ ->
+ R
+ end.
+
+%% @spec frexp(F::float()) -> {Frac::float(), Exp::float()}
+%% @doc Return the fractional and exponent part of an IEEE 754 double,
+%% equivalent to the libc function of the same name.
+%% F = Frac * pow(2, Exp).
+frexp(F) ->
+ frexp1(unpack(F)).
+
+%% @spec int_pow(X::integer(), N::integer()) -> Y::integer()
+%% @doc Moderately efficient way to exponentiate integers.
+%% int_pow(10, 2) = 100.
+int_pow(_X, 0) ->
+ 1;
+int_pow(X, N) when N > 0 ->
+ int_pow(X, N, 1).
+
+%% @spec int_ceil(F::float()) -> integer()
+%% @doc Return the ceiling of F as an integer. The ceiling is defined as
+%% F when F == trunc(F);
+%% trunc(F) when F &lt; 0;
+%% trunc(F) + 1 when F &gt; 0.
+int_ceil(X) ->
+ T = trunc(X),
+ case (X - T) of
+ Pos when Pos > 0 -> T + 1;
+ _ -> T
+ end.
+
+
+%% Internal API
+
+int_pow(X, N, R) when N < 2 ->
+ R * X;
+int_pow(X, N, R) ->
+ int_pow(X * X, N bsr 1, case N band 1 of 1 -> R * X; 0 -> R end).
+
+insert_decimal(0, S) ->
+ "0." ++ S;
+insert_decimal(Place, S) when Place > 0 ->
+ L = length(S),
+ case Place - L of
+ 0 ->
+ S ++ ".0";
+ N when N < 0 ->
+ {S0, S1} = lists:split(L + N, S),
+ S0 ++ "." ++ S1;
+ N when N < 6 ->
+ %% More places than digits
+ S ++ lists:duplicate(N, $0) ++ ".0";
+ _ ->
+ insert_decimal_exp(Place, S)
+ end;
+insert_decimal(Place, S) when Place > -6 ->
+ "0." ++ lists:duplicate(abs(Place), $0) ++ S;
+insert_decimal(Place, S) ->
+ insert_decimal_exp(Place, S).
+
+insert_decimal_exp(Place, S) ->
+ [C | S0] = S,
+ S1 = case S0 of
+ [] ->
+ "0";
+ _ ->
+ S0
+ end,
+ Exp = case Place < 0 of
+ true ->
+ "e-";
+ false ->
+ "e+"
+ end,
+ [C] ++ "." ++ S1 ++ Exp ++ integer_to_list(abs(Place - 1)).
+
+
+digits1(Float, Exp, Frac) ->
+ Round = ((Frac band 1) =:= 0),
+ case Exp >= 0 of
+ true ->
+ BExp = 1 bsl Exp,
+ case (Frac =/= ?BIG_POW) of
+ true ->
+ scale((Frac * BExp * 2), 2, BExp, BExp,
+ Round, Round, Float);
+ false ->
+ scale((Frac * BExp * 4), 4, (BExp * 2), BExp,
+ Round, Round, Float)
+ end;
+ false ->
+ case (Exp =:= ?MIN_EXP) orelse (Frac =/= ?BIG_POW) of
+ true ->
+ scale((Frac * 2), 1 bsl (1 - Exp), 1, 1,
+ Round, Round, Float);
+ false ->
+ scale((Frac * 4), 1 bsl (2 - Exp), 2, 1,
+ Round, Round, Float)
+ end
+ end.
+
+scale(R, S, MPlus, MMinus, LowOk, HighOk, Float) ->
+ Est = int_ceil(math:log10(abs(Float)) - 1.0e-10),
+ %% Note that the scheme implementation uses a 326 element look-up table
+ %% for int_pow(10, N) where we do not.
+ case Est >= 0 of
+ true ->
+ fixup(R, S * int_pow(10, Est), MPlus, MMinus, Est,
+ LowOk, HighOk);
+ false ->
+ Scale = int_pow(10, -Est),
+ fixup(R * Scale, S, MPlus * Scale, MMinus * Scale, Est,
+ LowOk, HighOk)
+ end.
+
+fixup(R, S, MPlus, MMinus, K, LowOk, HighOk) ->
+ TooLow = case HighOk of
+ true ->
+ (R + MPlus) >= S;
+ false ->
+ (R + MPlus) > S
+ end,
+ case TooLow of
+ true ->
+ [(K + 1) | generate(R, S, MPlus, MMinus, LowOk, HighOk)];
+ false ->
+ [K | generate(R * 10, S, MPlus * 10, MMinus * 10, LowOk, HighOk)]
+ end.
+
+generate(R0, S, MPlus, MMinus, LowOk, HighOk) ->
+ D = R0 div S,
+ R = R0 rem S,
+ TC1 = case LowOk of
+ true ->
+ R =< MMinus;
+ false ->
+ R < MMinus
+ end,
+ TC2 = case HighOk of
+ true ->
+ (R + MPlus) >= S;
+ false ->
+ (R + MPlus) > S
+ end,
+ case TC1 of
+ false ->
+ case TC2 of
+ false ->
+ [D | generate(R * 10, S, MPlus * 10, MMinus * 10,
+ LowOk, HighOk)];
+ true ->
+ [D + 1]
+ end;
+ true ->
+ case TC2 of
+ false ->
+ [D];
+ true ->
+ case R * 2 < S of
+ true ->
+ [D];
+ false ->
+ [D + 1]
+ end
+ end
+ end.
+
+unpack(Float) ->
+ <<Sign:1, Exp:11, Frac:52>> = <<Float:64/float>>,
+ {Sign, Exp, Frac}.
+
+frexp1({_Sign, 0, 0}) ->
+ {0.0, 0};
+frexp1({Sign, 0, Frac}) ->
+ Exp = log2floor(Frac),
+ <<Frac1:64/float>> = <<Sign:1, ?FLOAT_BIAS:11, (Frac-1):52>>,
+ {Frac1, -(?FLOAT_BIAS) - 52 + Exp};
+frexp1({Sign, Exp, Frac}) ->
+ <<Frac1:64/float>> = <<Sign:1, ?FLOAT_BIAS:11, Frac:52>>,
+ {Frac1, Exp - ?FLOAT_BIAS}.
+
+log2floor(Int) ->
+ log2floor(Int, 0).
+
+log2floor(0, N) ->
+ N;
+log2floor(Int, N) ->
+ log2floor(Int bsr 1, 1 + N).
+
+
+transform_digits(Place, [0 | Rest]) ->
+ transform_digits(Place, Rest);
+transform_digits(Place, Digits) ->
+ {Place, [$0 + D || D <- Digits]}.
+
+
+frexp_int(F) ->
+ case unpack(F) of
+ {_Sign, 0, Frac} ->
+ {Frac, ?MIN_EXP};
+ {_Sign, Exp, Frac} ->
+ {Frac + (1 bsl 52), Exp - 53 - ?FLOAT_BIAS}
+ end.
+
+%%
+%% Tests
+%%
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+int_ceil_test() ->
+ ?assertEqual(1, int_ceil(0.0001)),
+ ?assertEqual(0, int_ceil(0.0)),
+ ?assertEqual(1, int_ceil(0.99)),
+ ?assertEqual(1, int_ceil(1.0)),
+ ?assertEqual(-1, int_ceil(-1.5)),
+ ?assertEqual(-2, int_ceil(-2.0)),
+ ok.
+
+int_pow_test() ->
+ ?assertEqual(1, int_pow(1, 1)),
+ ?assertEqual(1, int_pow(1, 0)),
+ ?assertEqual(1, int_pow(10, 0)),
+ ?assertEqual(10, int_pow(10, 1)),
+ ?assertEqual(100, int_pow(10, 2)),
+ ?assertEqual(1000, int_pow(10, 3)),
+ ok.
+
+digits_test() ->
+ ?assertEqual("0",
+ digits(0)),
+ ?assertEqual("0.0",
+ digits(0.0)),
+ ?assertEqual("1.0",
+ digits(1.0)),
+ ?assertEqual("-1.0",
+ digits(-1.0)),
+ ?assertEqual("0.1",
+ digits(0.1)),
+ ?assertEqual("0.01",
+ digits(0.01)),
+ ?assertEqual("0.001",
+ digits(0.001)),
+ ?assertEqual("1.0e+6",
+ digits(1000000.0)),
+ ?assertEqual("0.5",
+ digits(0.5)),
+ ?assertEqual("4503599627370496.0",
+ digits(4503599627370496.0)),
+ %% small denormalized number
+ %% 4.94065645841246544177e-324 =:= 5.0e-324
+ <<SmallDenorm/float>> = <<0,0,0,0,0,0,0,1>>,
+ ?assertEqual("5.0e-324",
+ digits(SmallDenorm)),
+ ?assertEqual(SmallDenorm,
+ list_to_float(digits(SmallDenorm))),
+ %% large denormalized number
+ %% 2.22507385850720088902e-308
+ <<BigDenorm/float>> = <<0,15,255,255,255,255,255,255>>,
+ ?assertEqual("2.225073858507201e-308",
+ digits(BigDenorm)),
+ ?assertEqual(BigDenorm,
+ list_to_float(digits(BigDenorm))),
+ %% small normalized number
+ %% 2.22507385850720138309e-308
+ <<SmallNorm/float>> = <<0,16,0,0,0,0,0,0>>,
+ ?assertEqual("2.2250738585072014e-308",
+ digits(SmallNorm)),
+ ?assertEqual(SmallNorm,
+ list_to_float(digits(SmallNorm))),
+ %% large normalized number
+ %% 1.79769313486231570815e+308
+ <<LargeNorm/float>> = <<127,239,255,255,255,255,255,255>>,
+ ?assertEqual("1.7976931348623157e+308",
+ digits(LargeNorm)),
+ ?assertEqual(LargeNorm,
+ list_to_float(digits(LargeNorm))),
+ %% issue #10 - mochinum:frexp(math:pow(2, -1074)).
+ ?assertEqual("5.0e-324",
+ digits(math:pow(2, -1074))),
+ ok.
+
+frexp_test() ->
+ %% zero
+ ?assertEqual({0.0, 0}, frexp(0.0)),
+ %% one
+ ?assertEqual({0.5, 1}, frexp(1.0)),
+ %% negative one
+ ?assertEqual({-0.5, 1}, frexp(-1.0)),
+ %% small denormalized number
+ %% 4.94065645841246544177e-324
+ <<SmallDenorm/float>> = <<0,0,0,0,0,0,0,1>>,
+ ?assertEqual({0.5, -1073}, frexp(SmallDenorm)),
+ %% large denormalized number
+ %% 2.22507385850720088902e-308
+ <<BigDenorm/float>> = <<0,15,255,255,255,255,255,255>>,
+ ?assertEqual(
+ {0.99999999999999978, -1022},
+ frexp(BigDenorm)),
+ %% small normalized number
+ %% 2.22507385850720138309e-308
+ <<SmallNorm/float>> = <<0,16,0,0,0,0,0,0>>,
+ ?assertEqual({0.5, -1021}, frexp(SmallNorm)),
+ %% large normalized number
+ %% 1.79769313486231570815e+308
+ <<LargeNorm/float>> = <<127,239,255,255,255,255,255,255>>,
+ ?assertEqual(
+ {0.99999999999999989, 1024},
+ frexp(LargeNorm)),
+ %% issue #10 - mochinum:frexp(math:pow(2, -1074)).
+ ?assertEqual(
+ {0.5, -1073},
+ frexp(math:pow(2, -1074))),
+ ok.
+
+-endif.
View
75 src/mochiweb_cover.erl
@@ -0,0 +1,75 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2010 Mochi Media, Inc.
+
+%% @doc Workarounds for various cover deficiencies.
+-module(mochiweb_cover).
+-export([get_beam/1, get_abstract_code/1,
+ get_clauses/2, clause_lookup_table/1]).
+-export([clause_lookup_table/2]).
+
+%% Internal
+
+get_beam(Module) ->
+ {Module, Beam, _Path} = code:get_object_code(Module),
+ Beam.
+
+get_abstract_code(Beam) ->
+ {ok, {_Module,
+ [{abstract_code,
+ {raw_abstract_v1, L}}]}} = beam_lib:chunks(Beam, [abstract_code]),
+ L.
+
+get_clauses(Function, Code) ->
+ [L] = [Clauses || {function, _, FName, _, Clauses}
+ <- Code, FName =:= Function],
+ L.
+
+clause_lookup_table(Module, Function) ->
+ clause_lookup_table(
+ get_clauses(Function,
+ get_abstract_code(get_beam(Module)))).
+
+clause_lookup_table(Clauses) ->
+ lists:foldr(fun clause_fold/2, [], Clauses).
+
+clause_fold({clause, _,
+ [InTerm],
+ _Guards=[],
+ [OutTerm]},
+ Acc) ->
+ try [{erl_parse:normalise(InTerm), erl_parse:normalise(OutTerm)} | Acc]
+ catch error:_ -> Acc
+ end;
+clause_fold(_, Acc) ->
+ Acc.
+
+%%
+%% Tests
+%%
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+foo_table(a) -> b;
+foo_table("a") -> <<"b">>;
+foo_table(123) -> {4, 3, 2};
+foo_table([list]) -> [];
+foo_table([list1, list2]) -> [list1, list2, list3];
+foo_table(ignored) -> some, code, ignored;
+foo_table(Var) -> Var.
+
+foo_table_test() ->
+ T = clause_lookup_table(?MODULE, foo_table),
+ [?assertEqual(V, foo_table(K)) || {K, V} <- T].
+
+clause_lookup_table_test() ->
+ ?assertEqual(b, foo_table(a)),
+ ?assertEqual(ignored, foo_table(ignored)),
+ ?assertEqual('Var', foo_table('Var')),
+ ?assertEqual(
+ [{a, b},
+ {"a", <<"b">>},
+ {123, {4, 3, 2}},
+ {[list], []},
+ {[list1, list2], [list1, list2, list3]}],
+ clause_lookup_table(?MODULE, foo_table)).
+
+-endif.
View
393 src/mochiweb_mime.erl
@@ -0,0 +1,393 @@
+%% @author Bob Ippolito <bob@mochimedia.com>
+%% @copyright 2007 Mochi Media, Inc.
+
+%% @doc Gives a good MIME type guess based on file extension.
+
+-module(mochiweb_mime).
+-author('bob@mochimedia.com').
+-export([from_extension/1]).
+
+%% @spec from_extension(S::string()) -> string() | undefined
+%% @doc Given a filename extension (e.g. ".html") return a guess for the MIME
+%% type such as "text/html". Will return the atom undefined if no good
+%% guess is available.
+
+from_extension(".stl") ->
+ "application/SLA";
+from_extension(".stp") ->
+ "application/STEP";
+from_extension(".step") ->
+ "application/STEP";
+from_extension(".dwg") ->
+ "application/acad";
+from_extension(".ez") ->
+ "application/andrew-inset";
+from_extension(".ccad") ->
+ "application/clariscad";
+from_extension(".drw") ->
+ "application/drafting";
+from_extension(".tsp") ->
+ "application/dsptype";
+from_extension(".dxf") ->
+ "application/dxf";
+from_extension(".xls") ->
+ "application/excel";
+from_extension(".unv") ->
+ "application/i-deas";
+from_extension(".jar") ->
+ "application/java-archive";
+from_extension(".hqx") ->
+ "application/mac-binhex40";
+from_extension(".cpt") ->
+ "application/mac-compactpro";
+from_extension(".pot") ->
+ "application/vnd.ms-powerpoint";
+from_extension(".ppt") ->
+ "application/vnd.ms-powerpoint";
+from_extension(".dms") ->
+ "application/octet-stream";
+from_extension(".lha") ->
+ "application/octet-stream";
+from_extension(".lzh") ->
+ "application/octet-stream";
+from_extension(".oda") ->
+ "application/oda";
+from_extension(".ogg") ->
+ "application/ogg";
+from_extension(".ogm") ->
+ "application/ogg";
+from_extension(".pdf") ->
+ "application/pdf";
+from_extension(".pgp") ->
+ "application/pgp";
+from_extension(".ai") ->
+ "application/postscript";
+from_extension(".eps") ->
+ "application/postscript";
+from_extension(".ps") ->
+ "application/postscript";
+from_extension(".prt") ->
+ "application/pro_eng";
+from_extension(".rtf") ->
+ "application/rtf";
+from_extension(".smi") ->
+ "application/smil";
+from_extension(".smil") ->
+ "application/smil";
+from_extension(".sol") ->
+ "application/solids";
+from_extension(".vda") ->
+ "application/vda";
+from_extension(".xlm") ->
+ "application/vnd.ms-excel";
+from_extension(".cod") ->
+ "application/vnd.rim.cod";
+from_extension(".pgn") ->
+ "application/x-chess-pgn";
+from_extension(".cpio") ->
+ "application/x-cpio";
+from_extension(".csh") ->
+ "application/x-csh";
+from_extension(".deb") ->
+ "application/x-debian-package";
+from_extension(".dcr") ->
+ "application/x-director";
+from_extension(".dir") ->
+ "application/x-director";
+from_extension(".dxr") ->
+ "application/x-director";
+from_extension(".gz") ->
+ "application/x-gzip";
+from_extension(".hdf") ->
+ "application/x-hdf";
+from_extension(".ipx") ->
+ "application/x-ipix";
+from_extension(".ips") ->
+ "application/x-ipscript";
+from_extension(".js") ->
+ "application/x-javascript";
+from_extension(".skd") ->
+ "application/x-koan";
+from_extension(".skm") ->
+ "application/x-koan";
+from_extension(".skp") ->
+ "application/x-koan";
+from_extension(".skt") ->
+ "application/x-koan";
+from_extension(".latex") ->
+ "application/x-latex";
+from_extension(".lsp") ->
+ "application/x-lisp";
+from_extension(".scm") ->
+ "application/x-lotusscreencam";
+from_extension(".mif") ->
+ "application/x-mif";
+from_extension(".com") ->
+ "application/x-msdos-program";
+from_extension(".exe") ->
+ "application/octet-stream";
+from_extension(".cdf") ->
+ "application/x-netcdf";
+from_extension(".nc") ->
+ "application/x-netcdf";
+from_extension(".pl") ->
+ "application/x-perl";
+from_extension(".pm") ->
+ "application/x-perl";
+from_extension(".rar") ->
+ "application/x-rar-compressed";
+from_extension(".sh") ->
+ "application/x-sh";
+from_extension(".shar") ->
+ "application/x-shar";
+from_extension(".swf") ->
+ "application/x-shockwave-flash";
+from_extension(".sit") ->
+ "application/x-stuffit";
+from_extension(".sv4cpio") ->
+ "application/x-sv4cpio";
+from_extension(".sv4crc") ->
+ "application/x-sv4crc";
+from_extension(".tar.gz") ->
+ "application/x-tar-gz";
+from_extension(".tgz") ->
+ "application/x-tar-gz";
+from_extension(".tar") ->
+ "application/x-tar";
+from_extension(".tcl") ->
+ "application/x-tcl";
+from_extension(".texi") ->
+ "application/x-texinfo";
+from_extension(".texinfo") ->
+ "application/x-texinfo";
+from_extension(".man") ->
+ "application/x-troff-man";
+from_extension(".me") ->
+ "application/x-troff-me";
+from_extension(".ms") ->
+ "application/x-troff-ms";
+from_extension(".roff") ->
+ "application/x-troff";
+from_extension(".t") ->
+ "application/x-troff";
+from_extension(".tr") ->
+ "application/x-troff";
+from_extension(".ustar") ->
+ "application/x-ustar";
+from_extension(".src") ->
+ "application/x-wais-source";
+from_extension(".zip") ->
+ "application/zip";
+from_extension(".tsi") ->
+ "audio/TSP-audio";
+from_extension(".au") ->
+ "audio/basic";
+from_extension(".snd") ->
+ "audio/basic";
+from_extension(".kar") ->
+ "audio/midi";
+from_extension(".mid") ->
+ "audio/midi";
+from_extension(".midi") ->
+ "audio/midi";
+from_extension(".mp2") ->
+ "audio/mpeg";
+from_extension(".mp3") ->
+ "audio/mpeg";
+from_extension(".mpga") ->
+ "audio/mpeg";
+from_extension(".aif") ->
+ "audio/x-aiff";
+from_extension(".aifc") ->
+ "audio/x-aiff";
+from_extension(".aiff") ->
+ "audio/x-aiff";
+from_extension(".m3u") ->
+ "audio/x-mpegurl";
+from_extension(".wax") ->
+ "audio/x-ms-wax";
+from_extension(".wma") ->
+ "audio/x-ms-wma";
+from_extension(".rpm") ->
+ "audio/x-pn-realaudio-plugin";
+from_extension(".ram") ->
+ "audio/x-pn-realaudio";
+from_extension(".rm") ->
+ "audio/x-pn-realaudio";
+from_extension(".ra") ->
+ "audio/x-realaudio";
+from_extension(".wav") ->
+ "audio/x-wav";
+from_extension(".pdb") ->
+ "chemical/x-pdb";
+from_extension(".ras") ->
+ "image/cmu-raster";
+from_extension(".gif") ->
+ "image/gif";
+from_extension(".ief") ->
+ "image/ief";
+from_extension(".jpe") ->
+ "image/jpeg";
+from_extension(".jpeg") ->
+ "image/jpeg";
+from_extension(".jpg") ->
+ "image/jpeg";
+from_extension(".jp2") ->
+ "image/jp2";
+from_extension(".png") ->
+ "image/png";
+from_extension(".tif") ->
+ "image/tiff";
+from_extension(".tiff") ->
+ "image/tiff";
+from_extension(".pnm") ->
+ "image/x-portable-anymap";
+from_extension(".pbm") ->
+ "image/x-portable-bitmap";
+from_extension(".pgm") ->
+ "image/x-portable-graymap";
+from_extension(".ppm") ->
+ "image/x-portable-pixmap";
+from_extension(".rgb") ->
+ "image/x-rgb";
+from_extension(".xbm") ->
+ "image/x-xbitmap";
+from_extension(".xwd") ->
+ "image/x-xwindowdump";
+from_extension(".iges") ->
+ "model/iges";
+from_extension(".igs") ->
+ "model/iges";
+from_extension(".mesh") ->
+ "model/mesh";
+from_extension(".") ->
+ "";
+from_extension(".msh") ->
+ "model/mesh";
+from_extension(".silo") ->
+ "model/mesh";
+from_extension(".vrml") ->
+ "model/vrml";
+from_extension(".wrl") ->
+ "model/vrml";
+from_extension(".css") ->
+ "text/css";
+from_extension(".htm") ->
+ "text/html";
+from_extension(".html") ->
+ "text/html";
+from_extension(".asc") ->
+ "text/plain";
+from_extension(".c") ->
+ "text/plain";
+from_extension(".cc") ->
+ "text/plain";
+from_extension(".f90") ->
+ "text/plain";
+from_extension(".f") ->
+ "text/plain";
+from_extension(".hh") ->
+ "text/plain";
+from_extension(".m") ->
+ "text/plain";
+from_extension(".txt") ->
+ "text/plain";
+from_extension(".rtx") ->
+ "text/richtext";
+from_extension(".sgm") ->
+ "text/sgml";
+from_extension(".sgml") ->
+ "text/sgml";
+from_extension(".tsv") ->
+ "text/tab-separated-values";
+from_extension(".jad") ->
+ "text/vnd.sun.j2me.app-descriptor";
+from_extension(".etx") ->
+ "text/x-setext";
+from_extension(".xml") ->
+ "application/xml";
+from_extension(".dl") ->
+ "video/dl";
+from_extension(".fli") ->
+ "video/fli";
+from_extension(".flv") ->
+ "video/flv";
+from_extension(".gl") ->
+ "video/gl";
+from_extension(".mp4") ->
+ "video/mp4";
+from_extension(".mpe") ->
+ "video/mpeg";
+from_extension(".mpeg") ->
+ "video/mpeg";
+from_extension(".mpg") ->
+ "video/mpeg";
+from_extension(".mov") ->
+ "video/quicktime";
+from_extension(".qt") ->
+ "video/quicktime";
+from_extension(".viv") ->
+ "video/vnd.vivo";
+from_extension(".vivo") ->
+ "video/vnd.vivo";
+from_extension(".asf") ->
+ "video/x-ms-asf";
+from_extension(".asx") ->
+ "video/x-ms-asx";
+from_extension(".wmv") ->
+ "video/x-ms-wmv";
+from_extension(".wmx") ->
+ "video/x-ms-wmx";
+from_extension(".wvx") ->
+ "video/x-ms-wvx";
+from_extension(".avi") ->
+ "video/x-msvideo";
+from_extension(".movie") ->
+ "video/x-sgi-movie";
+from_extension(".mime") ->
+ "www/mime";
+from_extension(".ice") ->
+ "x-conference/x-cooltalk";
+from_extension(".vrm") ->
+ "x-world/x-vrml";
+from_extension(".spx") ->
+ "audio/ogg";
+from_extension(".xhtml") ->
+ "application/xhtml+xml";
+from_extension(".bz2") ->
+ "application/x-bzip2";
+from_extension(".doc") ->
+ "application/msword";
+from_extension(".z") ->
+ "application/x-compress";
+from_extension(".ico") ->
+ "image/x-icon";
+from_extension(".bmp") ->
+ "image/bmp";
+from_extension(".m4a") ->
+ "audio/mpeg";
+from_extension(".csv") ->
+ "text/csv";
+from_extension(_) ->
+ undefined.
+
+%%
+%% Tests
+%%
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+exhaustive_from_extension_test() ->
+ T = mochiweb_cover:clause_lookup_table(?MODULE, from_extension),
+ [?assertEqual(V, from_extension(K)) || {K, V} <- T].
+
+from_extension_test() ->
+ ?assertEqual("text/html",
+ from_extension(".html")),
+ ?assertEqual(undefined,
+ from_extension("")),
+ ?assertEqual(undefined,
+ from_extension(".wtf")),
+ ok.
+
+-endif.

0 comments on commit 6c51e3e

Please sign in to comment.
Something went wrong with that request. Please try again.