1、/body/html The out(Arg) function is supplied one argument, an #arg structure. We have the following relevant record definitions: -record(arg, clisock, % the socket leading to the peer client client_ip_port, % ClientIp, ClientPort tuple headers, % headers req, % request clidata, % The client data (as
2、 a binary in POST requests) server_path, % The normalized server path querydata, % Was the URL on the form of .?query (GET reqs) appmoddata, % the remainder of the path up to the query docroot, % wheres the data fullpath, % full path to yaws file cont, % Continuation for chunked multipart uploads st
3、ate, % 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, % path in front of:appmodappmoddata pathinfo % Set to d/e when calling c.yaws for the request % http:/some.host/a/b/c.yaws/d/e ). The headers argument is als
4、o a record: -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, content_length, authorization, other = % misc other headers it likes. We have the following functions to
5、 aid that generation.API ssi(DocRoot, ListOfFiles) Server side include. Just include the files as is in the docu- ment. The files will not be parsed and searched for Server side include of pre indented code. The data in Files will be included but contained in a tag. The data will be htmlized. pre_ss
6、i_string(String) Include htmlized content from String. f(Fmt, Args) The equivalent of io_lib:format/2. This function is automati- cally -included in all erlang code which is a part of a yaws page. htmlize(Binary | List | Char) Htmlize an IO list object. setcookie(Name, Value, Path, Expire, Domain ,
7、Secure) Sets a cookie to the browser. find_cookie_val(Cookie, Header) This function can be used to search for a cookie that was previ- ously set by setcookie/2-6. For example if we set a cookie assetcookie(sid,SomeRandomSid) , then on subsequent requests from the browser we can call: find_cookie(,(A
8、rg#arg.headers)#headers.cookie) The function returns if no cookie was found, otherwise the actual cookie is returned as a string. redirect(Url This function generates a redirect to the browser. It will clear any previously set headers. So to generate a redirect and set a cookie, we need to set the c
9、ookie after the redirect as in: . do some stuff Ret = redirect, , setcookie(, Random) . redirect_self(Arg) If we want to issue a redirect to ourselves, this function is useful. It returns a record #redir_self defined in yaws_api.hrl. The record contains fields to construct a URL to ourselves. -recor
10、d(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 get_line(String) This function is convenient when getting rn terminated lines from a stream of data. It
11、returns: line, Line, Tail or lastline, Line, Tail The function handles multilines as defined in e.g. SMTP or HTTP mime_type(FileName) Returns the mime type as defined by the extension of FileName stream_chunk_deliver(YawsPid, Data) When a yaws function needs to deliver chunks of data which it gets f
12、rom a process. The other process can call this function to deliver these chunks. It requires the out/1 function to return the value streamcontent, MimeType, FirstChunk to work. YawsPid is the process identifier of the yaws process delivering the original The Pid must typically be passed (somehow) to
13、 the producer of the stream. stream_chunk_deliver_blocking(YawsPid, Data) 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. stream_chunk_
14、end(YawsPid) 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. stream_process_deliver(Socket, IoList) Yaws allows application processes to deliver data directly to the client. The applicatio
15、n tells yaws about such a process by returning streamcontent_from_pid, MimeType, Pid from its out/1 function. In this case, Pid uses the stream_process_deliver/2 function to deliver data to the client. The application gets Socket from Arg#arg.clisock, and IoList is the data to be sent to the client.
16、 stream_process_deliver_chunk(Socket, IoList) Same as above but delivers IoList using HTTP chunked transfer format. IoList 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 m
17、ode, either by ensuring no Content-Length header is set or by specifically setting the Transfer-Encoding header to chunked. stream_process_deliver_final_chunk(Socket, IoList) If the application process delivering data to the client uses chunked transfer mode, it must call this to deliver the final c
18、hunk of the transfer. This tells yaws to create a special final chunk in the format required by the HTTP specification (RFC 2616). IoList may be empty, but if its size is greater than zero, that data will be sent as a separate chunk before the final chunk. stream_process_end(Socket, YawsPid) Applica
19、tion processes delivering data directly to clients must call this function to inform yaws that theyve finished using Socket. The YawsPid argument will have been passed to the process earlier when yaws sent it a message telling it to proceed with data delivery. Yaws expects Socket to be open. stream_
20、process_end(closed, YawsPid) 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 YawsPid argument w
21、ill have been passed to the application process earlier when yaws sent it a message telling it to proceed with data delivery. parse_query(Arg) 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. queryvar(Arg, VarNam
22、e) 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 Val1, . parse_post(Arg) This function will parse the POST
23、data as supplied from the browser. It will return a Key, Value list of the items set by the browser. postvar(Arg, VarName) POSTed data from the client. Returns ok, Val or undefined. getvar(Arg, VarName) This function looks at the HTTP request method from the client and invokes postvar/2 if it is a P
24、OST from the client and queryvar/2 if it is a GET request from the client. parse_multipart_post(Arg) 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
25、 result, Res no more data will come from the browser. If the function returns cont, Cont, Res 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 m
26、ight usefully be a File Descriptor. The Res value is a list of either: header, Header | part_body, Binary | body, Binary Example usage could be: 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(
27、Done ,) end. handle_res(A, head, Name|T) - io:format(head:pn,Name), handle_res(A, T); handle_res(A, part_body, Data|T) -part_body:,Data), handle_res(A, body, Data|T) -body: handle_res(A, ) -End_resn). new_cookie_session(Opaque) Create a new cookie based session, the yaws system will set the cookie. The new random generated cookie is returned from this function. The Opaque argument will typically contain user data such as user name and pa
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1