Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

arg_rewrite mods can now return HTTP responses (capflam)

Module used to rewrite arg records can now return any HTTP
response. This can be used to redirect requests or to return an
error. To do so, such a module must set the element #arg.state using
the record #rewrite_response{}. The record #rewrite_response{}
contains 3 elements:

  status: any valid HTTP status code
  headers: a list of {header, H}
  content: an iolist

For example, to do an unconditional redirect to http://www.yakaz.com,
you can use the module simple_redir_mod.erl:

  -module(simple_redir_mod.erl).
  -export([arg_rewrite/1]).
  -include_lib("yaws/include/yaws_api.hrl").

  arg_rewrite(Arg) ->
    L = "http://www.yakaz.com",
    H = [{header, {location, L}}],
    RwResp = #rewrite_response{status=301, headers=H}
    Arg#arg{state=RwResp}.
  • Loading branch information...
commit f983a1b55aae80a1db170893a483522570232011 1 parent 3ad1703
@capflam capflam authored vinoski committed
Showing with 20 additions and 1 deletion.
  1. +4 −0 include/yaws_api.hrl
  2. +16 −1 src/yaws_server.erl
View
4 include/yaws_api.hrl
@@ -47,6 +47,10 @@
status,
phrase}).
+-record(rewrite_response, {status,
+ headers = [],
+ content = <<>>}).
+
-record(headers, {
connection,
accept,
View
17 src/yaws_server.erl
@@ -1490,6 +1490,22 @@ handle_extension_method(_Method, CliSock, Req, Head) ->
%% Return values:
%% continue, done, {page, Page}
+handle_request(CliSock, ARG, _N)
+ when is_record(ARG#arg.state, rewrite_response) ->
+ State = ARG#arg.state,
+ ?Debug("SrvReq=~s - RwResp=~s~n",[?format_record(ARG#arg.req, http_request),
+ ?format_record(State, rewrite_response)]),
+ yaws:outh_set_status_code(State#rewrite_response.status),
+ deepforeach(fun(X) ->
+ case X of
+ {header, H} -> yaws:accumulate_header(H);
+ _ -> ok
+ end
+ end, State#rewrite_response.headers),
+ accumulate_content(State#rewrite_response.content),
+ deliver_accumulated(ARG, CliSock, decide, undefined, final),
+ done_or_continue();
+
handle_request(CliSock, ARG, N) ->
Req = ARG#arg.req,
?Debug("SrvReq=~s~n",[?format_record(Req, http_request)]),
@@ -2281,7 +2297,6 @@ new_redir_h(OH, Loc, Status) ->
put(outh, OH2).
-
%% we must deliver a 302 if the browser asks for a dir
%% without a trailing / in the HTTP req
%% otherwise the relative urls in /dir/index.html will be broken.
Please sign in to comment.
Something went wrong with that request. Please try again.