1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
<HTML><HEAD>
<TITLE>C Examples -- Example 3: counter</TITLE>
<LINK rel=Previous href="c-app2.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="c-app4.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="c-app2.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-app4.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="15848">
</a><h3>Example 3: counter</h3>
<p><a name="15868">
</a>The following example implements a request function that generates the popular odometer-like page count image. The code is based on the popular odometer.c CGI program available at many archive sites, but because it's loaded directly into the AOLserver it generates the odometer at lightening speed, easily 10x faster! </p>
<p><a name="15866">
</a>This example can be found in the <code>examples/c/counter</code> directory.</p>
<p><a name="15895">
</a></p>
<pre> <a name="15896"></a>/*-
<a name="15901"></a> *
<a name="15902"></a> * This code implements an odometer-like page counter extension to the
<a name="15903"></a> * AOLserver. The code is based on the popular "odometer" CGI
<a name="15904"></a> * by Chris Stephens, stephenc@pcmail.cbil.vcu.edu, and
<a name="15905"></a> * Fred Christiansen, fredch@fc.hp.com (see below).
<a name="15906"></a> *
<a name="15907"></a> * Why use this module instead of the CGI? By loading the code directly
<a name="15908"></a> * into the AOLserver, you avoid all the painful overhead of CGI.
<a name="15909"></a> * The result is this module is more than 10 times as fast as the CGI!
<a name="15910"></a> * Plus, it compiles and run on all platforms!
<a name="15911"></a> *
<a name="15912"></a> * To use the counter, load the module and insert an image
<a name="15913"></a> * link of the form <IMG SRC="/NS/Counter/my/page.htm">.
<a name="15914"></a> * The NsCounter() function will return a small odometer image of the
<a name="15915"></a> * number of hits to the corresponding page (i.e., /my/page.htm). The
<a name="15916"></a> * page count is kept in a file prefixed by .odo (i.e., /my/.odo.page.htm).
<a name="15917"></a> *
<a name="15918"></a> * To load this module, add the following to the [ns\server\<vs>\modules]
<a name="15919"></a> * section of your nsd.ini file where <vs> is the name of the
<a name="15920"></a> * server you want to add the counter too:
<a name="15921"></a> *
<a name="15922"></a> * counter=counter.dll ; or counter.so on Unix platforms
<a name="15923"></a> *
<a name="15924"></a> * This prefix(s) of the count URL can be set with the "UrlPrefix"
<a name="15925"></a> * key in the module's nsd.ini configuration file section (i.e.,
<a name="15926"></a> * [ns\server\<vs>\module\counter]). The value is a comma separated
<a name="15927"></a> * list of URL prefixes. For example, if the list includes the
<a name="15928"></a> * "/cnt,/count" prefixes, then any URL which begins with /cnt or
<a name="15929"></a> * /count will be processed by the NsCounter() function.
<a name="15930"></a> *
<a name="15931"></a> * Note: the setup interface in AOLserver 2.0 is open-ended, so you can
<a name="15932"></a> * use it to view/edit these parameters by using the "List AOLserver
<a name="15933"></a> * Configuration Sections" available from the "Setup Home" page in expert
<a name="15934"></a> * mode
<a name="15935"></a> *
<a name="15936"></a> */
<a name="15937"></a>
<a name="15938"></a>
<a name="17875"></a>#include "ns.h"
<a name="15939"></a>
<a name="15940"></a>#include <errno.h>
<a name="15941"></a>#include <assert.h>
<a name="15942"></a>#include <string.h>
<a name="15943"></a>#ifdef WIN32
<a name="15944"></a>#include <io.h>
<a name="15945"></a>#include <fcntl.h>
<a name="15946"></a>#else
<a name="15947"></a>#include <unistd.h>
<a name="15948"></a>#endif
<a name="15949"></a>
<a name="15950"></a>
<a name="15951"></a>#define CONFIG_PREFIX "UrlPrefix"
<a name="15952"></a>#define DEFAULT_PREFIX "/cgi-bin/counter,/NS/Counter,/ns/counter"
<a name="15953"></a>#define COUNT_PREFIX ".odo"
<a name="15954"></a>
<a name="15955"></a>static Ns_OpProc NsCounter;
<a name="15956"></a>
<a name="15957"></a>int Ns_ModuleVersion = 1;
<a name="15958"></a>
<a name="15959"></a>int
<a name="15960"></a>Ns_ModuleInit(char *hServer, char *hModule)
<a name="15961"></a>{
<a name="15962"></a> char *configPath;
<a name="15963"></a> char *urlPrefix;
<a name="15964"></a> char *next;
<a name="15965"></a> Ns_DString dsBuf;
<a name="15966"></a> Ns_DString dsPath;
<a name="15967"></a>
<a name="15968"></a>
<a name="15969"></a> /*
<a name="15970"></a> * Fetch the UrlPrefix from the config file.
<a name="15971"></a> */
<a name="15972"></a> configPath = Ns_ConfigGetPath(hServer, hModule, NULL);
<a name="15973"></a> urlPrefix = Ns_ConfigGetValue(configPath, CONFIG_PREFIX);
<a name="15974"></a> if (urlPrefix == NULL) {
<a name="15975"></a> urlPrefix = DEFAULT_PREFIX;
<a name="15976"></a> }
<a name="15977"></a>
<a name="15978"></a> /*
<a name="15979"></a> * Register NsCounter at each of the URL's specified
<a name="15980"></a> * in the comma seperated UrlPrefix list.
<a name="15981"></a> */
<a name="15982"></a> Ns_DStringInit(&dsBuf);
<a name="15983"></a> Ns_DStringInit(&dsPath);
<a name="15984"></a> Ns_DStringAppend(&dsBuf, urlPrefix);
<a name="15985"></a> next = dsBuf.string;
<a name="15986"></a>
<a name="17883"></a> do {
<a name="15987"></a> urlPrefix = next;
<a name="15988"></a> next = strchr(urlPrefix, `,');
<a name="15989"></a> if (next != NULL) {
<a name="15990"></a> *next++ = `\0';
<a name="15991"></a> }
<a name="15992"></a> if (*urlPrefix != `/') {
<a name="15993"></a> Ns_Log(Warning, "%s(%s): Invalid URL prefix skipped: %s",
<a name="16236"></a> hModule, hServer, urlPrefix);
<a name="15994"></a> } else {
<a name="15995"></a> Ns_NormalizePath(&dsPath, urlPrefix);
<a name="15996"></a> urlPrefix = Ns_DStringExport(&dsPath);
<a name="15997"></a> Ns_RegisterRequest(hServer, "GET", urlPrefix, NsCounter, ns_free,
urlPrefix, 0);
<a name="15998"></a> }
<a name="15999"></a> } while (next != NULL);
<a name="16000"></a> Ns_DStringFree(&dsBuf);
<a name="16001"></a> Ns_DStringFree(&dsPath);
<a name="16002"></a>
<a name="16003"></a> return NS_OK;
<a name="16004"></a>}
<a name="16005"></a>
<a name="16006"></a>
<a name="16007"></a>/*-
<a name="16008"></a>** Originally count.c, by Chris Stephens, stephenc@pcmail.cbil.vcu.edu, (c)1995.
<a name="16009"></a>** Code cleaned up and enhanced by Fred Christiansen, fredch@fc.hp.com,
<a name="16010"></a>** as odometer.c; supports:
<a name="16011"></a>** - 8 digits instead of 7 (and by changing ODO_DIGITS, up to 10 (number
<a name="16012"></a>** of digits in an unsigned long))
<a name="16013"></a>*/
<a name="16014"></a>
<a name="16015"></a>#define NL `\n' /* new line char */
<a name="16016"></a>#define ODO_DIGITS 8 /* # digits displayed */
<a name="16017"></a>#define BUFSIZE (ODO_DIGITS+2) /* 8 digits + NL + nul */
<a name="16018"></a>#define BM_HT 16 /* bitmap height */
<a name="16019"></a>#define BM_WD 8 /* bitmap width */
<a name="16020"></a>
<a name="16021"></a>
<a name="16022"></a>static char *bitmap[] = {
<a name="16023"></a>"0xff", "0xff", "0xff", "0xc3", "0x99", "0x99", "0x99", "0x99", /* rows 1-8 of 0 */
<a name="16024"></a>"0x99", "0x99", "0x99", "0x99", "0xc3", "0xff", "0xff", "0xff", /* rows 9-16 of 0 */
<a name="16025"></a>"0xff", "0xff", "0xff", "0xcf", "0xc7", "0xcf", "0xcf", "0xcf", /* rows 1-8 of 1 */
<a name="16026"></a>"0xcf", "0xcf", "0xcf", "0xcf", "0xcf", "0xff", "0xff", "0xff", /* rows 9-16 of 1 */
<a name="16027"></a>"0xff", "0xff", "0xff", "0xc3", "0x99", "0x9f", "0x9f", "0xcf", /* rows 1-8 of 2 */
<a name="16028"></a>"0xe7", "0xf3", "0xf9", "0xf9", "0x81", "0xff", "0xff", "0xff", /* rows 9-16 of 2 */
<a name="16029"></a>"0xff", "0xff", "0xff", "0xc3", "0x99", "0x9f", "0x9f", "0xc7", /* rows 1-8 of 3 */
<a name="16264"></a>"0x9f", "0x9f", "0x9f", "0x99", "0xc3", "0xff", "0xff", "0xff", /* rows 9-16 of 3 */
<a name="16265"></a>"0xff", "0xff", "0xff", "0xcf", "0xcf", "0xc7", "0xc7", "0xcb", /* rows 1-8 of 4 */
<a name="16266"></a>"0xcb", "0xcd", "0x81", "0xcf", "0x87", "0xff", "0xff", "0xff", /* rows 9-16 of 4 */
<a name="16033"></a>"0xff", "0xff", "0xff", "0x81", "0xf9", "0xf9", "0xf9", "0xc1", /* rows 1-8 of 5 */
<a name="16034"></a>"0x9f", "0x9f", "0x9f", "0x99", "0xc3", "0xff", "0xff", "0xff", /* rows 9-16 of 5 */
<a name="16035"></a>"0xff", "0xff", "0xff", "0xc7", "0xf3", "0xf9", "0xf9", "0xc1", /* rows 1-8 of 6 */
<a name="16036"></a>"0x99", "0x99", "0x99", "0x99", "0xc3", "0xff", "0xff", "0xff", /* rows 9-16 of 6 */
<a name="16037"></a>"0xff", "0xff", "0xff", "0x81", "0x99", "0x9f", "0x9f", "0xcf", /* rows 1-8 of 7 */
<a name="16038"></a>"0xcf", "0xe7", "0xe7", "0xf3", "0xf3", "0xff", "0xff", "0xff", /* rows 9-16 of 7 */
<a name="16039"></a>"0xff", "0xff", "0xff", "0xc3", "0x99", "0x99", "0x99", "0xc3", /* rows 1-8 of 8 */
<a name="16040"></a>"0x99", "0x99", "0x99", "0x99", "0xc3", "0xff", "0xff", "0xff", /* rows 9-16 of 8 */
<a name="16041"></a>"0xff", "0xff", "0xff", "0xc3", "0x99", "0x99", "0x99", "0x99", /* rows 1-8 of 9 */
<a name="16042"></a>"0x83", "0x9f", "0x9f", "0xcf", "0xe3", "0xff", "0xff", "0xff" /* rows 9-16 of 9 */
<a name="16043"></a>};
<a name="16044"></a>
<a name="16045"></a>
<a name="16046"></a>/*
<a name="16047"></a> * NsCounter -
<a name="16048"></a> *
<a name="16049"></a> * Handle a request of the form "/NS/Counter/my/page.htm" to create an
<a name="16050"></a> * odometer-like count of visits to the /my/page.htm page.
<a name="16051"></a> *
<a name="16052"></a> */
<a name="16053"></a>static int
<a name="16054"></a>NsCounter(void *context, Ns_Conn *conn)
<a name="16055"></a>{
<a name="16056"></a> char buf[BUFSIZE], dial[ODO_DIGITS];
<a name="16057"></a> unsigned long cnt;
<a name="16058"></a> int status, i, l;
<a name="16059"></a> char *p;
<a name="16060"></a> Ns_DString ds;
<a name="16061"></a> int fd;
<a name="16062"></a> char *uri;
<a name="16063"></a> char *hServer;
<a name="16064"></a> char *urlPrefix;
<a name="16065"></a> int nPrefix;
<a name="16066"></a>
<a name="16067"></a> hServer = Ns_ConnServer(conn);
<a name="16068"></a> urlPrefix = (char *) context;
<a name="16069"></a>
<a name="16070"></a> /*
<a name="16071"></a> * Figure out how many parts are in the
<a name="16072"></a> * prefix, i.e., /NS/Counter has 2 parts.
<a name="16073"></a> */
<a name="16074"></a> nPrefix = 0;
<a name="16075"></a> p = urlPrefix;
<a name="16076"></a> while ((p = strchr(p, `/')) != NULL) {
<a name="16077"></a> ++p;
<a name="16078"></a> ++nPrefix;
<a name="16079"></a> }
<a name="16080"></a> if (conn->request->urlc > nPrefix) {
<a name="16081"></a> uri = Ns_SkipUrl(conn->request, nPrefix);
<a name="16082"></a> } else {
<a name="16083"></a> return Ns_ConnReturnBadRequest(conn, "Missing URI");
<a name="16084"></a> }
<a name="16085"></a>
<a name="16086"></a>
<a name="16087"></a> /*
<a name="16088"></a> * Convert the URI to the file, sneaking in counter prefix before the last
<a name="16089"></a> * path element.
<a name="16090"></a> */
<a name="16091"></a> Ns_DStringInit(&ds);
<a name="16092"></a> p = strrchr(uri, `/');
<a name="16093"></a> *p = `\0';
<a name="16094"></a> Ns_UrlToFile(&ds, hServer, uri);
<a name="16095"></a> Ns_DStringAppend(&ds, "/");
<a name="16096"></a> Ns_DStringAppend(&ds, COUNT_PREFIX);
<a name="16097"></a> *p++ = `/';
<a name="16098"></a> Ns_DStringAppend(&ds, ".");
<a name="16099"></a> Ns_DStringAppend(&ds, p);
<a name="16100"></a>
<a name="16101"></a>
<a name="16102"></a> /*
<a name="16103"></a> * Open (or create) the counter file and increment the page count.
<a name="16104"></a> */
<a name="16105"></a> fd = open(ds.string, O_RDWR | O_CREAT, 0660);
<a name="16106"></a> if (fd < 0) {
<a name="16107"></a> Ns_Log(Error, "Could not open counter file `%s' for URI `%s': %s",
<a name="16108"></a> ds.string, uri, strerror(errno));
<a name="16109"></a> Ns_DStringFree(&ds);
<a name="16110"></a> return Ns_ConnReturnInternalError(conn);
<a name="16111"></a> }
<a name="16112"></a> Ns_DStringFree(&ds);
<a name="16113"></a> i = read(fd, buf, BUFSIZE);
<a name="16114"></a> if (i > 0) {
<a name="16115"></a> buf[i] = `\0';
<a name="16116"></a> cnt = strtoul(buf, (char **) NULL, 10);
<a name="16117"></a> } else {
<a name="16118"></a> cnt = 0;
<a name="16119"></a> }
<a name="16120"></a> lseek(fd, 0, SEEK_SET);
<a name="16121"></a> sprintf(buf, "%u\n", ++cnt);
<a name="16122"></a> write(fd, buf, strlen(buf));
<a name="16123"></a> close(fd);
<a name="16124"></a>
<a name="16125"></a>
<a name="16126"></a> /* Calc length (ignoring NL) and copy right-to-left into dial */
<a name="16127"></a>
<a name="16128"></a> for (l = 0, p = buf; *p != `\0' && *p != NL; l++, p++);
<a name="16129"></a> for (i = 0, p = buf; i < l; i++, p++)
<a name="16130"></a> dial[ODO_DIGITS - l + i] = *p;
<a name="16131"></a> for (i = 0; i < (ODO_DIGITS - l); i++) /* backfill with zeros */
<a name="16132"></a> dial[i] = `0';
<a name="16133"></a>
<a name="16134"></a> /*
<a name="16135"></a> * Build up the odometer xbm.
<a name="16136"></a> */
<a name="16137"></a> Ns_DStringPrintf(&ds, "#define odo_width %d\n", (BM_WD * ODO_DIGITS));
<a name="16138"></a> Ns_DStringPrintf(&ds, "#define odo_height %d\n", BM_HT);
<a name="16139"></a> Ns_DStringAppend(&ds, "static char odo_bits[] = {\n");
<a name="16140"></a> for (i = 0; i < BM_HT; i++) {
<a name="16141"></a> for (l = 0; l < ODO_DIGITS; l++) {
<a name="16142"></a> Ns_DStringAppend(&ds, bitmap[(((dial[l] - `0') * BM_HT) + i)]);
<a name="16143"></a> Ns_DStringAppend(&ds, ", ");
<a name="16144"></a> if (l == 7)
<a name="16145"></a> Ns_DStringAppend(&ds, "\n");
<a name="16146"></a> }
<a name="16147"></a> if (i == 15) {
<a name="16148"></a> Ns_DStringAppend(&ds, "};");
<a name="16149"></a> }
<a name="16150"></a> }
<a name="16151"></a> Ns_DStringAppend(&ds, "\n");
<a name="16152"></a>
<a name="16153"></a> /*
<a name="16154"></a> * Setup the HTTP headers.
<a name="16155"></a> */
<a name="16156"></a> Ns_ConnSetRequiredHeaders(conn, "image/x-xbitmap", ds.length);
<a name="16157"></a> status = Ns_ConnFlushHeaders(conn, 200);
<a name="16158"></a> if (status == NS_OK) {
<a name="16159"></a> /*
<a name="16160"></a> * Send the bitmap if the headers were sent o.k.
<a name="16161"></a> *
<a name="16162"></a> * Note that the underlying communications driver may send
<a name="16163"></a> * less than the number of bytes requested so we need to
<a name="16164"></a> * call Ns_ConnWrite() in a while loop to ensure all the
<a name="16165"></a> * bytes are sent. If the connection is shut down while
<a name="16166"></a> * attempting to send, the communication driver returns
<a name="16167"></a> * -1 and we set status to NS_ERROR and immediately break
<a name="16168"></a> * out of the loop. Whenever a status other than NS_OK
<a name="16169"></a> * is returned from a request function, the AOLserver does
<a name="16170"></a> * not run the registered trace functions (trace functions
<a name="16171"></a> * are normally used for access logging and you shouldn't
<a name="16172"></a> * normally log aborted connections).
<a name="16173"></a> *
<a name="16174"></a> * Actually, in this trivial case, you could simply use:
<a name="16175"></a> *
<a name="16176"></a> * status = Ns_ConnPuts(conn, ds.string)
<a name="16177"></a> *
<a name="16178"></a> * which pretty much implements the loop below.
<a name="16179"></a> */
<a name="16180"></a> p = ds.string;
<a name="16181"></a> i = ds.length;
<a name="16182"></a> while (i > 0) {
<a name="16183"></a> cnt = Ns_ConnWrite(conn, p, i);
<a name="16184"></a> if (cnt < 0) {
<a name="16185"></a> status = NS_ERROR;
<a name="16186"></a> break;
<a name="16187"></a> }
<a name="16188"></a> i -= cnt;
<a name="16189"></a> p += cnt;
<a name="16190"></a> }
<a name="16191"></a> }
<a name="16192"></a> Ns_DStringFree(&ds);
<a name="16193"></a> return status;
<a name="16194"></a>}
<a name="16195"></a>
</pre><p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="c-app2.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="c-app4.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|