<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 12, 2015 at 7:07 PM, Imants Cekusins <span dir="ltr"><<a href="mailto:imantc@gmail.com" target="_blank">imantc@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">actually, does Yaws interpret  #rewrite_response{} in any way?<br>
or is it but a convenience hint to the appmod?<br>
<br>
if Yaws is looking for #rewrite_response,<br>
maybe add a rewrite_response field to #arg so it is obvious where to put it?<br>
<br>
just an idea..<br>
</blockquote></div><br></div><div class="gmail_extra">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.<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">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:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">-module(methods_dpmod).</div><div class="gmail_extra">-export([dispatch/1]).</div><div class="gmail_extra"><br></div><div class="gmail_extra">-include_lib("yaws_api.hrl").</div><div class="gmail_extra"><br></div><div class="gmail_extra">dispatch(Arg) -></div><div class="gmail_extra">    Allowed = ['GET', 'POST'],</div><div class="gmail_extra">    Req = yaws_api:arg_req(Arg),</div><div class="gmail_extra">    Method = yaws_api:http_request_method(Req),</div><div class="gmail_extra">    case lists:member(Method, Allowed) of</div><div class="gmail_extra">        true -></div><div class="gmail_extra">            continue;</div><div class="gmail_extra">        false -></div><div class="gmail_extra">            Vsn = yaws_api:http_request_version(Req),</div><div class="gmail_extra">            Resp = #http_response{</div><div class="gmail_extra">                      version=Vsn,</div><div class="gmail_extra">                      status=405,</div><div class="gmail_extra">                      phrase=yaws_api:code_to_phrase(405)},</div><div class="gmail_extra">            AllowedStrs = string:join([atom_to_list(M) || M <- Allowed], ","),</div><div class="gmail_extra">            HdrVals = [{"Allow", AllowedStrs},</div><div class="gmail_extra">                       {"Content-Length", "0"}],</div><div class="gmail_extra">            Headers = lists:foldl(fun({H,V}, Hdrs) -></div><div class="gmail_extra">                                          yaws_api:set_header(Hdrs, H, V)</div><div class="gmail_extra">                                  end, #headers{}, HdrVals),</div><div class="gmail_extra">            HdrStrings = yaws_api:reformat_header(Headers),</div><div class="gmail_extra">            Reply = [yaws_api:reformat_response(Resp), "\r\n",</div><div class="gmail_extra">                     string:join(HdrStrings, "\r\n"), "\r\n\r\n"],</div><div class="gmail_extra">            Sock = yaws_api:arg_clisock(Arg),</div><div class="gmail_extra">            case yaws_api:get_sslsocket(Sock) of</div><div class="gmail_extra">                {ok, SslSock} -></div><div class="gmail_extra">                    ssl:send(SslSock, Reply);</div><div class="gmail_extra">                undefined -></div><div class="gmail_extra">                    gen_tcp:send(Sock, Reply)</div><div class="gmail_extra">            end,</div><div class="gmail_extra">            done</div><div class="gmail_extra">    end.</div><div class="gmail_extra"><br></div><div class="gmail_extra">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.)</div><div class="gmail_extra"><br></div><div class="gmail_extra">--steve</div></div></div>