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

1.1     ! frystyk     1: /*
        !             2: **     @(#) $Id: LoadToFile.c,v 1.5 1999/02/23 17:53:30 frystyk Exp $
        !             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);
        !           103: 
        !           104:     /* Add our own filter to terminate the application */
        !           105:     HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
        !           106: 
        !           107:     /* Setup cookies */
        !           108:     HTCookie_init();
        !           109:     HTCookie_setCallbacks(setCookie, NULL, findCookie, NULL);
        !           110: 
        !           111:     /* Set the timeout for long we are going to wait for a response */
        !           112:     HTHost_setEventTimeout(10000);
        !           113: 
        !           114:     /* Scan command line for parameters */
        !           115:     for (arg=1; arg<argc; arg++) {
        !           116:         if (!strcmp(argv[arg], "-o")) { 
        !           117:             outputfile = (arg+1 < argc && *argv[arg+1] != '-') ?
        !           118:                 argv[++arg] : DEFAULT_OUTPUT_FILE;
        !           119:             
        !           120:         } else {
        !           121:             getme = argv[arg];
        !           122:         }
        !           123:     }
        !           124: 
        !           125:     /* Make sure we have an output */
        !           126:     if (!outputfile) outputfile = DEFAULT_OUTPUT_FILE;
        !           127: 
        !           128:     if (getme && *getme) {
        !           129:         request = HTRequest_new();
        !           130: 
        !           131:         /* Start the load */
        !           132:         status = HTLoadToFile(getme, request, outputfile);
        !           133: 
        !           134:         /* Go into the event loop... */
        !           135:         HTEventList_loop(request);
        !           136: 
        !           137:     } else {
        !           138:        HTPrint("Type the URI of document you want to load and the name of the local file.\n");
        !           139:        HTPrint("\t%s <address> -o <localfile>\n", argv[0]);
        !           140:        HTPrint("For example, %s http://www.w3.org -o w3chome.html\n", argv[0]);
        !           141: 
        !           142:         /* Delete our profile if no load */
        !           143:         HTProfile_delete();
        !           144:     }
        !           145: 
        !           146:     return 0;
        !           147: }

Webmaster