Define the functions that implements the tag in C, using NSAPI.
Include the header shtml_public.h, which is in the directory install_dir/plugins/include/shtml.
Link against the shtml shared library.
On UNIX platforms, libshtml.so or .sl is in install_dir/bin/https/lib.
ShtmlTagExecuteFunc is the actual tag handler. It gets called with the usual NSAPI pblock, Session, and Request variables. In addition, it also gets passed to the TagUserData created from the result of executing the tag loading and page loading functions (if defined) for that tag.
The signature for the tag execution function is:
typedef int (*ShtmlTagExecuteFunc)(pblock*, Session*, Request*, TagUserData, TagUserData);
Write the body of the tag execution function to generate the output to replace the tag in the .shtml page. Do this in the usual NSAPI way, using the net_write NSAPI function, which writes a specified number of bytes to a specified socket from a specified buffer.
For more information about NSAPI plugins and functions, see the Sun Java System Web Server 6.1 SP12 NSAPI Programmer’s Guide.
The tag execution function must return an int that indicates whether the server should proceed to the next instruction in obj.conf, and is one of the following:
The other functions you must define for your tag are:
This is called when a page containing the tag is parsed. It is not called if the page is retrieved from the browser’s cache. It basically serves as a constructor, the result of which is cached and is passed into ShtmlTagExecuteFunc whenever the execution function is called.
This is basically a destructor for cleaning up what was created in the ShtmlTagInstanceLoad function. It gets passed the result that was originally returned from the ShtmlTagInstanceLoad function.
This is called when a page containing the tag is executed, regardless of whether the page is still in the browser’s cache. This provides a way to make information persistent between occurrences of the same tag on the same page.
This is called after a page containing the tag has executed. It provides a way to clean up any allocations done in a ShtmlTagPageLoadFunc and thus gets passed the result returned from the ShtmlTagPageLoadFunc.
The signatures for these functions are:
Here is the code that implements the HELLO tag:
/* * mytag.c: NSAPI functions to implement #HELLO SSI calls * * */ #include "nsapi.h" #include "shtml/shtml_public.h" /* FUNCTION : mytag_con * * DESCRIPTION: ShtmlTagInstanceLoad function */ #ifdef __cplusplus extern "C" #endif TagUserData mytag_con(const char* tag, pblock* pb, const char* c1, size_t t1) { return NULL; } /* FUNCTION : mytag_des * * DESCRIPTION: ShtmlTagInstanceUnload */ #ifdef __cplusplus extern "C" #endif void mytag_des(TagUserData v1) { } /* FUNCTION : mytag_load * * DESCRIPTION: ShtmlTagPageLoadFunc */ #ifdef __cplusplus extern "C" #endif TagUserData mytag_load(pblock *pb, Session *sn, Request *rq) { return NULL; } /* FUNCTION : mytag_unload * * DESCRIPTION: ShtmlTagPageUnloadFunc */ # #ifdef __cplusplus extern "C" #endif void mytag_unload(TagUserData v2) { } /* FUNCTION : mytag * * DESCRIPTION: ShtmlTagExecuteFunc */ #ifdef __cplusplus extern "C" #endif int mytag(pblock* pb, Session* sn, Request* rq, TagUserData t1, TagUserData t2) { char* buf; int length; char* client; buf = (char *) MALLOC(100*sizeof(char)); length = util_sprintf(buf, "<h1>Hello World! </h1>", client); if (net_write(sn->csd, buf, length) == IO_ERROR) { FREE(buf); return REQ_ABORTED; } FREE(buf); return REQ_PROCEED; } /* FUNCTION : mytag_init * * DESCRIPTION: initialization function, calls shtml_add_tag() to * load new tag */ # #ifdef __cplusplus extern "C" #endif int mytag_init(pblock* pb, Session* sn, Request* rq) { int retVal = 0; // NOTE: ALL arguments are required in the shtml_add_tag() function retVal = shtml_add_tag("HELLO", mytag_con, mytag_des, mytag, mytag_load, mytag_unload); return retVal; } /* end mytag.c */