[erlang-questions] configure http methods in yaws

Steve Vinoski vinoski@REDACTED
Thu Feb 12 22:54:46 CET 2015


On Thu, Feb 12, 2015 at 3:30 PM, Imants Cekusins <imantc@REDACTED> wrote:

> >  I want to avoid to test at yapp level for allowable methods
>
> config:
> arg_rewrite_mod = your_module
>
> your_module:out(Arg)->
>  Arg1 = test(Arg)
>  yapp:out(Arg1).


Right idea, but wrong implementation. A rewriter exports arg_rewrite/1, not
out/1, and it doesn't call the target resource directly but lets Yaws do
that (if appropriate).

Here's an args rewriter that does what's required:

-module(methods_rewriter).
-export([arg_rewrite/1]).

-include_lib("yaws_api.hrl").

arg_rewrite(Arg) ->
    Allowed = ['GET', 'POST'],
    Req = yaws_api:arg_req(Arg),
    Method = yaws_api:http_request_method(Req),
    case lists:member(Method, Allowed) of
        true ->
            Arg;
        false ->
            AllowedStrs = string:join([atom_to_list(M) || M <- Allowed],
","),
            Arg#arg{state=
                        #rewrite_response{
                           status=405,
                           headers=[{header, {"Allow", AllowedStrs}},
                                    {header, {connection, "close"}}]}}
    end.

If the incoming method is GET or POST, the dispatch continues as normal,
otherwise Yaws returns a 405 Method Not Allowed and includes an Allow
header indicating only GET and POST are allowed.

Compile this and make sure its beam is in the Yaws code load path. In the
server config block for your server, set

arg_rewrite_mod = methods_rewriter

so Yaws knows to use the module for rewriting.

Any problems, let me know.

--steve
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150212/45dcdf8e/attachment.htm>


More information about the erlang-questions mailing list