1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
|
.TH YAWS_API "5" "" "" "User API" -*- nroff -*-
.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
<html>
<body>
<h1> Header 1</h1>
<erl>
out(Arg) ->
{html, "<p> Insert this text into the document"}.
</erl>
</body>
</html>
.fi
\fR
.PP
The \fBout(Arg)\fR function is supplied one argument, an #arg{} structure.
We have the following relevant record definitions:
\fI
.nf
-record(arg, {
clisock, % the socket leading to the peer client
client_ip_port, % {ClientIp, ClientPort} tuple
headers, % headers
req, % request (possibly rewritten)
orig_req, % original request
clidata, % The client data (as a binary in POST requests)
server_path, % The normalized server path
% (pre-querystring part of URI)
querydata, % For URIs of the form ...?querydata
% equiv of cgi QUERY_STRING
appmoddata, % (deprecated - use pathinfo instead) the remainder
% of the path leading up to the query
docroot, % Physical base location of data for this request
docroot_mount, % virtual directory e.g /myapp/ that the docroot
% refers to.
fullpath, % full deep path to yaws file
cont, % Continuation for chunked multipart uploads
state, % State for use by users of the out/1 callback
pid, % pid of the yaws worker process
opaque, % useful to pass static data
appmod_prepath, % (deprecated - use prepath instead) path in front
% of: <appmod><appmoddata>
prepath, % Path prior to 'dynamic' segment of URI.
% ie http://some.host/<prepath>/<script-point>/d/e
% where <script-point> is an appmod mount point,
% or .yaws,.php,.cgi,.fcgi etc script file.
pathinfo, % Set to '/d/e' when calling c.yaws for the request
% http://some.host/a/b/c.yaws/d/e
% equiv of cgi PATH_INFO
appmod_name % name of the appmod handling a request,
% or undefined if not applicable
}).
.fi
\fR
The headers argument is also a record:
\fI
.nf
-record(headers, {
connection,
accept,
host,
if_modified_since,
if_match,
if_none_match,
if_range,
if_unmodified_since,
range,
referer,
user_agent,
accept_ranges,
cookie = [],
keep_alive,
location,
content_length,
content_type,
content_encoding,
authorization,
transfer_encoding,
x_forwarded_for,
other = [] % misc other headers
}).
.fi
\fR
.PP
The \fBout/1\fR function can use the Arg to generate any content
it likes. We have the following functions to aid that generation.
.SH API
.TP
\fBssi(DocRoot, ListOfFiles)\fR
Server side include. Just include the files as is in the document. The files
will \fBnot\fR be parsed and searched for <erl> tags.
.TP
\fBpre_ssi_files(DocRoot, ListOfFiles) ->
Server side include of pre-indented code. The data in Files
will be included but contained in a <pre> 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
\fBset_cookie(Name, Value, Options])\fR
Sets a cookie to the browser. Options are:
\fI
.nf
{expires, UtcTime} - Cookie expiration time, where UtcTime is
a tuple returned by calendar:universal_time/0.
{max_age, Age} - Defines the lifetime of the cookie, in seconds,
where age is an integer >= 0.
{path, Path} - Path is a string that specifies the subset of URLs to
which this cookie applies.
{domain, Domain} - Domain is a string that specifies the domain for which
the cookie is valid.
{comment, Comment} - Comment is a string that doccuments the server's
intended use of the cookie.
secure - Directs the user agent to use only secure means to
contact the origin server whenever it sends back this
cookie.
http_only - Restricts cookie access from other non-HTTP APIs.
.fi
\fR
.TP
\fBsetcookie(Name, Value, [Path, [ Expire, [Domain , [Secure]]]])\fR
Sets a cookie to the browser. This function is deprecated by set_cookie/3.
.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
\fBparse_set_cookie(Str)\fR
This function parses the value of a \fBSet-Cookie\fR header, following the
RFC6265. Because old RFCs (2109 and 2965) are still used, it is backward
compatible. So this function returns a \fI#setcookie{}\fR record when only one
cookie is found. If multiple cookies are set in a single \fBSet-Cookie\fR
header, it returns a list of \fI#setcookie{}\fR records. If no cookie was found
or if an error occurred, it returns [].
\fI#setcookie{}\fR record is defined in \fIyaws_api.hrl\fR:
\fI
.nf
-record(setcookie, {key,
value,
quoted = false,
domain,
max_age,
expires,
path,
secure = false,
http_only = false,
extensions = []}).
.fi
\fR
.TP
\fBparse_cookie(Str)\fR
This function parses the value of \fBCookie\fR header, following the RFC6265. It
returns a list of \fI#cookie{}\fR records. If no cookie was found or if an error
occurred, it returns [].
\fI#cookie{}\fR record is defined in \fIyaws_api.hrl\fR:
\fI
.nf
-record(cookie, {key,
value,
quoted = false}).
.fi
\fR
.TP
\fBformat_set_cookie(SetCookie)\fR
Build a cookie string from a \fI#setcookie{}\fR record like returned by
\fBparse_set_cookie/1\fR.
.TP
\fBformat_cookie(Cookie | [Cookie])\fR
Build a cookie string from a \fI#cookie{}\fR record (or a list or records) like
returned by \fBparse_cookie/1\fR.
.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
\fBredirect_self(Arg)\fR
If we want to issue a redirect to ourselves, this function
is useful. It returns a record \fI#redir_self{}\fR defined in
\fIyaws_api.hrl\fR. The record contains fields to construct
a URL to ourselves.
\fI
.nf
-record(redir_self, {
host, % string() - our own host
scheme, % http | https
scheme_str, % "https://" | "http://"
port, % integer() - our own port
port_str % "" | ":<int>" - the optional port part
% to append to the url
}).
.fi
.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(Scope, FileName)\fR
Returns the MIME type as defined by the extension of \fIFileName\fR. \fIScope\fR
can have following values:
.RS 12
\fBglobal\fR - returns the result obtained from the global context.
.br
\fB#sconf{} | {ServerName, Port}\fR - returns the result obtained from the
virtual server's context. If no MIME type is found in this scope, it falls back
on the global one.
.RE
.TP
\fBmime_type(FileName)\fR
Tries to determine the right \fIScope\fR before calling mime_type/2.
.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 synchronous version of the above function. This synchronous 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
\fBstream_process_deliver(Socket, IoList)\fR
Yaws allows application processes to deliver data directly to the
client. The application tells yaws about such a process by returning
\fB{streamcontent_from_pid, MimeType, Pid}\fR from its \fBout/1\fR
function. In this case, \fIPid\fR uses the
\fBstream_process_deliver/2\fR function to deliver data to the
client. The application gets \fISocket\fR from \fIArg#arg.clisock\fR,
and \fIIoList\fR is the data to be sent to the client.
.TP
\fBstream_process_deliver_chunk(Socket, IoList)\fR
Same as above but delivers \fIIoList\fR using HTTP chunked transfer
format. \fIIoList\fR must have a size greater than zero. The
application process delivering the data will have had to have make
sure that the HTTP headers of the response indicate chunked transfer
mode, either by ensuring no Content-Length header is set or by
specifically setting the Transfer-Encoding header to chunked.
.TP
\fBstream_process_deliver_final_chunk(Socket, IoList)\fR
If the application process delivering data to the client uses chunked
transfer mode, it must call this to deliver the final chunk of the
transfer. This tells yaws to create a special final chunk in the
format required by the HTTP specification (RFC 2616). \fIIoList\fR may
be empty, but if its size is greater than zero, that data will be
sent as a separate chunk before the final chunk.
.TP
\fBstream_process_end(Socket, YawsPid)\fR
Application processes delivering data directly to clients must call
this function to inform yaws that they've finished using
\fISocket\fR. The \fIYawsPid\fR argument will have been passed to the
process earlier when yaws sent it a message telling it to proceed with
data delivery. Yaws expects \fISocket\fR to be open.
.TP
\fBstream_process_end(closed, YawsPid)\fR
Same as the previous function but the application calls this if it
closes the client socket as part of its data delivery process. This
allows yaws to continue without assuming the socket is still open and
encountering errors due to that assumption. The \fIYawsPid\fR argument
will have been passed to the application process earlier when yaws
sent it a message telling it to proceed with data delivery.
.TP
\fBparse_query(Arg)\fR
This function will parse the query part of the URL. It will return a {Key,
Value} list.
.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. If a variable is defined multiple times, the function may also return
\fI{Val1, Val2...}\fR.
.TP
\fBparse_post(Arg)\fR
If the browser has set the Content-Type header to the value
"application/x-www-form-urlencoded", this function will parse the request's
body. It will return a {Key, Value} list.
.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 request's body sent by the client. Returns
{ok, Val} or undefined. If a variable is defined multiple times, the function
may also return \fI{Val1, Val2...}\fR.
.TP
\fBgetvar(Arg, VarName)\fR
This function is used to search a variable in the query part of the URL and in
the request's body. it invokes queryvar/2 and postvar/2 and merges the results.
.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 too 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{head, {Name, Headers}}\fR | \fB{part_body, Binary}\fR | \fB{body, Binary}\fR
The function returns \fB{error, Reason}\fR when an error occurred during the
parsing.
Example usage could be:
\fI
.nf
<erl>
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("<pre>Done </pre>",[])};
{error, Reason} ->
{html, f("An error occured: ~p", [Reason])}
end.
handle_res(A, [{head, {Name, _Hdrs}}|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").
</erl>
.fi
\fR
.TP
\fBnew_cookie_session(Opaque)\fR
Create a new cookie-based session. Yaws will either generate the cookie
itself or, if a \fIysession_cookiegen\fR module is configured, call
\fInew_cookie()\fR on that module to get a new cookie. The new cookie is
returned from this function. The \fIOpaque\fR argument will typically
contain user data such as user name and password
.TP
\fBnew_cookie_session(Opaque, TTL)\fR
As above, but allows to set a session specific time-out value,
overriding the system specified time-out value.
.TP
\fBnew_cookie_session(Opaque, TTL, CleanupPid)\fR
As above, but also sends a message \fI{yaws_session_end, Reason, Cookie,
Opaque}\fR to the provided \fICleanupPid\fR where Reason can be either of
\fItimeout\fR or \fInormal\fR. The \fICookie\fR is the HTTP cookie as
returned by \fInew_session()\fR and \fIOpaque\fR is the user-provided
\fIOpaque\fR parameter to \fInew_session()\fR. The purpose of the feature
is to cleanup resources assigned to the session.
.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 /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.
To create a suitable initial #gconf{} record see the code in
yaws_config:make_default_gconf/2. Especially the \fIyaws_dir\fR parameter
is important to get right.
.TP
\fBurl_decode(Str)\fR
Decode url-encoded string. A URL encoded 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 significant 4 bits in the 8 bit
character.
.TP
\fBurl_encode(URL)\fR
URL-encodes a string or binary, and returns a string. All URLs in HTML
documents must be URL encoded.
.TP
\fBget_sslsocket(Socket)\fR
Returns a socket for SSL sockets or the atom \fIundefined\fR for non-SSL
sockets. Useful for applications that have to deal with both SSL and
non-SSL sockets.
.TP
\fBget_listen_port(Sconf)\fR
Return the actual port number used by the listen socket of the virtual
server indicated by the function argument, an \fI#sconf{}\fR record
instance. If successful, returns the requested port number, or returns
\fI{error, not_found}\fR if the function argument does not match any known
virtual server. This function is useful for retrieving the actual port
number when, e.g. for testing purposes, a virtual server is configured to
use port 0, which will cause it to have an ephemeral port assigned by the
operating system.
.TP
\fBreformat_header(H)\fR
Returns a list of reformatted header values from a #headers{}
record. The return list is suitable for retransmit.
.TP
\fBreformat_header(H, FormatFun)\fR
Returns a list of reformatted header values from a #headers{} record,
with each element of the list formatted via a call to
\fIFormatFun\fR. This enables converting #headers{} records into
various lists of headers and their values. Note that sometimes the
\fISet-Cookie\fR header or other headers will contain a tuple value of
the form \fI{multi, ValueList}\fR. (The \fI{multi, ValueList}\fR
construct typically results from calls to \fImerge_header/2\fR or
\fImerge_header/3\fR, where multiple values are set in separate calls
for the same header; see \fImerge_header/2\fR below for details.)
Formatting functions should therefore be capable of handling a
\fI{multi, ValueList}\fR tuple. They should handle it by formatting
each member of \fIValueList\fR as a separate header string, storing
all such header strings in a list, and returning that list in a
\fI{multi, HdrList}\fR tuple. Note that in versions of Yaws 2.0.6 and
older, formatting functions returned such header lists directly, which
implies that sometimes the return values of \fIreformat_header/1\fR
and \fIreformat_header/2\fR can be a multi-level list if constructed
by one of these older formatting functions.
.TP
\fBreformat_header(H, FormatFun, Options)\fR
Same as \fIreformat_header/2\fR except that header and value data
passed to \fIFormatFun\fR are first converted to the data format
specified in \fIOptions\fR. \fIOptions\fR is expected to be either an
atom or a list of atoms, either \fIstring\fR or \fIbinary\fR. If the
list contains multiple items, options earlier in the list override
those later in the list, so for example \fI[string, binary]\fR is the
same as \fI[string]\fR. If the first item in \fIOptions\fR specifies
anything other than \fIstring\fR or \fIbinary\fR, data are passed to
\fIFormatFun\fR without conversion.
.TP
\fBset_header(Headers, {Header, Value})\fR
Sets header \fIHeader\fR with value \fIValue\fR in the #headers{} record
\fIHeaders\fR, and returns a new #headers{} record. Using the atom
\fIundefined\fR for \fIValue\fR effectively deletes the header, same as
\fIdelete_header/2\fR.
.TP
\fBset_header(Headers, Header, Value)\fR
Same as \fIset_header/2\fR above, except \fIHeader\fR and \fIValue\fR are
not passed in a tuple.
.TP
\fBmerge_header(Headers, {Header, Value})\fR
Merges value \fIValue\fR for header \fIHeader\fR with any existing value
for that header in the #headers{} record \fIHeaders\fR, and returns a new
#headers{} record. Using the atom \fIundefined\fR for \fIValue\fR simply
returns \fIHeaders\fR. Otherwise, \fIValue\fR is merged with any existing
value already present in the \fIHeaders\fR record for header \fIHeader\fR,
comma-separated from that existing value. If no such value exists in the
\fIHeaders\fR record, the effect is the same as \fIset_header/2\fR. Note
that for the \fISet-Cookie\fR header, values are not comma-separated but
are instead collected into a tuple \fI{multi, ValueList}\fR where
\fIValueList\fR is the collection of \fISet-Cookie\fR values. This implies
that any formatting fun passed to \fIreformat_header/2\fR must be prepared
to handle such tuples.
.TP
\fBmerge_header(Headers, Header, Value)\fR
Same as \fImerge_header/2\fR above, except \fIHeader\fR and \fIValue\fR are
not passed in a tuple.
.TP
\fBget_header(Headers, Header)\fR
Gets the value of header \fIHeader\fR from the #headers{} record
\fIHeaders\fR and returns it. If the header isn't set, the atom
\fIundefined\fR is returned.
.TP
\fBdelete_header(Headers, Header)\fR
Deletes any value set for header \fIHeader\fR in the #headers{} record
\fIHeaders\fR, and returns a new #headers{} record.
.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
\fBcall_fcgi_responder(Arg)\fR
Calls a FastCGI responder.
The address and port of the FastCGI application server are taken
from the server configuration (see yaws.conf).
Used to make `.yaws' wrappers for FastCGI responders.
Returns the same return values as out/1 (see below).
.TP
\fBcall_fcgi_responder(Arg, Options)\fR
Same as above, but Options overrides the defaults from the server
configuration:
\fI
.nf
Options = [Option]
Option -- one of the following:
.fi
\fR
\fB{app_server_host, string() | ip_address()}\fR
The hostname or the IP address of the FastCGI application server.
\fB{app_server_port, 0..65535}\fR
The TCP port number of the FastCGI application server.
\fB{path_info, string()}\fR
Override default pathinfo in Arg#arg.pathinfo.
\fB{extra_env, ExtraEnv}\fR
Extra environment variables to be passed to the FastCGI application server,
as a list of name-value pairs.
\fI
.nf
ExtraEnv = [Var]
Var = {Name, Value}
Name = string() | binary()
Value = string() | binary()
.fi
\fR
\fB{trace_protocol, boolean()}\fR
Enable or disable tracing of FastCGI protocol messages as info
log messages.
\fB{log_app_error, boolean()}\fR
Enable or disable logging of application error messages: output
to stderr and non-zero exit value.
.TP
\fBcall_fcgi_authorizer(Arg) -> {allowed, Out} | {denied, Out}\fR
Calls a FastCGI authorizer.
The address and port of the FastCGI application server are taken
from the server configuration (see yaws.conf).
Used to make `.yaws' wrappers for FastCGI authorizers.
Variables contains the values of the variables returned by the FastCGI
application server in the "Variable-XXX: YYY" headers.
If access is denied, Out contains the complete response returned by
the FastCGI application server. This response is typically returned
as-is to the HTTP client.
If access is allowed, Out contains the response returned by the
FastCGI application server minus the body (i.e. minus the content)
which should be ignored per the FastCGI specification. This response
is typically not returned to the HTTP client. The calling application
module may wish to inspect the response, for example by extracting
variables (see fcgi_extract_variables below) or by inspecting the
headers returned by the FastCGI application server.
\fI
.nf
Out -- See return values for out/1 below
.fi
\fR
.TP
\fBcall_fcgi_authorizer(Arg, Options) -> {allowed, Out} | {denied, Out}\fR
Same as above, but Options overrides the defaults from the server
configuration. See call_fcgi_responder/2 above for a description
of Options.
.TP
\fBfcgi_extract_variables(Out) -> [{Name, Value}]\fR
Extracts the environment variables from a FastCGI authorizer response
by looking for headers of the form "Variable-Name: Value".
\fI
.nf
Name = string() -- The name of the variable (the "Variable-" prefix
has already been removed).
Value = string() -- The value of the variable.
.fi
\fR
.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|exhtml, Term}\fR
This will transform the erlang term Term into a
stream of HTML content. The exhtml variant transforms into
strict XHTML code. The basic syntax of Term
is
\fI
.nf
EHTML = [EHTML] | {Tag, Attrs, Body} | {Tag, Attrs} | {Tag} |
{Module, Fun, [Args]} | fun/0 |
binary() | character()
Tag = atom()
Attrs = [{Key, Value}]
Key = atom()
Value = string() | binary() | atom() | integer() | float() |
{Module, Fun, [Args]} | fun/0
Body = EHTML
.fi
\fR
For example, \fI{p, [], "Howdy"}\fR expands into
"<p>Howdy</p>" and
\fI
.nf
{form, [{action, "a.yaws"}],
{input, [{type,text}]}}
.fi
\fR
expands into
\fI
.nf
<form action="a.yaws"
<input type="text">
</form>
.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 <erl> </erl> part and no
html parts at all.
.TP
\fB{streamcontent, MimeType, FirstChunk}\fR
This return value plays the same role as the \fIcontent\fR return
value above.
However it makes it possible to stream data to the client
if the yaws code doesn't have access to all the data in one go. (Typically
if a file is very large or if data arrives from back end servers on the network.
.TP
\fB{streamcontent_with_timeout, MimeType, FirstChunk, Timeout}\fR
Similar to above, but with an explicit timeout. The default timeout
is 30 secs. I.e if the application fails to deliver data to the
Yaws process, the streaming will stop. This is often not the
desired behaviour in Comet/Ajax applications. It's possible to
provide 'infinity' as timeout.
.TP
\fB{streamcontent_from_pid, MimeType, Pid}\fR
This return value is similar to the \fIstreamcontent\fR return value above.
However it makes it possible to stream data to the client directly from an
application process to the socket. This approach can be useful for applications
that employ long-polling (Comet) techniques, for example, and for applications
wanting to avoid buffering data or avoid HTTP chunked mode transfer for streamed
data.
.TP
\fB{streamcontent_with_size, Sz, MimeType, FirstChunk}\fR
This return value is similar to the \fIstreamcontent\fR return value above.
However it makes it possible to stream data to the client by setting the content
length of the response. As the opposite of other ways to stream data, in this
case, the response is not chunked encoded.
.TP
\fB{header, H}\fR
Accumulates a HTTP header. The trailing CRNL which is supposed
to end all HTTP headers must NOT be added. It is added by the server.
The following list of headers are given special treatment.
\fI{connection, What}\fR
This sets the Connection: header. If \fIWhat\fR is the special value
\fI"close"\fR, the connection will be closed once the yaws page is delivered
to the client.
\fI{server, What}\fR
Sets the Server: header. By setting this header, the server's signature will be
dynamically overloaded.
\fI{location, Url}\fR
Sets the Location: header. This header is typically combined with
the \fI{status, 302}\fR return value.
\fI{cache_control, What}\fR
Sets the Cache-Control: header.
\fI{expires, What}\fR
Sets the Expires: header.
\fI{date, What}\fR
Sets the Date: header.
\fI{allow, What}\fR
Sets the Allow: header.
\fI{last_modified, What}\fR
Sets the Last-Modified: header.
\fI{etag, What}\fR
Sets the Etag: header.
\fI{set_cookie, Cookie}\fR
Prepends a Set-Cookie: header to the list of previously
set Set-Cookie: headers.
\fI{content_range, What}\fR
Sets the Content-Range: header.
\fI{content_type, MimeType}\fR
Sets the Content-Type: header.
\fI{content_encoding, What}\fR
Sets the Content-Encoding: header. If this header is defined, no deflate is
performed by Yaws, allowing you to compress data yourself if you wish to do so.
\fI{content_length, Len}\fR
Normally yaws will ship Yaws pages using Transfer-Encoding: chunked. This
is because we generally can't know how long a yaws page will be. If we for
some reason want to force a Content-Length: header (and we actually do
know the length of the content, we can force Yaws to not ship the
page chunked.
\fI{transfer_encoding, What}\fR
Sets the Transfer-Encoding: header.
\fI{www_authenticate, What}\fR
Sets the WWW-Authenticate: header.
\fI{vary, What}\fR
Sets the Vary: header.
\fI{accept_ranges, What}\fR
Sets the Accept-Ranges: header.
All other headers must be added using the normal HTTP syntax.
Example:
\fI{header, {"My-X-Header", "gadong"}}\fR or \fI{header, "My-X-Header: gadong"}\fR
.TP
\fB{header, {HeaderName, erase}}\fR
Clears the header named \fIHeaderName\fR from the accumulated headers.
.TP
\fB{allheaders, HeaderList}\fR
Will clear all previously accumulated headers and replace them.
.TP
\fB{status, Code}\fR
Will set another HTTP status code than 200.
.TP
\fBbreak\fR
Will stop processing of any consecutive chunks of erl or html code
in the yaws file.
.TP
\fBok\fR
Do nothing.
.TP
\fBflush\fR
Flush remaining data sent by the client.
.TP
\fB{redirect, Url}\fR
Erase all previous headers and accumulate a single
Location header. Set the status code.
.TP
\fB{redirect_local, Path}\fR
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
and be invoked again when more Data arrives.
.TP
\fB{page, Page}\fR
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
\fB{page, {Options, Page}}\fR
Like the above, but supplying an additional deep list of options. Supported
option types are:
\fI{status, C}\fR - Set the HTTP response status code \fIC\fR for page
\fIPage\fR.
\fI{header, H}\fR - Accumulate the HTTP header \fIH\fR for page \fIPage\fR.
\fI{disable_cache, Bool}\fR - if set to \fItrue\fR, disable the cache of
\fIPage\fR for this call.
.TP
\fB{websocket, CallbackModule, Options}\fR
Tell Yaws to use \fICallbackModule\fR as a WebSocket Protocol handler for
traffic on the client socket. See the Yaws websocket documentation for more
details.
.TP
\fB{ssi, File, Delimiter, Bindings}\fR
Server side include \fIFile\fR and macro expansion in \fIFile\fR.
Each occurrence of a string, say "xyz", inside \fIFile\fR that's
within a \fIDelimiter\fR pair is replaced with the corresponding
value in \fIBindings\fR.
Example:
Delimiter = %%
File contains the string .... %%xyz%% .....
Bindings contain the tuple {"xyz", "Dingbat"}
The occurrence of %%xyz%% in File will be replaced with "Dingbat"
in the Server side included output.
The {ssi, File, Delimiter, Bindings} statement can also
occur within a deep ehtml structure.
The special directive \fIstrip_undefined\fR can be specified in
the \fIBindings\fR list, just as it can for the \fI{bindings, ....}\fR
directive, but it's ignored because treating undefined variables
as empty is the default for \fIssi\fR bindings.
.TP
\fB{bindings, [{Key1, Value2}, {Key2, Value2} .....]}\fR
Establish variable bindings that can be used in the page.
All bindings can then be used in the rest of yaws code
(in HTML source and within erl tags).
In HTML source %%Key%% is expanded to Value and within erl
tags \fIyaws_api:binding(Key)\fR (which calls \fIerror\fR if
no such binding exists) or \fIyaws_api:binding_find(Key)\fR
(which returns \fIundefined\fR if no such binding exists)
can be used to extract Value, and \fIyaws_api:binding_exists(Key)\fR
can be used to check for the existence of a binding.
If a page happens to contains text that looks like a binding, e.g.
%%SomeText%%, but no key \fISomeText\fR is supplied, then by default
the original text is left as is. If you prefer that anything parsed
as a binding gets stripped out of a page whenever the \fBbindings\fR
directive does not specify its key, include the special directive
\fIstrip_undefined\fR in the bindings list:
{bindings, [{Key1, Value1}, strip_undefined]}
.TP
\fB{yssi, YawsFile}\fR
Include a yaws file. Compile it and expand as if it had
occured inline.
.TP
\fB#arg{}\fR
Return an instance of an \fI#arg{}\fR record. This can be useful when used
as part of a \fI[ListOfValues]\fR return value, so that any subsequent
elements in the return list that require an #arg{} get the returned
instance rather than the original. For example, an \fIout/1\fR function
might set the \fIstate\fR field of an #arg{}, then return both it and
\fI{yssi, YawsFile}\fR in a list, in which case Yaws will pass the returned
#arg{}, rather than the original instance, to the yaws file out/1 function.
.TP
\fB[ListOfValues]\fR
It is possible to return a deep list of the above defined return values. Any
occurrence of \fIstreamcontent\fR, \fIstreamcontent_with_timeout\fR,
\fIstreamcontent_with_size\fR, \fIstreamcontent_from_pid\fR, \fIget_more\fR,
\fIpage\fR or \fIbreak\fR in this list is legal only if it is the last position
of the list. If not, remaining values in the list are ignored.
.SH AUTHOR
Written by Claes Wikstrom
.SH "SEE ALSO"
.BR yaws.conf (5)
.BR erl (1)
|