.TH YAWS_API "5" "" "" "User API" .SH NAME yaws_api \- api available to yaws web server programmers .SH SYNOPSIS .B yaws_api:Function(...) .SH DESCRIPTION .PP This is the api available to yaws web server programmers. The erlang module yaws_api contains a wide variety of functions that can be used inside yaws pages. .PP Each chunk of yaws code is executed while the yaws page is being delivered from the server. We give a very simple example here to show the basic idea. Imagine the following html code: \fI .nf
Insert this text into the document"}.
tag. The data will be
htmlized.
.TP
\fBpre_ssi_string(String)\fR
Include htmlized content from String.
.TP
\fBf(Fmt, Args)\fR
The equivalent of io_lib:format/2. This function is automatically
-included in all erlang code which is a part of a yaws page.
.TP
\fBhtmlize(Binary | List | Char)\fR
Htmlize an IO list object.
.TP
\fBsetcookie(Name, Value, [Path, [ Expire, [Domain , [Secure]]]])\fR
Sets a cookie to the browser.
.TP
\fBfind_cookie_val(Cookie, Header)\fR
This function can be used to search for a cookie that was previously
set by \fBsetcookie/2-6\fR. For example if we set a cookie
as \fByaws_api:setcookie("sid",SomeRandomSid) \fR, then on subsequent requests
from the browser we can call:
\fBfind_cookie("sid",(Arg#arg.headers)#headers.cookie)\fR
The function returns [] if no cookie was found, otherwise the actual cookie
is returned as a string.
.TP
\fBredirect(Url\fR
This function generates a redirect to the browser.
It will clear any previously set headers. So to generate
a redirect \fBand\fR set a cookie, we need to set the cookie after
the redirect as in:
\fI
.nf
out(Arg) ->
... do some stuff
Ret = [{redirect, "http://www.somewhere.com"},
setcookie("sid", Random)
].
.fi
\fR
.TP
\fBget_line(String)\fR
This function is convenient when getting \\r\\n terminated lines
from a stream of data. It returns:
\fB{line, Line, Tail}\fR or \fB{lastline, Line, Tail}\fR
The function handles multilines as defined in e.g. SMTP or HTTP
.TP
\fBmime_type(FileName)\fR
Returns the mime type as defined by the extension of FileName
.TP
\fBstream_chunk_deliver(YawsPid, Data)\fR
When a yaws function needs to deliver chunks of data which it gets
from a process. The other process can call this function to deliver
these chunks. It requires the \fBout/1\fR function to return the
value \fB{streamcontent, MimeType, FirstChunk}\fR to work.
YawsPid is the process identifier of the yaws process delivering the original
.yaws file. That is self() in the yaws code.
The Pid must typically be passed (somehow) to the producer of the stream.
.TP
\fBstream_chunk_deliver_blocking(YawsPid, Data)\fR
A syncronous verion of the above function. This syncronous version must always
be used when the producer of the stream is faster than the consumer.
This is usually the case since the client is the WWW browser.
.TP
\fBstream_chunk_end(YawsPid)\fR
When the process discussed above is done delivering data, it must call
this function to let the yaws content delivering process finish up
the HTTP transaction.
.TP
\fBparse_query(Arg)\fR
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.
.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
\fBparse_post(Arg)\fR
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
\fBgetvar(Arg, VarName)\fR
This function looks at the HTTP request method from the
client and invokes postvar/2 if it is a POST from the client
and queryvar/2 if it is a GET request from the client.
.TP
\fBparse_multipart_post(Arg)\fR
If the browser has set the Content-Type header to the value
"multipart/form-data", which is the case when the browser
wants to upload a file to the server the following happens:
If the function returns \fB{result, Res}\fR no more data
will come from the browser.
If the function returns \fB{cont, Cont, Res}\fR the browser
will supply more data. (The file was to big to come in one read)
This indicates that there is more data to come and the out/1 function
should return {get_more, Cont, User_state} where User_state might
usefully be a File Descriptor.
The Res value is a list of either:
\fB{header, Header}\fR | \fB{part_body, Binary}\fR | \fB{body, Binary}\fR
Example usage could be:
\fI
.nf
out(A) ->
case yaws_api:parse_multipart_post(A) of
{cont, Cont, Res} ->
St = handle_res(A, Res),
{get_more, Cont, St};
{result, Res} ->
handle_res(A, Res),
{html, f("Done
",[])}
end.
handle_res(A, [{head, Name}|T]) ->
io:format("head:~p~n",[Name]),
handle_res(A, T);
handle_res(A, [{part_body, Data}|T]) ->
io:format("part_body:~p~n",[Data]),
handle_res(A, T);
handle_res(A, [{body, Data}|T]) ->
io:format("body:~p~n",[Data]),
handle_res(A, T);
handle_res(A, []) ->
io:format("End_res~n").
.fi
\fR
.TP
\fBnew_cookie_session(Opaque)\fR
Create a new cookie based session, the yaws system will set the
cookie. The new randomgenerated cookie is returned from this
function. The Opaque argument will typically contain user data
such as username and password
.TP
\fBnew_cookie_session(Opaque, TTL)\fR
As above, but allows to set a session specific time-out value,
overriding teh system specified time-out value.
.TP
\fBcookieval_to_opaque(CookieVal)\fR
.TP
\fBprint_cookie_sessions() \fR
.TP
\fBreplace_cookie_session(Cookie, NewOpaque)\fR
.TP
\fBdelete_cookie_session(Cookie)\fR
.TP
\fBsetconf(Gconf, Groups)\fR
This function is intended for embedded mode in yaws. It makes it possible
to load a yaws configuration from another data source than /usr/local/etc/yaws.conf, such
as a database.
If yaws is started with the environment \fI{embedded, true}\fR, yaws will
start with an empty default configuration, and wait for some other
program to execute a \fIsetconf/2\fR
The Gconf is a \fI#gconf{}\fR record and the Group variable is
a list of lists of \fI#sconf{}\fR records. Each sublist must
contain \fI#sconf{}\fR records with the same IP/Port listen address.
.TP
\fBurl_decode(Str)\fR
Decode url-encoded string. A URL ncoded string is a string where
all alfa numeric characters and the the character _ are preserved
and all other characters are encode as "%XY" where X and Y are the
hex values of the least respective most significat 4 bits in the 8 bit
character.
.TP
\fBurl_encode(Str)\fR
Url-encodes a string. All URLs in HTML documents must be URL encoded.
.TP
\fBreformat_header(H)\fR
Returns a list of reformated header values from a #header{}
record. The return list is suitable for retransmit.
.TP
\fBrequest_url(ARG)\fR
Return the url as requested by the client. Return value
is a #url{} record as defined in yaws_api.hrl
.TP
\fBparse_url(Str)\fR
Parse URL in a string, returns a #url record
.TP
\fBformat_url(UrlRecord)\fR
Takes a #url record a formats the Url as a string
.TP
\fBcall_cgi(Arg, Scriptfilename)\fR
Calls an executable CGI script,
given by its full path. Used to make `.yaws' wrappers for CGI
programs. This function usually returns \fIstreamcontent\fR.
.TP
\fBcall_cgi(Arg, Exefilename, Scriptfilename)\fR
Like before, but
calls \fIExefilename\fR to handle the script. The file name of the
script is handed to the executable via a CGI meta variable.
.TP
\fBdir_listing(Arg)\fR
Perform a directory listing. Can be used in special directories
when we don't want to turn on dir listings for the entire server.
Always returns ok.
.SH RETURN VALUES from out/1
.PP
The out/1 function can return different values to control the behavior
of the server.
.TP
\fB{html, DeepList}\fB
This assumes that DeepList is formatted HTML code.
The code will be inserted in the page.
.TP
\fB{ehtml, Term}\fR
This will transform the erlang term Term into a
stream of HTML content. The basic syntax of Term
is
\fI
.nf
EHTML = [EHTML] | {Tag, Attrs, Body} | {Tag, Attrs} | {Tag} |
binary() | character()
Tag = atom()
Attrs = [{Key, Value}] or {EventTag, {jscall, FunName, [Args]}}
Key = atom()
Value = string()
Body = EHTML
.fi
\fR
For example, \fI{p, [], "Howdy"}\fR expands into
"Howdy
and
\fI
.nf
{form, [{action, "a.yaws"}],
{input, [{type,text}]}}
.fi
\fR
expands into
\fI
.nf
.fi
\fR
It may be more convenient to generate erlang tuples
than plain html code.
.TP
\fB{content, MimeType, Content}\fR
This function will make the web server generate
different content than HTML. This return value is only allowed
in a yaws file which has only one