In this example we illustrate how to perform transformations on viewed pages in a proxy like fashion. In particular, we would like to build a highlight proxy that highlights all occurences of a particular word on the Web in red. The highlight proxy is contacted with
http://www.host.com:9092/bin/highlight?url=X&word=Y
where X denotes the starting point URL on the Web, and Y denotes the word that is to be highlighted (and of course www.host.com is the machine the proxy server is run on). Our proxy is written in such a way that all links that are followed from page X onwards, are redirected to our proxy again. This is accomplished by rewritting the contents of the page.
var url = req.param.url ? "http://www.compaq.com";
var word = req.param.word ? "Compaq";
var page = GetURL(url); // fetch the page
every w in Pat(page,word) !inside Elem(page,"title") do
// wrap a font element around it
var p = NewNamedPiece("font",w);
p.size := "+1"; // define its size attribute
p.color := "red"; // define its color attribute
"?word=" + Url_Encode(word) + // word parameter
"&url=" + Url_Encode(a.href) // url parameter
res.result = Markup(page); // this is the result
WebServer_Publish(where, Highlight);
WebServer_Start("/dev/null",port);
The highlight proxy consists of single function called Highlight (lines 6-25). This function is exported with the built-in WebL web server in lines 27 and 28. (More information about the built-in Web server can be found in the WebServer module documentation.) On lines 7 and 8 we extract the URL and word parameters passed to the proxy. Note how we use service combinators to provide sensible defaults in case no parameters are present.
Lines 11-16 does the actual highlighting of the word on the page. In line 13 a "font" element is wrapped around the occurrence of the word. In addition, lines 14 and 15 define the size and color attributes of the new font element. Note that we also make sure in line 11 that we only wrap word occurences outside of the title of the Web page.
The next step is to rewrite the href attribute of all anchors ("a" elements) in the page to work correctly with our proxy. This involves passing the old href attribute as the URL parameter to our proxy. We use the Url_Encode function to encode special characters in the URL as dictated by the URL specification. Finally, in line 24 we re-generate the markup of the (now modified) page, and return it back to the browser by assigning it to the appropriate field of the server response object res.