Unformatted text preview:

CSE399: Advanced ProgrammingHandout 16Web Scripting in HaskellCGI ScriptingCGI Scripts are a popular means of providing dynamicfunctionality for web sites.•Web server (e.g. Apache) recognizes certain “magicURLs” as dynamically generate d. E.g.,http://fling-l.s eas.upenn.edu/~bcpierce/cgi-bin/ex1.cgi•When one of these URLs is requested, the server runsthe corresponding program (ex1.cgi) as an externalprocess.Demo: ex1CGI Scripting•If the URL being requested comes from a FORM in anHTML page, the values in the form are also passed tothe CGI script.•The details of how this happens depend on whether theform uses theGET or POST method.•If GET, then the parameters are passed in an enviromentvariable call edQUERY_STRING.•If POST, then the p arameters are sent to the CGI programonstdin.In either case, a bunch of other information is passed inenvironment variables.•The server takes whatever this program prints on itsstdout (generally an HTML page) and sends it back tothe requesting client.CGI Scripting on SEAS Machines•Because badly written C GI scripts can open securityholes, CETS does not allow CGI scripts to be run on theregular SEAS web server.•Instead, a special machine, fling-lseas, is provided forthis purpose.•fling-l runs the same version of linux as the labmachines•However, it runs the same version of linux as the labmachines and has access to the same filesystem, so youcan compile things using GHC onminus or whereve rand put the binary in your~/html/cgi-bin directory•Caveat: make sure your executable program has theextension .cgi — otherwise the server won’t recognizeit.WASH/CGICGI Scripting in HaskellMost languages these days have libraries that handlelow-level details like parsing the information from forms.However, writing CGI scripts that present complexfunctionality to the user is a harder problem. In particular,the “one-shot” request/response model provided by theHTTP and CGI p rotocols doesn’t directly support extendedconversations b etween a browser and a server.For this, we need a higher-level notion ofsessions.WASH/CGIWASH is a collection of Haskell libraries that provides such asession abstraction (along with numerous other goodies).Demo:Adventure.cgi (via browser and text)bcpierce@minus:~/html/cgi-bin> ./Adventure.cgiContent-Type: text/html; charset=utf-8<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trans><!-- generated by WASH/HTML 0.11--><html xmlns="http://www.w3.org/1999/xhtml"><head><title>Adventures in Haskell...</title></head><body><h1>Adventures in Haskell...</h1><script type="text/javascript"><!--var SubmitAction=[];function OnSubmit(){var r=true;for(var i=0;i<SubmitAction.length;i++){r=r&&SubmitAction[//--></script><form enctype="application/x-www-form-urlencoded" target="_self" onsubmit="return OnSubmit();" method="post"/><img name="if0x0" align="center" title="non empty string expected" alt="non empty string expected" src="?nothing.g/><input onclick="this.form.WASHsub.value=this.name; return true" name="s0x1" value="Go" type="submit"/><p>Shortcuts: <input onclick="this.form.WASHsub.value=this.name; return true" name="s0x2" value="n" type="submit"/> <input onclick="this.form.WASHsub.value=this.name; return true" name="s0x3" value="s" type="submit"/> <input onclick="this.form.WASHsub.value=this.name; return true" name="s0x4" value="e" type="submit"/> <input onclick="this.form.WASHsub.value=this.name; return true" name="s0x5" value="w" type="submit"/></p><p style="color: red; background: #bbbbbb; "></p><hr width="95%"/><p>Welcome... </p><p>Enter a command (such as n, s, e, or w) to move to the next node.</p><input value="" name="WASHsub" type="hidden"/><input value="W10=" name="=CGI=parm=" type="hidden"/></form><script type="text/javascript"><!--document.forms[0].f0x0.focus();document.forms[0].f0x0.select();// --></script></body></html>bcpierce@minus:~/html/cgi-bin>The Document SublanguageComposing DocumentsWash provides functions corresponding to all the HTML tags.So we can write, for example,html (body (p (ul (li (text "Hello world"))) ))Composing DocumentsEach html constructor yiel ds a (singleton) list of html nodes,and these lists can be concatenated using the sequencingcombinator>>.ul (li (text "a") >> li (text "b ") >> li (text "c"))Also, these sequences of HTML nodes are an instance of theMonad class, so we can use the do syntax for composingdocuments.do p (text "This is a very")p (text "complicated way")p (do text "of sa ying"ul (do li (text "nothing")li (text "very")li (text "important")))Composing DocumentsThis makes it easy to write parameterized documents:standardPage t tl nodes =html (do head (title (text ttl))body (do h1 (text ttl)nodes))HTML Quasi-Valid ationWash includes some sophisticated (and interesting) trickeryusing Haskell’s type classes to perform “quasi-validation” ofgenerated HTML.We’ll return to this on Wednesday. For now, just think ofWithHTML x y m aas “the type of HTML”.The Session LanguageSessionsThe ask and tell functions are used to send responses tothe client b rowser.ask :: WithHTML x CGI a -> CGI ()tell :: (CGIOutput a) => a -> CG I ()tellis lower-leve l: it just takes soem content (like HTML)and ships it out. It is seldom used.ask is implemented in terms of tell. It takes some HTMLwith embedded forms, makes the necessary arrangements(filling in hidden fields, etc.) for restarting the session at theright place when these forms are activated, and usestell toship out the resulting HT ML.Whole ProgramsTo actually execute a C GI action, we need to turn it into anIO action. This is accomplished by the run function.run :: CGI () -> IO ()There is also a functionio :: (Read a, Show a) => IO a -> CGI athat embeds an IO action in a CGI action. The result of theIO must beReadable and Showable, so that it can berecorded in the session log.The Widget SublanguageWash-Style InputInput forms play a critical role in many interactive web sites.Wash provides very powerful (but initially somewhat trickyand puzzling!) facilities for dealing with forms in a high-levelway.HTML Fo rms<form method=POST action="http://www.kumquat.com/demo">Name:<input type=text name=name size=32 maxl ength=80><p>Sex:<input type=radio name=sex value="M"> Male<input type=radio name=sex value="F"> Female<p><input type=submit></form>WASH For msWraps an HTML form around its arguments. All standardattributes are computed and need not be supplied explicitly.makeForm


View Full Document

Penn CIS 399 - Web Scripting in Haskell

Download Web Scripting in Haskell
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Web Scripting in Haskell and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Web Scripting in Haskell 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?