Frontier Tutorials / Writing an ObjectNotFoundHandler / Mixing Modes

Mixing Modes

So far, we have created a new script for each different type of operation we wanted to do. However, sometimes we want to try several different approaches before we give up. For example, when Mode 3 (stuffing the page table) fails, we want to fall back on a customized Page Not Found response using Mode 1 or Mode 2.

As it turns out, we can easily do that, as long as we keep ordering in mind. But we have to do two things:

The Master ONFH Script

Replace your #objectNotFoundHandler entry with the following script. Note that we're naming the actual script #objectNotFoundHandler, instead of just putting its address there. This script becomes the master controller for other ONFH scripts.

« ObjectNotFoundHandler Master Script
local
space picturepageTbl = html.getPageTableAddress()
space picturetoolsTbl = pageTbl^.tools
 
bundle « Mode 3 ONFH scripts
space picture« Mode 3 scripts are assumed to return if unsuccessful, but not if successful.
space picture« As a result, we can simply call them one after another.
space picturetoolsTbl^.ReturnBinaryObject( pageTbl )
 
« URL not handled by Mode 3 scripts; call PageNotFound handler for custom 404 page.
return( toolsTbl^.PageNotFound( pageTbl ) )

This script first calls the Mode 3 ReturnBinaryObject script, to give it a chance to handle the URL. Other Mode 2 and Mode 3 scripts could be called from the same location. If ReturnBinaryObject does handle the URL, the remainder of ObjectNotFoundHandler will be skipped (because ReturnBinaryObject finishes with a scriptError("!return") call).

ObjectNotFoundHandler makes another assumption about the ReturnBinaryObject script (and other Mode 2 & 3 scripts), though. It assumes that ReturnBinaryObject will return to the caller if it doesn't handle the URL. A few minor modifications to the ReturnBinaryObject script we created earlier will take care of this.

Finally, after giving the Mode 2 or 3 script (or multiple Mode 2 & 3 scripts) a chance to handle the URL, the master script functions as a Mode 1 ONFH, returning some default page content.

Modified ReturnBinaryObject Script

fatpage picture space picture This page is a Fat Page. It includes the modified ReturnBinaryObject 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.

The ReturnBinaryObject script we wrote earlier is appropriate if it is the sole ONFH script. However, we need to modify it slightly to make it play nice with other ONFH scripts. The changes are:

After these changes, the script will simply return to the caller (the ObjectNotFoundHandler master script) if it is unable to handle a URL. If it does handle a URL, it will call scriptError("!return") to bypass further processing, as before.

Modified Mode 3 Scripts, In General

The general structure of a Mode 3 ONFH script for use with the master ONFH script is:

on ONFHMode3()
space picturelocal
space picturepageTbl = html.getPageTableAddress()
space picturelastResolvedAdr = pageTbl^.lastNomad
space pictureremainingPath = pageTbl^.remainingPath
space picture 
space pictureif ( IT ISN'T ONE I PROCESS )
space picturereturn
space picture 
space pictureif ( I CAN'T RESOLVE IT )
space picturereturn
space picture 
space pictureif ( IT ISN'T THE CORRECT TYPE )
space picturereturn
space picture 
space pictureCOPY THE OBJECT TO pageTbl^.responseBody
space pictureSET pageTbl^.responseHeaders.["Content-Type"]
space pictureOPTIONALLY SET pageTbl^.responseHeaders.["Last-Modified"]
space picture 
space picture« Don't let mainResponder.respond overwrite our response
space picturescriptError( "!return" )

Note that it simply returns without error if it doesn't handle the URL.

Modified Mode 2 Scripts

The general structure of a Mode 2 ONFH script for use with the master ONFH script is:

on ONFHMode2()
space picturelocal
space picturepageTbl = html.getPageTableAddress()
space picturelastResolvedAdr = pageTbl^.lastNomad
space pictureremainingPath = pageTbl^.remainingPath
space pictureif ( TEST CONDITION )
space picturelocal ( newUrl )
space picture« CALCULATE NEW URL VALUE
space picturescriptError( "!redirect " + newUrl )

Note that it simply returns without error if it doesn't handle the URL.

Try It Out

Go to your browser again, and and load the page "http://localhost/onfhTutorial/download/binary1.zip" (the same one as before). You should see exactly the same behavior as before.

Now change the filename in the URL from "binary1.zip" to "binary2.zip". You should see the page-not-found page returned by your PageNotFound script.

PageNotFound3 picture

Variations

Instead of the master script functioning as a Mode 1 ONFH handler, you could make it a Mode 2 handler by doing an unconditional redirect, as we did in the Mode 2 example.

Now, finally, it's time to resolve the misdirected-URL problem.

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 Applications
ONFH Resources
Bonus: The "Penultimate" Master Script
Bonus: Mode 3 Utility Script
About the Author