Hello everyone,

I just wrote the below rewrite mod.  Just add .htaccess like rewrite rules as a list of tuples {regexp, replacewith} into the Rewrites variable (some examples included) in arg_rewrite.  It's a work in progress, I will add reading of the rewrite rules from a .erlaccess file or yaws.conf.  Perhaps this isn't as quick as the alternatives, but to me it saves the mess of having the reverse proxy if rewrites are all you want to do.

Same as .htaccess, and erlang in general, the first match in the Rewrites list is used, so least specific matching rules should go last.  If no matches are made, the path is passed through unchanged.

Any comments on the code itself are welcome.

-module(my_rewrite).
-include("/usr/local/lib/yaws/include/yaws_api.hrl").
-export([arg_rewrite/1, do_rewrite/2]).

arg_rewrite(Arg) ->
    Req = Arg#arg.req,
    {abs_path, Path} = Req#http_request.path,
    Rewrites = [{"^/enter/?$", "/old/menu.yaws"},
                {"^/enter/([a-z0-9]+)/?$", "/old/menu.yaws?id=$1"},
                {"^/enter/([a-z0-9]+)/([0-9]+)/?", "/old/menu.yaws?id=$1&p=$2"}],
    case do_rewrite(Path, Rewrites) of
        NewPath when is_list(NewPath) -> NewArg = Arg#arg{req = Req#http_request{path = {abs_path, NewPath}}};
        nomatch -> NewArg = Arg
    end,
    NewArg.
   
do_rewrite(Path, [{Exp, Replace}|Tail]) ->
    case rewrite_path(Path, Exp, Replace) of
        nomatch -> do_rewrite(Path, Tail);
        Else -> Else
    end.
   
rewrite_path(Path, Expression, ReplaceString) ->
    case re:run(Path, Expression, [{capture, all_but_first, list}]) of
        match -> ReplaceString;
        {match, Matches} ->    replace_matches(ReplaceString, Matches);
        _Else -> nomatch
    end.
   
replace_matches(String, Matches) ->
    replace_matches(String, Matches, 1).
replace_matches(String, [], _) ->
    String;
replace_matches(String, [Head|Tail], Count) ->
    Match = io_lib:format("\\$~p", [Count]),
    replace_matches(re:replace(String, Match, Head, [{return, list}]), Tail, Count + 1).

Andrew

On Mon, Jul 6, 2009 at 8:04 AM, Russell Brown <russell.brown@mac.com> wrote:
Hi Edward,
I use ngnix in front of yaws as a reverse proxy too. I reckon I could
just rewrite requests to *.asp to $1.yaws.

Russell
On 6 Jul 2009, at 02:44, Edward Stow wrote:

> Hi Steve
>
> Have you considered placing a reverse proxy in front of Yaws, I use
> nginx for this purpose.
>
> apache and  lighttpd  will also do the job.
>
>
> 2009/7/6 Steve Vinoski <vinoski@ieee.org>:
>> On Sun, Jul 5, 2009 at 2:49 PM, Russell
>> Brown<russell.brown@mac.com> wrote:
>>> Hi,
>>> Is there anyway I can configure yaws to handle files with a
>>> different
>>> extension (than .yaws) I have seen in the config there is
>>> the allowed_scripts
>>> property but that is only limited to yaws/cgi/php. Is this the
>>> same as the file extension? I'd like to type yaws dynamic code
>>> into a file and save it as
>>> (say) myfile.jsp and have yaws serve it.
>>> Is an odd request I know. I have a new client who wants some changes
>>> performing on their site. They have a lot .asp pages that I want to
>>> gradually recode as .yaws pages. I was hoping to keep the suffix
>>> to make
>>> life easy for all those who have bookmarked. I know I could get
>>> nginx or
>>> apache to rewrite the urls (asp -> yaws) (or even the ywas rewrite
>>> module)
>>> but is there another way?
>>
>> Hi Russell, a yaws rewrite module is definitely a possibility, and
>> maybe Klacke knows an even better way than the following, but
>> assuming
>> you build Yaws yourself, you could modify src/mime_type_c.erl and add
>> asp and jsp as special and revspecial suffixes, just as the yaws,
>> php,
>> and cgi suffixes are already coded there. Add lines for "jsp" and
>> "asp" where the calls to special() are made within the gen(T)
>> function, like this:
>>
>>    special(Fd, "jsp", yaws),
>>    special(Fd, "asp", yaws),
>>
>> and then do the same thing further down for the revspecial() calls.
>>
>> Admittedly kind of a hack, but it seems to work.
>>
>> --steve
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Erlyaws-list mailing list
>> Erlyaws-list@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/erlyaws-list
>>
>
>
>
> --
>
> Edward Stow


------------------------------------------------------------------------------
_______________________________________________
Erlyaws-list mailing list
Erlyaws-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/erlyaws-list