Annotation of libwww/Library/src/HTAccess.c, revision 1.116

1.61      frystyk     1: /*                                                                  HTAccess.c
                      2: **     ACCESS MANAGER
                      3: **
1.75      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.61      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.1       timbl       6: **
                      7: ** Authors
1.79      frystyk     8: **     TBL     Tim Berners-Lee timbl@w3.org
1.4       timbl       9: **     JFG     Jean-Francois Groff jfg@dxcern.cern.ch
1.1       timbl      10: **     DD      Denis DeLaRoca (310) 825-4580  <CSP1DWD@mvs.oac.ucla.edu>
                     11: ** History
                     12: **       8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
                     13: **     26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
1.42      frystyk    14: **      6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
1.1       timbl      15: **     17 Dec 92 Tn3270 added, bug fix. DD
1.2       timbl      16: **      4 Feb 93 Access registration, Search escapes bad chars TBL
1.9       timbl      17: **               PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
                     18: **     28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.19      timbl      19: **        Dec 93 Bug change around, more reentrant, etc
1.42      frystyk    20: **     09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
1.114     frystyk    21: **      8 Jul 94 Insulate HT_FREE();
1.88      frystyk    22: **        Sep 95 Rewritten, HFN
1.1       timbl      23: */
                     24: 
1.68      frystyk    25: #if !defined(HT_DIRECT_WAIS) && !defined(HT_DEFAULT_WAIS_GATEWAY)
                     26: #define HT_DEFAULT_WAIS_GATEWAY "http://www.w3.org:8001/"
1.54      frystyk    27: #endif
1.8       timbl      28: 
1.67      frystyk    29: /* Library include files */
1.88      frystyk    30: #include "WWWLib.h"
1.93      frystyk    31: #include "HTReqMan.h"
                     32: #include "HTAccess.h"                                   /* Implemented here */
1.88      frystyk    33: 
1.99      frystyk    34: #ifndef VC
                     35: #define VC "unknown"
                     36: #endif
                     37: 
1.93      frystyk    38: PRIVATE char * HTAppName = NULL;         /* Application name: please supply */
                     39: PRIVATE char * HTAppVersion = NULL;    /* Application version: please supply */
1.2       timbl      40: 
1.99      frystyk    41: PRIVATE char * HTLibName = "libwww";
                     42: PRIVATE char * HTLibVersion = VC;
                     43: 
                     44: PRIVATE BOOL   HTSecure = NO;           /* Can we access local file system? */
                     45: 
1.111     frystyk    46: #define PUTBLOCK(b, l) (*target->isa->put_block)(target, b, l)
                     47: 
                     48: struct _HTStream {
                     49:     HTStreamClass * isa;
                     50: };
                     51: 
1.59      frystyk    52: /* --------------------------------------------------------------------------*/
1.61      frystyk    53: /*                Initialization and Termination of the Library             */
                     54: /* --------------------------------------------------------------------------*/
                     55: 
1.99      frystyk    56: /*     Information about the Application
                     57: **     ---------------------------------
1.93      frystyk    58: */
                     59: PUBLIC CONST char * HTLib_appName (void)
                     60: {
1.99      frystyk    61:     return HTAppName ? HTAppName : "UNKNOWN";
                     62: }
                     63: 
                     64: PUBLIC CONST char * HTLib_appVersion (void)
                     65: {
                     66:     return HTAppVersion ? HTAppVersion : "0.0";
1.93      frystyk    67: }
                     68: 
1.99      frystyk    69: /*     Information about libwww
                     70: **     ------------------------
                     71: */
                     72: PUBLIC CONST char * HTLib_name (void)
                     73: {
                     74:     return HTLibName ? HTLibName : "UNKNOWN";
                     75: }
                     76: 
                     77: PUBLIC CONST char * HTLib_version (void)
                     78: {
                     79:     return HTLibVersion ? HTLibVersion : "0.0";
                     80: }
                     81: 
                     82: /*     Access Local File System
                     83: **     ------------------------
                     84: **     In this mode we do not tough the local file system at all
1.93      frystyk    85: */
1.99      frystyk    86: PUBLIC BOOL HTLib_secure (void)
                     87: {
                     88:     return HTSecure;
                     89: }
                     90: 
                     91: PUBLIC void HTLib_setSecure (BOOL mode)
1.93      frystyk    92: {
1.99      frystyk    93:     HTSecure = mode;
1.93      frystyk    94: }
                     95: 
1.61      frystyk    96: /*                                                                  HTLibInit
                     97: **
                     98: **     This function initiates the Library and it MUST be called when
                     99: **     starting up an application. See also HTLibTerminate()
                    100: */
1.93      frystyk   101: PUBLIC BOOL HTLibInit (CONST char * AppName, CONST char * AppVersion)
1.61      frystyk   102: {
1.105     frystyk   103: #ifdef WWW_WIN_ASYNC
1.110     frystyk   104:     /*
                    105:     ** We are here starting a hidden window to take care of events from
                    106:     **  the async select() call in the async version of the event loop in
                    107:     ** the Internal event manager (HTEvntrg.c)
                    108:     */
1.105     frystyk   109:     HWND htSocketWin;
                    110:     static char className[] = "AsyncWindowClass";
                    111:     WNDCLASS wc;
1.113     frystyk   112:     OSVERSIONINFO osInfo;
1.105     frystyk   113:     
                    114:     wc.style=0;
                    115:     wc.lpfnWndProc=(WNDPROC)AsyncWindowProc;
                    116:     wc.cbClsExtra=0;
                    117:     wc.cbWndExtra=0;
                    118:     wc.hIcon=0;
                    119:     wc.hCursor=0;
                    120:     wc.hbrBackground=0;
                    121:     wc.lpszMenuName=(LPSTR)0;
                    122:     wc.lpszClassName=className;
1.113     frystyk   123: 
                    124:     osInfo.dwOSVersionInfoSize = sizeof(osInfo);
                    125:     GetVersionEx(&osInfo);
                    126:     if (osInfo.dwPlatformId == VER_PLATFORM_WIN32s || osInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
                    127:        wc.hInstance=GetModuleHandle(NULL); /* 95 and non threaded platforms */
                    128:     else
                    129:        wc.hInstance=GetCurrentProcess(); /* NT and hopefully everything following */
1.105     frystyk   130: #endif /* WWW_WIN_ASYNC */
                    131: 
1.91      frystyk   132:     if (WWWTRACE)
1.115     eric      133:        HTTrace("WWWLibInit.. INITIALIZING LIBRARY OF COMMON CODE\n");
1.63      frystyk   134: 
1.93      frystyk   135:     /* Set the application name and version */
                    136:     if (AppName) {
                    137:        char *ptr;
                    138:        StrAllocCopy(HTAppName, AppName);
                    139:        ptr = HTAppName;
                    140:        while (*ptr) {
                    141:            if (WHITE(*ptr)) *ptr = '_';
                    142:            ptr++;
                    143:        }
                    144:     }
                    145:     if (AppVersion) {
                    146:        char *ptr;
                    147:        StrAllocCopy(HTAppVersion, AppVersion);
                    148:        ptr = HTAppVersion;
                    149:        while (*ptr) {
                    150:            if (WHITE(*ptr)) *ptr = '_';
                    151:            ptr++;
                    152:        }
                    153:     }
                    154: 
                    155:     HTBind_init();                                   /* Initialize bindings */
1.61      frystyk   156: 
1.62      frystyk   157: #ifdef WWWLIB_SIG
1.61      frystyk   158:     /* On Solaris (and others?) we get a BROKEN PIPE signal when connecting
1.67      frystyk   159:     ** to a port where we should get `connection refused'. We ignore this 
1.61      frystyk   160:     ** using the following function call
                    161:     */
                    162:     HTSetSignal();                                /* Set signals in library */
1.1       timbl     163: #endif
                    164: 
1.105     frystyk   165: #ifdef WWW_WIN_ASYNC
                    166:     if (!RegisterClass(&wc)) {
1.115     eric      167:        HTTrace("HTEvent_Loop.. Can't RegisterClass \"%s\"\n", className);
1.105     frystyk   168:            return NO;
                    169:     }
1.113     frystyk   170:     if (!(htSocketWin = CreateWindow(className, "WWW_WIN_ASYNC", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 
                    171:                                      CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, wc.hInstance,0))) {
                    172:        char space[50];
1.115     eric      173:                HTTrace("HTEvent_Loop.. Can't CreateWindow \"WWW_WIN_ASYNC\" - error:");
1.113     frystyk   174:        sprintf(space, "%ld\n", GetLastError());
1.115     eric      175:        HTTrace(space);
1.105     frystyk   176:        return NO;
                    177:     }
                    178:     HTEvent_setWinHandle (htSocketWin, WM_USER);     /* use first available message since app uses none */
                    179: #endif /* WWW_WIN_ASYNC */
                    180: 
1.108     frystyk   181: #ifdef _WINSOCKAPI_
1.67      frystyk   182:     /*
                    183:     ** Initialise WinSock DLL. This must also be shut down! PMH
                    184:     */
                    185:     {
                    186:         WSADATA            wsadata;
                    187:        if (WSAStartup(DESIRED_WINSOCK_VERSION, &wsadata)) {
1.91      frystyk   188:            if (WWWTRACE)
1.115     eric      189:                HTTrace("WWWLibInit.. Can't initialize WinSoc\n");
1.67      frystyk   190:             WSACleanup();
                    191:             return NO;
                    192:         }
                    193:         if (wsadata.wVersion < MINIMUM_WINSOCK_VERSION) {
1.91      frystyk   194:             if (WWWTRACE)
1.115     eric      195:                HTTrace("WWWLibInit.. Bad version of WinSoc\n");
1.67      frystyk   196:             WSACleanup();
                    197:             return NO;
                    198:         }
                    199:     }
1.108     frystyk   200: #endif /* _WINSOCKAPI_ */
1.67      frystyk   201: 
1.71      frystyk   202: #ifndef NO_TIMEGM
                    203:     HTGetTimeZoneOffset();        /* Find offset from GMT if using mktime() */
                    204: #endif
1.70      frystyk   205:     HTTmp_setRoot(NULL);                    /* Set up default tmp directory */
1.61      frystyk   206:     return YES;
                    207: }
                    208: 
                    209: 
1.90      frystyk   210: /*     HTLibTerminate
                    211: **     --------------
1.61      frystyk   212: **     This function frees memory kept by the Library and should be called
1.63      frystyk   213: **     before exit of an application (if you are on a PC platform)
1.61      frystyk   214: */
1.101     frystyk   215: PUBLIC BOOL HTLibTerminate (void)
1.61      frystyk   216: {
1.116   ! frystyk   217:     if (WWWTRACE) HTTrace("WWWLibTerm.. Cleaning up LIBRARY OF COMMON CODE\n");
        !           218: 
        !           219:     HT_FREE(HTAppName);                /* Freed thanks to Wade Ogden <wade@ebt.com> */
        !           220:     HT_FREE(HTAppVersion);
        !           221: 
1.101     frystyk   222:     HTAtom_deleteAll();                                         /* Remove the atoms */
                    223:     HTDNS_deleteAll();                         /* Remove the DNS host cache */
                    224:     HTAnchor_deleteAll(NULL);          /* Delete anchors and drop hyperdocs */
1.73      frystyk   225: 
1.81      frystyk   226:     HTProtocol_deleteAll();  /* Remove bindings between access and protocols */
1.73      frystyk   227:     HTBind_deleteAll();            /* Remove bindings between suffixes, media types */
                    228: 
1.77      frystyk   229:     HTFreeHostName();                      /* Free up some internal strings */
1.63      frystyk   230:     HTFreeMailAddress();
1.70      frystyk   231:     HTTmp_freeRoot();
1.67      frystyk   232: 
1.108     frystyk   233: #ifdef _WINSOCKAPI_
1.67      frystyk   234:     WSACleanup();
1.105     frystyk   235: #endif
                    236: 
                    237: #ifdef WWW_WIN_ASYNC
                    238:     DestroyWindow(HTEvent_getWinHandle(0));
1.67      frystyk   239: #endif
                    240: 
1.61      frystyk   241:     return YES;
                    242: }
                    243: 
1.59      frystyk   244: /* --------------------------------------------------------------------------*/
1.104     frystyk   245: /*                             Load Access functions                        */
1.59      frystyk   246: /* --------------------------------------------------------------------------*/
1.33      luotonen  247: 
1.90      frystyk   248: /*     Request a document
                    249: **     -----------------
                    250: **     Private version that requests a document from the request manager
                    251: **     Returns YES if request accepted, else NO
1.88      frystyk   252: */
1.101     frystyk   253: PRIVATE BOOL HTLoadDocument (HTRequest * request, BOOL recursive)
1.88      frystyk   254: {
                    255:     if (PROT_TRACE) {
1.90      frystyk   256:        HTParentAnchor *anchor = HTRequest_anchor(request);
                    257:        char * full_address = HTAnchor_address((HTAnchor *) anchor);
1.115     eric      258:        HTTrace("HTAccess.... Accessing document %s\n", full_address);
1.114     frystyk   259:        HT_FREE(full_address);
1.88      frystyk   260:     }
1.96      frystyk   261:     return HTLoad(request, recursive);
1.58      frystyk   262: }
1.1       timbl     263: 
                    264: 
1.90      frystyk   265: /*     Request a document from absolute name
                    266: **     -------------------------------------
                    267: **     Request a document referencd by an absolute URL.
                    268: **     Returns YES if request accepted, else NO
                    269: */
                    270: PUBLIC BOOL HTLoadAbsolute (CONST char * url, HTRequest* request)
                    271: {
                    272:     if (url && request) {
                    273:        HTAnchor * anchor = HTAnchor_findAddress(url);
                    274:        HTRequest_setAnchor(request, anchor);
                    275:        return HTLoadDocument(request, NO);
                    276:     }
                    277:     return NO;
                    278: }
                    279: 
                    280: 
                    281: /*     Request a document from absolute name to stream
                    282: **     -----------------------------------------------
                    283: **     Request a document referencd by an absolute URL and sending the data
                    284: **     down a stream. This is _excactly_ the same as HTLoadAbsolute as
                    285: **     the ourputstream is specified using the function
                    286: **     HTRequest_setOutputStream(). 'filter' is ignored!
                    287: **     Returns YES if request accepted, else NO
                    288: */
                    289: PUBLIC BOOL HTLoadToStream (CONST char * url, BOOL filter, HTRequest *request)
                    290: {
                    291:     return HTLoadAbsolute(url, request);
                    292: }
                    293: 
                    294: 
                    295: /*     Request a document from relative name
                    296: **     -------------------------------------
                    297: **     Request a document referenced by a relative URL. The relative URL is 
                    298: **     made absolute by resolving it relative to the address of the 'base' 
                    299: **     anchor.
                    300: **     Returns YES if request accepted, else NO
                    301: */
                    302: PUBLIC BOOL HTLoadRelative (CONST char *       relative,
                    303:                            HTParentAnchor *    base,
                    304:                            HTRequest *         request)
                    305: {
                    306:     BOOL status = NO;
                    307:     if (relative && base && request) {
                    308:        char * rel = NULL;
                    309:        char * full_url = NULL;
                    310:        char * base_url = HTAnchor_address((HTAnchor *) base);
                    311:        StrAllocCopy(rel, relative);
                    312:        full_url = HTParse(HTStrip(rel), base_url,
                    313:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    314:        status = HTLoadAbsolute(full_url, request);
1.114     frystyk   315:        HT_FREE(rel);
                    316:        HT_FREE(full_url);
                    317:        HT_FREE(base_url);
1.90      frystyk   318:     }
                    319:     return status;
                    320: }
                    321: 
                    322: 
                    323: /*     Request an anchor
                    324: **     -----------------
                    325: **     Request the document referenced by the anchor
                    326: **     Returns YES if request accepted, else NO
                    327: */
                    328: PUBLIC BOOL HTLoadAnchor (HTAnchor * anchor, HTRequest * request)
                    329: {
                    330:     if (anchor && request) {
                    331:        HTRequest_setAnchor(request, anchor);
                    332:        return HTLoadDocument(request, NO);
                    333:     }
                    334:     return NO;
                    335: }
                    336: 
                    337: 
                    338: /*     Request an anchor
                    339: **     -----------------
                    340: **     Same as HTLoadAnchor but any information in the Error Stack in the 
                    341: **     request object is kept, so that any error messages in one 
1.52      frystyk   342: **     This function is almost identical to HTLoadAnchor, but it doesn't
                    343: **     clear the error stack so that the information in there is kept.
1.90      frystyk   344: **     Returns YES if request accepted, else NO
                    345: */
                    346: PUBLIC BOOL HTLoadAnchorRecursive (HTAnchor * anchor, HTRequest * request)
                    347: {
                    348:     if (anchor && request) {
                    349:        HTRequest_setAnchor(request, anchor);
                    350:         return HTLoadDocument(request, YES);
                    351:     }
                    352:     return NO;
                    353: }
                    354: 
                    355: 
                    356: /*     Search an Anchor
                    357: **     ----------------
                    358: **     Performs a keyword search on word given by the user. Adds the keyword
                    359: **     to the end of the current address and attempts to open the new address.
                    360: **     The list of keywords must be a space-separated list and spaces will
                    361: **     be converted to '+' before the request is issued.
                    362: **     Search can also be performed by HTLoadAbsolute() etc.
                    363: **     Returns YES if request accepted, else NO
                    364: */
                    365: PUBLIC BOOL HTSearch (CONST char *     keywords,
                    366:                      HTParentAnchor *  base,
                    367:                      HTRequest *       request)
                    368: {
1.99      frystyk   369:     BOOL status = NO;
1.90      frystyk   370:     if (keywords && base && request) {
                    371:        char *base_url = HTAnchor_address((HTAnchor *) base);
                    372:        if (*keywords) {
                    373:            char *plus;
                    374:            StrAllocCat(base_url, "?");
                    375:            StrAllocCat(base_url, keywords);
                    376:            plus = strchr(base_url, '?');
                    377:            while (*plus) {
                    378:                if (*plus == ' ') *plus = '+';
                    379:                plus++;
                    380:            }
1.2       timbl     381:        }
1.99      frystyk   382:        status = HTLoadAbsolute(base_url, request);
1.114     frystyk   383:        HT_FREE(base_url);
1.90      frystyk   384:     }
1.99      frystyk   385:     return status;
1.2       timbl     386: }
                    387: 
                    388: 
1.90      frystyk   389: /*     Search a document from absolute name
                    390: **     ------------------------------------
                    391: **     Request a document referencd by an absolute URL appended with the
                    392: **     keywords given. The URL can NOT contain any fragment identifier!
                    393: **     The list of keywords must be a space-separated list and spaces will
                    394: **     be converted to '+' before the request is issued.
                    395: **     Returns YES if request accepted, else NO
                    396: */
                    397: PUBLIC BOOL HTSearchAbsolute (CONST char *     keywords,
                    398:                              CONST char *      url,
                    399:                              HTRequest *       request)
                    400: {
                    401:     if (url && request) {
                    402:        HTAnchor * anchor = HTAnchor_findAddress(url);
                    403:        return HTSearch(keywords, HTAnchor_parent(anchor), request);
                    404:     }
                    405:     return NO;
1.57      howcome   406: }
                    407: 
1.70      frystyk   408: /* --------------------------------------------------------------------------*/
1.104     frystyk   409: /*                             Post Access Functions                        */
1.70      frystyk   410: /* --------------------------------------------------------------------------*/
                    411: 
1.90      frystyk   412: /*     Copy an anchor
1.70      frystyk   413: **     --------------
1.90      frystyk   414: **     Fetch the URL (possibly local file URL) and send it using either PUT
                    415: **     or POST to the remote destination using HTTP. The caller can decide the
                    416: **     exact method used and which HTTP header fields to transmit by setting
                    417: **     the user fields in the request structure.
1.92      frystyk   418: **     If posting to NNTP then we can't dispatch at this level but must pass
                    419: **     the source anchor to the news module that then takes all the refs
                    420: **     to NNTP and puts into the "newsgroups" header
1.90      frystyk   421: **     Returns YES if request accepted, else NO
1.70      frystyk   422: */
1.109     frystyk   423: PUBLIC BOOL HTCopyAnchor (HTAnchor * src_anchor, HTRequest * main_dest)
1.80      frystyk   424: { 
1.106     frystyk   425:     HTRequest * src_req;
                    426:     HTList * cur;
1.109     frystyk   427:     if (!src_anchor || !main_dest) {
1.115     eric      428:        if (WWWTRACE) HTTrace("Copy........ BAD ARGUMENT\n");
1.90      frystyk   429:        return NO;
1.109     frystyk   430:     }
1.70      frystyk   431: 
1.112     frystyk   432:     /* Set the source anchor */
                    433:     main_dest->source_anchor = HTAnchor_parent(src_anchor);
                    434: 
1.80      frystyk   435:     /* Build the POST web if not already there */
1.109     frystyk   436:     if (!main_dest->source) {
                    437:        src_req = HTRequest_dupInternal(main_dest);       /* Get a duplicate */
1.80      frystyk   438:        HTAnchor_clearHeader((HTParentAnchor *) src_anchor);
1.109     frystyk   439:        src_req->method = METHOD_GET;
1.85      frystyk   440:        src_req->reload = HT_MEM_REFRESH;
1.104     frystyk   441:        src_req->output_stream = NULL;
1.80      frystyk   442:        src_req->output_format = WWW_SOURCE;     /* We want source (for now) */
                    443: 
                    444:        /* Set up the main link in the source anchor */
                    445:        {
1.106     frystyk   446:            HTLink * main_link = HTAnchor_mainLink((HTAnchor *) src_anchor);
                    447:            HTAnchor *main_anchor = HTLink_destination(main_link);
                    448:            HTMethod method = HTLink_method(main_link);
1.85      frystyk   449:            if (!main_link || method==METHOD_INVALID) {
1.91      frystyk   450:                if (WWWTRACE)
1.115     eric      451:                    HTTrace("Copy Anchor. No destination found or unspecified method\n");
1.80      frystyk   452:                HTRequest_delete(src_req);
1.90      frystyk   453:                return NO;
1.80      frystyk   454:            }
1.109     frystyk   455:            main_dest->GenMask |= HT_G_DATE;             /* Send date header */
                    456:            main_dest->reload = HT_CACHE_REFRESH;
                    457:            main_dest->method = method;
                    458:            main_dest->input_format = WWW_SOURCE;
                    459:            HTRequest_addDestination(src_req, main_dest);
                    460:            if (HTLoadAnchor(main_anchor, main_dest) == NO)
                    461:                return NO;
1.80      frystyk   462:        }
1.78      frystyk   463: 
1.80      frystyk   464:        /* For all other links in the source anchor */
1.106     frystyk   465:        if ((cur = HTAnchor_subLinks(src_anchor))) {
                    466:            HTLink * pres;
1.109     frystyk   467:            while ((pres = (HTLink *) HTList_nextObject(cur))) {
1.106     frystyk   468:                HTAnchor *dest = HTLink_destination(pres);
                    469:                HTMethod method = HTLink_method(pres);
1.80      frystyk   470:                HTRequest *dest_req;
                    471:                if (!dest || method==METHOD_INVALID) {
1.91      frystyk   472:                    if (WWWTRACE)
1.115     eric      473:                        HTTrace("Copy Anchor. Bad anchor setup %p\n",
1.80      frystyk   474:                                dest);
1.90      frystyk   475:                    return NO;
1.80      frystyk   476:                }
1.109     frystyk   477:                dest_req = HTRequest_dupInternal(main_dest);
1.107     frystyk   478:                dest_req->GenMask |= HT_G_DATE;          /* Send date header */
1.85      frystyk   479:                dest_req->reload = HT_CACHE_REFRESH;
1.80      frystyk   480:                dest_req->method = method;
1.104     frystyk   481:                dest_req->output_stream = NULL;
                    482:                dest_req->output_format = WWW_SOURCE;
1.109     frystyk   483:                HTRequest_addDestination(src_req, dest_req);
1.104     frystyk   484: 
1.90      frystyk   485:                if (HTLoadAnchor(dest, dest_req) == NO)
                    486:                    return NO;
1.80      frystyk   487:            }
                    488:        }
                    489:     } else {                    /* Use the existing Post Web and restart it */
1.109     frystyk   490:        src_req = main_dest->source;
1.80      frystyk   491:        if (src_req->mainDestination)
1.109     frystyk   492:            if (HTLoadDocument(main_dest, NO) == NO)
1.90      frystyk   493:                return NO;
1.80      frystyk   494:        if (src_req->destinations) {
1.106     frystyk   495:            HTRequest * pres;
                    496:            cur = HTAnchor_subLinks(src_anchor);
1.80      frystyk   497:            while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) {
1.90      frystyk   498:                if (HTLoadDocument(pres, NO) == NO)
                    499:                    return NO;
1.80      frystyk   500:            }
                    501:        }
1.78      frystyk   502:     }
                    503: 
1.80      frystyk   504:     /* Now open the source */
                    505:     return HTLoadAnchor(src_anchor, src_req);
1.70      frystyk   506: }
                    507: 
                    508: 
1.90      frystyk   509: /*     Upload an Anchor
1.70      frystyk   510: **     ----------------
1.111     frystyk   511: **     This function can be used to send data along with a request to a remote
                    512: **     server. It can for example be used to POST form data to a remote HTTP
                    513: **     server - or it can be used to post a newsletter to a NNTP server. In
                    514: **     either case, you pass a callback function which the request calls when
                    515: **     the remote destination is ready to accept data. In this callback
                    516: **     you get the current request object and a stream into where you can 
                    517: **     write data. It is very important that you return the value returned
                    518: **     by this stream to the Library so that it knows what to do next. The
                    519: **     reason is that the outgoing stream might block or an error may
                    520: **     occur and in that case the Library must know about it. The source
                    521: **     anchor represents the data object in memory and it points to 
                    522: **     the destination anchor by using the POSTWeb method. The source anchor
                    523: **     contains metainformation about the data object in memory and the 
                    524: **     destination anchor represents the reponse from the remote server.
1.90      frystyk   525: **     Returns YES if request accepted, else NO
                    526: */
1.111     frystyk   527: PUBLIC BOOL HTUploadAnchor (HTAnchor *         source_anchor,
                    528:                            HTRequest *         request,
                    529:                            HTPostCallback *    callback)
                    530: {
                    531:     HTLink * link = HTAnchor_mainLink((HTAnchor *) source_anchor);
                    532:     HTAnchor * dest_anchor = HTLink_destination(link);
                    533:     HTMethod method = HTLink_method(link);
                    534:     if (!link || method==METHOD_INVALID || !callback) {
                    535:        if (WWWTRACE)
1.115     eric      536:            HTTrace("Upload...... No destination found or unspecified method\n");
1.90      frystyk   537:        return NO;
1.109     frystyk   538:     }
1.111     frystyk   539:     request->GenMask |= HT_G_DATE;                      /* Send date header */
                    540:     request->reload = HT_CACHE_REFRESH;
                    541:     request->method = method;
                    542:     request->source_anchor = HTAnchor_parent(source_anchor);
                    543:     request->PostCallback = callback;
                    544:     return HTLoadAnchor(dest_anchor, request);
                    545: }
                    546: 
                    547: /*     POST Callback Handler
                    548: **     ---------------------
                    549: **     If you do not want to handle the stream interface on your own, you
                    550: **     can use this function which writes the source anchor hyperdoc to the
                    551: **     target stream for the anchor upload and also handles the return value
                    552: **     from the stream. If you don't want to write the source anchor hyperdoc
                    553: **     then you can register your own callback function that can get the data
                    554: **     you want.
                    555: */
                    556: PUBLIC int HTUpload_callback (HTRequest * request, HTStream * target)
                    557: {
1.115     eric      558:     if (WWWTRACE) HTTrace("Uploading... from callback function\n");
1.111     frystyk   559:     if (!request || !request->source_anchor || !target) return HT_ERROR;
                    560:     {
                    561:        int status;
                    562:        HTParentAnchor * source = request->source_anchor;
                    563:        char * document = (char *) HTAnchor_document(request->source_anchor);
                    564:        int len = HTAnchor_length(source);
                    565:        if (len < 0) {
                    566:            len = strlen(document);
                    567:            HTAnchor_setLength(source, len);
                    568:        }
                    569:        status = (*target->isa->put_block)(target, document, len);
                    570:        if (status == HT_OK)
                    571:            return (*target->isa->flush)(target);
                    572:        if (status == HT_WOULD_BLOCK) {
1.115     eric      573:            if (PROT_TRACE)HTTrace("POST Anchor. Target WOULD BLOCK\n");
1.111     frystyk   574:            return HT_WOULD_BLOCK;
                    575:        } else if (status == HT_PAUSE) {
1.115     eric      576:            if (PROT_TRACE) HTTrace("POST Anchor. Target PAUSED\n");
1.111     frystyk   577:            return HT_PAUSE;
                    578:        } else if (status > 0) {              /* Stream specific return code */
                    579:            if (PROT_TRACE)
1.115     eric      580:                HTTrace("POST Anchor. Target returns %d\n", status);
1.111     frystyk   581:            return status;
                    582:        } else {                                     /* We have a real error */
1.115     eric      583:            if (PROT_TRACE) HTTrace("POST Anchor. Target ERROR\n");
1.111     frystyk   584:            return status;
                    585:        }
1.70      frystyk   586:     }
1.1       timbl     587: }

Webmaster