*Project* The Anubis Project *Title* Some common stuff for the web. *Copyright* Copyright (c) Alain Prouté 2003~2007. © Calexium 2007~2016 *Authors:* Alain Prouté David René *Public* *Name* HTTP_header *Description* transmit tools/basis.anubis transmit system/string.anubis /** * The type 'HTTP_header' describes HTTP headers, which are just pairs '(name,value)'. */ public type HTTP_header: http_header(String name, String value). public define Maybe(String) http_header_value ( List(HTTP_header) l, String name ) = if l is { [ ] then failure, [h . t] then if h is { http_header(n, v) then //print("DEBUG: http_header_value --> HEADER = " + n + ":" + v + "\n"); if insensitive_equal(name, n) then success(v) else http_header_value(t, name), } }. public define List(String) to_List_String ( List(HTTP_header) l )= map((HTTP_header h) |-> h.name +": "+h.value, l ). public define String to_String ( List(HTTP_header) l )= join("\n\r",to_List_String(l)). *Name* Web_arg *Description* The type 'Web_arg' describes 'web arguments'. A web argument is either a pair '(name,value)' (for example it may be 'web_arg("password","foobar")', if the client clicks on the submit button of a form containing a password input field named 'password'), or an uploaded file. In this last case, it is a triplet containing the name of the file upload input field, the value of this input field (name of the uploaded file), and the name of the temporary file as saved by the server in its 'upload temporary directory'; see 'web/http_server.anubis' and the 'upload' Web_item in 'web/html.anubis'). public type Web_arg: web_arg(String name, String value), upload (String name, // name of corresponding 'upload' Web_item String value, // name of uploaded file String temp_file_path). // temporary file path (relative to server directory) *Name* Web_arg_value *Description* Of course, within the body of a 'web page' operation, you may want to recover the value of a particular web argument. To that end, use the operation 'web_arg_value', which takes 2 argument: - the list of all web arguments (the operand of the web page operation), - the name of the argument whose value is wanted. This operation has the following return type: public type Web_arg_value: not_found, found(String value). *Name* Web_arg_value *Description* If the requested argument name is not found in the list, 'not_found' is returned. Otherwise, the value returned by 'web_arg_value' has the form 'found(v)', where 'v' is the value of the argument. The operation 'web_arg_value' is defined below. public define Web_arg_value web_arg_value ( List(Web_arg) l, String name ) = if l is { [ ] then not_found, [h . t] then if h is { web_arg(n,v) then if name=n then found(v) else web_arg_value(t,name), upload(n,v,tfn) then if name=n then found(v) else web_arg_value(t,name) } }. public define Web_arg_value web_arg_value_not_null ( List(Web_arg) lwa, String name, ) = if web_arg_value(lwa, name) is { not_found then not_found found(str) then if str = "" then not_found else found(str) }. *Ignore* public define Maybe((String,String)) file_upload_value ( List(Web_arg) l, String name ) = if l is { [ ] then failure, [h . t] then if h is { web_arg(_,_) then file_upload_value(t,name), upload(n,v,tfn) then if n = name then success((v,tfn)) else file_upload_value(t,name) } }. public define List((String,String)) file_upload_value ( List(Web_arg) l, String name, List((String,String)) new_list ) = if l is { [ ] then new_list, [h . t] then if h is { web_arg(_,_) then file_upload_value(t,name, new_list), upload(n,v,tfn) then if n = name then file_upload_value(t,name, [(v,tfn) . new_list]) else file_upload_value(t,name, new_list) } }. public type Redirection: redirect(String required_uri, // URI required by the client String required_host, // value of 'Host' HTTP header sent by the client String corresponding_uri). // URI which will be served to the client public type Redirections: redirection_list(List(Redirection) redirections), redirection_fn((String input_uri, // URI required by the client String host) -> String f). // value of 'Host' HTTP header sent by the client Now, you may also want to recover web argument values which have been encoded (by 'web_arg_encode'). In this case, use the following: read CXM_web_arg_encode.anubis public define Maybe($T) decode_web_arg_value ( List(Web_arg) l, String name ) = if web_arg_value(l,name) is { not_found then failure, found(v) then web_arg_decode(v) }. public define List(String) web_arg_list_values ( List(Web_arg) l, String name ) = if l is { [] then [], [h . t] then if h is web_arg(n, v) then if n = name then [ v . web_arg_list_values(t, name)] else web_arg_list_values(t, name) else web_arg_list_values(t, name) } .