Frontier Tutorials / Writing an ObjectNotFoundHandler / Bonus: Mode 3 Utility Script

Bonus: Mode 3 Utility Script

fatpage picture space picture This page is a Fat Page. It includes the ONFH_Mode3Return script, encoded by and for Frontier. To retrieve the script(s), save the page as source text and open it using the File->Open command.

This page contains a generic Mode 3 ONFH script. It is based on the ReturnBinaryObject script in the Mode 3 Example. You can call this script from small, simple scripts to implement Mode 3 ONFH handlers. See below for a complete discussion of the script.

Directions For Use

Install the "ultimate" master script.

Install the ONFH_Mode3Return script in the ["#tools"] table.

Install one or more of the scripts the following sections in the ["#tools"].ONFH_2and3 table.


Serving CSS Files

Frontier defines two standard macros for dealing with cascading style sheets: embedStyleSheet embeds the style sheet directly into a page. linkStyleSheet writes the style sheet out to disk, and returns a style sheet link tag for inclusion in the page. One of these is typically invoked from the #pageHeader or template.

One of these is typically invoked from the #pageHeader or template.

Without this script or some other method of serving CSS files, mainResponder sites cannot use the linkStyleSheet macro. Instead, they must use embedStyleSheet to embed the style sheet in each page, which can significantly increase the size of each page.

The following Mode 3 script returns a requested cascading style sheet. Installing this script in your mainResponder website allows you to use the linkStyleSheet macro in your #pageheader, and serve out the correct .css files on request.

In this script, we provide a small callback function. It calls html.utilities.renderStyleSheet, which is the workhorse script that does the heavy lifting for embedStyleSheets and linkStyleSheets.

Directions For Use

Create the ONFH_ReturnCSS script in the ["#tools"].ONFH_2and3 table:

on ONFH_ReturnCSS()
space picturelocal
space picturepageTbl = html.getPageTableAddress()
space pictureon ProcessEntryCB( entryAdr )
space picturereturn ( html.utilities.renderStyleSheet( entryAdr, pageTbl ) )
space picturepageTbl^.tools^.ONFH_Mode3Return( ".css", "stylesheets", "text/css", @ProcessEntryCB )

Create a default style sheet entry in your website, as described in this Userland CSS article.

Open your #pageHeader and change the embedStyleSheet call to linkStyleSheet. Or just add the linkStyleSheet call, if neither is there.

You can create additional stylesheets, as described in the Userland CSS article. To use a stylesheet other than the default, simply pass the stylesheet name in your linkStyleSheet call.


Serving JavaScript Source Files

This script should work. However, for some reason my browser doesn't properly request the file, so I've been unable to test it.

This Mode 3 script returns a requested JavaScript source "file". Installing this script in your mainResponder website allows you to use the <SCRIPT TYPE='text/javascript' SRC='filename.js'></SCRIPT> syntax, and serve out the specified JavaScript source "file" from a #javascript table in your website on request.

Directions For Use

Create the ONFH_ReturnJS script in the ["#tools"].ONFH_2and3 table:

on ONFH_ReturnJS()
space picturelocal
space picturepageTbl = html.getPageTableAddress()
space picturepageTbl^.tools^.ONFH_Mode3Return( ".js", "javascripts", "text/javascript" )

Create a subdirectory named "#javascripts" in your website table.

Inside the #javascripts directory, create (or paste) the JavaScript source text that you would like to include in your page.

Add the <script language='JavaScript' type='text/javascript' src='sourcefile.js'></script> tags to your page, #pageheader, or #template.


About The ONFH_Mode3Return Script

The ONFH_Mode3Return script is embedded in this page. It looks like this:

on ONFH_Mode3Return( suffix, subtableName, mimeType, processEntryCallbackAdr=nil )
space picturelocal
space picturepageTbl = html.getPageTableAddress()
space picture« Do we have anything to return at all?
space pictureif ! defined( pageTbl^.[ subtableName ] )
space picture« We have no matching subtable
space picturereturn
space picture 
space picturelocal
space picturelastResolvedAdr = pageTbl^.lastNomad
space pictureremainingPath = pageTbl^.remainingPath
space picture 
space picture« Is this the kind of request we're looking for?
space pictureif ! ( string.lower( remainingPath ) endsWith string.lower( suffix ) )
space picture« Wrong kind of request.
space picturereturn
space picture 
space picturewhile typeOf( lastResolvedAdr^ ) == addressType
space picturelastResolvedAdr = lastResolvedAdr^
space picture 
space picture« Parse name and strip suffix
space picturelocal
space picturesubTbl = pageTbl^.[ subtableName ]
space pictureterminal = string.nthField( remainingPath, "/", string.countFields( remainingPath, "/" ) )
space pictureentryName = string.popSuffix( terminal, '.' )
space picture 
space picture« Does a matching entry exist?
space picturelocal
space pictureentryAdr = @subTbl^.[ entryName ]
space pictureif ! defined( entryAdr^ )
space picture« No matching entry
space picturereturn
space picture 
space picture« We found the object! Now serve it up.
space pictureif defined( processEntryCallbackAdr^ )
space picturepageTbl^.responseBody = processEntryCallbackAdr^( entryAdr )
space pictureelse
space picturepageTbl^.responseBody = string( entryAdr )
space picturepageTbl^.responseHeaders.["Content-Type"] = mimeType
space picturelocal ( modDate = timeModified( entryAdr ) )
space pictureif ( typeOf( modDate ) == booleanType )
space picturemodDate = clock.now()
space picturepageTbl^.responseHeaders.["Last-Modified"] = date.netStandardString( modDate )
space picture 
space picture« Don't let mainResponder.respond overwrite our response
space picturescriptError ("!return")

This script should look familiar--it's very similar to the ReturnBinaryObject script in the Mode 3 example. The main differences are:

It does the following:

  1. Check whether the site has the specified subtable at all. If not, there's nothing for us to do, so we return to the caller without doing any further processing.

  2. Get the last resolved address and the remainder of the path from the page table. (Though in this case we won't use the last resolved address at all; everything we need is in the page table.)

  3. Check to make sure that the URL is, in fact, the type of URL we want. We do this by checking that the remainingPath ends with the specified suffix.

  4. Extract the download entry name. Strip off the suffix and discard it.

  5. Check for a matching entry in the specified subtable. If such an entry is not found, return to the caller without doing any further processing.

  6. If the address of a callback function is passed in, call the callback function to get the response body. Otherwise, simply coerce the target entry to a string.

  7. Set the content-type and last-modified fields of the reply. For entries for which the modification date is not available, the last-modified field is set to the current time.

  8. Throw a scriptError("!return") to bypass further mainResponder processing and return our response without further modification.

And finally, a bit about the author.

Tutorial Contents
Writing an ObjectNotFoundHandler
ONFH Overview
ONFH Modes of Operation
About The Examples
Mode 1 Example
Mode 2 Example
Mode 3 Example
Mixing Modes
Misdirected URLs
ONFH Summary
ONFH Resources
Bonus: The "Penultimate" Master Script
Bonus: Mode 3 Utility Script
About the Author