Annotation of libwww/Library/Examples/cookie.c, revision 1.2

1.1       frystyk     1: /*
1.2     ! frystyk     2: **     @(#) $Id: cookie.c,v 1.1 1999/04/01 19:37:43 frystyk Exp $
1.1       frystyk     3: **     
                      4: **     More libwww samples can be found at "http://www.w3.org/Library/Examples/"
                      5: **     
                      6: **     Copyright © 1995-1998 World Wide Web Consortium, (Massachusetts
                      7: **     Institute of Technology, Institut National de Recherche en
                      8: **     Informatique et en Automatique, Keio University). All Rights
                      9: **     Reserved. This program is distributed under the W3C's Software
                     10: **     Intellectual Property License. This program is distributed in the hope
                     11: **     that it will be useful, but WITHOUT ANY WARRANTY; without even the
                     12: **     implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
                     13: **     PURPOSE. See W3C License http://www.w3.org/Consortium/Legal/ for more
                     14: **     details.
                     15: **
                     16: **     Sample showing how to load a document and save it to local file
                     17: */
                     18: 
                     19: #include "WWWLib.h"                          /* Global Library Include file */
                     20: #include "WWWMIME.h"                               /* MIME parser/generator */
                     21: #include "WWWNews.h"                                  /* News access module */
                     22: #include "WWWHTTP.h"                                  /* HTTP access module */
                     23: #include "WWWFTP.h"
                     24: #include "WWWFile.h"
                     25: #include "WWWGophe.h"
                     26: #include "WWWInit.h"
                     27: 
                     28: #define APP_NAME               "GETTOOL"
                     29: #define APP_VERSION            "1.0"
                     30: #define DEFAULT_OUTPUT_FILE     "get.out"
                     31: 
                     32: PRIVATE int printer (const char * fmt, va_list pArgs)
                     33: {
                     34:     return (vfprintf(stdout, fmt, pArgs));
                     35: }
                     36: 
                     37: PRIVATE int tracer (const char * fmt, va_list pArgs)
                     38: {
                     39:     return (vfprintf(stderr, fmt, pArgs));
                     40: }
                     41: 
                     42: PRIVATE BOOL setCookie (HTRequest * request, HTCookie * cookie, void * param)
                     43: {
                     44:     if (cookie) {
                     45:        char * addr = HTAnchor_address((HTAnchor *) HTRequest_anchor(request));
                     46:        HTPrint("While accessing `%s\', we received a cookie with parameters:\n", addr);
                     47:        if (HTCookie_name(cookie))
                     48:            HTPrint("\tName   : `%s\'\n", HTCookie_name(cookie));
                     49:        if (HTCookie_value(cookie))
                     50:            HTPrint("\tValue  : `%s\'\n", HTCookie_value(cookie));
                     51:        if (HTCookie_domain(cookie))
                     52:            HTPrint("\tDomain : `%s\'\n", HTCookie_domain(cookie));
                     53:        if (HTCookie_path(cookie))
                     54:            HTPrint("\tPath   : `%s\'\n", HTCookie_path(cookie));
                     55:        if (HTCookie_expiration(cookie) > 0) {
                     56:            time_t t = HTCookie_expiration(cookie);
                     57:            HTPrint("\tExpires: `%s\'\n", HTDateTimeStr(&t, NO));
                     58:        }
                     59:        HTPrint("\tCookie is %ssecure\n\n", HTCookie_isSecure(cookie) ? "" : "not ");
                     60:        HT_FREE(addr);
                     61:     }
                     62:     return YES;
                     63: }
                     64: 
                     65: PRIVATE HTAssocList * findCookie (HTRequest * request, void * param)
                     66: {
                     67:     HTAssocList * alist = HTAssocList_new();   /* Is deleted by the cookie module */
                     68:     HTAssocList_addObject(alist, "dummy-name", "dummy-value");
                     69:     return alist;
                     70: }
                     71: 
                     72: /*
                     73: **  We get called here from the event loop when we are done
                     74: **  loading. Here we terminate the program as we have nothing
                     75: **  better to do.
                     76: */
                     77: int terminate_handler (HTRequest * request, HTResponse * response,
                     78:                       void * param, int status)
                     79: {
                     80:     /* Delete our request again */
                     81:     HTRequest_delete(request);
                     82: 
                     83:     /* Delete our profile */
                     84:     HTProfile_delete();
                     85: 
                     86:     exit(status ? status : 0);
                     87: }
                     88: 
                     89: int main (int argc, char ** argv)
                     90: {
                     91:     int                        status = 0;     
                     92:     int                        arg = 0;
                     93:     char *              outputfile = NULL;
                     94:     char *              getme = NULL;
                     95:     HTRequest *         request = NULL;
                     96: 
                     97:     /* Initiate W3C Reference Library with a client profile */
                     98:     HTProfile_newNoCacheClient(APP_NAME, APP_VERSION);
                     99: 
                    100:     /* Need our own trace and print functions */
                    101:     HTPrint_setCallback(printer);
                    102:     HTTrace_setCallback(tracer);
1.2     ! frystyk   103: 
        !           104: #if 0
        !           105:     HTSetTraceMessageMask("sop");
        !           106: #endif
1.1       frystyk   107: 
                    108:     /* Add our own filter to terminate the application */
                    109:     HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
                    110: 
                    111:     /* Setup cookies */
                    112:     HTCookie_init();
                    113:     HTCookie_setCallbacks(setCookie, NULL, findCookie, NULL);
                    114: 
                    115:     /* Set the timeout for long we are going to wait for a response */
                    116:     HTHost_setEventTimeout(10000);
                    117: 
                    118:     /* Scan command line for parameters */
                    119:     for (arg=1; arg<argc; arg++) {
                    120:         if (!strcmp(argv[arg], "-o")) { 
                    121:             outputfile = (arg+1 < argc && *argv[arg+1] != '-') ?
                    122:                 argv[++arg] : DEFAULT_OUTPUT_FILE;
                    123:             
                    124:         } else {
                    125:             getme = argv[arg];
                    126:         }
                    127:     }
                    128: 
                    129:     /* Make sure we have an output */
                    130:     if (!outputfile) outputfile = DEFAULT_OUTPUT_FILE;
                    131: 
                    132:     if (getme && *getme) {
                    133:         request = HTRequest_new();
                    134: 
                    135:         /* Start the load */
                    136:         status = HTLoadToFile(getme, request, outputfile);
                    137: 
                    138:         /* Go into the event loop... */
                    139:         HTEventList_loop(request);
                    140: 
                    141:     } else {
                    142:        HTPrint("Type the URI of document you want to load and the name of the local file.\n");
                    143:        HTPrint("\t%s <address> -o <localfile>\n", argv[0]);
                    144:        HTPrint("For example, %s http://www.w3.org -o w3chome.html\n", argv[0]);
                    145: 
                    146:         /* Delete our profile if no load */
                    147:         HTProfile_delete();
                    148:     }
                    149: 
                    150:     return 0;
                    151: }

Webmaster