Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

postvar(), queryvar(), ssi docs

git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@562 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
  • Loading branch information...
commit b2d5a0008a20a24e78a751f9ebe64e34d656aa39 1 parent dbab7a3
@klacke authored
View
27 include/yaws_api.hrl
@@ -86,30 +86,7 @@
version}).
-%% Search a var, find it or crash
-
--define(VAR(__ARG, __Key),
- if (__ARG#arg.req)#http_request.method == 'GET' ->
- {value,{_,__Val}}=lists:keysearch(__Key,1,yaws_api:parse_query(__ARG)), __Val;
- (__ARG#arg.req)#http_request.method == 'POST' ->
- {value,{_,__Val}}=lists:keysearch(__Key,1,yaws_api:parse_post_data(__ARG)), __Val
- end).
-
-
-%% Search a var: return {ok, Val} | false
--define(SVAR(__ARG, __Key),
- if (__ARG#arg.req)#http_request.method == 'GET' ->
- case lists:keysearch(__Key,1,yaws_api:parse_query(__ARG)) of
- {value,{_,__Val}} -> {ok, __Val};
- false -> false
- end;
- (__ARG#arg.req)#http_request.method == 'POST' ->
- case lists:keysearch(__Key,1,yaws_api:parse_post_data(__ARG)) of
- {value,{_,__Val}} ->
- {ok, __Val};
- false ->
- false
- end
- end).
+
+
View
40 man/yaws_api.5
@@ -190,9 +190,15 @@ the HTTP transaction.
.TP
\fBparse_query(Arg)\fR
-This function will parse the query part of the URL which the client GETs
+This function will parse the query part of the URL.
It will return a {Key, Value} list of the items supplied in the query
-part of the URL. This only works with GET requests.
+part of the URL.
+
+.TP
+\fBqueryvar(Arg, VarName)\fR
+This function is automatically included from yaws_api in all
+ .yaws pages. It is used to search for a variable in the
+querypart of the url. Returns {ok, Val} or undefined.
.TP
@@ -200,6 +206,12 @@ part of the URL. This only works with GET requests.
This function will parse the POST data as supplied from the browser.
It will return a {Key, Value} list of the items set by the browser.
+.TP
+\fBpostvar(Arg, VarName)\fR
+This function is automatically included from yaws_api in all
+ .yaws pages. It is used to search for a variable in the
+POSTed data from the client. Returns {ok, Val} or undefined.
+
.TP
\fBparse_multipart_post(Arg)\fR
@@ -436,9 +448,6 @@ Location header. Set the status code.
Does a redirect to the same Scheme://Host:Port/Path as we
currently are executing in.
-
-
-
.TP
\fB{get_more, Cont, State}\fR
When we are receiving large POSTs we can return this value
@@ -459,6 +468,27 @@ accumulating the HTTP header \fIH\fR for page \fIPage\fR.
.TP
+\fB{ssi, File, Delimiter, Bindings}\fR
+Server side include File and macro expansion in File.
+Each occurence of a string, say "xyz", inside File which
+is inside Delimters is replaced with the corresponsing
+value in Bindings.
+
+Example:
+Delimiter = %%
+
+File contains the string .... %%xyz%% .....
+
+Bindings contain the tuple {"xyz", "Dingbat"}
+
+The occurence of %%xyz%% in File will be replaced with "Dingbat"
+in the Server side included output.
+
+The {ssi, File, Delimiter, Bindings} statement can also
+occur inside a deep ehtml structure.
+
+
+.TP
\fB[ListOfValues]\fR
It is possible to return a deep list of the above defined
return values. Any occurrence of \fIstream_content\fR, \fIget_more\fR
View
70 src/yaws_api.erl
@@ -16,7 +16,7 @@
-include("yaws_debug.hrl").
--export([parse_post_data/1]).
+
-export([parse_query/1, parse_post/1, parse_multipart_post/1,
@@ -49,7 +49,12 @@
-export([ehtml_expand/1, ehtml_expander/1, ehtml_apply/2,
ehtml_expander_test/0]).
--export([parse_set_cookie/1, format_set_cookie/1]).
+-export([parse_set_cookie/1, format_set_cookie/1,
+ postvar/2, queryvar/2]).
+
+%% remove entirely
+-export([parse_post_data/1]).
+
-import(lists, [map/2, flatten/1, reverse/1]).
@@ -57,41 +62,6 @@
%% yaws scripts
-parse_post_data(Arg) ->
-
- error_logger:info_msg("Warning Warning !!!! function "
- "yaws_api:parse_post_data will be removed ", []),
-
-
- Headers = Arg#arg.headers,
- Req = Arg#arg.req,
- case lists:keysearch('Content-Type', 3, Headers#headers.other) of
- {value, {_,_,_,_,"multipart/form-data"++Line}} ->
- case Arg#arg.cont of
- {cont, Cont} ->
- parse_multipart(
- binary_to_list(un_partial(Arg#arg.clidata)),
- {cont, Cont});
- undefined ->
- LineArgs = parse_arg_line(Line),
- {value, {_, Boundary}} =
- lists:keysearch(boundary, 1, LineArgs),
- parse_multipart(
- binary_to_list(un_partial(Arg#arg.clidata)), Boundary)
- end;
- _ ->
- case Req#http_request.method of
- 'POST' ->
- parse_post_data_urlencoded(un_partial(Arg#arg.clidata));
- _ ->
- %% kinda weird default bahaviour here
- parse_post_data_urlencoded(Arg#arg.querydata)
- end
- end.
-
-
-
-
%% parse the command line query data
parse_query(Arg) ->
@@ -1607,3 +1577,29 @@ skip_space([$\t|T]) -> skip_space(T);
skip_space(T) -> T.
%
+
+
+queryvar(ARG,Key) ->
+ case lists:keysearch(Key,1,yaws_api:parse_query(ARG)) of
+ {value,{_,undefined}} -> undefined;
+ {value,{_,Val}} -> {ok, Val};
+ false -> undefined
+ end.
+
+postvar(ARG, Key) ->
+ case lists:keysearch(Key,1,yaws_api:parse_post(ARG)) of
+ {value, {_,undefined}} -> undefined;
+ {value,{_,Val}} -> {ok, Val};
+ false -> undefined
+ end.
+
+
+
+
+parse_post_data(_Arg) ->
+
+ error_logger:info_msg("Error !!!! function "
+ "yaws_api:parse_post_data has been removed ", []),
+ exit(removed).
+
+
View
2  src/yaws_compile.erl
@@ -260,7 +260,7 @@ new_out_file(Line, C, GC) ->
io:format(Out, "%%~n%% code at line ~w from file ~s~n%%~n",
[Line, C#comp.infile]),
- io:format(Out, "-import(yaws_api, [f/2, fl/1, parse_post_data/2]). ~n~n", []),
+ io:format(Out, "-import(yaws_api, [f/2, fl/1, postvar/2, queryvar/2]). ~n~n", []),
io:format(Out, '-include("~s/include/yaws_api.hrl").~n',
[GC#gconf.yaws_dir]),
C#comp{outfd = Out,
View
8 www/index.yaws
@@ -45,8 +45,12 @@ out(A) ->
gen_news(A) ->
- {ok, Fd} = file:open(A#arg.docroot ++ "/news",[read]),
- gen_news(Fd, io:get_line(Fd,'')).
+ case file:open(A#arg.docroot ++ "/news",[read]) of
+ {ok, Fd} ->
+ gen_news(Fd, io:get_line(Fd,''));
+ Err ->
+ {p,[], f("No news: ~p",[Err])}
+ end.
gen_news(Fd, eof) ->
[];
View
2  www/man.yaws
@@ -4,7 +4,7 @@
out(A) ->
H = yaws_api:ssi(A#arg.docroot, ["/HEAD", "/TOPTAB"]),
- L = case ?SVAR(A,page) of
+ L = case queryvar(A,page) of
{ok, Page} ->
%% only allow regular chars in Page
case lists:all(fun(C) ->
View
100 www/post.yaws
@@ -1,47 +1,65 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-<HTML>
-<HEAD>
- <meta name="keywords" content="post">
-
-
- <TITLE>Top EE page</TITLE>
-
-<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
-
-</HEAD>
-<BODY BGCOLOR="#C6C6B5" text="#333333">
-
-
-<HTML>
-
-<H1> post.yaws </H1>
-
-<p> This is the <tt>post.yaws</tt>. This yaws file gets invoked with client data.
-This is the data that is submitted from the forms page <a href="form.yaws">form.yaws</a>.
-<p>POST data is naturally organized as a <tt>{Key, Value}</tt> list. A call to
-the function <tt>yaws_api:parse_post_data(A)</tt> will parse the
-POSTed data.
-
-<pre>
-out(A) ->
- L = f("&lt;pre>~n clidata: ~p~n &lt;/pre>",
- [yaws_api:parse_post_data(A)]),
- {html,L}.
-</pre>
-
-<p>Will produce the following output:
-
<erl>
-out(A) ->
- L = f("<pre>~n clidata: ~p~n </pre>",
- [yaws_api:parse_post_data(A)]),
- {html,L}.
-</erl>
-<p> It is up to the yaws programmer to parse this
-structure and extract the data from it.
+box(Str) ->
+ {'div',[{class,"box"}],
+ {pre,[],Str}}.
+
+fpostvar(A, Key) ->
+ case postvar(A,lang) of
+ undefined -> "None";
+ {ok, Val} ->
+ Val
+ end.
+
+
+out(A) ->
+ [{ssi, "HEAD", [],[]},
+ {ssi, "EXHEAD", [],[]},
+ {ehtml,
+ [{h2, [],"post.yaws"},
+ {p,[],[ "This is the ", {tt, [], "post.yaws"}, " file ",
+ "This yaws file gets invoked with client data. "
+ "This is the data that is submitted from the forms page ",
+ {a, [{href, "form.yaws"}], " form.yaws "}]},
+
+ {p, [], ["POST data is naturally organized as a ",
+ {tt, [], "{Key, Value} "}, " list. A call to "
+ "the function ", {tt, [], "yaws_api:parse_post(A) "},
+ " will parse the POSTed data. and return a key/value list "]},
+ box("
+out(A) ->
+ {ehtml,
+ {pre, [],
+ io_lib:format('~p', [yaws_api:parse_post(A)])}}.
+"),
+
+ {p, [], "Produces the following output"},
+
+ box(io_lib:format('~p', [yaws_api:parse_post(A)])),
+ {p, [], ["The natural way to extract the \"Variables\" from "
+ " the posted data is to use the function (defined in yaws_api.erl) "
+ " called ", {tt,[], "postvar(Arg,Key) "}]},
+ {p, [], "Thus the following erlang code: "},
+ box("
+out(A) ->
+ {ehtml,
+ {p,[], [\"The favourite programming language is \",
+ case postvar(A,lang) of
+ undefined -> \"None\";
+ {ok, Val} -> Val
+ end]}}.
+"),
+
+ {p,[], "Produces the following output:"},
+ {hr},
+ {'div',[{class,"box"}],
+ {p,[], ["The favourite programming language is ",
+ fpostvar(A,lang)]}}
+
+ ]},
+ {ssi, "END",[],[]}].
-</HTML>
+</erl>
View
13 www/query.yaws
@@ -45,7 +45,7 @@ pair in the query part of the URL.
<p> This fairly awkward way of getting at the query (or POST)
data in the Arg structure was only illustrated for the sake of
explaining the Arg structure and its fields. It is much easier
-to access query data through the <tt>?VAR(ARG,Key)</tt> macro.
+to access query data through the <tt>queryvar(ARG,Key)</tt> function.
The above example again:
@@ -53,7 +53,7 @@ The above example again:
out(A) ->
L="
out(A) ->
- Page = ?VAR(A,page),
+ {ok,Page} = queryvar(A,page),
L = os:cmd(\"man \" ++ Page ++ \" | col -b\"),
yaws_api:pre_ssi_string(L, \"man\").
",
@@ -63,12 +63,9 @@ yaws_api:pre_ssi_string(L).
</erl>
-<p>The <tt>?VAR(ARG,Key)</tt> macro returns the value of the
-variable if it is found in the page. If the varaible is not found
-the <tt>?VAR(ARG,Key)</tt> macro crashes. There is a safe version of
-macro called :<tt>?SVAR(ARG,Key)</tt> which returns either :
-<tt{ok, Value}</tt> or <tt>false</tt>
-
+<p>The <tt>queryvar(ARG,Key)</tt> function returns the value of the
+variable if it is found in the query part of the request. If the variable is not found
+or if the variable is unset, the <tt>queryvar(ARG,Key)</tt> function returns <tt>undefined</tt>.
View
2  www/redirect.yaws
@@ -14,7 +14,7 @@
<HTML>
-<H1> post.yaws </H1>
+<H1> redirect.yaws </H1>
<p> This a redirection example, the following <a href="redirect2.yaws">redirection url</a> contains the following code in its first <erl> group.
View
2  www/ssi.yaws
@@ -64,7 +64,7 @@ out(A) ->
{p,[], "So this is the way to do when we want to embed dynamic content"
" deep inside an ehtml structure, but the content isn't representable"
" as ehtml. This is typically the case for dynamically generated "
- " javascript"}
+ " javascript as well as dynamically generated java applets "}
]}].
Please sign in to comment.
Something went wrong with that request. Please try again.