Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update docs & testsuite accordingly

  • Loading branch information...
commit fdc06fd38268737709b94ae32ccec3c19f300421 1 parent f5343a3
@capflam capflam authored
View
21 doc/yaws.tex
@@ -1699,8 +1699,9 @@ \section{All out/1 return values}
\verb+POST+s we can return this value and be invoked again when more
data arrives.
-\item \verb+{page, Page}+ Make \Yaws\ return a different local page
- than the one being requested.
+\item \verb+{page, Page}+ Make \Yaws\ returns a different local page than the
+ one being requested. \verb+Page+ is a Request-URI, so it must be url-encoded
+ and can contain a query-string.
\item \verb+{page, {Options, Page}}+ Like the above, but supplying an
additional deep list of options. For now, the only type of option
@@ -3028,14 +3029,14 @@ \section{Server Part}
Path = code
Path = code URL
\end{verbatim}
- \verb+Path+ must begin with a slash. \verb+URL+ may be either a
- relative URL (a path beginning with a slash), or an absolute
- URL. In the first case, the \textit{scheme:hostname:port} of the
- current server will be added. All accesses to \verb+Path+ will be
- redirected to \verb+URL/Path+ (or
- \verb+scheme:hostname:port/URL/Path+ if \verb+URL+ is
- relative). Note that the original path is appended to the
- redirected URL.
+ \verb+Path+ must be an url-decoded path beginning with a
+ slash. \verb+URL+ may be either a relative URL (a path beginning
+ with a slash), or an absolute URL. In the first case, the
+ \textit{scheme:hostname:port} of the current server will be
+ added. All accesses to \verb+Path+ will be redirected to
+ \verb+URL/Path+ (or \verb+scheme:hostname:port/URL/Path+ if
+ \verb+URL+ is relative). \verb+URL+ must be url-encoded. Note that
+ the original path is appended to the redirected URL.
For example, assume we have the following redirect configuration:
\begin{verbatim}
View
12 man/yaws.conf.5
@@ -1001,12 +1001,12 @@ We can have a series of redirect rules in one of formats below:
Path = code URL
.fi
-\fBPath\fR must begin with a slash. \fBURL\fR may be either a relative URL (a
-path beginning with a slash), or an absolute URL. In the first case, the
-\fIscheme:hostname:port\fR of the current server will be added. All accesses to
-\fBPath\fR will be redirected to \fBURL/Path\fR (or
-\fBscheme:hostname:port/URL/Path\fR if \fBURL\fR is relative). Note that the
-original path is appended to the redirected URL.
+\fBPath\fR must be an url-decoded path beginning with a slash. \fBURL\fR may be
+either a relative URL (a path beginning with a slash), or an absolute URL. In
+the first case, the \fIscheme:hostname:port\fR of the current server will be
+added. All accesses to \fBPath\fR will be redirected to \fBURL/Path\fR (or
+\fBscheme:hostname:port/URL/Path\fR if \fBURL\fR is relative). \fBURL\fR must be
+url-encoded. Note that the original path is appended to the redirected URL.
For example, assume we have the following redirect configuration:
View
5 man/yaws_api.5
@@ -918,8 +918,9 @@ and be invoked again when more Data arrives.
.TP
\fB{page, Page}\fR
-Make Yaws return a different page than the one being
-requested.
+
+Make Yaws returns a different page than the one being requested. \fIPage\fR is a
+Request-URI, so it must be url-encoded and can contain a query-string.
.TP
View
2  src/yaws_config.erl
@@ -2347,7 +2347,7 @@ parse_redirect(Path, [Code, URL], Mode, Lno) ->
{Path, I, P, Mode};
#url{} ->
{error, ?F("Bad redirect rule at line ~w: "
- " Absolute URL is forbidden here", [URL])}
+ " Absolute URL is forbidden here", [Lno])}
catch _:_ ->
{error, ?F("Bad redirect URL ~p at line ~w", [URL, Lno])}
end;
View
9 test/conf/redirectconf.conf
@@ -106,7 +106,8 @@ max_size_cached_file = 5120000
listen = 0.0.0.0
listen_backlog = 512
deflate = false
- docroot = %YTOP%/test/t9
+ docroot = %YTOP%/test/t9/www
+ partial_post_size = 5
<redirect>
/default_redirect1 = /redir
/default_redirect2 == /redir
@@ -122,5 +123,11 @@ max_size_cached_file = 5120000
/404_redirect2 == 404
/404_redirect3 = 404 /error404.yaws
/404_redirect4 == 404 /error404.yaws
+
+ "/redirect?url?encode1" = "/redir%3F"
+ "/redirect?url?encode2" == "/redir%3F"
+
+ /post_redirect1 == 302 /test/index.yaws
+ /post_redirect2 == 200 /test/index.yaws
</redirect>
</server>
View
107 test/t9/app_test.erl
@@ -18,6 +18,9 @@ start() ->
test_301_redirect(),
test_404_redirect(),
test_bad_redirect(),
+ test_redirect_url_encode(),
+ test_redirect_querystring(),
+ test_redirect_post(),
ibrowse:stop().
@@ -96,15 +99,15 @@ test_404_redirect() ->
?line {ok, "404", _, Body2} = ibrowse:send_req(Uri2, [], get),
?line Err404 = Body2,
- %% /404_redirect3 -> /error404 (append)
+ %% /404_redirect3 -> /error404.yaws (append)
Uri3 = "http://localhost:8000/404_redirect3/index.html",
?line {ok, "404", _, Body3} = ibrowse:send_req(Uri3, [], get),
- ?line "/404_redirect3/index.html\n" = Body3,
+ ?line "/error404.yaws/404_redirect3/index.html\n" = Body3,
- %% /404_redirect4 -> /error404 (noappend)
+ %% /404_redirect4 -> /error404.yaws (noappend)
Uri4 = "http://localhost:8000/404_redirect4/index.html",
?line {ok, "404", _, Body4} = ibrowse:send_req(Uri4, [], get),
- ?line "\n" = Body4,
+ ?line "/error404.yaws\n" = Body4,
ok.
@@ -121,3 +124,99 @@ test_bad_redirect() ->
?line {error, _} =
yaws_config:load(Env#env{conf={file, "bad_redirect4.conf"}}),
ok.
+
+
+test_redirect_url_encode() ->
+ io:format("test_redirect_url_encode\n", []),
+
+ %% /redirect?url?encode1 -> /redir? (append)
+ Uri1 = "http://localhost:8000/redirect%3Furl%3Fencode1/index.html",
+ ?line {ok, "302", Hdrs1, _} = ibrowse:send_req(Uri1, [], get),
+ ?line "http://localhost:8000/redir%3F/redirect%3Furl%3Fencode1/index.html" =
+ proplists:get_value("Location", Hdrs1),
+
+ %% /redirect?url?encode2 -> /redir? (noappend)
+ Uri2 = "http://localhost:8000/redirect%3Furl%3Fencode2/index.html",
+ ?line {ok, "302", Hdrs2, _} = ibrowse:send_req(Uri2, [], get),
+ ?line "http://localhost:8000/redir%3F" =
+ proplists:get_value("Location", Hdrs2),
+ ok.
+
+
+test_redirect_querystring() ->
+ io:format("test_redirect_querystring\n", []),
+
+ %% /default_redirect1 -> /redir (relative-url + append)
+ Uri1 = "http://localhost:8000/default_redirect1/index.html?a=b&c=d",
+ ?line {ok, "302", Hdrs1, _} = ibrowse:send_req(Uri1, [], get),
+ ?line "http://localhost:8000/redir/default_redirect1/index.html?a=b&c=d" =
+ proplists:get_value("Location", Hdrs1),
+
+ %% /default_redirect2 -> /redir (relative-url + noappend)
+ Uri2 = "http://localhost:8000/default_redirect2/index.html?a=b&c=d",
+ ?line {ok, "302", Hdrs2, _} = ibrowse:send_req(Uri2, [], get),
+ ?line "http://localhost:8000/redir?a=b&c=d" =
+ proplists:get_value("Location", Hdrs2),
+
+ %% /default_redirect3 -> /redir (absolute-url + append)
+ Uri3 = "http://localhost:8000/default_redirect3/index.html?a=b&c=d",
+ ?line {ok, "302", Hdrs3, _} = ibrowse:send_req(Uri3, [], get),
+ ?line "http://yaws.hyber.org/default_redirect3/index.html?a=b&c=d" =
+ proplists:get_value("Location", Hdrs3),
+
+ %% /default_redirect4 -> /redir (absolute-url + noappend)
+ Uri4 = "http://localhost:8000/default_redirect4/index.html?a=b&c=d",
+ ?line {ok, "302", Hdrs4, _} = ibrowse:send_req(Uri4, [], get),
+ ?line "http://yaws.hyber.org/?a=b&c=d" =
+ proplists:get_value("Location", Hdrs4),
+
+ %% /404_redirect3 -> /error404.yaws (append)
+ Uri5 = "http://localhost:8000/404_redirect3/index.html?a=b&c=d",
+ ?line {ok, "404", _, Body5} = ibrowse:send_req(Uri5, [], get),
+ ?line "/error404.yaws/404_redirect3/index.html?a=b&c=d\n" = Body5,
+
+ %% /404_redirect4 -> /error404.yaws (noappend)
+ Uri6 = "http://localhost:8000/404_redirect4/index.html?a=b&c=d",
+ ?line {ok, "404", _, Body6} = ibrowse:send_req(Uri6, [], get),
+ ?line "/error404.yaws?a=b&c=d\n" = Body6,
+ ok.
+
+
+test_redirect_post() ->
+ io:format("test_redirect_post\n", []),
+
+ %% (partial_post_size = 5 / content-length = 11)
+ Data = "foo=1&bar=2",
+ Sz = length(Data),
+ Hdrs = [{content_length, Sz},
+ {content_type, "application/x-www-form-urlencoded"}],
+
+ %% Use the same connection for all requests
+ {ok, ConnPid} = ibrowse:spawn_worker_process("localhost", 8000),
+
+ %% Data must be flushed on redirect (here, trailing slash is added)
+ Uri1 = "http://localhost:8000/test",
+ ?line {ok, "302", Hdrs1, _} = ibrowse:send_req_direct(ConnPid, Uri1, Hdrs,
+ post, Data, []),
+ ?line Uri2 = proplists:get_value("Location", Hdrs1),
+ ?line "http://localhost:8000/test/" = Uri2,
+ ?line {ok, "200", _, _} = ibrowse:send_req_direct(ConnPid, Uri2, [], get),
+
+ %% Data must be flushed on redirect (here, on configured redirect)
+ Uri3 = "http://localhost:8000/post_redirect1",
+ ?line {ok, "302", Hdrs3, _} = ibrowse:send_req_direct(ConnPid, Uri3, Hdrs,
+ post, Data, []),
+ ?line Uri4 = proplists:get_value("Location", Hdrs3),
+ ?line "http://localhost:8000/test/index.yaws" = Uri4,
+ ?line {ok, "200", _, _} = ibrowse:send_req_direct(ConnPid, Uri4, [], get),
+
+
+ %% Data must be readable by /test/index.yaws
+ Uri5 = "http://localhost:8000/post_redirect2",
+ ?line {ok, "200", _, Body5} = ibrowse:send_req_direct(ConnPid, Uri5, Hdrs,
+ post, Data, []),
+ ?line Body5 = Data ++ "\n",
+
+ ibrowse:stop_worker_process(ConnPid),
+
+ ok.
View
7 test/t9/error404.yaws
@@ -1,7 +0,0 @@
-<erl>
-out(Arg) ->
- case Arg#arg.pathinfo of
- undefined -> {content, "text/plain", ""};
- PathInfo -> {content, "text/plain", PathInfo}
- end.
-</erl>
View
5 test/t9/www/error404.yaws
@@ -0,0 +1,5 @@
+<erl>
+out(Arg) ->
+ {abs_path, Path} = (Arg#arg.req)#http_request.path,
+ {content, "text/plain", Path}.
+</erl>
View
17 test/t9/www/test/index.yaws
@@ -0,0 +1,17 @@
+<erl>
+out(Arg) ->
+ case Arg#arg.clidata of
+ undefined ->
+ {html, "no data"};
+ {partial, Bin} ->
+ case Arg#arg.cont of
+ undefined -> {get_more, [Bin], Arg#arg.state};
+ Cont -> {get_more, [Bin|Cont], Arg#arg.state}
+ end;
+ Bin ->
+ case Arg#arg.cont of
+ undefined -> {content, "text/plain", Bin};
+ Cont -> {content, "text/plain", lists:reverse([Bin|Cont])}
+ end
+ end.
+</erl>
Please sign in to comment.
Something went wrong with that request. Please try again.