[erlang-questions] configure http methods in yaws

Steve Vinoski vinoski@REDACTED
Fri Feb 13 02:39:04 CET 2015


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

> actually, does Yaws interpret  #rewrite_response{} in any way?
> or is it but a convenience hint to the appmod?
>
> if Yaws is looking for #rewrite_response,
> maybe add a rewrite_response field to #arg so it is obvious where to put
> it?
>
> just an idea..
>

Yes, Yaws knows to check the #arg.state field for a #rewrite_response
record and create a response from it if found. As for the new field, that
won't happen since it would mean supporting both the state field for
existing apps and the new field for new or revised apps. Instead, I'll just
add the example from the email thread to the Yaws docs.

BTW, here's another way to allow only GET and POST requests, this time
using a dispatchmod, which is documented in the PDF docs. A dispatchmod is
called very early in the dispatch flow and can either continue dispatching
or can send replies directly on the socket and tell Yaws the request is
already handled:

-module(methods_dpmod).
-export([dispatch/1]).

-include_lib("yaws_api.hrl").

dispatch(Arg) ->
    Allowed = ['GET', 'POST'],
    Req = yaws_api:arg_req(Arg),
    Method = yaws_api:http_request_method(Req),
    case lists:member(Method, Allowed) of
        true ->
            continue;
        false ->
            Vsn = yaws_api:http_request_version(Req),
            Resp = #http_response{
                      version=Vsn,
                      status=405,
                      phrase=yaws_api:code_to_phrase(405)},
            AllowedStrs = string:join([atom_to_list(M) || M <- Allowed],
","),
            HdrVals = [{"Allow", AllowedStrs},
                       {"Content-Length", "0"}],
            Headers = lists:foldl(fun({H,V}, Hdrs) ->
                                          yaws_api:set_header(Hdrs, H, V)
                                  end, #headers{}, HdrVals),
            HdrStrings = yaws_api:reformat_header(Headers),
            Reply = [yaws_api:reformat_response(Resp), "\r\n",
                     string:join(HdrStrings, "\r\n"), "\r\n\r\n"],
            Sock = yaws_api:arg_clisock(Arg),
            case yaws_api:get_sslsocket(Sock) of
                {ok, SslSock} ->
                    ssl:send(SslSock, Reply);
                undefined ->
                    gen_tcp:send(Sock, Reply)
            end,
            done
    end.

This is a bit more involved than the arg rewriter because it assembles and
sends the reply directly. (BTW note that the SSL portions of this example
won't work without a very recent version of Yaws, due to a bug I fixed Feb.
3.)

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


More information about the erlang-questions mailing list