From 571719a164326eebdc792b43170fe27f123aac0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 2 Nov 2018 15:31:54 +0100 Subject: Add a charset option to cowboy_static --- src/cowboy_static.erl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl index ae4e7c0..67cc08f 100644 --- a/src/cowboy_static.erl +++ b/src/cowboy_static.erl @@ -19,11 +19,13 @@ -export([malformed_request/2]). -export([forbidden/2]). -export([content_types_provided/2]). +-export([charsets_provided/2]). -export([resource_exists/2]). -export([last_modified/2]). -export([generate_etag/2]). -export([get_file/2]). +-type extra_charset() :: {charset, module(), function()} | {charset, binary()}. -type extra_etag() :: {etag, module(), function()} | {etag, false}. -type extra_mimetypes() :: {mimetypes, module(), function()} | {mimetypes, binary() | {binary(), binary(), [{binary(), binary()}]}}. @@ -322,6 +324,22 @@ content_types_provided(Req, State={Path, _, Extra}) -> {[{Type, get_file}], Req, State} end. +%% Detect the charset of the file. + +-spec charsets_provided(Req, State) + -> {[binary()], Req, State} + when State::state(). +charsets_provided(Req, State={Path, _, Extra}) -> + case lists:keyfind(charset, 1, Extra) of + %% We simulate the callback not being exported. + false -> + no_call; + {charset, Module, Function} -> + {[Module:Function(Path)], Req, State}; + {charset, Charset} -> + {[Charset], Req, State} + end. + %% Assume the resource doesn't exist if it's not a regular file. -spec resource_exists(Req, State) -- cgit v1.2.3