source: rtems/cpukit/httpd/webs.c @ 029c374c

4.104.114.95
Last change on this file since 029c374c was 029c374c, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/01/08 at 07:02:00

Stop using old-style function definitions.

  • Property mode set to 100644
File size: 66.4 KB
Line 
1/*
2 * webs.c -- GoAhead Embedded HTTP webs server
3 *
4 * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved.
5 *
6 * See the file "license.txt" for usage and redistribution license requirements
7 *
8 * $Id$
9 */
10
11/******************************** Description *********************************/
12
13/*
14 *      This module implements an embedded HTTP/1.1 web server. It supports
15 *      loadable URL handlers that define the nature of URL processing performed.
16 */
17
18/********************************* Includes ***********************************/
19
20#include        "wsIntrn.h"
21#ifdef DIGEST_ACCESS_SUPPORT
22        #include        "websda.h"
23#endif
24
25/******************************** Global Data *********************************/
26
27websStatsType   websStats;                              /* Web access stats */
28webs_t                  *webs;                                  /* Open connection list head */
29sym_fd_t                websMime;                               /* Set of mime types */
30int                             websMax;                                /* List size */
31int                             websPort;                               /* Listen port for server */
32char_t                  websHost[64];                   /* Host name for the server */
33char_t                  websIpaddr[64];                 /* IP address for the server */
34char_t                  *websHostUrl = NULL;    /* URL to access server */
35char_t                  *websIpaddrUrl = NULL;  /* URL to access server */
36
37/*********************************** Locals ***********************************/
38/*
39 *      Standard HTTP error codes
40 */
41
42websErrorType websErrors[] = {
43        { 200, T("Data follows") },
44        { 204, T("No Content") },
45        { 301, T("Redirect") },
46        { 302, T("Redirect") },
47        { 304, T("Use local copy") },
48        { 400, T("Page not found") },
49        { 401, T("Unauthorized") },
50        { 403, T("Forbidden") },
51        { 404, T("Site or Page Not Found") },
52        { 405, T("Access Denied") },
53        { 500, T("Web Error") },
54        { 501, T("Not Implemented") },
55        { 503, T("Site Temporarily Unavailable. Try again.") },
56        { 0, NULL }
57};
58
59#ifdef WEBS_LOG_SUPPORT
60static char_t   websLogname[64] = T("log.txt"); /* Log filename */
61static int              websLogFd;                                              /* Log file handle */
62#endif
63
64static int              websListenSock;                                 /* Listen socket */
65static char_t   websRealm[64] = T("GoAhead");   /* Realm name */
66
67static int              websOpenCount = 0;              /* count of apps using this module */
68
69/**************************** Forward Declarations ****************************/
70
71
72/*static char_t         *websErrorMsg(int code);*/
73static int              websGetInput(webs_t wp, char_t **ptext, int *nbytes);
74static int              websParseFirst(webs_t wp, char_t *text);
75static void     websParseRequest(webs_t wp);
76static void             websSocketEvent(int sid, int mask, int data);
77static int              websGetTimeSinceMark(webs_t wp);
78
79#ifdef WEBS_LOG_SUPPORT
80static void     websLog(webs_t wp, int code);
81#endif
82#ifdef WEBS_IF_MODIFIED_SUPPORT
83static time_t   dateParse(time_t tip, char_t *cmd);
84#endif
85
86/*********************************** Code *************************************/
87/*
88 *      Open the GoAhead WebServer
89 */
90
91int websOpenServer(int port, int retries)
92{
93        websMimeType    *mt;
94
95        if (++websOpenCount != 1) {
96                return websPort;
97        }
98
99        a_assert(port > 0);
100        a_assert(retries >= 0);
101
102#ifdef WEBS_PAGE_ROM
103        websRomOpen();
104#endif
105
106        webs = NULL;
107        websMax = 0;
108/*
109 *      Create a mime type lookup table for quickly determining the content type
110 */
111        websMime = symOpen(WEBS_SYM_INIT * 4);
112        a_assert(websMime >= 0);
113        for (mt = websMimeList; mt->type; mt++) {
114                symEnter(websMime, mt->ext, valueString(mt->type, 0), 0);
115        }
116
117/*
118 *      Open the URL handler module. The caller should create the required
119 *      URL handlers after calling this function.
120 */
121        if (websUrlHandlerOpen() < 0) {
122                return -1;
123        }
124        websFormOpen();
125
126#ifdef WEBS_LOG_SUPPORT
127/*
128 *      Optional request log support
129 */
130        websLogFd = gopen(websLogname, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY,
131                0666);
132        a_assert(websLogFd >= 0);
133#endif
134       
135        return websOpenListen(port, retries);
136}
137
138/******************************************************************************/
139/*
140 *      Close the GoAhead WebServer
141 */
142
143void websCloseServer(void)
144{
145        webs_t  wp;
146        int             wid;
147
148        if (--websOpenCount > 0) {
149                return;
150        }
151
152/*
153 *      Close the listen handle first then all open connections.
154 */
155        websCloseListen();
156
157/*
158 *      Close each open browser connection and free all resources
159 */
160        for (wid = websMax; webs && wid >= 0; wid--) {
161                if ((wp = webs[wid]) == NULL) {
162                        continue;
163                }
164                socketCloseConnection(wp->sid);
165                websFree(wp);
166        }
167
168#ifdef WEBS_LOG_SUPPORT
169        if (websLogFd >= 0) {
170                close(websLogFd);
171                websLogFd = -1;
172        }
173#endif
174
175#ifdef WEBS_PAGE_ROM
176        websRomClose();
177#endif
178        symClose(websMime);
179        websFormClose();
180        websUrlHandlerClose();
181}
182
183/******************************************************************************/
184/*
185 *      Open the GoAhead WebServer listen port
186 */
187
188int websOpenListen(int port, int retries)
189{
190        int             i, orig;
191
192        a_assert(port > 0);
193        a_assert(retries >= 0);
194
195        orig = port;
196/*
197 *      Open the webs webs listen port. If we fail, try the next port.
198 */
199        for (i = 0; i <= retries; i++) {
200                websListenSock = socketOpenConnection(NULL, port, websAccept, 0);
201                if (websListenSock >= 0) {
202                        break;
203                }
204                port++;
205        }
206        if (i > retries) {
207                error(E_L, E_USER, T("Couldn't open a socket on ports %d - %d"),
208                        orig, port - 1);
209                return -1;
210        }
211
212/*
213 *      Determine the full URL address to access the home page for this web server
214 */
215        websPort = port;
216        bfreeSafe(B_L, websHostUrl);
217        bfreeSafe(B_L, websIpaddrUrl);
218        websIpaddrUrl = websHostUrl = NULL;
219
220        if (port == 80) {
221                websHostUrl = bstrdup(B_L, websHost);
222                websIpaddrUrl = bstrdup(B_L, websIpaddr);
223        } else {
224                fmtAlloc(&websHostUrl, WEBS_MAX_URL + 80, T("%s:%d"), websHost, port);
225                fmtAlloc(&websIpaddrUrl, WEBS_MAX_URL + 80, T("%s:%d"),
226                        websIpaddr, port);
227        }
228        trace(0, T("webs: Listening for HTTP requests at address %s\n"),
229                websIpaddrUrl);
230
231        return port;
232}
233
234/******************************************************************************/
235/*
236 *      Close webs listen port
237 */
238
239void websCloseListen(void)
240{
241        if (websListenSock >= 0) {
242                socketCloseConnection(websListenSock);
243                websListenSock = -1;
244        }
245        bfreeSafe(B_L, websHostUrl);
246        bfreeSafe(B_L, websIpaddrUrl);
247        websIpaddrUrl = websHostUrl = NULL;
248}
249
250/******************************************************************************/
251/*
252 *      Accept a connection
253 */
254
255int websAccept(int sid, char *ipaddr, int port, int listenSid)
256{
257        webs_t  wp;
258        int             wid;
259
260        a_assert(ipaddr && *ipaddr);
261        a_assert(sid >= 0);
262        a_assert(port >= 0);
263
264/*
265 *      Allocate a new handle for this accepted connection. This will allocate
266 *      a webs_t structure in the webs[] list
267 */
268        if ((wid = websAlloc(sid)) < 0) {
269                return -1;
270        }
271        wp = webs[wid];
272        a_assert(wp);
273        wp->listenSid = listenSid;
274
275        ascToUni(wp->ipaddr, ipaddr, min(sizeof(wp->ipaddr), strlen(ipaddr) + 1));
276
277/*
278 *      Check if this is a request from a browser on this system. This is useful
279 *      to know for permitting administrative operations only for local access
280 */
281        if (gstrcmp(wp->ipaddr, T("127.0.0.1")) == 0 ||
282                        gstrcmp(wp->ipaddr, websIpaddr) == 0 ||
283                        gstrcmp(wp->ipaddr, websHost) == 0) {
284                wp->flags |= WEBS_LOCAL_REQUEST;
285        }
286
287/*
288 *      Arrange for websSocketEvent to be called when read data is available
289 */
290        socketCreateHandler(sid, SOCKET_READABLE, websSocketEvent, (int) wp);
291
292/*
293 *      Arrange for a timeout to kill hung requests
294 */
295        wp->timeout = emfSchedCallback(WEBS_TIMEOUT, websTimeout, (void *) wp);
296        trace(8, T("webs: accept request\n"));
297        return 0;
298}
299
300/******************************************************************************/
301/*
302 *      The webs socket handler.  Called in response to I/O. We just pass control
303 *      to the relevant read or write handler. A pointer to the webs structure
304 *      is passed as an (int) in iwp.
305 */
306
307static void websSocketEvent(int sid, int mask, int iwp)
308{
309        webs_t  wp;
310
311        wp = (webs_t) iwp;
312        a_assert(wp);
313
314        if (! websValid(wp)) {
315                return;
316        }
317
318        if (mask & SOCKET_READABLE) {
319                websReadEvent(wp);
320        }
321        if (mask & SOCKET_WRITABLE) {
322                if (websValid(wp) && wp->writeSocket) {
323                        (*wp->writeSocket)(wp);
324                }
325        }
326}
327
328/******************************************************************************/
329/*
330 *      The webs read handler. This is the primary read event loop. It uses a
331 *      state machine to track progress while parsing the HTTP request.
332 *      Note: we never block as the socket is always in non-blocking mode.
333 */
334
335void websReadEvent(webs_t wp)
336{
337        char_t  *text;
338        int             rc, nbytes, len, done, fd;
339
340        a_assert(wp);
341        a_assert(websValid(wp));
342
343        websSetTimeMark(wp);
344
345/*
346 *      Read as many lines as possible. socketGets is called to read the header
347 *      and socketRead is called to read posted data.
348 */
349        text = NULL;
350        fd = -1;
351        for (done = 0; !done; ) {
352                if (text) {
353                        bfree(B_L, text);
354                        text = NULL;
355                }
356
357/*
358 *              Get more input into "text". Returns 0, if more data is needed
359 *              to continue, -1 if finished with the request, or 1 if all
360 *              required data is available for current state.
361 */
362                while ((rc = websGetInput(wp, &text, &nbytes)) == 0) {
363                        ;
364                }
365
366/*
367 *              websGetInput returns -1 if it finishes with the request
368 */
369                if (rc < 0) {
370                        break;
371                }
372
373/*
374 *              This is the state machine for the web server.
375 */
376                switch(wp->state) {
377                case WEBS_BEGIN:
378/*
379 *                      Parse the first line of the Http header
380 */
381                        if (websParseFirst(wp, text) < 0) {
382                                done++;
383                                break;
384                        }
385                        wp->state = WEBS_HEADER;
386                        break;
387               
388                case WEBS_HEADER:
389/*
390 *                      Store more of the HTTP header. As we are doing line reads, we
391 *                      need to separate the lines with '\n'
392 */
393                        if (ringqLen(&wp->header) > 0) {
394                                ringqPutStr(&wp->header, T("\n"));
395                        }
396                        ringqPutStr(&wp->header, text);
397                        break;
398
399                case WEBS_POST_CLEN:
400/*
401 *                      POST request with content specified by a content length.
402 *                      If this is a CGI request, write the data to the cgi stdin.
403 *                      socketGets was used to get the data and it strips \n's so
404 *                      add them back in here.
405 */
406#ifndef __NO_CGI_BIN
407                        if (wp->flags & WEBS_CGI_REQUEST) {
408                                if (fd == -1) {
409                                        fd = gopen(wp->cgiStdin, O_CREAT | O_WRONLY | O_BINARY,
410                                                0666);
411                                }
412                                gwrite(fd, text, gstrlen(text));
413            /*
414             * NOTE that the above comment is wrong -- if the content length
415             * is set, websGetInput() does NOT use socketGets(), it uses
416             * socketRead(), so the line below that adds an additional newline
417             * is destructive.
418             */
419                                /*gwrite(fd, T("\n"), sizeof(char_t));*/
420/*
421 *                              Line removed as per BUG02488
422 *
423                                nbytes += 1;
424 */
425                        } else
426#endif
427                        if (wp->query) {
428                                if (wp->query[0] && !(wp->flags & WEBS_POST_DATA)) {
429/*
430 *                                      Special case where the POST request also had query data
431 *                                      specified in the URL, ie. url?query_data. In this case
432 *                                      the URL query data is separated by a '&' from the posted
433 *                                      query data.
434 */
435                                        len = gstrlen(wp->query);
436                                        wp->query = brealloc(B_L, wp->query, (len + gstrlen(text) +
437                                                2) * sizeof(char_t));
438                                        wp->query[len++] = '&';
439                                        gstrcpy(&wp->query[len], text);
440
441                                } else {
442/*
443 *                                      The existing query data came from the POST request so just
444 *                                      append it.
445 */
446               if (text != NULL)
447               {
448                  len = gstrlen(wp->query);
449                  wp->query = brealloc(B_L, wp->query, (len +   gstrlen(text) +
450                     1) * sizeof(char_t));
451                  if (wp->query) {
452                     gstrcpy(&wp->query[len], text);
453                  }
454               }
455                                }
456
457                        } else {
458                                wp->query = bstrdup(B_L, text);
459                        }
460/*
461 *                      Calculate how much more post data is to be read.
462 */
463                        wp->flags |= WEBS_POST_DATA;
464                        wp->clen -= nbytes;
465                        if (wp->clen > 0) {
466                                if (nbytes > 0) {
467                                        break;
468                                }
469                                done++;
470                                break;
471                        }
472/*
473 *                      No more data so process the request, (but be sure to close
474 *                      the input file first!).
475 */
476                        if (fd != -1) {
477                                gclose (fd);
478                                fd = -1;
479                        }
480                        websUrlHandlerRequest(wp);
481                        done++;
482                        break;
483
484                case WEBS_POST:
485/*
486 *                      POST without content-length specification
487 *                      If this is a CGI request, write the data to the cgi stdin.
488 *                      socketGets was used to get the data and it strips \n's so
489 *                      add them back in here.
490 */
491
492#ifndef __NO_CGI_BIN
493                        if (wp->flags & WEBS_CGI_REQUEST) {
494                                if (fd == -1) {
495                                        fd = gopen(wp->cgiStdin, O_CREAT | O_WRONLY | O_BINARY,
496                                                0666);
497                                }
498                                gwrite(fd, text, gstrlen(text));
499                                gwrite(fd, T("\n"), sizeof(char_t));
500                        } else
501#endif
502                        if (wp->query && *wp->query && !(wp->flags & WEBS_POST_DATA)) {
503                                len = gstrlen(wp->query);
504                                wp->query = brealloc(B_L, wp->query, (len + gstrlen(text) +
505                                        2) * sizeof(char_t));
506                                if (wp->query) {
507                                        wp->query[len++] = '&';
508                                        gstrcpy(&wp->query[len], text);
509                                }
510
511                        } else {
512                                wp->query = bstrdup(B_L, text);
513                        }
514                        wp->flags |= WEBS_POST_DATA;
515                        done++;
516                        break;
517
518                default:
519                        websError(wp, 404, T("Bad state"));
520                        done++;
521                        break;
522                }
523        }
524
525        if (fd != -1) {
526                fd = gclose (fd);
527        }
528
529        if (text) {
530                bfree(B_L, text);
531        }
532}
533
534/******************************************************************************/
535/*
536 *      Get input from the browser. Return TRUE (!0) if the request has been
537 *      handled. Return -1 on errors or if the request has been processed,
538 *      1 if input read, and 0 to instruct the caller to call again for more input.
539 *
540 *      Note: socketRead will Return the number of bytes read if successful. This
541 *      may be less than the requested "bufsize" and may be zero. It returns -1 for
542 *      errors. It returns 0 for EOF. Otherwise it returns the number of bytes
543 *      read. Since this may be zero, callers should use socketEof() to
544 *      distinguish between this and EOF.
545 */
546
547static int websGetInput(webs_t wp, char_t **ptext, int *pnbytes)
548{
549        char_t  *text;
550        char    buf[WEBS_SOCKET_BUFSIZ+1];
551        int             nbytes, len, clen;
552
553        a_assert(websValid(wp));
554        a_assert(ptext);
555        a_assert(pnbytes);
556
557        *ptext = text = NULL;
558        *pnbytes = 0;
559
560/*
561 *      If this request is a POST with a content length, we know the number
562 *      of bytes to read so we use socketRead().
563 */
564        if (wp->state == WEBS_POST_CLEN) {
565                len = (wp->clen > WEBS_SOCKET_BUFSIZ) ? WEBS_SOCKET_BUFSIZ : wp->clen;
566        } else {
567                len = 0;
568        }
569
570        if (len > 0) {
571
572#ifdef WEBS_SSL_SUPPORT
573                if (wp->flags & WEBS_SECURE) {
574                        nbytes = websSSLRead(wp->wsp, buf, len);
575                } else {
576                        nbytes = socketRead(wp->sid, buf, len);
577                }
578#else
579                nbytes = socketRead(wp->sid, buf, len);
580#endif
581                if (nbytes < 0) {                                               /* Error */
582                        websDone(wp, 0);
583                        return -1;
584
585                }  else if (nbytes == 0) {                              /* EOF or No data available */
586                        /* Bugfix for POST DoS attack with invalid content length */
587                        if (socketEof(wp->sid)) {
588                                websDone(wp, 0);
589                        }
590                        /* End of bugfix */
591                        return -1;
592
593                } else {                                                                /* Valid data */
594/*
595 *                      Convert to UNICODE if necessary.  First be sure the string
596 *                      is NULL terminated.
597 */
598                        buf[nbytes] = '\0';
599                        if ((text = ballocAscToUni(buf, nbytes)) == NULL) {
600                                websError(wp, 503, T("Insufficient memory"));
601                                return -1;
602                        }
603                }
604
605        } else {
606#ifdef WEBS_SSL_SUPPORT
607                if (wp->flags & WEBS_SECURE) {
608                        nbytes = websSSLGets(wp->wsp, &text);
609                } else {
610                        nbytes = socketGets(wp->sid, &text);
611                }
612#else
613                nbytes = socketGets(wp->sid, &text);
614#endif
615
616                if (nbytes < 0) {
617                        int eof;
618/*
619 *                      Error, EOF or incomplete
620 */
621#ifdef WEBS_SSL_SUPPORT
622                        if (wp->flags & WEBS_SECURE) {
623/*
624 *                              If state is WEBS_BEGIN and the request is secure, a -1 will
625 *                              usually indicate SSL negotiation
626 */
627                                if (wp->state == WEBS_BEGIN) {
628                                        eof = 1;
629                                } else {
630                                        eof = websSSLEof(wp->wsp);
631                                }
632                        } else {
633                                eof = socketEof(wp->sid);
634                        }
635#else
636                        eof = socketEof(wp->sid);
637#endif
638
639                        if (eof) {
640/*
641 *                              If this is a post request without content length, process
642 *                              the request as we now have all the data. Otherwise just
643 *                              close the connection.
644 */
645                                if (wp->state == WEBS_POST) {
646                                        websUrlHandlerRequest(wp);
647                                } else {
648                                        websDone(wp, 0);
649                                }
650                        } else {
651/*
652 *                              If an error occurred and it wasn't an eof, close the connection
653 */
654#ifdef HP_FIX
655                                websDone(wp, 0);
656#endif /*HP_FIX*/
657
658                        }
659/*
660 *                      If state is WEBS_HEADER and the ringq is empty, then this is a
661 *                      simple request with no additional header fields to process and
662 *                      no empty line terminator.
663 */
664/*
665 *                      NOTE: this fix for earlier versions of browsers is troublesome
666 *                      because if we don't receive the entire header in the first pass
667 *                      this code assumes we were only expecting a one line header, which
668 *                      is not necessarily the case. So we weren't processing the whole
669 *                      header and weren't fufilling requests properly.
670 */
671#ifdef UNUSED
672                        if (wp->state == WEBS_HEADER && ringqLen(&wp->header) <= 0) {
673                                websParseRequest(wp);
674                                websUrlHandlerRequest(wp);
675                        }
676#endif
677                        return -1;
678
679                } else if (nbytes == 0) {
680                        if (wp->state == WEBS_HEADER) {
681/*
682 *                              Valid empty line, now finished with header
683 */
684                                websParseRequest(wp);
685                                if (wp->flags & WEBS_POST_REQUEST) {
686                                        if (wp->flags & WEBS_CLEN) {
687                                                wp->state = WEBS_POST_CLEN;
688                                                clen = wp->clen;
689                                        } else {
690                                                wp->state = WEBS_POST;
691                                                clen = 1;
692                                        }
693                                        if (clen > 0) {
694/*
695 *                                              Return 0 to get more data.
696 */
697                                                return 0;
698                                        }
699                                        return 1;
700                                }
701/*
702 *                              We've read the header so go and handle the request
703 */
704                                websUrlHandlerRequest(wp);
705                        }
706                        return -1;
707                }
708        }
709        a_assert(text);
710        a_assert(nbytes > 0);
711        *ptext = text;
712        *pnbytes = nbytes;
713        return 1;
714}
715
716/******************************************************************************/
717/*
718 *      Parse the first line of a HTTP request
719 */
720
721static int websParseFirst(webs_t wp, char_t *text)
722{
723        char_t  *op, *proto, *protoVer, *url, *host, *query, *path, *port, *ext;
724        char_t  *buf;
725        int             testPort;
726
727        a_assert(websValid(wp));
728        a_assert(text && *text);
729
730/*
731 *      Determine the request type: GET, HEAD or POST
732 */
733        op = gstrtok(text, T(" \t"));
734        if (op == NULL || *op == '\0') {
735                websError(wp, 400, T("Bad HTTP request"));
736                return -1;
737        }
738        if (gstrcmp(op, T("GET")) != 0) {
739                if (gstrcmp(op, T("POST")) == 0) {
740                        wp->flags |= WEBS_POST_REQUEST;
741                } else if (gstrcmp(op, T("HEAD")) == 0) {
742                        wp->flags |= WEBS_HEAD_REQUEST;
743                } else {
744                        websError(wp, 400, T("Bad request type"));
745                        return -1;
746                }
747        }
748
749/*
750 *      Store result in the form (CGI) variable store
751 */
752        websSetVar(wp, T("REQUEST_METHOD"), op);
753
754        url = gstrtok(NULL, T(" \t\n"));
755        if (url == NULL || *url == '\0') {
756                websError(wp, 400, T("Bad HTTP request"));
757                return -1;
758        }
759        protoVer = gstrtok(NULL, T(" \t\n"));
760
761/*
762 *      Parse the URL and store all the various URL components. websUrlParse
763 *      returns an allocated buffer in buf which we must free. We support both
764 *      proxied and non-proxied requests. Proxied requests will have http://host/
765 *      at the start of the URL. Non-proxied will just be local path names.
766 */
767        host = path = port = proto = query = ext = NULL;
768        if (websUrlParse(url, &buf, &host, &path, &port, &query, &proto,
769                        NULL, &ext) < 0) {
770                websError(wp, 400, T("Bad URL format"));
771                return -1;
772        }
773
774        wp->url = bstrdup(B_L, url);
775
776#ifndef __NO_CGI_BIN
777        if (gstrstr(url, CGI_BIN) != NULL) {
778                wp->flags |= WEBS_CGI_REQUEST;
779                if (wp->flags & WEBS_POST_REQUEST) {
780                        wp->cgiStdin = websGetCgiCommName();
781                }
782        }
783#endif
784
785        wp->query = bstrdup(B_L, query);
786        wp->host = bstrdup(B_L, host);
787        wp->path = bstrdup(B_L, path);
788        wp->protocol = bstrdup(B_L, proto);
789        wp->protoVersion = bstrdup(B_L, protoVer);
790       
791        if ((testPort = socketGetPort(wp->listenSid)) >= 0) {
792                wp->port = testPort;
793        } else {
794                wp->port = gatoi(port);
795        }
796
797        if (gstrcmp(ext, T(".asp")) == 0) {
798                wp->flags |= WEBS_ASP;
799        }
800        bfree(B_L, buf);
801
802        websUrlType(url, wp->type, TSZ(wp->type));
803
804#ifdef WEBS_PROXY_SUPPORT
805/*
806 *      Determine if this is a request for local webs data. If it is not a proxied
807 *      request from the browser, we won't see the "http://" or the system name, so
808 *      we assume it must be talking to us directly for local webs data.
809 *      Note: not fully implemented yet.
810 */
811        if (gstrstr(wp->url, T("http://")) == NULL ||
812                ((gstrcmp(wp->host, T("localhost")) == 0 ||
813                        gstrcmp(wp->host, websHost) == 0) && (wp->port == websPort))) {
814                wp->flags |= WEBS_LOCAL_PAGE;
815                if (gstrcmp(wp->path, T("/")) == 0) {
816                        wp->flags |= WEBS_HOME_PAGE;
817                }
818        }
819#endif
820
821        ringqFlush(&wp->header);
822        return 0;
823}
824
825/******************************************************************************/
826/*
827 *      Parse a full request
828 */
829
830#define isgoodchar(s) (gisalnum((s)) || ((s) == '/') || ((s) == '_') || \
831                                                ((s) == '.')  || ((s) == '-') )
832
833static void websParseRequest(webs_t wp)
834{
835        char_t  *authType, *upperKey, *cp, *browser, *lp, *key, *value;
836
837        a_assert(websValid(wp));
838
839/*
840 *      Define default CGI values
841 */
842        websSetVar(wp, T("HTTP_AUTHORIZATION"), T(""));
843
844/*
845 *      Parse the header and create the Http header keyword variables
846 *      We rewrite the header as we go for non-local requests.  NOTE: this
847 *      modifies the header string directly and tokenizes each line with '\0'.
848 */
849        browser = NULL;
850        for (lp = (char_t*) wp->header.servp; lp && *lp; ) {
851                cp = lp;
852                if ((lp = gstrchr(lp, '\n')) != NULL) {
853                        lp++;
854                }
855
856                if ((key = gstrtok(cp, T(": \t\n"))) == NULL) {
857                        continue;
858                }
859
860                if ((value = gstrtok(NULL, T("\n"))) == NULL) {
861                        value = T("");
862                }
863
864                while (gisspace(*value)) {
865                        value++;
866                }
867                strlower(key);
868
869/*
870 *              Create a variable (CGI) for each line in the header
871 */
872                fmtAlloc(&upperKey, (gstrlen(key) + 6), T("HTTP_%s"), key);
873                for (cp = upperKey; *cp; cp++) {
874                        if (*cp == '-')
875                                *cp = '_';
876                }
877                strupper(upperKey);
878                websSetVar(wp, upperKey, value);
879                bfree(B_L, upperKey);
880
881/*
882 *              Track the requesting agent (browser) type
883 */
884                if (gstrcmp(key, T("user-agent")) == 0) {
885                        wp->userAgent = bstrdup(B_L, value);
886
887/*
888 *              Parse the user authorization. ie. password
889 */
890                } else if (gstricmp(key, T("authorization")) == 0) {
891/*
892 *                      Determine the type of Authorization Request
893 */
894                        authType = bstrdup (B_L, value);
895                        a_assert (authType);
896/*                     
897 *                      Truncate authType at the next non-alpha character
898 */
899                        cp = authType;
900                        while (gisalpha(*cp)) {
901                                cp++;
902                        }
903                        *cp = '\0';
904
905                        wp->authType = bstrdup(B_L, authType);
906                        bfree(B_L, authType);
907
908                        if (gstricmp(wp->authType, T("basic")) == 0) {
909                                char_t  userAuth[FNAMESIZE];
910/*
911 *                              The incoming value is username:password (Basic authentication)
912 */
913                                if ((cp = gstrchr(value, ' ')) != NULL) {
914                                        *cp = '\0';
915               /*
916                * bugfix 5/24/02 -- we were leaking the memory pointed to by
917                * wp->authType that was allocated just before the if()
918                * statement that we are currently in. Thanks to Simon Byholm.
919                */
920               bfree(B_L, wp->authType);
921                                        wp->authType = bstrdup(B_L, value);
922                                        websDecode64(userAuth, ++cp, sizeof(userAuth));
923                                } else {
924                                        websDecode64(userAuth, value, sizeof(userAuth));
925                                }
926/*
927 *                              Split userAuth into userid and password
928 */
929                                if ((cp = gstrchr(userAuth, ':')) != NULL) {
930                                        *cp++ = '\0';
931                                }
932                                if (cp) {
933                                        wp->userName = bstrdup(B_L, userAuth);
934                                        wp->password = bstrdup(B_L, cp);
935                                } else {
936                                        wp->userName = bstrdup(B_L, T(""));
937                                        wp->password = bstrdup(B_L, T(""));
938                                }
939/*
940 *                              Set the flags to indicate digest authentication
941 */
942                                wp->flags |= WEBS_AUTH_BASIC;
943                        } else {
944#ifdef DIGEST_ACCESS_SUPPORT
945/*
946 *                              The incoming value is slightly more complicated (Digest)
947 */
948                                char_t *np;             /* pointer to end of tag name */
949                                char_t tp;              /* temporary character holding space */
950                                char_t *vp;             /* pointer to value */
951                                char_t *npv;    /* pointer to end of value, "next" pointer */
952                                char_t tpv;             /* temporary character holding space */
953/*
954 *                              Set the flags to indicate digest authentication
955 */
956                                wp->flags |= WEBS_AUTH_DIGEST;
957/*
958 *                              Move cp to Next word beyond "Digest",
959 *                              vp to first char after '='.
960 */
961                                cp = value;
962                                while (isgoodchar(*cp)) {
963                                        cp++;
964                                }
965                                while (!isgoodchar(*cp)) {
966                                        cp++;
967                                }
968
969/*
970 *                              Find beginning of value
971 */
972                                vp = gstrchr(cp, '=');
973                                while (vp) {
974/*
975 *                                      Zero-terminate tag name
976 */
977                                        np = cp;
978                                        while (isgoodchar(*np)) {
979                                                np++;
980                                        }
981                                        tp = *np;
982                                        *np = 0;
983/*
984 *                                      Advance value pointer to first legit character
985 */
986                                        vp++;
987                                        while (!isgoodchar(*vp)) {
988                                                vp++;
989                                        }
990/*
991 *                                      Zero-terminate value
992 */
993                                        npv = vp;
994                                        while (isgoodchar(*npv)) {
995                                                npv++;
996                                        }
997                                        tpv = *npv;
998                                        *npv = 0;
999/*
1000 *                                      Extract the fields
1001 */
1002                                        if (gstricmp(cp, T("username")) == 0) {
1003                                                wp->userName = bstrdup(B_L, vp);
1004                                        } else if (gstricmp(cp, T("response")) == 0) {
1005                                                wp->digest = bstrdup(B_L, vp);
1006                                        } else if (gstricmp(cp, T("opaque")) == 0) {
1007                                                wp->opaque = bstrdup(B_L, vp);
1008                                        } else if (gstricmp(cp, T("uri")) == 0) {
1009                                                wp->uri = bstrdup(B_L, vp);
1010                                        } else if (gstricmp(cp, T("realm")) == 0) {
1011                                                wp->realm = bstrdup(B_L, vp);
1012                                        } else if (gstricmp(cp, T("nonce")) == 0) {
1013                                                wp->nonce = bstrdup(B_L, vp);
1014                                        } else if (gstricmp(cp, T("nc")) == 0) {
1015                                                wp->nc = bstrdup(B_L, vp);
1016                                        } else if (gstricmp(cp, T("cnonce")) == 0) {
1017                                                wp->cnonce = bstrdup(B_L, vp);
1018                                        } else if (gstricmp(cp, T("qop")) == 0) {
1019                                                wp->qop = bstrdup(B_L, vp);
1020                                        }
1021/*
1022 *                                      Restore tag name and value zero-terminations
1023 */
1024                                        *np = tp;
1025                                        *npv = tpv;
1026/*
1027 *                                      Advance tag name and value pointers
1028 */
1029                                        cp = npv;
1030                                        while (*cp && isgoodchar(*cp)) {
1031                                                cp++;
1032                                        }
1033                                        while (*cp && !isgoodchar(*cp)) {
1034                                                cp++;
1035                                        }
1036
1037                                        if (*cp) {
1038                                                vp = gstrchr(cp, '=');
1039                                        } else {
1040                                                vp = NULL;
1041                                        }
1042                                }
1043#endif /* DIGEST_ACCESS_SUPPORT */
1044                        } /* if (gstrcmp(wp->authType)) */
1045/*
1046 *              Parse the content length
1047 */
1048                } else if (gstrcmp(key, T("content-length")) == 0) {
1049         /*
1050          * 11 Oct 02 BgP -- The server would crash if an attacker sent a POST
1051          * message with a content-length value <= 0. We assume that anyone
1052          * sending this is malicious, and the POST is read from the socket,
1053          * but it is ignored, and the socket is closed.
1054          */
1055         wp->clen = gatoi(value);
1056         if (wp->clen > 0)
1057         {
1058                           wp->flags |= WEBS_CLEN;                     
1059                           websSetVar(wp, T("CONTENT_LENGTH"), value);
1060         }
1061         else
1062         {
1063            wp->clen = 0;
1064         }
1065
1066/*
1067 *              Parse the content type
1068 */
1069                } else if (gstrcmp(key, T("content-type")) == 0) {
1070                        websSetVar(wp, T("CONTENT_TYPE"), value);
1071
1072#ifdef WEBS_KEEP_ALIVE_SUPPORT
1073                } else if (gstrcmp(key, T("connection")) == 0) {
1074                        strlower(value);
1075                        if (gstrcmp(value, T("keep-alive")) == 0) {
1076                                wp->flags |= WEBS_KEEP_ALIVE;
1077                        }
1078#endif
1079
1080#ifdef WEBS_PROXY_SUPPORT
1081/*
1082 *              This may be useful if you wish to keep a local cache of web pages
1083 *              for proxied requests.
1084 */
1085                } else if (gstrcmp(key, T("pragma")) == 0) {
1086                        char_t  tmp[256];
1087                        gstrncpy(tmp, value, TSZ(tmp));
1088                        strlower(tmp);
1089                        if (gstrstr(tmp, T("no-cache"))) {
1090                                wp->flags |= WEBS_DONT_USE_CACHE;
1091                        }
1092#endif /* WEBS_PROXY_SUPPORT */
1093
1094/*
1095 *              Store the cookie
1096 */
1097                } else if (gstrcmp(key, T("cookie")) == 0) {
1098                        wp->flags |= WEBS_COOKIE;
1099                        wp->cookie = bstrdup(B_L, value);
1100
1101#ifdef WEBS_IF_MODIFIED_SUPPORT
1102/*
1103 *              See if the local page has been modified since the browser last
1104 *              requested this document. If not, just return a 302
1105 */
1106                } else if (gstrcmp(key, T("if-modified-since")) == 0) {
1107                        char_t *cmd;
1108                        time_t tip = 0;
1109
1110                        if ((cp = gstrchr(value, ';')) != NULL) {
1111                                *cp = '\0';
1112                        }
1113
1114                        fmtAlloc(&cmd, 64, T("%s"), value);
1115
1116                        if ((wp->since = dateParse(tip, cmd)) != 0) {
1117                                wp->flags |= WEBS_IF_MODIFIED;
1118                        }
1119
1120                        bfreeSafe(B_L, cmd);
1121#endif /* WEBS_IF_MODIFIED_SUPPORT */
1122                }
1123        }
1124}
1125
1126/******************************************************************************/
1127/*
1128 *      Set the variable (CGI) environment for this request. Create variables
1129 *      for all standard CGI variables. Also decode the query string and create
1130 *      a variable for each name=value pair.
1131 */
1132
1133void websSetEnv(webs_t wp)
1134{
1135        char_t  portBuf[8];
1136        char_t  *keyword, *value, *valCheck, *valNew;
1137
1138        a_assert(websValid(wp));
1139
1140        websSetVar(wp, T("QUERY_STRING"), wp->query);
1141        websSetVar(wp, T("GATEWAY_INTERFACE"), T("CGI/1.1"));
1142        websSetVar(wp, T("SERVER_HOST"), websHost);
1143        websSetVar(wp, T("SERVER_NAME"), websHost);
1144        websSetVar(wp, T("SERVER_URL"), websHostUrl);
1145        websSetVar(wp, T("REMOTE_HOST"), wp->ipaddr);
1146        websSetVar(wp, T("REMOTE_ADDR"), wp->ipaddr);
1147        websSetVar(wp, T("PATH_INFO"), wp->path);
1148        stritoa(websPort, portBuf, sizeof(portBuf));
1149        websSetVar(wp, T("SERVER_PORT"), portBuf);
1150        websSetVar(wp, T("SERVER_ADDR"), websIpaddr);
1151        fmtAlloc(&value, FNAMESIZE, T("%s/%s"), WEBS_NAME, WEBS_VERSION);
1152        websSetVar(wp, T("SERVER_SOFTWARE"), value);
1153        bfreeSafe(B_L, value);
1154        websSetVar(wp, T("SERVER_PROTOCOL"), wp->protoVersion);
1155
1156/*
1157 *      Decode and create an environment query variable for each query keyword.
1158 *      We split into pairs at each '&', then split pairs at the '='.
1159 *      Note: we rely on wp->decodedQuery preserving the decoded values in the
1160 *      symbol table.
1161 */
1162        wp->decodedQuery = bstrdup(B_L, wp->query);
1163        keyword = gstrtok(wp->decodedQuery, T("&"));
1164        while (keyword != NULL) {
1165                if ((value = gstrchr(keyword, '=')) != NULL) {
1166                        *value++ = '\0';
1167                        websDecodeUrl(keyword, keyword, gstrlen(keyword));
1168                        websDecodeUrl(value, value, gstrlen(value));
1169                } else {
1170                        value = T("");
1171                }
1172
1173                if (*keyword) {
1174/*
1175 *                      If keyword has already been set, append the new value to what has
1176 *                      been stored.
1177 */
1178                        if ((valCheck = websGetVar(wp, keyword, NULL)) != 0) {
1179                                fmtAlloc(&valNew, 256, T("%s %s"), valCheck, value);
1180                                websSetVar(wp, keyword, valNew);
1181                                bfreeSafe(B_L, valNew);
1182                        } else {
1183                                websSetVar(wp, keyword, value);
1184                        }
1185                }
1186                keyword = gstrtok(NULL, T("&"));
1187        }
1188
1189#ifdef EMF
1190/*
1191 *      Add GoAhead Embedded Management Framework defines
1192 */
1193        websSetEmfEnvironment(wp);
1194#endif
1195}
1196
1197/******************************************************************************/
1198/*
1199 *      Define a webs (CGI) variable for this connection. Also create in relevant
1200 *      scripting engines. Note: the incoming value may be volatile.
1201 */
1202
1203void websSetVar(webs_t wp, char_t *var, char_t *value)
1204{
1205        value_t          v;
1206
1207        a_assert(websValid(wp));
1208
1209/*
1210 *      value_instring will allocate the string if required.
1211 */
1212        if (value) {
1213                v = valueString(value, VALUE_ALLOCATE);
1214        } else {
1215                v = valueString(T(""), VALUE_ALLOCATE);
1216        }
1217        symEnter(wp->cgiVars, var, v, 0);
1218}
1219
1220/******************************************************************************/
1221/*
1222 *      Return TRUE if a webs variable exists for this connection.
1223 */
1224
1225int websTestVar(webs_t wp, char_t *var)
1226{
1227        sym_t           *sp;
1228
1229        a_assert(websValid(wp));
1230
1231        if (var == NULL || *var == '\0') {
1232                return 0;
1233        }
1234
1235        if ((sp = symLookup(wp->cgiVars, var)) == NULL) {
1236                return 0;
1237        }
1238        return 1;
1239}
1240
1241/******************************************************************************/
1242/*
1243 *      Get a webs variable but return a default value if string not found.
1244 *      Note, defaultGetValue can be NULL to permit testing existence.
1245 */
1246
1247char_t *websGetVar(webs_t wp, char_t *var, char_t *defaultGetValue)
1248{
1249        sym_t   *sp;
1250
1251        a_assert(websValid(wp));
1252        a_assert(var && *var);
1253 
1254        if ((sp = symLookup(wp->cgiVars, var)) != NULL) {
1255                a_assert(sp->content.type == string);
1256                if (sp->content.value.string) {
1257                        return sp->content.value.string;
1258                } else {
1259                        return T("");
1260                }
1261        }
1262        return defaultGetValue;
1263}
1264
1265/******************************************************************************/
1266/*
1267 *      Return TRUE if a webs variable is set to a given value
1268 */
1269
1270int websCompareVar(webs_t wp, char_t *var, char_t *value)
1271{
1272        a_assert(websValid(wp));
1273        a_assert(var && *var);
1274 
1275        if (gstrcmp(value, websGetVar(wp, var, T(" __UNDEF__ "))) == 0) {
1276                return 1;
1277        }
1278        return 0;
1279}
1280
1281/******************************************************************************/
1282/*
1283 *      Cancel the request timeout. Note may be called multiple times.
1284 */
1285
1286void websTimeoutCancel(webs_t wp)
1287{
1288        a_assert(websValid(wp));
1289
1290        if (wp->timeout >= 0) {
1291                emfUnschedCallback(wp->timeout);
1292                wp->timeout = -1;
1293        }
1294}
1295
1296/******************************************************************************/
1297/*
1298 *      Output a HTTP response back to the browser. If redirect is set to a
1299 *      URL, the browser will be sent to this location.
1300 */
1301
1302void websResponse(webs_t wp, int code, char_t *message, char_t *redirect)
1303{
1304        char_t          *date;
1305
1306        a_assert(websValid(wp));
1307
1308/*
1309 *      IE3.0 needs no Keep Alive for some return codes.
1310 */
1311        wp->flags &= ~WEBS_KEEP_ALIVE;
1312
1313/*
1314 *      Only output the header if a header has not already been output.
1315 */
1316        if ( !(wp->flags & WEBS_HEADER_DONE)) {
1317                wp->flags |= WEBS_HEADER_DONE;
1318/*
1319 *              Redirect behaves much better when sent with HTTP/1.0
1320 */
1321                if (redirect != NULL) {
1322                        websWrite(wp, T("HTTP/1.0 %d %s\r\n"), code, websErrorMsg(code));
1323                } else {
1324                        websWrite(wp, T("HTTP/1.1 %d %s\r\n"), code, websErrorMsg(code));
1325                }
1326
1327/*             
1328 *              By license terms the following line of code must not be modified.
1329 */
1330                websWrite(wp, T("Server: %s\r\n"), WEBS_NAME);
1331
1332/*             
1333 *              Timestamp/Date is usually the next to go
1334 */
1335                if ((date = websGetDateString(NULL)) != NULL) {
1336                        websWrite(wp, T("Date: %s\r\n"), date);
1337                        bfree(B_L, date);
1338                }
1339/*
1340 *              If authentication is required, send the auth header info
1341 */
1342                if (code == 401) {
1343                        if (!(wp->flags & WEBS_AUTH_DIGEST)) {
1344                                websWrite(wp, T("WWW-Authenticate: Basic realm=\"%s\"\r\n"),
1345                                        websGetRealm());
1346#ifdef DIGEST_ACCESS_SUPPORT
1347                        } else {
1348                                char_t *nonce, *opaque;
1349
1350            /* $$$ before... (note commas instead of semicolons...)
1351                                nonce = websCalcNonce(wp),
1352                                opaque = websCalcOpaque(wp),
1353            $$$ after */
1354                                nonce = websCalcNonce(wp);
1355                                opaque = websCalcOpaque(wp);
1356            /* ...$$$ end */
1357                                websWrite(wp,
1358                                        T("WWW-Authenticate: Digest realm=\"%s\", domain=\"%s\",")
1359                                        T("qop=\"%s\", nonce=\"%s\", opaque=\"%s\",")
1360                                        T("algorithm=\"%s\", stale=\"%s\"\r\n"),
1361                                        websGetRealm(),
1362                                        websGetHostUrl(),
1363                                        T("auth"),
1364                                        nonce,
1365                                        opaque, T("MD5"), T("FALSE"));
1366                                bfree(B_L, nonce);
1367                                bfree(B_L, opaque);
1368#endif
1369                        }
1370                }
1371
1372                if (wp->flags & WEBS_KEEP_ALIVE) {
1373                        websWrite(wp, T("Connection: keep-alive\r\n"));
1374                }
1375
1376                websWrite(wp, T("Pragma: no-cache\r\nCache-Control: no-cache\r\n"));
1377                websWrite(wp, T("Content-Type: text/html\r\n"));
1378/*
1379 *              We don't do a string length here as the message may be multi-line.
1380 *              Ie. <CR><LF> will count as only one and we will have a content-length
1381 *              that is too short.
1382 *
1383 *              websWrite(wp, T("Content-Length: %s\r\n"), message);
1384 */
1385                if (redirect) {
1386                        websWrite(wp, T("Location: %s\r\n"), redirect);
1387                }
1388                websWrite(wp, T("\r\n"));
1389        }
1390
1391/*
1392 *      If the browser didn't do a HEAD only request, send the message as well.
1393 */
1394        if ((wp->flags & WEBS_HEAD_REQUEST) == 0 && message && *message) {
1395                websWrite(wp, T("%s\r\n"), message);
1396        }
1397        websDone(wp, code);
1398}
1399
1400/******************************************************************************/
1401/*
1402 *      Redirect the user to another webs page
1403 */
1404
1405void websRedirect(webs_t wp, char_t *url)
1406{
1407        char_t  *msgbuf, *urlbuf, *redirectFmt;
1408
1409        a_assert(websValid(wp));
1410        a_assert(url);
1411
1412        websStats.redirects++;
1413        msgbuf = urlbuf = NULL;
1414
1415/*
1416 *      Some browsers require a http://host qualified URL for redirection
1417 */
1418        if (gstrstr(url, T("http://")) == NULL) {
1419                if (*url == '/') {
1420                        url++;
1421                }
1422
1423                redirectFmt = T("http://%s/%s");
1424
1425#ifdef WEBS_SSL_SUPPORT
1426                if (wp->flags & WEBS_SECURE) {
1427                        redirectFmt = T("https://%s/%s");
1428                }
1429#endif
1430
1431                fmtAlloc(&urlbuf, WEBS_MAX_URL + 80, redirectFmt,
1432                        websGetVar(wp, T("HTTP_HOST"),  websHostUrl), url);
1433                url = urlbuf;
1434        }
1435
1436/*
1437 *      Add human readable message for completeness. Should not be required.
1438 */
1439        fmtAlloc(&msgbuf, WEBS_MAX_URL + 80,
1440                T("<html><head></head><body>\r\n\
1441                This document has moved to a new <a href=\"%s\">location</a>.\r\n\
1442                Please update your documents to reflect the new location.\r\n\
1443                </body></html>\r\n"), url);
1444
1445        websResponse(wp, 302, msgbuf, url);
1446
1447        bfreeSafe(B_L, msgbuf);
1448        bfreeSafe(B_L, urlbuf);
1449}
1450
1451/******************************************************************************/
1452/*     
1453 *      Output an error message and cleanup
1454 */
1455
1456#ifdef qRichErrorPage
1457extern int dmfRichError(webs_t wp, int code, char_t* userMsg);
1458#endif
1459void websError(webs_t wp, int code, char_t *fmt, ...)
1460{
1461        va_list         args;
1462        char_t          *msg, *userMsg, *buf;
1463#ifdef qRichErrorPage
1464   static int reEntry = 0;
1465   int errorOk;
1466#endif
1467
1468        a_assert(websValid(wp));
1469        a_assert(fmt);
1470
1471        websStats.errors++;
1472
1473        va_start(args, fmt);
1474        userMsg = NULL;
1475        fmtValloc(&userMsg, WEBS_BUFSIZE, fmt, args);
1476        va_end(args);
1477
1478#ifdef qRichErrorPage
1479   if (!reEntry)
1480   {
1481      /*
1482       * The dmfRichError function that we're about to call may very well call
1483       * websError() as part of its work. If that happens, we do NOT want to
1484       * get into a never-ending recursive call chain. When we get back here
1485       * in a call from inside dmfRichError(), we check to see if we're
1486       * already trying to call dmfRichError. If we are, we just revert to the
1487       * old non-rich behavior and display a black on white error page.
1488       */
1489
1490      reEntry = 1;
1491      errorOk = dmfRichError(wp, code, userMsg);
1492      reEntry = 0;
1493      if (errorOk)
1494      {
1495         return;
1496      }
1497      /* ...else we need to fall through and execute the simple error page. */
1498   }
1499   /* implicit else... */
1500#endif
1501
1502        msg = T("<html><head><title>Document Error: %s</title></head>\r\n\
1503                <body><h2>Access Error: %s</h2>\r\n\
1504                when trying to obtain <b>%s</b><br><p>%s</p></body></html>\r\n");
1505/*
1506 *      Ensure we have plenty of room
1507 */
1508        buf = NULL;
1509        fmtAlloc(&buf, WEBS_BUFSIZE, msg, websErrorMsg(code),
1510                websErrorMsg(code), wp->url, userMsg);
1511
1512        websResponse(wp, code, buf, NULL);
1513        bfreeSafe(B_L, buf);
1514        bfreeSafe(B_L, userMsg);
1515}
1516
1517/******************************************************************************/
1518/*
1519 *      Return the error message for a given code
1520 */
1521
1522/*static char_t *websErrorMsg(int code)*/
1523char_t *websErrorMsg(int code)
1524{
1525        websErrorType   *ep;
1526
1527        for (ep = websErrors; ep->code; ep++) {
1528                if (code == ep->code) {
1529                        return ep->msg;
1530                }
1531        }
1532        a_assert(0);
1533        return T("");
1534}
1535
1536/******************************************************************************/
1537/*
1538 *      Do formatted output to the browser. This is the public ASP and form
1539 *      write procedure.
1540 */
1541
1542int websWrite(webs_t wp, char_t *fmt, ...)
1543{
1544        va_list          vargs;
1545        char_t          *buf;
1546        int                      rc;
1547       
1548        a_assert(websValid(wp));
1549
1550        va_start(vargs, fmt);
1551
1552        buf = NULL;
1553        rc = 0;
1554
1555        if (fmtValloc(&buf, WEBS_BUFSIZE, fmt, vargs) >= WEBS_BUFSIZE) {
1556                trace(0, T("webs: websWrite lost data, buffer overflow\n"));
1557        }
1558   
1559        va_end(vargs);
1560        a_assert(buf);
1561        if (buf) {
1562                rc = websWriteBlock(wp, buf, gstrlen(buf));
1563                bfree(B_L, buf);
1564        }
1565        return rc;
1566}
1567
1568/******************************************************************************/
1569/*
1570 *      Write a block of data of length "nChars" to the user's browser. Public
1571 *      write block procedure.  If unicode is turned on this function expects
1572 *      buf to be a unicode string and it converts it to ASCII before writing.
1573 *      See websWriteDataNonBlock to always write binary or ASCII data with no
1574 *      unicode conversion.  This returns the number of char_t's processed.
1575 *      It spins until nChars are flushed to the socket.  For non-blocking
1576 *      behavior, use websWriteDataNonBlock.
1577 */
1578
1579int websWriteBlock(webs_t wp, char_t *buf, int nChars)
1580{
1581        int             len, done;
1582        char    *asciiBuf, *pBuf;
1583
1584        a_assert(wp);
1585        a_assert(websValid(wp));
1586        a_assert(buf);
1587        a_assert(nChars >= 0);
1588
1589        done = len = 0;
1590
1591/*
1592 *      ballocUniToAsc will convert Unicode to strings to Ascii.  If Unicode is
1593 *      not turned on then ballocUniToAsc will not do the conversion.
1594 */
1595        pBuf = asciiBuf = ballocUniToAsc(buf, nChars);
1596
1597        while (nChars > 0) { 
1598#ifdef WEBS_SSL_SUPPORT
1599                if (wp->flags & WEBS_SECURE) {
1600                        if ((len = websSSLWrite(wp->wsp, pBuf, nChars)) < 0) {
1601                                bfree(B_L, asciiBuf);
1602                                return -1;
1603                        }
1604                        websSSLFlush(wp->wsp);
1605                } else {
1606                        if ((len = socketWrite(wp->sid, pBuf, nChars)) < 0) {
1607                                bfree(B_L, asciiBuf);
1608                                return -1;
1609                        }
1610                        socketFlush(wp->sid);
1611                }
1612#else /* ! WEBS_SSL_SUPPORT */
1613                if ((len = socketWrite(wp->sid, pBuf, nChars)) < 0) {
1614                        bfree(B_L, asciiBuf);
1615                        return -1;
1616                }
1617                socketFlush(wp->sid);
1618#endif /* WEBS_SSL_SUPPORT */
1619                nChars -= len;
1620                pBuf += len;
1621                done += len;
1622        }
1623
1624        bfree(B_L, asciiBuf);
1625        return done;
1626}
1627
1628/******************************************************************************/
1629/*
1630 *      Write a block of data of length "nChars" to the user's browser. Same as
1631 *      websWriteBlock except that it expects straight ASCII or binary and does no
1632 *      unicode conversion before writing the data.  If the socket cannot hold all
1633 *      the data, it will return the number of bytes flushed to the socket before
1634 *      it would have blocked.  This returns the number of chars processed or -1
1635 *      if socketWrite fails.
1636 */
1637
1638int websWriteDataNonBlock(webs_t wp, char *buf, int nChars)
1639{
1640        int r;
1641
1642        a_assert(wp);
1643        a_assert(websValid(wp));
1644        a_assert(buf);
1645        a_assert(nChars >= 0);
1646
1647#ifdef WEBS_SSL_SUPPORT
1648        if (wp->flags & WEBS_SECURE) {
1649                r = websSSLWrite(wp->wsp, buf, nChars);
1650                websSSLFlush(wp->wsp);
1651        } else {
1652                r = socketWrite(wp->sid, buf, nChars);
1653                socketFlush(wp->sid);
1654        }
1655#else
1656        r = socketWrite(wp->sid, buf, nChars);
1657        socketFlush(wp->sid);
1658#endif
1659
1660        return r;
1661}
1662
1663/******************************************************************************/
1664/*
1665 *      Decode a URL (or part thereof). Allows insitu decoding.
1666 */
1667
1668void websDecodeUrl(char_t *decoded, char_t *token, int len)
1669{
1670        char_t  *ip,  *op;
1671        int             num, i, c;
1672       
1673        a_assert(decoded);
1674        a_assert(token);
1675
1676        op = decoded;
1677        for (ip = token; *ip && len > 0; ip++, op++) {
1678                if (*ip == '+') {
1679                        *op = ' ';
1680                } else if (*ip == '%' && gisxdigit(ip[1]) && gisxdigit(ip[2])) {
1681
1682/*
1683 *                      Convert %nn to a single character
1684 */
1685                        ip++;
1686                        for (i = 0, num = 0; i < 2; i++, ip++) {
1687                                c = tolower(*ip);
1688                                if (c >= 'a' && c <= 'f') {
1689                                        num = (num * 16) + 10 + c - 'a';
1690                                } else {
1691                                        num = (num * 16) + c - '0';
1692                                }
1693                        }
1694                        *op = (char_t) num;
1695                        ip--;
1696
1697                } else {
1698                        *op = *ip;
1699                }
1700                len--;
1701        }
1702        *op = '\0';
1703}
1704
1705/******************************************************************************/
1706#ifdef WEBS_LOG_SUPPORT
1707/*
1708 *      Output a log message
1709 */
1710
1711static void websLog(webs_t wp, int code)
1712{
1713        char_t  *buf;
1714        char    *abuf;
1715        int             len;
1716#define qAnlLog 1
1717#ifdef qAnlLog
1718   time_t timer;
1719   char_t* newLine = NULL;
1720   char_t* timeStr = NULL;
1721#endif
1722        a_assert(websValid(wp));
1723
1724        buf = NULL;
1725
1726#ifdef qAnlLog
1727   time(&timer);
1728   timeStr = ctime(&timer);
1729   newLine = gstrchr(timeStr, '\n');
1730   if (newLine)
1731   {
1732      *newLine = '\0';
1733   }
1734   fmtAlloc(&buf, WEBS_MAX_URL + 80, T("%s\t%s\t%s\tcode = %d\n"),
1735      timeStr, wp->ipaddr, wp->url, code);
1736#else
1737        fmtAlloc(&buf, WEBS_MAX_URL + 80, T("%d %s %d %d\n"), time(0),
1738                wp->url, code, wp->written);
1739#endif
1740        len = gstrlen(buf);
1741        abuf = ballocUniToAsc(buf, len+1);
1742        write(websLogFd, abuf, len);
1743        bfreeSafe(B_L, buf);
1744        bfreeSafe(B_L, abuf);
1745}
1746
1747#endif /* WEBS_LOG_SUPPORT */
1748
1749/******************************************************************************/
1750/*
1751 *      Request timeout. The timeout triggers if we have not read any data from
1752 *      the users browser in the last WEBS_TIMEOUT period. If we have heard from
1753 *      the browser, simply re-issue the timeout.
1754 */
1755
1756void websTimeout(void *arg, int id)
1757{
1758        webs_t          wp;
1759        int                     delay, tm;
1760
1761        wp = (webs_t) arg;
1762        a_assert(websValid(wp));
1763
1764        tm = websGetTimeSinceMark(wp) * 1000;
1765        if (tm >= WEBS_TIMEOUT) {
1766                websStats.timeouts++;
1767                emfUnschedCallback(id);
1768
1769/*
1770 *              Clear the timeout id
1771 */
1772                wp->timeout = -1;
1773                websDone(wp, 404);
1774        } else {
1775                delay = WEBS_TIMEOUT - tm;
1776                a_assert(delay > 0);
1777                emfReschedCallback(id, delay);
1778        }
1779}
1780
1781/******************************************************************************/
1782/*
1783 *      Called when the request is done.
1784 */
1785
1786void websDone(webs_t wp, int code)
1787{
1788        a_assert(websValid(wp));
1789
1790/*
1791 *      Disable socket handler in case keep alive set.
1792 */
1793        socketDeleteHandler(wp->sid);
1794
1795        if (code != 200) {
1796                wp->flags &= ~WEBS_KEEP_ALIVE;
1797        }
1798
1799#ifdef WEBS_PROXY_SUPPORT
1800        if (! (wp->flags & WEBS_LOCAL_PAGE)) {
1801                websStats.activeNetRequests--;
1802        }
1803#endif
1804
1805#ifdef WEBS_LOG_SUPPORT
1806        if (! (wp->flags & WEBS_REQUEST_DONE)) {
1807                websLog(wp, code);
1808        }
1809#endif
1810
1811/*
1812 *      Close any opened document by a handler
1813 */
1814        websPageClose(wp);
1815
1816/*
1817 *      Exit if secure.
1818 */
1819#ifdef WEBS_SSL_SUPPORT
1820        if (wp->flags & WEBS_SECURE) {
1821                websTimeoutCancel(wp);
1822                websSSLFlush(wp->wsp);
1823                socketCloseConnection(wp->sid);
1824                websFree(wp);
1825                return;
1826        }
1827#endif
1828
1829/*
1830 *      If using Keep Alive (HTTP/1.1) we keep the socket open for a period
1831 *      while waiting for another request on the socket.
1832 */
1833        if (wp->flags & WEBS_KEEP_ALIVE) {
1834                if (socketFlush(wp->sid) == 0) {
1835                        wp->state = WEBS_BEGIN;
1836                        wp->flags |= WEBS_REQUEST_DONE;
1837                        if (wp->header.buf) {
1838                                ringqFlush(&wp->header);
1839                        }
1840                        socketCreateHandler(wp->sid, SOCKET_READABLE, websSocketEvent,
1841                                (int) wp);
1842                        websTimeoutCancel(wp);
1843                        wp->timeout = emfSchedCallback(WEBS_TIMEOUT, websTimeout,
1844                                (void *) wp);
1845                        return;
1846                }
1847        } else {
1848                websTimeoutCancel(wp);
1849                socketSetBlock(wp->sid, 1);
1850                socketFlush(wp->sid);
1851                socketCloseConnection(wp->sid);
1852        }
1853        websFree(wp);
1854}
1855
1856/******************************************************************************/
1857/*
1858 *      Allocate a new webs structure
1859 */
1860
1861int websAlloc(int sid)
1862{
1863        webs_t          wp;
1864        int                     wid;
1865
1866/*
1867 *      Allocate a new handle for this connection
1868 */
1869        if ((wid = hAllocEntry((void*) &webs, &websMax,
1870                        sizeof(struct websRec))) < 0) {
1871                return -1;
1872        }
1873        wp = webs[wid];
1874
1875        wp->wid = wid;
1876        wp->sid = sid;
1877        wp->state = WEBS_BEGIN;
1878        wp->docfd = -1;
1879        wp->timeout = -1;
1880        wp->dir = NULL;
1881        wp->authType = NULL;
1882        wp->protocol = NULL;
1883        wp->protoVersion = NULL;
1884        wp->password = NULL;
1885        wp->userName = NULL;
1886#ifdef DIGEST_ACCESS_SUPPORT
1887        wp->realm = NULL;
1888        wp->nonce = NULL;
1889        wp->digest = NULL;
1890        wp->uri = NULL;
1891        wp->opaque = NULL;
1892        wp->nc = NULL;
1893        wp->cnonce = NULL;
1894        wp->qop = NULL;
1895#endif
1896#ifdef WEBS_SSL_SUPPORT
1897        wp->wsp = NULL;
1898#endif
1899
1900        ringqOpen(&wp->header, WEBS_HEADER_BUFINC, WEBS_MAX_HEADER);
1901
1902/*
1903 *      Create storage for the CGI variables. We supply the symbol tables for
1904 *      both the CGI variables and for the global functions. The function table
1905 *      is common to all webs instances (ie. all browsers)
1906 */
1907        wp->cgiVars = symOpen(WEBS_SYM_INIT);
1908
1909        return wid;
1910}
1911
1912/******************************************************************************/
1913/*
1914 *      Free a webs structure
1915 */
1916
1917void websFree(webs_t wp)
1918{
1919        a_assert(websValid(wp));
1920
1921        if (wp->path)
1922                bfree(B_L, wp->path);
1923        if (wp->url)
1924                bfree(B_L, wp->url);
1925        if (wp->host)
1926                bfree(B_L, wp->host);
1927        if (wp->lpath)
1928                bfree(B_L, wp->lpath);
1929        if (wp->query)
1930                bfree(B_L, wp->query);
1931        if (wp->decodedQuery)
1932                bfree(B_L, wp->decodedQuery);
1933        if (wp->authType)
1934                bfree(B_L, wp->authType);
1935        if (wp->password)
1936                bfree(B_L, wp->password);
1937        if (wp->userName)
1938                bfree(B_L, wp->userName);
1939        if (wp->cookie)
1940                bfree(B_L, wp->cookie);
1941        if (wp->userAgent)
1942                bfree(B_L, wp->userAgent);
1943        if (wp->dir)
1944                bfree(B_L, wp->dir);
1945        if (wp->protocol)
1946                bfree(B_L, wp->protocol);
1947        if (wp->protoVersion)
1948                bfree(B_L, wp->protoVersion);
1949        if (wp->cgiStdin)
1950                bfree(B_L, wp->cgiStdin);
1951
1952
1953#ifdef DIGEST_ACCESS_SUPPORT
1954        if (wp->realm)
1955                bfree(B_L, wp->realm);
1956        if (wp->uri)
1957                bfree(B_L, wp->uri);
1958        if (wp->digest)
1959                bfree(B_L, wp->digest);
1960        if (wp->opaque)
1961                bfree(B_L, wp->opaque);
1962        if (wp->nonce)
1963                bfree(B_L, wp->nonce);
1964        if (wp->nc)
1965                bfree(B_L, wp->nc);
1966        if (wp->cnonce)
1967                bfree(B_L, wp->cnonce);
1968        if (wp->qop)
1969                bfree(B_L, wp->qop);
1970#endif
1971#ifdef WEBS_SSL_SUPPORT
1972        websSSLFree(wp->wsp);
1973#endif
1974        symClose(wp->cgiVars);
1975
1976        if (wp->header.buf) {
1977                ringqClose(&wp->header);
1978        }
1979
1980        websMax = hFree((void*) &webs, wp->wid);
1981        bfree(B_L, wp);
1982        a_assert(websMax >= 0);
1983}
1984
1985/******************************************************************************/
1986/*
1987 *      Return the server address
1988 */
1989
1990char_t *websGetHost(void)
1991{
1992        return websHost;
1993}
1994
1995/******************************************************************************/
1996/*
1997 *      Return the the url to access the server. (ip address)
1998 */
1999
2000char_t *websGetIpaddrUrl(void)
2001{
2002        return websIpaddrUrl;
2003}
2004
2005/******************************************************************************/
2006/*
2007 *      Return the server address
2008 */
2009
2010char_t *websGetHostUrl(void)
2011{
2012        return websHostUrl;
2013}
2014
2015/******************************************************************************/
2016/*
2017 *      Return the listen port
2018 */
2019
2020int websGetPort(void)
2021{
2022        return websPort;
2023}
2024
2025/******************************************************************************/
2026/*
2027 *      Get the number of bytes to write
2028 */
2029
2030int websGetRequestBytes(webs_t wp)
2031{
2032        a_assert(websValid(wp));
2033
2034        return wp->numbytes;
2035}
2036
2037/******************************************************************************/
2038/*
2039 *      Get the directory for this request
2040 */
2041
2042char_t *websGetRequestDir(webs_t wp)
2043{
2044        a_assert(websValid(wp));
2045
2046        if (wp->dir == NULL) {
2047                return T("");
2048        }
2049
2050        return wp->dir;
2051}
2052
2053/******************************************************************************/
2054/*
2055 *      Get the flags for this request
2056 */
2057
2058int websGetRequestFlags(webs_t wp)
2059{
2060        a_assert(websValid(wp));
2061
2062        return wp->flags;
2063}
2064
2065/******************************************************************************/
2066/*
2067 *      Return the IP address
2068 */
2069
2070char_t *websGetRequestIpaddr(webs_t wp)
2071{
2072        a_assert(websValid(wp));
2073
2074        return wp->ipaddr;
2075}
2076
2077/******************************************************************************/
2078/*
2079 *      Set the local path for the request
2080 */
2081
2082char_t *websGetRequestLpath(webs_t wp)
2083{
2084        a_assert(websValid(wp));
2085
2086#ifdef WEBS_PAGE_ROM
2087        return wp->path;
2088#else
2089        return wp->lpath;
2090#endif
2091}
2092
2093/******************************************************************************/
2094/*
2095 *      Get the path for this request
2096 */
2097
2098char_t *websGetRequestPath(webs_t wp)
2099{
2100        a_assert(websValid(wp));
2101
2102        if (wp->path == NULL) {
2103                return T("");
2104        }
2105
2106        return wp->path;
2107}
2108
2109/******************************************************************************/
2110/*
2111 *      Return the password
2112 */
2113
2114char_t *websGetRequestPassword(webs_t wp)
2115{
2116        a_assert(websValid(wp));
2117
2118        return wp->password;
2119}
2120
2121/******************************************************************************/
2122/*
2123 *      Return the request type
2124 */
2125
2126char_t *websGetRequestType(webs_t wp)
2127{
2128        a_assert(websValid(wp));
2129
2130        return wp->type;
2131}
2132
2133/******************************************************************************/
2134/*
2135 *      Return the username
2136 */
2137
2138char_t *websGetRequestUserName(webs_t wp)
2139{
2140        a_assert(websValid(wp));
2141
2142        return wp->userName;
2143}
2144
2145/******************************************************************************/
2146/*
2147 *      Get the number of bytes written
2148 */
2149
2150int websGetRequestWritten(webs_t wp)
2151{
2152        a_assert(websValid(wp));
2153
2154        return wp->written;
2155}
2156
2157/******************************************************************************/
2158/*
2159 *      Set the hostname
2160 */
2161
2162void websSetHost(char_t *host)
2163{
2164        gstrncpy(websHost, host, TSZ(websHost));
2165}
2166
2167/******************************************************************************/
2168/*
2169 *      Set the host URL
2170 */
2171
2172void websSetHostUrl(char_t *url)
2173{
2174        a_assert(url && *url);
2175
2176        bfreeSafe(B_L, websHostUrl);
2177        websHostUrl = gstrdup(B_L, url);
2178}
2179
2180/******************************************************************************/
2181/*
2182 *      Set the IP address
2183 */
2184
2185void websSetIpaddr(char_t *ipaddr)
2186{
2187        a_assert(ipaddr && *ipaddr);
2188
2189        gstrncpy(websIpaddr, ipaddr, TSZ(websIpaddr));
2190}
2191
2192/******************************************************************************/
2193/*
2194 *      Set the number of bytes to write
2195 */
2196
2197void websSetRequestBytes(webs_t wp, int bytes)
2198{
2199        a_assert(websValid(wp));
2200        a_assert(bytes >= 0);
2201
2202        wp->numbytes = bytes;
2203}
2204
2205/******************************************************************************/
2206/*
2207 *      Set the flags for this request
2208 */
2209
2210void websSetRequestFlags(webs_t wp, int flags)
2211{
2212        a_assert(websValid(wp));
2213
2214        wp->flags = flags;
2215}
2216
2217/******************************************************************************/
2218/*
2219 *      Set the local path for the request
2220 */
2221
2222void websSetRequestLpath(webs_t wp, char_t *lpath)
2223{
2224        a_assert(websValid(wp));
2225        a_assert(lpath && *lpath);
2226
2227        if (wp->lpath) {
2228                bfree(B_L, wp->lpath);
2229        }
2230        wp->lpath = bstrdup(B_L, lpath);
2231        websSetVar(wp, T("PATH_TRANSLATED"), wp->lpath);
2232}
2233
2234/******************************************************************************/
2235/*
2236 *      Update the URL path and the directory containing the web page
2237 */
2238
2239void websSetRequestPath(webs_t wp, char_t *dir, char_t *path)
2240{
2241        char_t  *tmp;
2242
2243        a_assert(websValid(wp));
2244
2245        if (dir) {
2246                tmp = wp->dir;
2247                wp->dir = bstrdup(B_L, dir);
2248                if (tmp) {
2249                        bfree(B_L, tmp);
2250                }
2251        }
2252        if (path) {
2253                tmp = wp->path;
2254                wp->path = bstrdup(B_L, path);
2255                websSetVar(wp, T("PATH_INFO"), wp->path);
2256                if (tmp) {
2257                        bfree(B_L, tmp);
2258                }
2259        }
2260}
2261
2262/******************************************************************************/
2263/*
2264 *      Set the Write handler for this socket
2265 */
2266
2267void websSetRequestSocketHandler(webs_t wp, int mask, void (*fn)(webs_t wp))
2268{
2269        a_assert(websValid(wp));
2270
2271        wp->writeSocket = fn;
2272        socketCreateHandler(wp->sid, SOCKET_WRITABLE, websSocketEvent, (int) wp);
2273}
2274
2275/******************************************************************************/
2276/*
2277 *      Set the number of bytes written
2278 */
2279
2280void websSetRequestWritten(webs_t wp, int written)
2281{
2282        a_assert(websValid(wp));
2283
2284        wp->written = written;
2285}
2286
2287/******************************************************************************/
2288/*
2289 *      Reurn true if the webs handle is valid
2290 */
2291
2292int websValid(webs_t wp)
2293{
2294        int             wid;
2295
2296        for (wid = 0; wid < websMax; wid++) {
2297                if (wp == webs[wid]) {
2298                        return 1;
2299                }
2300        }
2301        return 0;
2302}
2303
2304/******************************************************************************/
2305/*
2306 *      Build an ASCII time string.  If sbuf is NULL we use the current time,
2307 *      else we use the last modified time of sbuf;
2308 */
2309
2310char_t *websGetDateString(websStatType *sbuf)
2311{
2312        char_t* cp, *r;
2313        time_t  now;
2314
2315        if (sbuf == NULL) {
2316                time(&now);
2317        } else {
2318                now = sbuf->mtime;
2319        }
2320        if ((cp = gctime(&now)) != NULL) {
2321                cp[gstrlen(cp) - 1] = '\0';
2322                r = bstrdup(B_L, cp);
2323                return r;
2324        }
2325        return NULL;
2326}
2327
2328/******************************************************************************/
2329/*
2330 *      Mark time. Set a timestamp so that, later, we can return the number of
2331 *      seconds since we made the mark. Note that the mark my not be a
2332 *      "real" time, but rather a relative marker.
2333 */
2334
2335void websSetTimeMark(webs_t wp)
2336{
2337        wp->timestamp = time(0);
2338}
2339
2340/******************************************************************************/
2341/*
2342 *      Get the number of seconds since the last mark.
2343 */
2344
2345static int websGetTimeSinceMark(webs_t wp)
2346{
2347        return time(0) - wp->timestamp;
2348}
2349
2350/******************************************************************************/
2351/*
2352 *      Store the new realm name
2353 */
2354
2355void websSetRealm(char_t *realmName)
2356{
2357        a_assert(realmName);
2358
2359        gstrncpy(websRealm, realmName, TSZ(websRealm));
2360}
2361
2362/******************************************************************************/
2363/*
2364 *      Return the realm name (used for authorization)
2365 */
2366
2367char_t *websGetRealm(void)
2368{
2369        return websRealm;
2370}
2371
2372
2373#ifdef WEBS_IF_MODIFIED_SUPPORT
2374/******************************************************************************/
2375/*     
2376 *      These functions are intended to closely mirror the syntax for HTTP-date
2377 *      from RFC 2616 (HTTP/1.1 spec).  This code was submitted by Pete Bergstrom.
2378 */
2379
2380/*     
2381 *      RFC1123Date     = wkday "," SP date1 SP time SP "GMT"
2382 *      RFC850Date      = weekday "," SP date2 SP time SP "GMT"
2383 *      ASCTimeDate     = wkday SP date3 SP time SP 4DIGIT
2384 *
2385 *      Each of these functions tries to parse the value and update the index to
2386 *      the point it leaves off parsing.
2387 */
2388
2389typedef enum { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } MonthEnumeration;
2390typedef enum { SUN, MON, TUE, WED, THU, FRI, SAT } WeekdayEnumeration;
2391
2392/******************************************************************************/
2393/*     
2394 *      Parse an N-digit value
2395 */
2396
2397static int parseNDIGIT(char_t *buf, int digits, int *index)
2398{
2399        int tmpIndex, returnValue;
2400
2401        returnValue = 0;
2402
2403        for (tmpIndex = *index; tmpIndex < *index+digits; tmpIndex++) {
2404                if (gisdigit(buf[tmpIndex])) {
2405                        returnValue = returnValue * 10 + (buf[tmpIndex] - T('0'));
2406                }
2407        }
2408        *index = tmpIndex;
2409       
2410        return returnValue;
2411}
2412
2413/******************************************************************************/
2414/*
2415 *      Return an index into the month array
2416 */
2417
2418static int parseMonth(char_t *buf, int *index)
2419{
2420/*     
2421 *      "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun" |
2422 *      "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec"
2423 */
2424        int tmpIndex, returnValue;
2425
2426        returnValue = -1;
2427        tmpIndex = *index;
2428
2429        switch (buf[tmpIndex]) {
2430                case 'A':
2431                        switch (buf[tmpIndex+1]) {
2432                                case 'p':
2433                                        returnValue = APR;
2434                                        break;
2435                                case 'u':
2436                                        returnValue = AUG;
2437                                        break;
2438                        }
2439                        break;
2440                case 'D':
2441                        returnValue = DEC;
2442                        break;
2443                case 'F':
2444                        returnValue = FEB;
2445                        break;
2446                case 'J':
2447                        switch (buf[tmpIndex+1]) {
2448                                case 'a':
2449                                        returnValue = JAN;
2450                                        break;
2451                                case 'u':
2452                                        switch (buf[tmpIndex+2]) {
2453                                                case 'l':
2454                                                        returnValue = JUL;
2455                                                        break;
2456                                                case 'n':
2457                                                        returnValue = JUN;
2458                                                        break;
2459                                        }
2460                                        break;
2461                        }
2462                        break;
2463                case 'M':
2464                        switch (buf[tmpIndex+1]) {
2465                                case 'a':
2466                                        switch (buf[tmpIndex+2]) {
2467                                                case 'r':
2468                                                        returnValue = MAR;
2469                                                        break;
2470                                                case 'y':
2471                                                        returnValue = MAY;
2472                                                        break;
2473                                        }
2474                                        break;
2475                        }
2476                        break;
2477                case 'N':
2478                        returnValue = NOV;
2479                        break;
2480                case 'O':
2481                        returnValue = OCT;
2482                        break;
2483                case 'S':
2484                        returnValue = SEP;
2485                        break;
2486        }
2487
2488        if (returnValue >= 0) {
2489                *index += 3;
2490        }
2491
2492        return returnValue;
2493}
2494
2495/******************************************************************************/
2496/*
2497 *      Parse a year value (either 2 or 4 digits)
2498 */
2499
2500static int parseYear(char_t *buf, int *index)
2501{
2502        int tmpIndex, returnValue;
2503
2504        tmpIndex = *index;
2505        returnValue = parseNDIGIT(buf, 4, &tmpIndex);
2506
2507        if (returnValue >= 0) {
2508                *index = tmpIndex;
2509        } else {
2510                returnValue = parseNDIGIT(buf, 2, &tmpIndex);
2511                if (returnValue >= 0) {
2512/*
2513 *                      Assume that any year earlier than the start of the
2514 *                      epoch for time_t (1970) specifies 20xx
2515 */
2516                        if (returnValue < 70) {
2517                                returnValue += 2000;
2518                        } else {
2519                                returnValue += 1900;
2520                        }
2521
2522                        *index = tmpIndex;
2523                }
2524        }
2525
2526        return returnValue;
2527}
2528
2529/******************************************************************************/
2530/*
2531 *      The formulas used to build these functions are from "Calendrical Calculations",
2532 *      by Nachum Dershowitz, Edward M. Reingold, Cambridge University Press, 1997.
2533 */
2534
2535#include <math.h>
2536
2537const int GregorianEpoch = 1;
2538
2539/******************************************************************************/
2540/*
2541 *  Determine if year is a leap year
2542 */
2543
2544int GregorianLeapYearP(long year)
2545{
2546        int             result;
2547        long    tmp;
2548       
2549        tmp = year % 400;
2550
2551        if ((year % 4 == 0) &&
2552                (tmp != 100) &&
2553                (tmp != 200) &&
2554                (tmp != 300)) {
2555                result = TRUE;
2556        } else {
2557                result = FALSE;
2558        }
2559
2560        return result;
2561}
2562
2563/******************************************************************************/
2564/*
2565 *  Return the fixed date from the gregorian date
2566 */
2567
2568long FixedFromGregorian(long month, long day, long year)
2569{
2570        long fixedDate;
2571
2572        fixedDate = (long)(GregorianEpoch - 1 + 365 * (year - 1) +
2573                floor((year - 1) / 4.0) -
2574                floor((double)(year - 1) / 100.0) +
2575                floor((double)(year - 1) / 400.0) +
2576                floor((367.0 * ((double)month) - 362.0) / 12.0));
2577
2578        if (month <= 2) {
2579                fixedDate += 0;
2580        } else if (TRUE == GregorianLeapYearP(year)) {
2581                fixedDate += -1;
2582        } else {
2583                fixedDate += -2;
2584        }
2585
2586        fixedDate += day;
2587
2588        return fixedDate;
2589}
2590
2591/******************************************************************************/
2592/*
2593 *  Return the gregorian year from a fixed date
2594 */
2595
2596long GregorianYearFromFixed(long fixedDate)
2597{
2598        long result, d0, n400, d1, n100, d2, n4, d3, n1, d4, year;
2599
2600        d0 =    fixedDate - GregorianEpoch;
2601        n400 =  (long)(floor((double)d0 / (double)146097));
2602        d1 =    d0 % 146097;
2603        n100 =  (long)(floor((double)d1 / (double)36524));
2604        d2 =    d1 % 36524;
2605        n4 =    (long)(floor((double)d2 / (double)1461));
2606        d3 =    d2 % 1461;
2607        n1 =    (long)(floor((double)d3 / (double)365));
2608        d4 =    (d3 % 365) + 1;
2609        year =  400 * n400 + 100 * n100 + 4 * n4 + n1;
2610
2611        if ((n100 == 4) || (n1 == 4)) {
2612                result = year;
2613        } else {
2614                result = year + 1;
2615        }
2616
2617        return result;
2618}
2619
2620/******************************************************************************/
2621/*
2622 *      Returns the Gregorian date from a fixed date
2623 *      (not needed for this use, but included for completeness
2624 */
2625
2626#if 0
2627GregorianFromFixed(long fixedDate, long *month, long *day, long *year)
2628{
2629        long priorDays, correction;
2630
2631        *year =                 GregorianYearFromFixed(fixedDate);
2632        priorDays =             fixedDate - FixedFromGregorian(1, 1, *year);
2633
2634        if (fixedDate < FixedFromGregorian(3,1,*year)) {
2635                correction = 0;
2636        } else if (true == GregorianLeapYearP(*year)) {
2637                correction = 1;
2638        } else {
2639                correction = 2;
2640        }
2641
2642        *month = (long)(floor((12.0 * (double)(priorDays + correction) + 373.0) / 367.0));
2643        *day = fixedDate - FixedFromGregorian(*month, 1, *year);
2644}
2645#endif
2646
2647/******************************************************************************/
2648/*
2649 *      Returns the difference between two Gregorian dates
2650 */
2651
2652long GregorianDateDifference(   long month1, long day1, long year1,
2653                                                                long month2, long day2, long year2)
2654{
2655        return FixedFromGregorian(month2, day2, year2) -
2656                FixedFromGregorian(month1, day1, year1);
2657}
2658
2659
2660/******************************************************************************/
2661/*
2662 *      Return the number of seconds into the current day
2663 */
2664
2665#define SECONDS_PER_DAY 24*60*60
2666
2667static int parseTime(char_t *buf, int *index)
2668{
2669/*     
2670 *      Format of buf is - 2DIGIT ":" 2DIGIT ":" 2DIGIT
2671 */
2672        int returnValue, tmpIndex, hourValue, minuteValue, secondValue;
2673
2674        hourValue = minuteValue = secondValue = -1;
2675        returnValue = -1;
2676        tmpIndex = *index;
2677
2678        hourValue = parseNDIGIT(buf, 2, &tmpIndex);
2679
2680        if (hourValue >= 0) {
2681                tmpIndex++;
2682                minuteValue = parseNDIGIT(buf, 2, &tmpIndex);
2683                if (minuteValue >= 0) {
2684                        tmpIndex++;
2685                        secondValue = parseNDIGIT(buf, 2, &tmpIndex);
2686                }
2687        }
2688
2689        if ((hourValue >= 0) &&
2690                (minuteValue >= 0) &&
2691                (secondValue >= 0)) {
2692                returnValue = (((hourValue * 60) + minuteValue) * 60) + secondValue;
2693                *index = tmpIndex;
2694        }
2695
2696        return returnValue;
2697}
2698
2699/******************************************************************************/
2700/*
2701 *      Return the equivalent of time() given a gregorian date
2702 */
2703
2704static time_t dateToTimet(int year, int month, int day)
2705{
2706        long dayDifference;
2707
2708     /*
2709      * Bug fix by Jeff Reeder (Jun 14, 2002): The 'month' parameter is
2710      * numbered from  0 (Jan == 0), but FixedFromGregorian() takes
2711      * months numbered from 1 (January == 1). We need to add 1
2712      * to the month
2713      */
2714        dayDifference = FixedFromGregorian(month + 1, day, year) -
2715                FixedFromGregorian(1, 1, 1970);
2716
2717        return dayDifference * SECONDS_PER_DAY;
2718}
2719
2720/******************************************************************************/
2721/*
2722 *      Return the number of seconds between Jan 1, 1970 and the parsed date
2723 *      (corresponds to documentation for time() function)
2724 */
2725
2726static time_t parseDate1or2(char_t *buf, int *index)
2727{
2728/*     
2729 *      Format of buf is either
2730 *      2DIGIT SP month SP 4DIGIT
2731 *      or
2732 *      2DIGIT "-" month "-" 2DIGIT
2733 */
2734        int             dayValue, monthValue, yearValue, tmpIndex;
2735        time_t  returnValue;
2736
2737        returnValue = (time_t) -1;
2738        tmpIndex = *index;
2739
2740        dayValue = monthValue = yearValue = -1;
2741
2742        if (buf[tmpIndex] == T(',')) {
2743/*
2744 *              Skip over the ", "
2745 */
2746                tmpIndex += 2;
2747
2748                dayValue = parseNDIGIT(buf, 2, &tmpIndex);
2749                if (dayValue >= 0) {
2750/*
2751 *                      Skip over the space or hyphen
2752 */
2753                        tmpIndex++;
2754                        monthValue = parseMonth(buf, &tmpIndex);
2755                        if (monthValue >= 0) {
2756/*
2757 *                              Skip over the space or hyphen
2758 */
2759                                tmpIndex++;
2760                                yearValue = parseYear(buf, &tmpIndex);
2761                        }
2762                }
2763
2764                if ((dayValue >= 0) &&
2765                        (monthValue >= 0) &&
2766                        (yearValue >= 0)) {
2767                        if (yearValue < 1970) {
2768/*                             
2769 *                              Allow for Microsoft IE's year 1601 dates
2770 */
2771                                returnValue = 0;
2772                        } else {
2773                                returnValue = dateToTimet(yearValue, monthValue, dayValue);
2774                        }
2775                        *index = tmpIndex;
2776                }
2777        }
2778       
2779        return returnValue;
2780}
2781
2782/******************************************************************************/
2783/*
2784 *      Return the number of seconds between Jan 1, 1970 and the parsed date
2785 */
2786
2787static time_t parseDate3Time(char_t *buf, int *index)
2788{
2789/*
2790 *      Format of buf is month SP ( 2DIGIT | ( SP 1DIGIT ))
2791 */
2792        int             dayValue, monthValue, yearValue, timeValue, tmpIndex;
2793        time_t  returnValue;
2794
2795        returnValue = (time_t) -1;
2796        tmpIndex = *index;
2797
2798        dayValue = monthValue = yearValue = timeValue = -1;
2799
2800        monthValue = parseMonth(buf, &tmpIndex);
2801        if (monthValue >= 0) {
2802/*             
2803 *              Skip over the space
2804 */
2805                tmpIndex++;
2806                if (buf[tmpIndex] == T(' ')) {
2807/*
2808 *                      Skip over this space too
2809 */
2810                        tmpIndex++;
2811                        dayValue = parseNDIGIT(buf, 1, &tmpIndex);
2812                } else {
2813                        dayValue = parseNDIGIT(buf, 2, &tmpIndex);
2814                }
2815/*             
2816 *              Now get the time and time SP 4DIGIT
2817 */
2818                timeValue = parseTime(buf, &tmpIndex);
2819                if (timeValue >= 0) {
2820/*                     
2821 *                      Now grab the 4DIGIT year value
2822 */
2823                        yearValue = parseYear(buf, &tmpIndex);
2824                }
2825        }
2826
2827        if ((dayValue >= 0) &&
2828                (monthValue >= 0) &&
2829                (yearValue >= 0)) {
2830                returnValue = dateToTimet(yearValue, monthValue, dayValue);
2831                returnValue += timeValue;
2832                *index = tmpIndex;
2833        }
2834       
2835        return returnValue;
2836}
2837
2838
2839/******************************************************************************/
2840/*
2841 *      Although this looks like a trivial function, I found I was replicating the implementation
2842 *      seven times in the parseWeekday function. In the interests of minimizing code size
2843 *      and redundancy, it is broken out into a separate function. The cost of an extra
2844 *      function call I can live with given that it should only be called once per HTTP request.
2845 */
2846
2847static int bufferIndexIncrementGivenNTest(char_t *buf, int testIndex, char_t testChar,
2848                                                                                  int foundIncrement, int notfoundIncrement)
2849{
2850        if (buf[testIndex] == testChar) {
2851                return foundIncrement;
2852        }
2853
2854        return notfoundIncrement;
2855}
2856
2857/******************************************************************************/
2858/*
2859 *      Return an index into a logical weekday array
2860 */
2861
2862static int parseWeekday(char_t *buf, int *index)
2863{
2864/*     
2865 *      Format of buf is either
2866 *      "Mon" | "Tue" | "Wed" | "Thu" | "Fri" | "Sat" | "Sun"
2867 *      or
2868 *      "Monday" | "Tuesday" | "Wednesday" | "Thursday" | "Friday" | "Saturday" | "Sunday"
2869 */
2870        int tmpIndex, returnValue;
2871
2872        returnValue = -1;
2873        tmpIndex = *index;
2874
2875        switch (buf[tmpIndex]) {
2876                case 'F':
2877                        returnValue = FRI;
2878                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 'd', sizeof("Friday"), 3);
2879                        break;
2880                case 'M':
2881                        returnValue = MON;
2882                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 'd', sizeof("Monday"), 3);
2883                        break;
2884                case 'S':
2885                        switch (buf[tmpIndex+1]) {
2886                                case 'a':
2887                                        returnValue = SAT;
2888                                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 'u', sizeof("Saturday"), 3);
2889                                        break;
2890                                case 'u':
2891                                        returnValue = SUN;
2892                                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 'd', sizeof("Sunday"), 3);
2893                                        break;
2894                        }
2895                        break;
2896                case 'T':
2897                        switch (buf[tmpIndex+1]) {
2898                                case 'h':
2899                                        returnValue = THU;
2900                                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 'r', sizeof("Thursday"), 3);
2901                                        break;
2902                                case 'u':
2903                                        returnValue = TUE;
2904                                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 's', sizeof("Tuesday"), 3);
2905                                        break;
2906                        }
2907                        break;
2908                case 'W':
2909                        returnValue = WED;
2910                        *index += bufferIndexIncrementGivenNTest(buf, tmpIndex+3, 'n', sizeof("Wednesday"), 3);
2911                        break;
2912        }
2913        return returnValue;
2914}
2915
2916/******************************************************************************/
2917/*
2918 *              Parse the date and time string.
2919 */
2920
2921static time_t dateParse(time_t tip, char_t *cmd)
2922{
2923        int index, tmpIndex, weekday, timeValue;
2924        time_t parsedValue, dateValue;
2925
2926        parsedValue = (time_t) 0;
2927        index = timeValue = 0;
2928        weekday = parseWeekday(cmd, &index);
2929
2930        if (weekday >= 0) {
2931                tmpIndex = index;
2932                dateValue = parseDate1or2(cmd, &tmpIndex);
2933                if (dateValue >= 0) {
2934                        index = tmpIndex + 1;
2935/*
2936 *                      One of these two forms is being used
2937 *                      wkday "," SP date1 SP time SP "GMT"
2938 *                      weekday "," SP date2 SP time SP "GMT"
2939 */
2940                        timeValue = parseTime(cmd, &index);
2941                        if (timeValue >= 0) {
2942/*                             
2943 *                              Now match up that "GMT" string for completeness
2944 *                              Compute the final value if there were no problems in the parse
2945 */
2946                                if ((weekday >= 0) &&
2947                                        (dateValue >= 0) &&
2948                                        (timeValue >= 0)) {
2949                                        parsedValue = dateValue + timeValue;
2950                                }
2951                        }
2952                } else {
2953/*
2954 *                      Try the other form - wkday SP date3 SP time SP 4DIGIT
2955 */
2956                        tmpIndex = index;
2957                        parsedValue = parseDate3Time(cmd, &tmpIndex);
2958                }
2959        }
2960
2961        return parsedValue;
2962}
2963
2964#endif /* WEBS_IF_MODIFIED_SUPPORT */
2965
2966/******************************************************************************/
Note: See TracBrowser for help on using the repository browser.