Web page handling

Examples are given here for pages in memory and pages in files. Feel free to experiment with mixed operation, but note that the code in <PNet>\Services\Pages.fth is maintained by MPE and may/will change with future releases, so we suggest that you keep changes in a separate file.

Configuration

The following two equates are only used if they are not already defined.

1 equ MemPages? \ -- n
Set this non-zero to compile the memory pages code.

1 equ FilePages?        \ -- n
Set this non-zero to compile the file pages code.

Data structures

Since Web pages may be stored in a file system, other mass storage or memory, a common data structure is used based on the needs of pages stored in CPU memory. Pages stored in CPU memory are identified by name in the HTTPPAGES.VOC vocabulary. Executing the word returns the address of the data structure and its type.

struct /PageDef \ -- n
The structure used for a page definition.

The following equates define the types of pages that are available. They are stored in the PG.TYPE field of a /PageDef structure.

0 equ NoPage    \ -- 0
Page not found or unusable.

1 equ FilePage  \ -- n
The page is in a file.

2 equ SMemPage  \ -- n
The page is in memory (e.g. Flash) and need not be released.

3 equ xtPage    \ -- n
The page is created by executing a Forth word, whose xt is in the PG.addr field.

4 equ HMemPage  \ -- n
The page has been loaded into the heap.

vocabulary HTTPpages.voc        \ --
The default vocabulary used to hold page data.

: voc>wid       \ xt(voc) -- wid
Return the WID of a vocabulary whose XT is supplied. This definition is implementation dependent.

: FindVocPage   \ caddr len -- struct|0 type|0
Find a page from its name caddr/len, returning the data structure address struct and a type code type. If the page cannot be found, both return values are zero. The action is to look up the name in the HTTPpages.voc vocabulary and extract the page type from the /PAGEDEF structure.

Executable pages

: xtPage:       \ xt "<name> -- ; -- struct type
Creates a page which later executes the given action, e.g.

  ' doNewApp xtPage: /NewApp.asp

The specified action must have the stack effect

  qaddr qlen --

where the input is the rest of the line after GET or POST.

Memory pages

: MemPage:      \ "<Forthname>" "<filename>" -- ; -- struct type
An INTERPRETER definition that creates <Forthname> in the HTTPPAGES.VOC vocabulary and a /PAGEDEF structure, and then loads file <filename> into the dictionary as data. At run-time the address of the /PAGEDEF structure is returned. When the HTTP server is running it will serve the data in response to a GET request for Forthname.


MemPage: /home.htm %IPSTACK%\powernet.htm

Example memory pages

These files are compiled by default unless the equate TestPages? exists and is set to zero.

MemPage: /home.htm %IPSTACK%\TestPages\home.htm
Creates embedded memory page /HOME.HTM from the data in %IPSTACK%\home.htm. The leading '/' is required as it is not removed by the command parser.

MemPage: /bgimage.jpg %IPSTACK%\TestPages\bgimage.jpg
The background image for POWERNET.HTM.

MemPage: /favicon.ico %IPSTACK%\TestPages\favicon.ico
The page icon for these pages.

MemPage: /contact.htm %IPSTACK%\TestPages\contact.htm
Creates embedded memory page /CONTACT.HTM from the data in %IPSTACK%\contact.htm.

MemPage: /form1.htm %IPSTACK%\TestPages\form1.htm
A simple form.

MemPage: /thanks.htm %IPSTACK%\TestPages\thanks.htm
The response to FORM1.HTM.

MemPage: /form2.htm %IPSTACK%\TestPages\form2.htm
A simple form with ASP and GET handling.

MemPage: /form3.htm %IPSTACK%\TestPages\form3.htm
A simple form with ASP and POST handling.

MemPage: /thanks.asp %IPSTACK%\TestPages\thanks.asp
The scripted response to FORM2.HTM.

MemPage: /thanks3.asp %IPSTACK%\TestPages\thanks3.asp
The scripted response to FORM3.HTM.

File pages

The defaults for this section assume that the FAT file system in in use.

create PageDir$ \ -- addr
The base directory for pages. This will be prepended to the page name given to SearchPage. The string is held as a counted string in the buffer. Make sure that your application sets PageDir$ to point to its own pages. The directory name must not end in a separator. The version here sets the contents to \PAGES and is only used if PageDir$ is undefined here.

: PrepFilename  \ caddr len --
Prepare a URL to be a file name. For Windows/DOS file systems, convert '/' characters to '\'. For Unices, do nothing.

: [io           \ -- ; R: -- ipvec opvec
Save the current I/O devices on the return stack.

: io]           \ -- ; R: ipvec opvec --
Restore the I/O devices from the return stack.

: FindFilePage  \ caddr len -- struct|0 type|0
The action of SearchPage for pages in files. Find a page into memory from its name caddr/len, returning the structure address and length.

Page look up

: SearchPage    \ caddr len -- struct|0 type|0
Find a page from its name caddr/len, returning the data structure address struct and a type code type. If the page cannot be found, both return values are zero. The action is to look up the name in the HTTPpages.voc vocabulary and extract the page type from the /PAGEDEF structure. If file pages are required, the page directory is searched.