%%% BEGIN openflax/handler/txt.erl %%% %%% %%% openflax - Open Source web server for Erlang/OTP %%% Copyright (c)2003 Cat's Eye Technologies. All rights reserved. %%% %%% Redistribution and use in source and binary forms, with or without %%% modification, are permitted provided that the following conditions %%% are met: %%% %%% Redistributions of source code must retain the above copyright %%% notice, this list of conditions and the following disclaimer. %%% %%% Redistributions in binary form must reproduce the above copyright %%% notice, this list of conditions and the following disclaimer in %%% the documentation and/or other materials provided with the %%% distribution. %%% %%% Neither the name of Cat's Eye Technologies nor the names of its %%% contributors may be used to endorse or promote products derived %%% from this software without specific prior written permission. %%% %%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND %%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, %%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF %%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE %%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE %%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, %%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, %%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, %%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON %%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, %%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY %%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE %%% POSSIBILITY OF SUCH DAMAGE. %% @doc View-as-text handler for OpenFlax. %% %%

Some browsers (notably Internet Explorer) prefer to download files %% if there is a file assocation for the files' extension configured. %% This handler gets around that by displaying the file as plaintext, %% generally with an extension of .txt to convince the %% browser likewise.

%% %%

This handler's security/featurefulness tradeoff level is %% 5 (Moderate.) It requires openflax.handler.file.

%% %% @end -module(openflax.handler.txt). -vsn('$Id: txt.erl 31 2004-04-23 07:00:11Z catseye $'). -author('cpressey@catseye.mine.nu'). -copyright('Copyright (c)2003 Cat`s Eye Technologies. All rights reserved.'). -behaviour(openflax.handler). -export([start/1, stop/1, serve/1]). % behaviour -import(filename). -import(filelib). %% @spec start(conf()) -> conf() %% @doc Initializes the view-as-text subsystem. start(Conf) -> openflax.conf:new(). %% @spec stop(conf()) -> conf() %% @doc Shuts down the view-as-text subsystem. stop(Conf) -> Conf. %% @spec serve(conf()) -> {response(), conf()} %% @doc Serves a text file generated from another file. serve(Conf) -> FileName = openflax.handler.file:document(Conf), case openflax.handler.file:is_accessible(FileName, Conf) of true -> % if it exists as a real file, send that. Conf0 = openflax.conf:put_value(res_content_type, "text/plain", Conf), {openflax.handler.file, Conf0}; false -> % otherwise, look for some file that has had .txt added to it RootName = filename:rootname(FileName), case openflax.handler.file:is_accessible(RootName, Conf) andalso not filelib:is_dir(RootName) of true -> BasicResource = openflax.conf:get_string(sreq_basic_resource, Conf), Conf0 = openflax.conf:put_value(sreq_basic_resource, filename:rootname(BasicResource), Conf), Conf1 = openflax.conf:put_value(res_content_type, "text/plain", Conf0), {openflax.handler.file, Conf1}; false -> {not_found, Conf} end end. %%% END of openflax/handler/txt.erl %%%