Annotation of Amaya/amaya/query.c, revision 1.200

1.14      cvs         1: /*
                      2:  *
                      3:  *  (c) COPYRIGHT MIT and INRIA, 1996.
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
1.156     cvs         5:  * 
1.14      cvs         6:  */
1.4       cvs         7: 
1.19      cvs         8: /*
1.83      cvs         9:  * query.c : contains all the functions for requesting and publishing
1.19      cvs        10:  * URLs via libwww. It handles any eventual HTTP error code
                     11:  * (redirection, authentication needed, not found, etc.)
                     12:  *
1.69      cvs        13:  * Author: J. Kahan
1.92      cvs        14:  *         J. Kahan/R. Guetari Windows 95/NT routines
1.19      cvs        15:  */
1.17      cvs        16: 
1.44      cvs        17: #ifndef AMAYA_JAVA
                     18: 
1.116     cvs        19: /* defines to include elsewhere
                     20: *********************************/
                     21: 
1.185     cvs        22: #ifdef _WINDOWS
                     23: #include <string.h>
1.130     cvs        24: #endif /* !_WINDOWS */
1.116     cvs        25: #define AMAYA_WWW_CACHE
1.168     cvs        26: #define AMAYA_LOST_UPDATE
1.116     cvs        27: 
1.198     cvs        28: #if defined(_I18N_) || defined(__JIS__)
                     29: #   ifdef _WINDOWS
                     30: #         define CACHE_DIR_NAME L"\\libwww-cache\\"
                     31: #   else
                     32: #         define CACHE_DIR_NAME L"/libwww-cache/"
                     33: #   endif /* _WINDOWS */
                     34: #else /*  */
                     35: #     ifdef _WINDOWS
                     36: #           define CACHE_DIR_NAME "\\libwww-cache\\"
                     37: #     else
                     38: #           define CACHE_DIR_NAME "/libwww-cache/"
                     39: #     endif /* _WINDOWS */
                     40: #endif /*  */
                     41: 
1.176     cvs        42: #define DEFAULT_CACHE_SIZE 10
                     43: #define DEFAULT_MAX_CACHE_ENTRY_SIZE 3
1.177     cvs        44: #define DEFAULT_MAX_SOCKET 32
1.176     cvs        45: #define DEFAULT_DNS_TIMEOUT 1800L
1.151     cvs        46: #define DEFAULT_PERSIST_TIMEOUT 60L
1.165     cvs        47: #define DEFAULT_NET_EVENT_TIMEOUT 60000
1.116     cvs        48: 
1.12      cvs        49: /* Amaya includes  */
1.25      cvs        50: #define THOT_EXPORT extern
1.4       cvs        51: #include "amaya.h"
1.130     cvs        52: #include <sys/types.h>
1.70      cvs        53: #include <fcntl.h>
1.140     cvs        54: #include "HTEvtLst.h"
1.157     cvs        55: #include "HTAABrow.h"
1.7       cvs        56: 
1.141     cvs        57: #if defined(__svr4__) || defined (_AIX)
1.4       cvs        58: #define CATCH_SIG
                     59: #endif
                     60: 
                     61: /* local structures coming from libwww and which are
1.17      cvs        62:    not found in any .h file
1.7       cvs        63:  */
1.68      cvs        64: 
1.7       cvs        65: struct _HTStream
                     66:   {
1.15      cvs        67:      const HTStreamClass *isa;
                     68:      FILE               *fp;
1.45      cvs        69:      BOOL                leave_open;   /* Close file when TtaFreeMemory? */
1.15      cvs        70:      char               *end_command;  /* Command to execute       */
                     71:      BOOL                remove_on_close;      /* Remove file?             */
                     72:      char               *filename;     /* Name of file             */
                     73:      HTRequest          *request;      /* saved for callback       */
                     74:      HTRequestCallback  *callback;
1.7       cvs        75:   };
                     76: 
1.15      cvs        77: 
1.7       cvs        78: struct _HTError
                     79:   {
                     80:      HTErrorElement      element;      /* Index number into HTError */
                     81:      HTSeverity          severity;     /* A la VMS */
                     82:      BOOL                ignore;       /* YES if msg should not go to user */
                     83:      void               *par;  /* Explanation, e.g. filename  */
                     84:      int                 length;       /* For copying by generic routine */
                     85:      char               *where;        /* Which function */
                     86:   };
                     87: 
1.4       cvs        88: /* Type definitions and global variables etc. local to this module */
                     89: 
1.17      cvs        90: /*----------------------------------------------------------------------*/
                     91: 
1.4       cvs        92: /*** private variables ***/
1.17      cvs        93: 
1.4       cvs        94: static HTList      *converters = NULL; /* List of global converters */
1.27      cvs        95: static HTList      *acceptTypes = NULL; /* List of types for the Accept header */
1.193     cvs        96: static HTList      *acceptLanguages = NULL; /* List of language priorities for the Accept header */
1.151     cvs        97: static HTList      *transfer_encodings = NULL;
                     98: static HTList      *content_encodings = NULL;
1.16      cvs        99: static int          object_counter = 0;        /* loaded objects counter */
1.191     cvs       100: static  ThotBool    AmayaAlive_flag; /* set to 1 if the application is active;
1.140     cvs       101:                                        0 if we have killed */
1.191     cvs       102: static  ThotBool    CanDoStop_flag; /* set to 1 if we can do a stop, 0
1.140     cvs       103:                                       if we're inside a critical section */
1.128     cvs       104: #ifdef  AMAYA_WWW_CACHE
1.130     cvs       105: static int          fd_cachelock; /* open handle to the .lock cache file */
                    106: #endif /* AMAYA_WWW_CACHE */
1.4       cvs       107: 
1.15      cvs       108: #include "answer_f.h"
                    109: #include "query_f.h"
                    110: #include "AHTURLTools_f.h"
                    111: #include "AHTBridge_f.h"
                    112: #include "AHTMemConv_f.h"
                    113: #include "AHTFWrite_f.h"
1.4       cvs       114: 
1.116     cvs       115: /* prototypes */
                    116: 
                    117: #ifdef __STDC__
1.198     cvs       118: static void RecCleanCache (STRING dirname);
1.123     cvs       119: #ifdef _WINDOWS
1.70      cvs       120: int WIN_Activate_Request (HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar*);
1.116     cvs       121: #endif /* _WINDOWS */
1.70      cvs       122: #else
1.128     cvs       123: static void RecCleanCache (/* char *dirname */);
1.123     cvs       124: #ifdef _WINDOWS
1.128     cvs       125: int WIN_Activate_Request (/* HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar* */);
1.116     cvs       126: #endif /* _WINDOWS */
1.70      cvs       127: #endif /* __STDC__ */
1.116     cvs       128: 
1.15      cvs       129: 
1.130     cvs       130: #ifdef AMAYA_WWW_CACHE
                    131: /***************************************************************
                    132:  lock functions, used to avoid concurrent use of the cache
                    133:  Mostly based on W. Richard Stevens' APUE book.
                    134:  ***************************************************************/
                    135: /*----------------------------------------------------------------------
                    136:   set_cachelock
                    137:   sets a write lock on filename. 
                    138:   Returns -1 in case of failure, otherwise, it'll open a handle on
                    139:   filename and fd_cachelock will take its value.
                    140:   ----------------------------------------------------------------------*/
                    141: #ifdef __STDC__
1.198     cvs       142: static int set_cachelock (STRING filename)
1.130     cvs       143: #else        
                    144: static int set_cachelock (filename)
1.198     cvs       145: STRING filename;
1.130     cvs       146: #endif /* __STDC__ */
                    147: {
1.188     cvs       148:   int status;
1.130     cvs       149: #ifdef _WINDOWS
1.188     cvs       150: 
                    151:   status = TtaFileExist (filename);
                    152:   return ((status) ? 0 : -1);
1.130     cvs       153: #else
                    154:   struct flock lock;
                    155:  
                    156:   lock.l_type = F_WRLCK;
                    157:   lock.l_start = 0;
                    158:   lock.l_whence = SEEK_SET;
                    159:   lock.l_len = 0;
                    160:   
                    161:   fd_cachelock = open (filename, O_WRONLY);
                    162:   
                    163:   if (fd_cachelock == -1)
                    164:     status = -1;
                    165:   else
                    166:     status = fcntl(fd_cachelock, F_SETLK, &lock);
                    167:   
                    168:   if (status == -1)
1.190     cvs       169:     {
1.179     cvs       170:       if (fd_cachelock > 0)
1.190     cvs       171:          close (fd_cachelock);
1.179     cvs       172:       fd_cachelock = 0;
1.178     cvs       173:     }
1.130     cvs       174:   return (status);
                    175: #endif /* _WINDOWS */
                    176: }
                    177: 
                    178: /*----------------------------------------------------------------------
                    179:   clear_cachelock
                    180:   remove the write lock set on a filename.
                    181:   It'll close the fd handle on filename and reset *fd to 0.
                    182:   ----------------------------------------------------------------------*/
                    183: #ifdef __STDC__
                    184: static int clear_cachelock (void)
                    185: #else        
                    186: static int clear_cachelock ()
                    187: int *fd;
                    188: #endif /* __STDC__ */
                    189: {
                    190: #ifdef _WINDOWS
                    191:   return 0;
                    192: #else
                    193:   int status;
                    194: 
1.178     cvs       195:   if (!fd_cachelock)
1.130     cvs       196:     return (-1);
                    197:  
1.178     cvs       198:   status = close (fd_cachelock);
1.130     cvs       199:   fd_cachelock = 0;
                    200: 
                    201:   return (status);
                    202: #endif /* _WINDOWS */
                    203: }
                    204: 
                    205: /*----------------------------------------------------------------------
                    206:   test_cachelock
                    207:   returns 0 if a fd is not locked by other process, otherwise 
                    208:   returns the pid of the process who has the lock
                    209:   ----------------------------------------------------------------------*/
                    210: #ifdef __STDC__
1.198     cvs       211: static int test_cachelock (STRING filename)
1.130     cvs       212: #else
                    213: static int test_cachelock (filename)
1.198     cvs       214: STRING filename;
                    215: #endif /* __STDC__ */
1.130     cvs       216: {
                    217: #ifdef _WINDOWS
1.185     cvs       218:   /* if the lock is set, we can't unlink the file under Windows */
1.188     cvs       219:   if (!TtaFileUnlink (filename))
                    220:     return 0;
                    221:   else
1.185     cvs       222:     return -1;
1.130     cvs       223: #else
                    224:   struct flock lock;
                    225:   int fd, status;
                    226: 
                    227:   lock.l_type = F_WRLCK;
                    228:   lock.l_start = 0;
                    229:   lock.l_whence = SEEK_SET;
                    230:   lock.l_len = 0;
                    231: 
                    232:   fd = open (filename, O_WRONLY);
                    233: 
                    234:   if (fd < 0)
                    235:     return (-1);
                    236:   /* is this file locked ? */
                    237:   status = fcntl (fd,  F_GETLK, &lock);
                    238:   close (fd);
                    239: 
                    240:   if (status < 0)
                    241:     return (-1);
                    242: 
                    243:   if (lock.l_type == F_UNLCK)
                    244:     return (0); /* false, region is not locked by another proc */
                    245:   return (lock.l_pid); /* true, return pid of lock owner */
                    246: #endif /* _WINDOWS */
                    247: }
                    248: 
                    249: #endif /* AMAYA_WWW_CACHE */
                    250: 
1.15      cvs       251: /*----------------------------------------------------------------------
1.17      cvs       252:   GetDocIdStatus
                    253:   gets the status associated to a docid                         
1.15      cvs       254:   ----------------------------------------------------------------------*/
                    255: #ifdef __STDC__
                    256: AHTDocId_Status    *GetDocIdStatus (int docid, HTList * documents)
                    257: #else
                    258: AHTDocID_Status    *GetDocIdStatus (docid, documents)
                    259: int                 docid;
                    260: HTList             *documents;
                    261: 
                    262: #endif
                    263: {
                    264:    AHTDocId_Status    *me;
                    265:    HTList             *cur;
                    266: 
                    267:    if (documents)
                    268:      {
                    269:        cur = documents;
                    270: 
                    271:        while ((me = (AHTDocId_Status *) HTList_nextObject (cur)))
                    272:          {
                    273:             if (me->docid == docid)
                    274:                return (me);
1.18      cvs       275:          }
                    276:      }
1.15      cvs       277:    return (AHTDocId_Status *) NULL;
                    278: 
                    279: }
                    280: 
1.5       cvs       281: /*----------------------------------------------------------------------
1.27      cvs       282:   AHTGuessAtom_for
                    283:   Converts an Amaya type descriptor into the equivalent MIME type.
                    284:   ----------------------------------------------------------------------*/
                    285: #ifdef __STDC__
1.198     cvs       286: static  HTAtom *AHTGuessAtom_for (STRING urlName, PicType contentType)
1.27      cvs       287: #else
                    288: static  HTAtom *AHTGuessAtom_for (urlName, contentType)
1.198     cvs       289: STRING  urlName;
1.27      cvs       290: PicType contentType;
                    291: #endif
                    292: {
                    293:  HTAtom           *atom;
1.198     cvs       294:  STRING            filename;
1.160     cvs       295:  HTEncoding        enc = NULL;
                    296:  HTEncoding        cte = NULL;
                    297:  HTLanguage        lang = NULL;
1.50      cvs       298:  double            quality = 1.0;
1.27      cvs       299: 
                    300:  switch (contentType)
                    301:    {
                    302:     case xbm_type:
                    303:       atom = HTAtom_for("image/xbm");
                    304:       break;
                    305:     case eps_type:
1.35      cvs       306:       atom = HTAtom_for("application/postscript");
1.27      cvs       307:       break;
                    308:    case xpm_type:
                    309:       atom = HTAtom_for("image/xpm");
                    310:      break;
                    311:     case gif_type:
                    312:       atom = HTAtom_for("image/gif");
                    313:       break;
                    314:     case jpeg_type:
                    315:       atom = HTAtom_for("image/jpeg");
                    316:       break;
                    317:     case png_type:
                    318:       atom = HTAtom_for("image/png");
                    319:       break;
                    320:    case unknown_type:
                    321:    default:
                    322:      /* 
                    323:      ** Amaya could not detect the type, so 
                    324:      ** we try to use the filename's suffix to do so.
                    325:      */
1.198     cvs       326:      filename = AmayaParseUrl (urlName, _EMPTYSTR_, AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
                    327:      HTBind_getFormat (WideChar2ISO (filename), &atom, &enc, &cte, &lang, &quality);
1.45      cvs       328:      TtaFreeMemory (filename);
1.27      cvs       329:      if (atom ==  WWW_UNKNOWN)
                    330:         /*
                    331:         ** we could not identify the suffix, so we assign it
                    332:         ** a default type
                    333:         */
                    334:         atom = HTAtom_for ("text/html");
                    335:      break;
                    336:    }
                    337: 
                    338:  return atom;
                    339: }
                    340: 
                    341: /*----------------------------------------------------------------------
1.17      cvs       342:   AHTReqContext_new
                    343:   create a new Amaya Context Object and update the global Amaya
                    344:   request status.
1.5       cvs       345:   ----------------------------------------------------------------------*/
1.4       cvs       346: #ifdef __STDC__
                    347: static AHTReqContext *AHTReqContext_new (int docid)
                    348: #else
                    349: static AHTReqContext *AHTReqContext_new (docid)
                    350: int                 docid;
                    351: 
                    352: #endif
                    353: {
                    354:    AHTReqContext      *me;
                    355:    AHTDocId_Status    *docid_status;
                    356: 
                    357:    if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
1.116     cvs       358:      outofmem (__FILE__, "AHTReqContext_new");
                    359: 
                    360:    /* clear the structure */
                    361:    memset ((void *) me, 0, sizeof (AHTReqContext));
1.4       cvs       362: 
1.81      cvs       363:    /* Bind the Context object together with the Request Object */
                    364:    me->request = HTRequest_new ();
                    365:   
1.80      cvs       366:    /* clean the associated file structure) */
1.79      cvs       367:    HTRequest_setOutputStream (me->request, NULL);
1.81      cvs       368:  
1.4       cvs       369:    /* Initialize the other members of the structure */
1.17      cvs       370:    me->reqStatus = HT_NEW; /* initial status of a request */
1.4       cvs       371:    me->docid = docid;
                    372:    HTRequest_setMethod (me->request, METHOD_GET);
                    373:    HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    374:    HTRequest_setContext (me->request, me);
1.116     cvs       375: 
1.36      cvs       376:    /* experimental */
                    377:    me->read_sock = INVSOC;
                    378:    me->write_sock = INVSOC;
                    379:    me->except_sock = INVSOC;
1.4       cvs       380: 
                    381:    /* Update the global context */
                    382:    HTList_appendObject (Amaya->reqlist, (void *) me);
                    383: 
                    384:    docid_status = GetDocIdStatus (docid, Amaya->docid_status);
                    385: 
1.7       cvs       386:    if (docid_status == NULL)
                    387:      {
1.116     cvs       388:        docid_status = (AHTDocId_Status *) 
                    389:          TtaGetMemory (sizeof (AHTDocId_Status));
1.7       cvs       390:        docid_status->docid = docid;
                    391:        docid_status->counter = 1;
                    392:        HTList_addObject (Amaya->docid_status, (void *) docid_status);
                    393:      }
                    394:    else
1.4       cvs       395:       docid_status->counter++;
                    396: 
                    397:    Amaya->open_requests++;
                    398: 
1.74      cvs       399: #ifdef DEBUG_LIBWWW
                    400:    fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
                    401: #endif   
1.116     cvs       402: 
1.4       cvs       403:    return me;
                    404: }
                    405: 
1.5       cvs       406: /*----------------------------------------------------------------------
1.17      cvs       407:   AHTReqContext_delete
                    408:   Delete an Amaya Context Object and update the global Amaya request
                    409:   status.
1.5       cvs       410:   ----------------------------------------------------------------------*/
1.4       cvs       411: 
                    412: #ifdef __STDC__
1.191     cvs       413: ThotBool  AHTReqContext_delete (AHTReqContext * me)
1.4       cvs       414: #else
1.191     cvs       415: ThotBool  AHTReqContext_delete (me)
1.4       cvs       416: AHTReqContext      *me;
                    417: 
                    418: #endif
                    419: {
                    420:    AHTDocId_Status    *docid_status;
                    421: 
1.7       cvs       422:    if (me)
                    423:      {
1.74      cvs       424: #ifdef DEBUG_LIBWWW
1.138     cvs       425:        fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
1.74      cvs       426: #endif   
1.138     cvs       427:        if (Amaya->reqlist)
                    428:         HTList_removeObject (Amaya->reqlist, (void *) me);
                    429:        
                    430:        docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
                    431:        if (docid_status)
                    432:         {
                    433:           docid_status->counter--;
                    434:           
                    435:           if (docid_status->counter == 0)
                    436:             {
                    437:               HTList_removeObject (Amaya->docid_status, (void *) docid_status);
                    438:               TtaFreeMemory ((void *) docid_status);
                    439:             }
                    440:         }
1.190     cvs       441:        /* JK: no longer needed in libwww 5.2.3
                    442:          if (me->method != METHOD_PUT && HTRequest_outputStream (me->request))
                    443:          AHTFWriter_FREE (HTRequest_outputStream (me->request));
                    444:        */
1.138     cvs       445:        HTRequest_delete (me->request);
                    446:        
                    447:        if (me->output && me->output != stdout)
                    448:         {      
1.85      cvs       449: #ifdef DEBUG_LIBWWW       
1.138     cvs       450:           fprintf (stderr, "AHTReqContext_delete: URL is  %s, closing "
                    451:                    "FILE %p\n", me->urlName, me->output); 
1.85      cvs       452: #endif
1.138     cvs       453:           fclose (me->output);
                    454:           me->output = NULL;
                    455:         }
1.79      cvs       456:          
1.138     cvs       457:        if (me->error_stream != (char *) NULL)
                    458:         HT_FREE (me->error_stream);
                    459: #ifndef _WINDOWS
                    460: #ifdef WWW_XWINDOWS    
                    461:        if (me->read_xtinput_id || me->write_xtinput_id ||
                    462:           me->except_xtinput_id)
                    463:         RequestKillAllXtevents(me);
                    464: #endif /* WWW_XWINDOWS */
                    465: #endif /* !_WINDOWS */
                    466:        
                    467:        if (me->reqStatus == HT_ABORT)
                    468:         {
                    469:           if (me->outputfile && me->outputfile[0] != EOS)
                    470:             {
1.198     cvs       471:               TtaFileUnlink (ISO2WideChar (me->outputfile));
1.138     cvs       472:               me->outputfile[0] = EOS;
                    473:             }
                    474:         }
                    475:        
                    476:        if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
                    477:         /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
                    478:            or in PutObjectWWW */
                    479:         {
                    480:           if (me->urlName)
                    481:             TtaFreeMemory (me->urlName);
                    482:           if (me->outputfile)
                    483:             TtaFreeMemory (me->outputfile);
                    484:         }
                    485:        
                    486:        if (me->content_type)
                    487:         TtaFreeMemory (me->content_type);
                    488:        
                    489:        if (me->formdata)
                    490:         HTAssocList_delete (me->formdata);
                    491:        
                    492:        /* to trace bugs */
                    493:        memset ((void *) me, 0, sizeof (AHTReqContext));
                    494:        
                    495:        TtaFreeMemory ((void *) me);
                    496:        
                    497:        Amaya->open_requests--;
                    498:        
                    499:        return TRUE;
1.7       cvs       500:      }
1.15      cvs       501:    return FALSE;
1.4       cvs       502: }
                    503: 
                    504: 
1.15      cvs       505: /*----------------------------------------------------------------------
1.17      cvs       506:   Thread_deleteAll
                    507:   this function deletes the whole list of active threads.           
1.5       cvs       508:   ----------------------------------------------------------------------*/
1.4       cvs       509: #ifdef __STDC__
                    510: static void         Thread_deleteAll (void)
                    511: #else
                    512: static void         Thread_deleteAll ()
                    513: #endif
                    514: {
1.21      cvs       515:   HTList             *cur;
                    516:   AHTReqContext      *me;
                    517:   AHTDocId_Status    *docid_status;
                    518: 
1.74      cvs       519:   if (Amaya && Amaya->reqlist)
                    520:     {
                    521:       if (Amaya->open_requests > 0)
                    522: #ifdef DEBUG_LIBWWW
                    523:       fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
                    524:                       "requests\n", Amaya->open_requests);
                    525: #endif   
                    526:        {
                    527:          cur = Amaya->reqlist;
                    528:          
                    529:          /* erase the requests */
                    530:          while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
                    531:            {
                    532:              if (me->request)
                    533:                {
1.142     cvs       534: #ifndef _WINDOWS
                    535: #ifdef WWW_XWINDOWS 
1.74      cvs       536:                  RequestKillAllXtevents (me);
1.142     cvs       537: #endif /* WWW_XWINDOWS */
1.138     cvs       538: #endif /* !_WINDOWS */
1.140     cvs       539:                  if (!HTRequest_kill (me->request))
                    540:                    AHTReqContext_delete (me);
1.74      cvs       541:                }
                    542:            }           /* while */
                    543:          
                    544:          /* erase the docid_status entities */
                    545:          while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
                    546:            TtaFreeMemory ((void *) docid_status);
                    547:          
                    548:        }                       /* if */
1.84      cvs       549:        
1.74      cvs       550:     }
1.4       cvs       551: }
1.198     cvs       552:  
1.5       cvs       553: /*----------------------------------------------------------------------
1.83      cvs       554:   AHTOpen_file
                    555:   ----------------------------------------------------------------------*/
                    556: #ifdef __STDC__
1.85      cvs       557: int                 AHTOpen_file (HTRequest * request)
1.83      cvs       558: #else
1.85      cvs       559: int                 AHTOpen_file (request)
1.83      cvs       560: HTRequest           *request;
                    561: 
                    562: #endif /* __STDC__ */
                    563: {
                    564:   AHTReqContext      *me;      /* current request */
                    565: 
                    566:   me = HTRequest_context (request);
                    567: 
1.93      cvs       568:   if (!me)
                    569:       return HT_ERROR;
                    570: 
1.83      cvs       571: #ifdef DEBUG_LIBWWW
1.116     cvs       572:   fprintf(stderr, "AHTOpen_file: start for object : %p\n", me);
1.85      cvs       573: #endif /* DEBUG_LIBWWW */
                    574: 
                    575:   if (me->reqStatus == HT_ABORT) 
                    576:     {
                    577: #ifdef DEBUG_LIBWWW
                    578:       fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83      cvs       579: #endif /* DEBUG_LIBWWW */
                    580: 
1.85      cvs       581:       return HT_OK;
                    582:     }
                    583: 
1.136     cvs       584:   if (me->method == METHOD_PUT)
                    585:     {
                    586:       me->reqStatus = HT_WAITING;
                    587:       return HT_OK;
                    588:     }
                    589: 
1.85      cvs       590:   if (HTRequest_outputStream (me->request)) 
                    591:     {
                    592: 
                    593: #ifdef DEBUG_LIBWWW
                    594:       fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
                    595: #endif /* DEBUG_LIBWWW */      
                    596:       return HT_OK;
                    597:     }
                    598: 
                    599: #ifdef DEBUG_LIBWWW
                    600:       fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
                    601: #endif /* DEBUG_LIBWWW */      
                    602: 
1.83      cvs       603:   if (!(me->output) && 
                    604:       (me->output != stdout) && 
                    605: #ifndef _WINDOWS
                    606:       (me->output = fopen (me->outputfile, "w")) == NULL)
                    607:     {
1.116     cvs       608: #else /* !_WINDOWS */
1.83      cvs       609:     (me->output = fopen (me->outputfile, "wb")) == NULL) 
                    610:     {
                    611: #endif /* !_WINDOWS */
                    612: 
1.131     cvs       613:       me->outputfile[0] = EOS; /* file could not be opened */
1.85      cvs       614: #ifdef DEBUG_LIBWWW
                    615:       fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
                    616: #endif
1.83      cvs       617:       TtaSetStatus (me->docid, 1, 
                    618:                    TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
1.198     cvs       619:                    ISO2WideChar (me->outputfile));
1.83      cvs       620:       me->reqStatus = HT_ERR;
                    621:       return (HT_ERROR);
                    622:     }
                    623:          
                    624:   HTRequest_setOutputStream (me->request,
                    625:                             AHTFWriter_new (me->request, 
                    626:                                             me->output, YES));
                    627:   me->reqStatus = HT_WAITING;
                    628:   
                    629:   return HT_OK;
                    630: }
                    631: 
                    632: /*----------------------------------------------------------------------
1.17      cvs       633:   redirection_handler
                    634:   this function is registered to handle permanent and temporary
                    635:   redirections.
                    636:   ----------------------------------------------------------------------*/
1.4       cvs       637: #ifdef __STDC__
1.7       cvs       638: static int          redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       639: #else
                    640: static int          redirection_handler (request, context, status)
                    641: HTRequest          *request;
                    642: HTResponse         *response;
                    643: void               *param;
                    644: int                 status;
                    645: 
                    646: #endif
                    647: {
                    648: 
                    649:    HTAnchor           *new_anchor = HTResponse_redirection (response);
1.7       cvs       650:    AHTReqContext      *me = HTRequest_context (request);
1.4       cvs       651:    HTMethod            method = HTRequest_method (request);
1.198     cvs       652:    STRING              ref;
1.98      cvs       653:    char               *tmp;
1.4       cvs       654: 
1.153     cvs       655:    if (!me)
                    656:      /* if the redirect doesn't come from Amaya, we call libwww's standard
                    657:        redirect filter */
                    658:      return (HTRedirectFilter (request, response, param, status));
                    659: 
1.7       cvs       660:    if (!new_anchor)
                    661:      {
                    662:        if (PROT_TRACE)
                    663:           HTTrace ("Redirection. No destination\n");
                    664:        return HT_OK;
                    665:      }
1.4       cvs       666: 
                    667:    /*
1.166     cvs       668:    ** Only do redirect on GET and HEAD
1.4       cvs       669:     */
1.7       cvs       670:    if (!HTMethod_isSafe (method))
                    671:      {
1.130     cvs       672:        /*
                    673:        ** If we got a 303 See Other then change the method to GET.
                    674:        ** Otherwise ask the user whether we should continue.
                    675:        */
                    676:        if (status == HT_SEE_OTHER) 
                    677:         {
                    678:           if (PROT_TRACE)
                    679:             HTTrace("Redirection. Changing method from %s to GET\n",
                    680:                     HTMethod_name(method));
                    681:           HTRequest_setMethod(request, METHOD_GET);
                    682:         }
                    683:        else 
                    684:         {
                    685:           HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
                    686:           if (prompt)
                    687:             {
                    688:               if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    689:                              NULL, NULL, NULL) != YES)
1.153     cvs       690:                 return HT_OK;
1.130     cvs       691:             }
                    692:         }
1.7       cvs       693:      }
1.4       cvs       694:    /*
                    695:     **  Start new request with the redirect anchor found in the headers.
                    696:     **  Note that we reuse the same request object which means that we must
                    697:     **  keep this around until the redirected request has terminated. It also           
                    698:     **  allows us in an easy way to keep track of the number of redirections
                    699:     **  so that we can detect endless loops.
                    700:     */
1.166     cvs       701: 
1.7       cvs       702:    if (HTRequest_doRetry (request))
                    703:      {
1.174     cvs       704:        /*
                    705:        ** Start request with new credentials 
                    706:        */
                    707: 
1.166     cvs       708:        /* only do a redirect using a network protocol understood by Amaya */
1.198     cvs       709:        if (IsValidProtocol (ISO2WideChar (new_anchor->parent->address)))
1.7       cvs       710:          {
1.166     cvs       711:            /* if it's a valid URL, we try to normalize it */
                    712:             /* We use the pre-redirection anchor as a base name */
1.198     cvs       713:             ref = AmayaParseUrl (ISO2WideChar (new_anchor->parent->address), 
                    714:                                  ISO2WideChar (me->urlName), AMAYA_PARSE_ALL);
1.7       cvs       715:             if (ref)
                    716:               {
1.98      cvs       717:                 HT_FREE (new_anchor->parent->address);
                    718:                 tmp = NULL;
1.198     cvs       719:                 HTSACopy (&tmp, WideChar2ISO (ref));
1.98      cvs       720:                 new_anchor->parent->address = tmp;
                    721:                 TtaFreeMemory (ref);
1.7       cvs       722:               }
1.98      cvs       723:             else
1.166     cvs       724:               return HT_OK; /* We can't redirect anymore */
1.7       cvs       725:          }
1.166     cvs       726:        else
                    727:          return HT_OK; /* We can't redirect anymore */
1.7       cvs       728: 
                    729:        /* update the current file name */
1.130     cvs       730:        if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC)) 
1.7       cvs       731:          {
1.130     cvs       732:            TtaFreeMemory (me->urlName);
1.198     cvs       733:            me->urlName = WideChar2ISO (TtaStrdup (ISO2WideChar (new_anchor->parent->address)));
1.7       cvs       734:          }
                    735:        else
1.130     cvs       736:          /* it's a SYNC mode, so we should keep the urlName */
                    737:          {
                    738:            strncpy (me->urlName, new_anchor->parent->address, 
                    739:                     MAX_LENGTH - 1);
                    740:            me->urlName[MAX_LENGTH - 1] = EOS;
                    741:          }
1.38      cvs       742:        ChopURL (me->status_urlName, me->urlName);
1.116     cvs       743: 
1.190     cvs       744:        /* @@ verify if this is important */
                    745:        /* @@@ new libwww doesn't need this free stream while making
                    746:           a PUT. Is it the case everywhere or just for PUT? */
                    747:        if (me->method != METHOD_PUT 
1.192     cvs       748:            && me->request->orig_output_stream != NULL) 
                    749:          {
                    750:            AHTFWriter_FREE (me->request->orig_output_stream);
                    751:            me->request->orig_output_stream = NULL;
                    752:            if (me->output != stdout) { /* Are we writing to a file? */
1.74      cvs       753: #ifdef DEBUG_LIBWWW
1.192     cvs       754:              fprintf (stderr, "redirection_handler: New URL is  %s, closing "
                    755:                       "FILE %p\n", me->urlName, me->output); 
1.74      cvs       756: #endif 
1.192     cvs       757:              fclose (me->output);
                    758:              me->output = NULL;
                    759:            }
1.79      cvs       760:          }
1.68      cvs       761: 
1.174     cvs       762:        /* tell the user what we're doing */
                    763:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.198     cvs       764:                      ISO2WideChar (me->status_urlName));
1.174     cvs       765: 
                    766: 
1.171     cvs       767:        /*
                    768:        ** launch the request
                    769:        */
1.192     cvs       770:        /* add a link relationship? */
1.174     cvs       771:        /* reset the request status */
1.116     cvs       772:        me->reqStatus = HT_NEW; 
1.105     cvs       773:        /* clear the errors */
1.116     cvs       774:        HTError_deleteAll (HTRequest_error (request));
1.105     cvs       775:        HTRequest_setError (request, NULL);
1.192     cvs       776:        /* clear the authentication credentials, as they get regenerated  */
1.174     cvs       777:        HTRequest_deleteCredentialsAll (request);
                    778:        
1.171     cvs       779:        if (me->method == METHOD_POST 
                    780:          || me->method == METHOD_PUT)  /* PUT, POST etc. */
                    781:        status = HTLoadAbsolute (me->urlName, request);
1.7       cvs       782:        else
1.74      cvs       783:          HTLoadAnchor (new_anchor, request);
1.7       cvs       784:      }
                    785:    else
1.68      cvs       786:      {
1.74      cvs       787:        HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    788:                           NULL, 0, "HTRedirectFilter");
                    789:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
                    790:                     NULL);
1.173     cvs       791:        /* so that we can show the error message */
1.74      cvs       792:        if (me->error_html)
1.173     cvs       793:         DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
                    794:        me->reqStatus = HT_ERR;
1.68      cvs       795:      }
                    796: 
1.4       cvs       797:    /*
1.74      cvs       798:    **  By returning HT_ERROR we make sure that this is the last handler to be
                    799:    **  called. We do this as we don't want any other filter to delete the 
                    800:    **  request object now when we have just started a new one ourselves
                    801:    */
1.4       cvs       802:    return HT_ERROR;
                    803: }
                    804: 
1.151     cvs       805: #ifdef AMAYA_LOST_UPDATE
                    806: /*----------------------------------------------------------------------
                    807:   precondition_handler
                    808:   412 "Precondition failed" handler
                    809:   ----------------------------------------------------------------------*/
                    810: #if __STDC__
                    811: static int          precondition_handler (HTRequest * request, HTResponse * response, void *context, int status)
                    812: #else
                    813: static int          precondition_handler (request, response, context, status)
                    814: HTRequest          *request;
                    815: HTResponse         *response;
                    816: void               *context;
                    817: int                 status;
                    818: #endif /* __STDC__ */
                    819: {
1.168     cvs       820:   AHTReqContext  *me = (AHTReqContext *) HTRequest_context (request);
                    821:   HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
1.191     cvs       822:   ThotBool force_put;
1.151     cvs       823: 
                    824:   if (!me)
                    825:     return HT_OK;              /* not an Amaya request */
                    826: 
1.168     cvs       827:   if (prompt)
                    828:        force_put = (*prompt) (request, HT_A_CONFIRM,  HT_MSG_RULES,
                    829:                              NULL, NULL, NULL);
                    830:   else
                    831:     force_put = NO;
                    832:   
                    833:   if (force_put)
                    834:     {
                    835:       /* start a new PUT request without preconditions */
                    836:       /* @@ do we need to kill the request? */
                    837:       if (me->output && me->output != stdout)
                    838:        {
                    839:          fclose (me->output);
                    840:          me->output = NULL;
                    841:        }
                    842:       /*
                    843:       ** reset the Amaya and libwww request status 
                    844:       */
1.176     cvs       845: 
                    846:       /* start with a new, clean request structure */
                    847:       HTRequest_delete (me->request);
                    848:       me->request = HTRequest_new ();
                    849:       /* clean the associated file structure) */
                    850:       HTRequest_setOutputStream (me->request, NULL);
                    851:       /* Initialize the other members of the structure */
1.171     cvs       852:       HTRequest_setMethod (me->request, METHOD_GET);
1.176     cvs       853:       HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    854:       HTRequest_setContext (me->request, me);
                    855:       HTRequest_setPreemptive (me->request, NO);
                    856:       /*
                    857:       ** Make sure that the first request is flushed immediately and not
                    858:       ** buffered in the output buffer
                    859:       */
                    860:       if (me->mode & AMAYA_FLUSH_REQUEST)
                    861:        HTRequest_setFlush(me->request, YES);
                    862: 
1.171     cvs       863:       /* turn off preconditions */
                    864:       HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                    865:       me->reqStatus = HT_NEW; 
                    866:       /* make the request */      
1.176     cvs       867:       status = HTPutDocumentAnchor (HTAnchor_parent (me->source), 
                    868:                                    me->dest, me->request);
1.168     cvs       869:       /* stop here */
                    870:       return HT_ERROR;
                    871:     }
                    872:   else
                    873:     {
                    874:       /* abort the request */
                    875:       /* @@ should we call StopRequest here? */
                    876:       me->reqStatus = HT_ABORT;
                    877:       /* stop here */
                    878:       return HT_ERROR; 
                    879:     }
                    880: }
1.151     cvs       881: 
1.168     cvs       882: /*----------------------------------------------------------------------
                    883:   check_handler
                    884:   Request HEAD checker filter
                    885:   ----------------------------------------------------------------------*/
                    886: static int check_handler (HTRequest * request, HTResponse * response,
1.151     cvs       887:                            void * param, int status)
                    888: {
1.168     cvs       889:   AHTReqContext  *me = (AHTReqContext *) HTRequest_context (request);
                    890:   HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
1.191     cvs       891:   ThotBool force_put;
1.168     cvs       892: 
                    893:   if (!me)
                    894:     return HT_OK;              /* not an Amaya request */
                    895: 
                    896:   HTRequest_deleteAfter(me->request, check_handler);
                    897:   /*
                    898:   ** If head request showed that the document doesn't exist
                    899:   ** then just go ahead and PUT it. Otherwise ask for help
                    900:   */
                    901:   if (status == HT_ERROR || status == HT_INTERRUPTED || status == HT_TIMEOUT)
                    902:     {
                    903:       /* we'd need to call terminate_handler, to free the resources */
                    904:       /* abort the request */
                    905:       /* @@ should we call StopRequest here? */
                    906:       me->reqStatus = HT_ABORT;
                    907:       /* stop here */
                    908:       return HT_ERROR; 
                    909:     } 
                    910:   else if (status == HT_NO_ACCESS || status == HT_NO_PROXY_ACCESS) 
                    911:     {
                    912:       /* we'd need to call terminate_handler, to free the resources */
                    913:       /* abort the request */
                    914:       /* @@ should we call StopRequest here? */
                    915:       me->reqStatus = HT_ABORT;
                    916:       /* stop here */
                    917:       return HT_ERROR; 
                    918:     }
                    919:   else if ( (abs(status)/100) != 2) 
                    920:     {
1.176     cvs       921:       /* start with a new, clean request structure */
                    922:       HTRequest_delete (me->request);
                    923:       me->request = HTRequest_new ();
                    924:       /* clean the associated file structure) */
                    925:       HTRequest_setOutputStream (me->request, NULL);
                    926:       /* Initialize the other members of the structure */
1.171     cvs       927:       HTRequest_setMethod (me->request, METHOD_GET);
1.176     cvs       928:       HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    929:       HTRequest_setContext (me->request, me);
                    930:       HTRequest_setPreemptive (me->request, NO);
                    931:       /*
                    932:       ** Make sure that the first request is flushed immediately and not
                    933:       ** buffered in the output buffer
                    934:       */
                    935:       if (me->mode & AMAYA_FLUSH_REQUEST)
                    936:        HTRequest_setFlush(me->request, YES);
                    937: 
                    938:       /* turn on the special preconditions, to avoid having this
                    939:         ressource appear before we do the PUT */
1.168     cvs       940:       HTRequest_setPreconditions(me->request, HT_DONT_MATCH_ANY);
1.171     cvs       941:       me->reqStatus = HT_NEW;
1.168     cvs       942:       status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
                    943:       return HT_ERROR; /* stop here */
1.176     cvs       944:     }
1.168     cvs       945:   else 
                    946:     {
                    947:       if (prompt)
                    948:          force_put = (*prompt) (request, HT_A_CONFIRM, HT_MSG_FILE_REPLACE,
                    949:                                 NULL, NULL, NULL);
                    950:       else
                    951:        force_put = FALSE;
                    952: 
                    953:       if (force_put)
1.151     cvs       954:        {
                    955:          /* Start a new PUT request without preconditions */
1.176     cvs       956: 
                    957:          /* get a new, clean request structure */
                    958:          HTRequest_delete (me->request);
                    959:          me->request = HTRequest_new ();
                    960:          /* clean the associated file structure) */
                    961:          HTRequest_setOutputStream (me->request, NULL);
                    962:          /* Initialize the other members of the structure */
1.171     cvs       963:          HTRequest_setMethod (me->request, METHOD_GET);
1.176     cvs       964:          HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    965:          HTRequest_setContext (me->request, me);
                    966:          HTRequest_setPreemptive (me->request, NO);
                    967:          /*
                    968:          ** Make sure that the first request is flushed immediately and not
                    969:          ** buffered in the output buffer
                    970:          */
                    971:          if (me->mode & AMAYA_FLUSH_REQUEST)
                    972:            HTRequest_setFlush(me->request, YES);
                    973:          /* remove the preconditions */
1.168     cvs       974:          HTRequest_setPreconditions(me->request, HT_NO_MATCH);
1.171     cvs       975:          me->reqStatus = HT_NEW; 
1.176     cvs       976:          status = HTPutDocumentAnchor (HTAnchor_parent (me->source), 
                    977:                                        me->dest, me->request);
1.168     cvs       978:          return HT_ERROR; /* stop here */
1.151     cvs       979:        } 
1.168     cvs       980:       else
                    981:        {
                    982:          /* we'd need to call terminate_handler, to free the resources */
                    983:          /* abort the request */
                    984:          /* @@ should we call StopRequest here? */
                    985:          me->reqStatus = HT_ABORT;
                    986:          /* stop here */
                    987:          return HT_ERROR; 
                    988:        }
                    989:     }
                    990:   return HT_ERROR;
1.151     cvs       991: }
                    992: #endif /* AMAYA_LOST_UPDATE */
                    993: 
1.5       cvs       994: /*----------------------------------------------------------------------
1.17      cvs       995:   terminate_handler
                    996:   this function is registered to handle the result of the request
1.5       cvs       997:   ----------------------------------------------------------------------*/
1.4       cvs       998: #if __STDC__
1.7       cvs       999: static int          terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4       cvs      1000: #else
                   1001: static int          terminate_handler (request, response, context, status)
                   1002: HTRequest          *request;
                   1003: HTResponse         *response;
                   1004: void               *context;
                   1005: int                 status;
1.123     cvs      1006: #endif /* __STDC__ */
1.4       cvs      1007: {
1.116     cvs      1008:   AHTReqContext      *me = (AHTReqContext *) HTRequest_context (request);
1.191     cvs      1009:   ThotBool            error_flag;
1.116     cvs      1010:   char               *content_type;
1.184     cvs      1011:   HTParentAnchor     *anchor;
1.116     cvs      1012: 
                   1013:   if (!me)
1.153     cvs      1014:     return HT_ERROR;           /* not an Amaya request */
1.116     cvs      1015: 
                   1016:   if (me->reqStatus == HT_END)
                   1017:     /* we have already processed this request! */
                   1018:     return HT_OK;
                   1019: 
                   1020:   /* if Amaya was killed, treat with this request as if it were
                   1021:      issued by a Stop button event */
                   1022: 
                   1023:    if (!AmayaIsAlive ())           
1.80      cvs      1024:       me->reqStatus = HT_ABORT; 
1.74      cvs      1025:    
1.77      cvs      1026:    if (status == HT_LOADED || 
                   1027:        status == HT_CREATED || 
1.79      cvs      1028:        status == HT_NO_DATA ||
1.160     cvs      1029:        /* kludge for work around libwww problem */
                   1030:        (status == HT_INTERRUPTED && me->method == METHOD_PUT) ||
1.116     cvs      1031: #ifdef AMAYA_WWW_CACHE
                   1032:        /* what status to use to know we're downloading from a cache? */
                   1033:        status ==  HT_NOT_MODIFIED ||
                   1034: #endif /* AMAYA_WWW_CACHE */
1.79      cvs      1035:        me->reqStatus == HT_ABORT)
1.74      cvs      1036:      error_flag = FALSE;
1.13      cvs      1037:    else
1.74      cvs      1038:      error_flag = TRUE;
1.130     cvs      1039: 
1.4       cvs      1040:    /* output any errors from the server */
1.130     cvs      1041: 
1.77      cvs      1042:    /*
1.74      cvs      1043:    ** me->output = output file which will receive an html file
                   1044:    ** me->error_html = yes, output HTML errors in the screen
                   1045:    ** request->error_stack == if there are any errors, they will be here
                   1046:    ** me->error_stream_size If it's != 0 means an error message has already
                   1047:    **                       been written to the stack
                   1048:    */
                   1049:    
1.4       cvs      1050:    /* First, we verify if there are any errors and if they are not
1.17      cvs      1051:    ** yet written to the error stack. If no, then let's try to write them
                   1052:    ** ourselves
                   1053:    */
1.74      cvs      1054:    
                   1055: #ifdef DEBUG_LIBWWW
                   1056:    fprintf (stderr, "terminate_handler: URL is "
                   1057:            "%s, closing FILE %p status is %d\n", me->urlName, me->output, 
                   1058:            status); 
1.69      cvs      1059: #endif
1.74      cvs      1060:    
1.69      cvs      1061:    if (me->output && me->output != stdout)
                   1062:      {
1.74      cvs      1063:        /* we are writing to a file */
                   1064:        if (me->reqStatus != HT_ABORT)
                   1065:         {                      /* if the request was not aborted and */
1.80      cvs      1066:           if (error_flag &&
                   1067:               me->error_html == TRUE)
                   1068:               /* there were some errors and we want to print them */
                   1069:             {          
                   1070:               if (me->error_stream_size == 0)/* and the stream is empty */
                   1071:                 AHTError_MemPrint (request); /* copy errors from 
1.74      cvs      1072:                                                  **the error stack 
1.77      cvs      1073:                                                  ** into the error stream */
1.80      cvs      1074:               if (me->error_stream)
                   1075:                 {      /* if the stream is non-empty */
                   1076:                   fprintf (me->output, me->error_stream);/* output the errors */
1.85      cvs      1077:                   /* Clear the error context, so that we can deal with
                   1078:                      this answer as if it were a normal reply */
                   1079:                    HTError_deleteAll( HTRequest_error (request));
                   1080:                    HTRequest_setError (request, NULL);
                   1081:                    error_flag = 0;
1.74      cvs      1082:                 }
                   1083:             }                  /* if error_stack */
1.85      cvs      1084:         }
                   1085: 
                   1086:        /* if != HT_ABORT */
                   1087:        
                   1088: #ifdef DEBUG_LIBWWW       
                   1089:        fprintf (stderr, "terminate_handler: URL is  %s, closing "
                   1090:                "FILE %p\n", me->urlName, me->output); 
                   1091: #endif
1.74      cvs      1092:        fclose (me->output);
                   1093:        me->output = NULL;
1.69      cvs      1094:      }
1.80      cvs      1095: 
                   1096:    if (error_flag)
                   1097:      me->reqStatus = HT_ERR;
                   1098:    else if (me->reqStatus != HT_ABORT)
                   1099:      me->reqStatus = HT_END;
1.151     cvs      1100:  
1.116     cvs      1101:    /* copy the content_type */
1.151     cvs      1102:    if (!me->content_type)
1.88      cvs      1103:      {
1.184     cvs      1104:        anchor = HTRequest_anchor (request);
                   1105:        if (anchor && HTAnchor_format (anchor))
                   1106:         content_type = HTAtom_name (HTAnchor_format (anchor));
1.154     cvs      1107:        else
                   1108:         content_type = "www/unknown";
                   1109: 
1.151     cvs      1110:        if (content_type && content_type [0] != EOS)
                   1111:         {
                   1112:           /* libwww gives www/unknown when it gets an error. As this is 
                   1113:              an HTML test, we force the type to text/html */
                   1114:           if (!strcmp (content_type, "www/unknown"))
1.198     cvs      1115:             me->content_type = WideChar2ISO (TtaStrdup (TEXT("text/html")));
1.151     cvs      1116:           else
1.198     cvs      1117:             me->content_type = WideChar2ISO (TtaStrdup (ISO2WideChar (content_type)));
1.151     cvs      1118:           
                   1119:           /* Content-Type can be specified by an httpd  server's admin.
                   1120:              To be on the safe side, we normalize its case */
1.198     cvs      1121:           ConvertToLowerCase (ISO2WideChar (me->content_type));
1.151     cvs      1122:           
1.88      cvs      1123: #ifdef DEBUG_LIBWWW
1.151     cvs      1124:           fprintf (stderr, "content type is: %s\n", me->content_type);
1.88      cvs      1125: #endif /* DEBUG_LIBWWW */
1.151     cvs      1126:         } 
                   1127:      }
1.114     cvs      1128: 
1.115     cvs      1129:    /* to avoid a hangup while downloading css files */
1.140     cvs      1130:    if (AmayaAlive_flag && (me->mode & AMAYA_LOAD_CSS))
1.116     cvs      1131:      TtaSetStatus (me->docid, 1, 
                   1132:                   TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1.198     cvs      1133:                   ISO2WideChar (me->status_urlName));
1.116     cvs      1134:    
                   1135:   /* don't remove or Xt will hang up during the PUT */
                   1136:    if (AmayaIsAlive ()  && ((me->method == METHOD_POST) ||
                   1137:                            (me->method == METHOD_PUT)))
1.7       cvs      1138:      {
1.80      cvs      1139:        PrintTerminateStatus (me, status);
                   1140:      } 
1.114     cvs      1141: 
1.111     cvs      1142:    ProcessTerminateRequest (request, response, context, status);
1.116     cvs      1143:    
1.151     cvs      1144:    /* stop here */
1.150     cvs      1145:    return HT_ERROR;
1.4       cvs      1146: }
                   1147: 
1.5       cvs      1148: /*----------------------------------------------------------------------
1.17      cvs      1149:   AHTLoadTerminate_handler
1.74      cvs      1150:   this is an application "AFTER" Callback. It's called by the library
                   1151:   when a request has ended, so that we can setup the correct status.
1.5       cvs      1152:   ----------------------------------------------------------------------*/
1.4       cvs      1153: 
                   1154: #ifdef __STDC__
1.111     cvs      1155: int          AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs      1156: #else
1.111     cvs      1157: int          AHTLoadTerminate_handler (request, response, param, status)
1.4       cvs      1158: HTRequest          *request;
                   1159: HTResponse         *response;
                   1160: void               *param;
                   1161: int                 status;
1.69      cvs      1162: 
1.4       cvs      1163: #endif
                   1164: {
1.116     cvs      1165: 
                   1166:   /** @@@@ use this with printstatus ?? */
                   1167: 
1.4       cvs      1168:    AHTReqContext      *me = HTRequest_context (request);
                   1169:    HTAlertCallback    *cbf;
                   1170:    AHTDocId_Status    *docid_status;
                   1171: 
1.7       cvs      1172:    switch (status)
1.116     cvs      1173:      {
                   1174:         case HT_LOADED:
                   1175:           if (PROT_TRACE)
                   1176:             HTTrace ("Load End.... OK: `%s\' has been accessed\n",
                   1177:                      me->status_urlName);
1.4       cvs      1178: 
1.116     cvs      1179:           docid_status = GetDocIdStatus (me->docid,
                   1180:                                          Amaya->docid_status);
1.7       cvs      1181: 
1.116     cvs      1182:           if (docid_status != NULL && docid_status->counter > 1)
                   1183:             TtaSetStatus (me->docid, 1, 
                   1184:                           TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1.198     cvs      1185:                           ISO2WideChar (me->status_urlName));
1.7       cvs      1186:               break;
1.116     cvs      1187:               
                   1188:      case HT_NO_DATA:
                   1189:        if (PROT_TRACE)
                   1190:         HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", 
                   1191:                  me->status_urlName);
                   1192:        TtaSetStatus (me->docid, 1, 
                   1193:                     TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.198     cvs      1194:                     ISO2WideChar (me->status_urlName));
1.116     cvs      1195:        break;
                   1196:        
                   1197:      case HT_INTERRUPTED:
                   1198:        if (PROT_TRACE)
                   1199:         HTTrace ("Load End.... INTERRUPTED: `%s\'\n", 
                   1200:                  me->status_urlName);
                   1201:        TtaSetStatus (me->docid, 1, 
                   1202:                     TtaGetMessage (AMAYA, AM_LOAD_ABORT), 
                   1203:                     NULL);
                   1204:        break;
1.7       cvs      1205: 
1.116     cvs      1206:      case HT_RETRY:
                   1207:        if (PROT_TRACE)
                   1208:         HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                   1209:                  HTResponse_retryTime (response));
                   1210:        TtaSetStatus (me->docid, 1, 
                   1211:                     TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.198     cvs      1212:                     ISO2WideChar (me->status_urlName));
1.116     cvs      1213:        break;
1.7       cvs      1214: 
1.116     cvs      1215:      case HT_ERROR:
                   1216:        cbf = HTAlert_find (HT_A_MESSAGE);
                   1217:        if (cbf)
                   1218:         (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                   1219:                 HTRequest_error (request), NULL);
                   1220:        break;
                   1221:        
                   1222:        if (PROT_TRACE)
                   1223:         HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
                   1224:                  me->status_urlName ? me->status_urlName :"<UNKNOWN>");
                   1225:        TtaSetStatus (me->docid, 1,
                   1226:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1.198     cvs      1227:                     me->status_urlName ? ISO2WideChar (me->status_urlName) : TEXT("<UNKNOWN>"));
1.116     cvs      1228:        break;
                   1229:      default:
                   1230:        if (PROT_TRACE)
                   1231:         HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
                   1232:        break;
                   1233:      }
                   1234:    
                   1235:    return HT_OK;
                   1236: }
1.7       cvs      1237: 
1.116     cvs      1238: #ifdef DEBUG_LIBWWW
                   1239: static  int LineTrace (const char * fmt, va_list pArgs)
                   1240: {
                   1241:     return (vfprintf(stderr, fmt, pArgs));
1.4       cvs      1242: }
1.116     cvs      1243: #endif DEBUG_LIBWWW
1.4       cvs      1244: 
1.27      cvs      1245: /*----------------------------------------------------------------------
                   1246:   AHTAcceptTypesInit
1.74      cvs      1247:   This function prepares the Accept header used by Amaya during
                   1248:   the HTTP content negotiation phase
1.27      cvs      1249:   ----------------------------------------------------------------------*/
                   1250: #ifdef __STDC__
                   1251: static void           AHTAcceptTypesInit (HTList *c)
                   1252: #else  /* __STDC__ */
                   1253: static void           AHTAcceptTypesInit (c)
                   1254: HTList             *c;
                   1255: #endif /* __STDC__ */
                   1256: {
                   1257:   if (c == (HTList *) NULL) 
                   1258:       return;
                   1259: 
                   1260:       /* define here all the mime types that Amaya can accept */
                   1261: 
1.74      cvs      1262:       HTConversion_add (c, "image/png",  "www/present", 
                   1263:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1264:       HTConversion_add (c, "image/jpeg", "www/present", 
                   1265:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1266:       HTConversion_add (c, "image/gif",  "www/present", 
                   1267:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1268:       HTConversion_add (c, "image/xbm",  "www/present", 
                   1269:                        HTThroughLine, 1.0, 0.0, 0.0);
                   1270:       HTConversion_add (c, "image/xpm",  "www/present", 
                   1271:                        HTThroughLine, 1.0, 0.0, 0.0);
1.183     cvs      1272: 
1.27      cvs      1273:    /* Define here the equivalences between MIME types and file extensions for
                   1274:     the types that Amaya can display */
                   1275: 
1.176     cvs      1276:    /* Initialize suffix bindings for local files */
                   1277:    HTBind_init();
                   1278: 
1.27      cvs      1279:    /* Register the default set of file suffix bindings */
                   1280:    HTFileInit ();
                   1281: 
                   1282:    /* Don't do any case distinction */
                   1283:    HTBind_caseSensitive (FALSE);
                   1284: }
1.4       cvs      1285: 
1.5       cvs      1286: /*----------------------------------------------------------------------
1.193     cvs      1287:   AHTAcceptLanguagesInit
                   1288:   This function prepares the Accept header used by Amaya during
                   1289:   the HTTP content negotiation phase
                   1290:   ----------------------------------------------------------------------*/
                   1291: #ifdef __STDC__
1.196     cvs      1292: static void         AHTAcceptLanguagesInit (HTList *c)
1.193     cvs      1293: #else  /* __STDC__ */
1.196     cvs      1294: static void         AHTAcceptLanguagesInit (c)
1.193     cvs      1295: HTList             *c;
                   1296: #endif /* __STDC__ */
                   1297: {
1.196     cvs      1298:   STRING            ptr;
                   1299:   STRING            lang_list;
                   1300:   CHAR_T            s[3];
                   1301:   int               count, lg;
                   1302:   double            quality;
                   1303:   ThotBool          still;
1.193     cvs      1304: 
                   1305:   if (c == (HTList *) NULL) 
                   1306:       return;
                   1307:   
1.198     cvs      1308:   lang_list = TtaGetEnvString (TEXT("ACCEPT_LANGUAGES"));
1.197     cvs      1309:   s[2] = EOS;
1.196     cvs      1310:   if (lang_list && *lang_list != EOS)
1.193     cvs      1311:     {
                   1312:       /* add the default language first  */
                   1313:       HTLanguage_add (c, "*", -1.0);
                   1314:       /* how many languages do we have? */
1.196     cvs      1315:       ptr = lang_list;
                   1316:       count = 0;
                   1317:       while (*ptr != EOS)
                   1318:        {
                   1319:          while (*ptr != EOS &&
                   1320:                 (*ptr < 'A' || (*ptr > 'Z' && *ptr < 'a') || *ptr > 'z'))
                   1321:            /* skip the whole separator */
                   1322:            ptr++;
                   1323:          lg = 0;
                   1324:          while ((*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= 'a' && *ptr <= 'z') || *ptr == '-')
                   1325:            {
                   1326:              /* it's a new language */
                   1327:              ptr++;
                   1328:              lg++;
                   1329:            }
                   1330:          if (lg >= 2)
1.193     cvs      1331:            count++;
1.196     cvs      1332:          if (*ptr != EOS)
                   1333:            ptr++;
                   1334:        }
1.193     cvs      1335: 
1.196     cvs      1336:       if (count > 0)
1.193     cvs      1337:        quality = 1.1 - (double) count/10.0;
                   1338:       else
                   1339:        quality = 1.0;
                   1340: 
1.196     cvs      1341:       /*
                   1342:        Read the languages from the registry, then inject them one, by one.
                   1343:        The first one is the one with the highest priority.
                   1344:        The libwww ask for the lowest priority first.
                   1345:       */
                   1346:       ptr--;
                   1347:       still = TRUE;
                   1348:       while (count) 
1.193     cvs      1349:        {
1.196     cvs      1350:          while (still &&
                   1351:                 (*ptr < 'A' || (*ptr > 'Z' && *ptr < 'a') || *ptr > 'z'))
                   1352:            /* skip the whole separator */
                   1353:            if (ptr > lang_list)
                   1354:              ptr--;
                   1355:            else
                   1356:              still = FALSE;
                   1357:          lg = 0;
                   1358:          while (still &&
                   1359:                 ((*ptr >= 'A' && *ptr <= 'Z') || (*ptr >= 'a' && *ptr <= 'z') || *ptr == '-'))
                   1360:            {
                   1361:              /* it's a new language */
                   1362:              if (ptr > lang_list)
                   1363:                ptr--;
                   1364:              else
                   1365:                still = FALSE;
                   1366:              lg++;
                   1367:            }
                   1368:          if (lg >= 2)
                   1369:            {
                   1370:              if (still)
                   1371:                ustrncpy (s, &ptr[1], 2);
                   1372:              else
                   1373:                ustrncpy (s, lang_list, 2);
                   1374:              count--;
1.198     cvs      1375:              HTLanguage_add (c, WideChar2ISO(s), quality);
1.196     cvs      1376:              quality += 0.1;
                   1377:            }
                   1378:          ptr--;
1.193     cvs      1379:        }
                   1380:     }
                   1381: }
                   1382: 
                   1383: /*----------------------------------------------------------------------
1.17      cvs      1384:   AHTConverterInit
                   1385:   Bindings between a source media type and a destination media type
                   1386:   (conversion).
1.5       cvs      1387:   ----------------------------------------------------------------------*/
1.15      cvs      1388: #ifdef __STDC__
                   1389: static void         AHTConverterInit (HTList *c)
                   1390: #else  /* __STDC__ */
                   1391: static void         AHTConverterInit (c)
                   1392: HTList             *c;
                   1393: #endif /* __STDC__ */
1.4       cvs      1394: {
                   1395: 
                   1396:    /* Handler for custom http error messages */
1.7       cvs      1397:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs      1398: 
                   1399:    /*
                   1400:     ** These are converters that converts to something other than www/present,
                   1401:     ** that is not directly outputting someting to the user on the screen
                   1402:     */
                   1403: 
1.116     cvs      1404:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 
                   1405:                     1.0, 0.0, 0.0);
1.4       cvs      1406:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
                   1407:                     1.0, 0.0, 0.0);
                   1408:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
                   1409:                     1.0, 0.0, 0.0);
1.116     cvs      1410:    HTConversion_add(c,"message/x-rfc822-cont", "*/*", HTMIMEContinue,  
                   1411:                    1.0, 0.0, 0.0);
                   1412:    HTConversion_add(c,"message/x-rfc822-partial","*/*",        HTMIMEPartial,
                   1413:                    1.0, 0.0, 0.0);
1.4       cvs      1414:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
                   1415:                     1.0, 0.0, 0.0);
                   1416:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
                   1417:                     1.0, 0.0, 0.0);
                   1418: 
                   1419:    /*
1.116     cvs      1420:    ** The following conversions are converting ASCII output from various
                   1421:    ** protocols to HTML objects.
                   1422:    */
1.4       cvs      1423:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
                   1424:                     1.0, 0.0, 0.0);
                   1425: 
                   1426:    /*
1.116     cvs      1427:    ** We also register a special content type guess stream that can figure out
                   1428:    ** the content type by reading the first bytes of the stream
                   1429:    */
1.4       cvs      1430:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
                   1431:                     1.0, 0.0, 0.0);
                   1432: 
1.116     cvs      1433: #ifdef AMAYA_WWW_CACHE
1.4       cvs      1434:    /*
1.116     cvs      1435:    ** Register a persistent cache stream which can save an object to local
                   1436:    ** file
                   1437:    */
1.4       cvs      1438:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
                   1439:                     1.0, 0.0, 0.0);
1.116     cvs      1440:    HTConversion_add(c,"www/cache-append", "*/*", HTCacheAppend,
                   1441:                    1.0, 0.0, 0.0);
                   1442: #endif AMAYA_WWW_CACHE
1.4       cvs      1443: 
                   1444:    /*
1.116     cvs      1445:    ** This dumps all other formats to local disk without any further
                   1446:    ** action taken
                   1447:    */
1.4       cvs      1448:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
1.190     cvs      1449:                     0.3, 0.0, 0.0);  
1.4       cvs      1450: }
                   1451: 
1.27      cvs      1452: 
1.15      cvs      1453: /*----------------------------------------------------------------------
1.17      cvs      1454:   AHTProtocolInit
                   1455:   Registers all amaya supported protocols.
1.15      cvs      1456:   ----------------------------------------------------------------------*/
1.4       cvs      1457: static void         AHTProtocolInit (void)
                   1458: {
1.116     cvs      1459:   char *strptr;
1.4       cvs      1460: 
1.116     cvs      1461:   /* 
                   1462:      NB. Preemptive == YES means Blocking requests
                   1463:      Non-preemptive == NO means Non-blocking requests
                   1464:      */
                   1465:   HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new);
                   1466:   HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new, 
                   1467:                  HTBufferWriter_new);
                   1468:   HTProtocol_add ("http", "buffered_tcp", HTTP_PORT, NO, HTLoadHTTP, NULL);
1.136     cvs      1469: #ifdef _WINDOWS
                   1470:   HTProtocol_add ("file", "local", 0, YES, HTLoadFile, NULL);
                   1471: #else
1.116     cvs      1472:   HTProtocol_add ("file", "local", 0, NO, HTLoadFile, NULL);
1.198     cvs      1473: #endif /* _WINDOWS */
1.116     cvs      1474: #ifdef AMAYA_WWW_CACHE
                   1475:    HTProtocol_add("cache",  "local", 0, YES, HTLoadCache, NULL);
                   1476: #endif /* AMAYA_WWW_CACHE */
1.103     cvs      1477: #if 0 /* experimental code */
1.116     cvs      1478:    HTProtocol_add ("ftp", "tcp", FTP_PORT, NO, HTLoadFTP, NULL);
1.17      cvs      1479: #endif
1.116     cvs      1480: 
                   1481:    /* initialize pipelining */
1.198     cvs      1482:   strptr = (char *) TtaGetEnvString (TEXT ("ENABLE_PIPELINING"));
1.116     cvs      1483:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
                   1484:     HTTP_setConnectionMode (HTTP_11_NO_PIPELINING);
1.4       cvs      1485: }
                   1486: 
1.15      cvs      1487: /*----------------------------------------------------------------------
1.17      cvs      1488:   AHTNetInit
                   1489:   Reegisters "before" and "after" request filters.
1.15      cvs      1490:   ----------------------------------------------------------------------*/
1.4       cvs      1491: static void         AHTNetInit (void)
                   1492: {
                   1493: 
                   1494: /*      Register BEFORE filters
1.74      cvs      1495: **      The BEFORE filters handle proxies, caches, rule files etc.
                   1496: **      The filters are called in the order by which the are registered
                   1497: **      Not done automaticly - may be done by application!
                   1498: */
1.4       cvs      1499: 
1.116     cvs      1500:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, HT_FILTER_LATE);
                   1501:   HTNet_addBefore (HTProxyFilter, NULL, NULL, HT_FILTER_LATE);
1.85      cvs      1502:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1503: 
                   1504: /*      register AFTER filters
1.74      cvs      1505: **      The AFTER filters handle error messages, logging, redirection,
                   1506: **      authentication etc.
                   1507: **      The filters are called in the order by which the are registered
                   1508: **      Not done automaticly - may be done by application!
                   1509: */
1.4       cvs      1510: 
1.116     cvs      1511:   HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS,
                   1512:                  HT_FILTER_MIDDLE);
                   1513:   HTNet_addAfter(HTAuthFilter, "http://*", NULL, HT_REAUTH,
                   1514:                 HT_FILTER_MIDDLE);
                   1515:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT,
                   1516:                  HT_FILTER_MIDDLE);
                   1517:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_FOUND, 
                   1518:                  HT_FILTER_MIDDLE);
                   1519:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_SEE_OTHER,
                   1520:                  HT_FILTER_MIDDLE);
                   1521:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT,
                   1522:                  HT_FILTER_MIDDLE);
1.168     cvs      1523:   HTNet_addAfter(HTAuthInfoFilter,     "http://*", NULL, HT_ALL, 
                   1524:                 HT_FILTER_MIDDLE);
1.116     cvs      1525:   HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY,
                   1526:                  HT_FILTER_MIDDLE);
1.151     cvs      1527: #ifdef AMAYA_LOST_UPDATE
                   1528:   HTNet_addAfter (precondition_handler, NULL, NULL, HT_PRECONDITION_FAILED,
                   1529:                  HT_FILTER_MIDDLE);
                   1530: #endif /* AMAYA_LOST_UPDATE */
1.111     cvs      1531: #ifndef _WINDOWS
1.116     cvs      1532:   HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, 
                   1533:                   HT_FILTER_LAST);     
1.111     cvs      1534: #endif /* !_WINDOWS */
1.116     cvs      1535:    /**** for later ?? ****/
                   1536:    /*  HTNet_addAfter(HTInfoFilter,    NULL,           NULL, HT_ALL,           HT_FILTER_LATE); */
1.51      cvs      1537:    /* handles all errors */
1.116     cvs      1538:   HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1539: }
                   1540: 
1.15      cvs      1541: /*----------------------------------------------------------------------
1.17      cvs      1542:   AHTAlertInit
                   1543:   Register alert messages and their callbacks.
1.15      cvs      1544:   ----------------------------------------------------------------------*/
                   1545: #ifdef __STDC__
                   1546: static void         AHTAlertInit (void)
                   1547: #else
                   1548: static void         AHTAlertInit ()
                   1549: #endif
                   1550: {
                   1551:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.120     cvs      1552: #ifdef __WINDOWS
1.70      cvs      1553:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1.116     cvs      1554: #endif /* _WINDOWS */
1.15      cvs      1555:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1556:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1557:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1558:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1559:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1.116     cvs      1560: }
                   1561: 
1.128     cvs      1562: /*----------------------------------------------------------------------
                   1563:   libwww_CleanCache
                   1564:   frontend to the recursive cache cleaning function
                   1565:   ----------------------------------------------------------------------*/
                   1566: #ifdef __STDC__
                   1567: void libwww_CleanCache (void)
                   1568: #else
                   1569: void libwww_CleanCache ()
                   1570: Document doc;
                   1571: View view;
                   1572: #endif /* __STDC__ */
                   1573: {
1.130     cvs      1574: #ifdef AMAYA_WWW_CACHE
1.198     cvs      1575:   STRING real_dir;
                   1576:   STRING cache_dir;
                   1577:   STRING tmp;
1.128     cvs      1578:   int cache_size;
1.177     cvs      1579:   int cache_expire;
                   1580:   int cache_disconnect;
1.191     cvs      1581:   ThotBool error;
1.185     cvs      1582:   STRING ptr;
1.130     cvs      1583: 
1.128     cvs      1584:   if (!HTCacheMode_enabled ())
1.183     cvs      1585:     /* don't do anything if we're not using a cache */
                   1586:     return;
1.128     cvs      1587:   /* temporarily close down the cache, purge it, then restart */
1.198     cvs      1588:   tmp = ISO2WideChar (HTCacheMode_getRoot ());
1.190     cvs      1589:   /* don't do anything if we don't have a valid cache dir */
                   1590:   if (!tmp || *tmp == EOS)
                   1591:          return;
                   1592:   cache_dir = TtaStrdup (tmp);
1.128     cvs      1593:   cache_size = HTCacheMode_maxSize ();
1.177     cvs      1594:   cache_expire = HTCacheMode_expires ();
                   1595:   cache_disconnect = HTCacheMode_disconnected ();
                   1596: 
1.185     cvs      1597:   /* get something we can work on :) */
1.198     cvs      1598:   tmp = ISO2WideChar (HTWWWToLocal (WideChar2ISO (cache_dir), "file:", NULL));
                   1599:   real_dir = TtaAllocString (ustrlen (tmp) + 20);
                   1600:   ustrcpy (real_dir, tmp);
1.190     cvs      1601:   HT_FREE (tmp);
1.185     cvs      1602: 
                   1603:   /* safeguard... abort the operation if cache_dir doesn't end with
                   1604:      CACHE_DIR_NAME */
                   1605:   error = TRUE;
1.198     cvs      1606:   ptr = ustrstr (real_dir, CACHE_DIR_NAME);  
                   1607:     if (ptr && *ptr && !ustrcasecmp (ptr, CACHE_DIR_NAME))
1.185     cvs      1608:       error = FALSE;
                   1609:   if (error)
                   1610:     return;  
                   1611:   
1.130     cvs      1612:   /* remove the concurrent cache lock */
                   1613: #ifdef DEBUG_LIBWWW
                   1614:   fprintf (stderr, "Clearing the cache lock\n");
                   1615: #endif /* DEBUG_LIBWWW */
                   1616:   clear_cachelock ();
1.128     cvs      1617:   HTCacheTerminate ();
1.185     cvs      1618:   HTCacheMode_setEnabled (FALSE);
                   1619:   
                   1620:   RecCleanCache (real_dir);
1.128     cvs      1621: 
1.177     cvs      1622:   HTCacheMode_setExpires (cache_expire);
                   1623:   HTCacheMode_setDisconnected (cache_disconnect);
1.198     cvs      1624:   HTCacheInit (WideChar2ISO (cache_dir), cache_size);
1.130     cvs      1625:   /* set a new concurrent cache lock */
1.198     cvs      1626:   ustrcat (real_dir, TEXT(".lock"));
1.185     cvs      1627:   if (set_cachelock (real_dir) == -1)
1.130     cvs      1628:     /* couldn't open the .lock file, so, we close the cache to
                   1629:        be in the safe side */
                   1630:     {
                   1631: #ifdef DEBUG_LIBWWW
                   1632:       fprintf (stderr, "couldnt set the cache lock\n");
                   1633: #endif /* DEBUG_LIBWWW */
                   1634:       HTCacheTerminate ();
1.185     cvs      1635:       HTCacheMode_setEnabled (FALSE);
1.130     cvs      1636:     }
                   1637: #ifdef DEBUG_LIBWWW
                   1638:   fprintf (stderr, "set a cache lock\n");
                   1639: #endif /* DEBUG_LIBWWW */
1.185     cvs      1640:   TtaFreeMemory (real_dir);
1.128     cvs      1641:   TtaFreeMemory (cache_dir);
                   1642: #endif /* AMAYA_WWW_CACHE */
                   1643: }
                   1644: 
1.118     cvs      1645: #ifdef AMAYA_WWW_CACHE
                   1646: /*----------------------------------------------------------------------
1.128     cvs      1647:   RecCleanCache
1.118     cvs      1648:   Clears an existing cache directory
                   1649:   ----------------------------------------------------------------------*/
                   1650: #ifdef __STDC__
1.198     cvs      1651: static void RecCleanCache (STRING dirname)
1.118     cvs      1652: #else
1.198     cvs      1653: static void RecCleanCache (dirname)
                   1654: STRING dirname;
1.118     cvs      1655: #endif /* __STDC__ */
1.128     cvs      1656: 
                   1657: #ifdef _WINDOWS
                   1658: {
                   1659:   HANDLE hFindFile;
1.191     cvs      1660:   ThotBool status;
1.128     cvs      1661:   WIN32_FIND_DATA ffd;
                   1662:   
1.198     cvs      1663:   CHAR_T t_dir [MAX_LENGTH];
                   1664:   STRING ptr;
1.128     cvs      1665: 
1.130     cvs      1666:   /* create a t_dir name to start searching for files */
1.198     cvs      1667:   if ((ustrlen (dirname) + 10) > MAX_LENGTH)
1.128     cvs      1668:     /* ERROR: directory name is too big */
                   1669:     return;
                   1670: 
1.198     cvs      1671:   ustrcpy (t_dir, dirname);
1.130     cvs      1672:   /* save the end of the dirname. We'll use it to make
                   1673:      a complete pathname when erasing files */
1.198     cvs      1674:   ptr = &t_dir[ustrlen (t_dir)];
                   1675:   ustrcat (t_dir, TEXT("*"));
1.128     cvs      1676: 
1.130     cvs      1677:   hFindFile = FindFirstFile (t_dir, &ffd);
1.128     cvs      1678:     
                   1679:   if (hFindFile == INVALID_HANDLE_VALUE)
                   1680:     /* nothing to erase? */
                   1681:     return;
                   1682: 
                   1683:   status = TRUE;
1.130     cvs      1684:   while (status) 
                   1685:     {
                   1686:       if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1.133     cvs      1687:        {
                   1688:          /* it's a directory, erase it recursively */
1.198     cvs      1689:          if (ustrcmp (ffd.cFileName, TEXT("..")) && ustrcmp (ffd.cFileName, TEXT(".")))
1.133     cvs      1690:            {
1.198     cvs      1691:              ustrcpy (ptr, ffd.cFileName);
                   1692:              ustrcat (ptr, DIR_STR);
1.133     cvs      1693:              RecCleanCache (t_dir);
1.198     cvs      1694:              urmdir (t_dir);
1.133     cvs      1695:            }
                   1696:        }
                   1697:        else
1.130     cvs      1698:          {
1.133     cvs      1699:            /* it's a file, erase it */
1.198     cvs      1700:            ustrcpy (ptr, ffd.cFileName);
1.133     cvs      1701:            TtaFileUnlink (t_dir);
1.130     cvs      1702:          }
                   1703:       status = FindNextFile (hFindFile, &ffd);
                   1704:     }
1.128     cvs      1705:   FindClose (hFindFile);
                   1706: }
                   1707: 
                   1708: #else /* _WINDOWS */
1.118     cvs      1709: {
                   1710:   DIR *dp;
                   1711:   struct stat st;
                   1712: #ifdef HAVE_DIRENT_H
                   1713:   struct dirent *d;
                   1714: #else
                   1715:   struct direct *d;
                   1716: #endif /* HAVE_DIRENT_H */
1.128     cvs      1717:   char filename[BUFSIZ+1];
1.118     cvs      1718: 
                   1719:   if ((dp = opendir (dirname)) == NULL) 
                   1720:     {
                   1721:       /* @@@ we couldn't open the directory ... we need some msg */
                   1722:       perror (dirname);
1.176     cvs      1723:       return;
1.118     cvs      1724:     }
                   1725:   
                   1726:   while ((d = readdir (dp)) != NULL)
                   1727:     {
                   1728:       /* skip the UNIX . and .. links */
                   1729:       if (!strcmp (d->d_name, "..")
                   1730:          || !strcmp (d->d_name, "."))
                   1731:        continue;
                   1732: 
1.188     cvs      1733:       sprintf (filename, "%s%c%s", dirname, DIR_SEP, d->d_name);
1.118     cvs      1734:       if  (lstat (filename, &st) < 0 ) 
                   1735:        {
                   1736:          /* @@2 need some error message */
                   1737:          perror (filename);
                   1738:          continue;
                   1739:        }
                   1740:       
                   1741:       switch (st.st_mode & S_IFMT)
                   1742:        {
                   1743:        case S_IFDIR:
                   1744:          /* if we find a directory, we erase it, recursively */
1.128     cvs      1745:          strcat (filename, DIR_STR);
                   1746:          RecCleanCache (filename);
                   1747:          rmdir (filename);
1.118     cvs      1748:          break;
                   1749:        case S_IFLNK:
                   1750:          /* skip any links we find */
                   1751:          continue;
                   1752:          break;
                   1753:        default:
                   1754:          /* erase the filename */
                   1755:          TtaFileUnlink (filename);
                   1756:          break;
                   1757:        }
                   1758:     }
                   1759:   closedir (dp);
                   1760: }
1.128     cvs      1761: #endif /* _WINDOWS */
1.118     cvs      1762: #endif /* AMAYA_WWW_CACHE */
                   1763: 
1.116     cvs      1764: /*----------------------------------------------------------------------
                   1765:   CacheInit
                   1766:   Reads the cache settings from the thot.ini file.
                   1767:   ----------------------------------------------------------------------*/
1.118     cvs      1768: #ifdef __STDC__
1.116     cvs      1769: static void CacheInit (void)
1.118     cvs      1770: #else
                   1771: static void Cacheinit ()
                   1772: #endif
                   1773: 
1.116     cvs      1774: {
                   1775: #ifndef AMAYA_WWW_CACHE
                   1776:    HTCacheMode_setEnabled (NO);
                   1777: 
                   1778: #else /* AMAYA_WWW_CACHE */
1.198     cvs      1779:   STRING strptr;
                   1780:   STRING cache_dir = NULL;
                   1781:   STRING real_dir = NULL;
                   1782:   STRING cache_lockfile;
1.130     cvs      1783:   int cache_size;
1.176     cvs      1784:   int cache_entry_size;
1.191     cvs      1785:   ThotBool cache_enabled;
                   1786:   ThotBool cache_locked;
                   1787:   ThotBool tmp_bool;
1.130     cvs      1788: 
1.185     cvs      1789: int i;
                   1790: 
1.116     cvs      1791:   /* activate cache? */
1.198     cvs      1792:   strptr = TtaGetEnvString (TEXT("ENABLE_CACHE"));
                   1793:   if (strptr && *strptr && ustrcasecmp (strptr, TEXT("yes")))
1.130     cvs      1794:     cache_enabled = NO;
1.116     cvs      1795:   else
1.130     cvs      1796:     cache_enabled = YES;
1.116     cvs      1797: 
1.151     cvs      1798:   /* cache protected documents? */
1.198     cvs      1799:   strptr = TtaGetEnvString (TEXT("CACHE_PROTECTED_DOCS"));
                   1800:   if (strptr && *strptr && !ustrcasecmp (strptr, TEXT("yes")))
1.151     cvs      1801:     HTCacheMode_setProtected (YES);
                   1802:   else
                   1803:     HTCacheMode_setProtected (NO);
                   1804: 
1.116     cvs      1805:   /* get the cache dir (or use a default one) */
1.198     cvs      1806:   strptr = TtaGetEnvString (TEXT("CACHE_DIR"));
1.116     cvs      1807:   if (strptr && *strptr) 
                   1808:     {
1.198     cvs      1809:       real_dir = TtaAllocString (ustrlen (strptr) + ustrlen (CACHE_DIR_NAME) + 20);
                   1810:       ustrcpy (real_dir, strptr);
                   1811:          if (*(real_dir + ustrlen (real_dir) - 1) != DIR_SEP)
                   1812:            ustrcat (real_dir, DIR_STR);
1.116     cvs      1813:     }
                   1814:   else
                   1815:     {
1.198     cvs      1816:       real_dir = TtaAllocString (ustrlen (TempFileDirectory)
                   1817:                                + ustrlen (CACHE_DIR_NAME) + 20);
                   1818:       usprintf (real_dir, TEXT("%s%s"), TempFileDirectory, CACHE_DIR_NAME);
1.185     cvs      1819:     }
                   1820: 
                   1821:   /* compatiblity with previous versions of Amaya: does real_dir
                   1822:      include CACHE_DIR_NAME? If not, add it */
1.198     cvs      1823:   strptr = ustrstr (real_dir, CACHE_DIR_NAME);
1.185     cvs      1824:   if (!strptr)
                   1825:   {
1.198     cvs      1826:     ustrcat (real_dir, CACHE_DIR_NAME);
1.185     cvs      1827:   }
                   1828:   else
                   1829:     {
1.198     cvs      1830:       i = ustrlen (CACHE_DIR_NAME);
1.185     cvs      1831:          if (strptr[i] != EOS)
1.198     cvs      1832:           ustrcat (real_dir, CACHE_DIR_NAME);
1.116     cvs      1833:     }
1.185     cvs      1834: 
                   1835:  
1.198     cvs      1836:   cache_dir = TtaAllocString (ustrlen (real_dir) + 10);
                   1837:   usprintf (cache_dir, TEXT("file:%s"), real_dir);
1.116     cvs      1838: 
                   1839:   /* get the cache size (or use a default one) */
1.198     cvs      1840:   strptr = TtaGetEnvString (TEXT("CACHE_SIZE"));
1.116     cvs      1841:   if (strptr && *strptr) 
1.198     cvs      1842:     cache_size = uctoi (strptr);
1.116     cvs      1843:   else
                   1844:     cache_size = DEFAULT_CACHE_SIZE;
                   1845: 
1.176     cvs      1846:   /* get the max cached file size (or use a default one) */
1.198     cvs      1847:   if (!TtaGetEnvInt (TEXT("MAX_CACHE_ENTRY_SIZE"), &cache_entry_size))
1.176     cvs      1848:     cache_entry_size = DEFAULT_MAX_CACHE_ENTRY_SIZE;
                   1849: 
1.130     cvs      1850:   if (cache_enabled) 
1.116     cvs      1851:     {
                   1852:       /* how to remove the lock? force remove it? */
1.198     cvs      1853:       cache_lockfile = TtaAllocString (ustrlen (real_dir) + 20);
                   1854:       ustrcpy (cache_lockfile, real_dir);
                   1855:       ustrcat (cache_lockfile, TEXT(".lock"));
1.130     cvs      1856:       cache_locked = FALSE;
                   1857:       if (TtaFileExist (cache_lockfile)
                   1858:          && !(cache_locked = test_cachelock (cache_lockfile)))
1.116     cvs      1859:        {
1.130     cvs      1860: #ifdef DEBUG_LIBWWW
                   1861:          fprintf (stderr, "found a stale cache, removing it\n");
                   1862: #endif /* DEBUG_LIBWWW */
                   1863:          /* remove the lock and clean the cache (the clean cache 
                   1864:             will remove all, making the following call unnecessary */
                   1865:          /* little trick to win some memory */
1.198     cvs      1866:          strptr = ustrrchr (cache_lockfile, TEXT('.'));
1.130     cvs      1867:          *strptr = EOS;
                   1868:          RecCleanCache (cache_lockfile);
                   1869:          *strptr = '.';
1.116     cvs      1870:        }
                   1871: 
1.130     cvs      1872:       if (!cache_locked) 
                   1873:        {
                   1874:          /* initialize the cache if there's no other amaya
                   1875:             instance running */
1.177     cvs      1876:          HTCacheMode_setMaxCacheEntrySize (cache_entry_size);
1.198     cvs      1877:          if (TtaGetEnvBoolean (TEXT("CACHE_EXPIRE_IGNORE"), &tmp_bool) 
1.177     cvs      1878:              && tmp_bool)
                   1879:            HTCacheMode_setExpires (HT_EXPIRES_IGNORE);
                   1880:          else
                   1881:            HTCacheMode_setExpires (HT_EXPIRES_AUTO);
1.198     cvs      1882:          TtaGetEnvBoolean (TEXT("CACHE_DISCONNECTED_MODE"), &tmp_bool);
1.177     cvs      1883:          if (tmp_bool)
                   1884:            HTCacheMode_setDisconnected (HT_DISCONNECT_NORMAL);
                   1885:          else
                   1886:            HTCacheMode_setDisconnected (HT_DISCONNECT_NONE);
1.198     cvs      1887:          if (HTCacheInit (WideChar2ISO (cache_dir), cache_size))
1.130     cvs      1888:            {
1.188     cvs      1889:              if (set_cachelock (cache_lockfile) == -1)
                   1890:                /* couldn't open the .lock file, so, we close the cache to
                   1891:                   be in the safe side */
                   1892:                {
                   1893:                  HTCacheTerminate ();
                   1894:                  HTCacheMode_setEnabled (FALSE);
                   1895: #ifdef DEBUG_LIBWWW
                   1896:                  fprintf (stderr, "couldnt set the cache lock\n");
                   1897: #endif /* DEBUG_LIBWWW */
                   1898:                }
1.130     cvs      1899: #ifdef DEBUG_LIBWWW
1.188     cvs      1900:              else
                   1901:                fprintf (stderr, "created the cache lock\n");
1.130     cvs      1902: #endif /* DEBUG_LIBWWW */
                   1903:            }
1.188     cvs      1904:          else
                   1905:            {
1.130     cvs      1906: #ifdef DEBUG_LIBWWW
1.188     cvs      1907:              fprintf (stderr, "couldn't create the cache\n");
                   1908: #endif /* DEBUG_LIBWWW */            
                   1909:              HTCacheTerminate ();
                   1910:            }
1.130     cvs      1911:        }
                   1912:       else 
                   1913:        {
1.185     cvs      1914:          HTCacheMode_setEnabled (FALSE);
1.130     cvs      1915: #ifdef DEBUG_LIBWWW
                   1916:          fprintf (stderr, "lock detected, starting libwww without a cache/n");
                   1917: #endif /* DEBUG_LIBWWW */
                   1918:        }
                   1919:       TtaFreeMemory (cache_lockfile);
1.116     cvs      1920:     }
                   1921:   else
1.185     cvs      1922:     {
                   1923:       HTCacheMode_setEnabled (FALSE);
                   1924:     }
1.116     cvs      1925:   if (cache_dir)
                   1926:     TtaFreeMemory (cache_dir);
1.189     cvs      1927:   if (real_dir)
                   1928:     TtaFreeMemory (real_dir);
1.185     cvs      1929:   /* warn the user if the cache isn't active */
                   1930:   if (cache_enabled && !HTCacheMode_enabled ())
                   1931:     {
                   1932: #ifdef _WINDOWS   
1.198     cvs      1933:       MessageBox (NULL, TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE), TEXT("Cache"), MB_OK);
1.185     cvs      1934: #else /* !_WINDOWS */
                   1935:       TtaDisplayMessage (CONFIRM, TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE),
                   1936:                         NULL);
                   1937: #endif /* _WINDOWS */
                   1938:     }
1.116     cvs      1939: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      1940: }
                   1941: 
                   1942: /*----------------------------------------------------------------------
1.97      cvs      1943:   ProxyInit
                   1944:   Reads any proxies settings which may be declared as environmental
                   1945:   variables or in the thot.ini file. The former overrides the latter.
                   1946:   ----------------------------------------------------------------------*/
1.118     cvs      1947: #ifdef __STDC__
1.97      cvs      1948: static void ProxyInit (void)
1.118     cvs      1949: #else
                   1950: static void ProxyInit ()
                   1951: #endif /* __STDC__ */
1.97      cvs      1952: {
1.198     cvs      1953:   STRING strptr;
                   1954:   STRING str = NULL;
                   1955:   STRING name;
                   1956:   char*  strptrA;
1.97      cvs      1957: 
                   1958:   /* get the proxy settings from the thot.ini file */
1.198     cvs      1959:   strptr = TtaGetEnvString (TEXT("HTTP_PROXY"));
1.97      cvs      1960:   if (strptr && *strptr)
1.198     cvs      1961:     HTProxy_add ("http", WideChar2ISO (strptr));
1.97      cvs      1962:   /* get the no_proxy settings from the thot.ini file */
1.198     cvs      1963:   strptr = TtaGetEnvString (TEXT("NO_PROXY"));
1.97      cvs      1964:   if (strptr && *strptr) 
                   1965:     {
                   1966:       str = TtaStrdup (strptr);          /* Get copy we can mutilate */
                   1967:       strptr = str;
1.198     cvs      1968:       strptrA = WideChar2ISO (strptr);
1.200   ! cvs      1969:       while ((name = HTNextField (&strptrA)) != NULL) {
1.198     cvs      1970:        STRING portstr = ustrchr (name, TEXT(':'));
1.97      cvs      1971:        unsigned port=0;
1.198     cvs      1972:        if (portstr) { 
                   1973:          *portstr++ = EOS;
                   1974:          if (*portstr) port = (unsigned) uctoi (portstr);
1.97      cvs      1975:        }
                   1976:        /* Register it for all access methods */
1.198     cvs      1977:        HTNoProxy_add (WideChar2ISO (name), NULL, port);
1.97      cvs      1978:       }
                   1979:       TtaFreeMemory (str);
                   1980:     }
                   1981:   
                   1982:   /* use libw3's routine to get all proxy settings from the environment */
                   1983:    HTProxy_getEnvVar ();
                   1984: }
                   1985: 
                   1986: 
                   1987: /*----------------------------------------------------------------------
1.17      cvs      1988:   AHTProfile_newAmaya
                   1989:   creates the Amaya client profile for libwww.
1.15      cvs      1990:   ----------------------------------------------------------------------*/
                   1991: #ifdef __STDC__
1.198     cvs      1992: static void         AHTProfile_newAmaya (STRING AppName, STRING AppVersion)
1.15      cvs      1993: #else  /* __STDC__ */
                   1994: static void         AHTProfile_newAmaya (AppName, AppVersion)
1.198     cvs      1995: STRING AppName;
                   1996: STRING AppVersion;
1.15      cvs      1997: #endif /* __STDC__ */
1.4       cvs      1998: {
1.198     cvs      1999:    STRING strptr;
1.158     cvs      2000: 
1.4       cvs      2001:    /* If the Library is not already initialized then do it */
                   2002:    if (!HTLib_isInitialized ())
1.198     cvs      2003:       HTLibInit (WideChar2ISO (AppName), WideChar2ISO (AppVersion));
1.4       cvs      2004: 
                   2005:    if (!converters)
                   2006:       converters = HTList_new ();
1.27      cvs      2007:    if (!acceptTypes)
                   2008:       acceptTypes = HTList_new ();
1.193     cvs      2009:    if (!acceptLanguages)
                   2010:       acceptLanguages = HTList_new ();
1.151     cvs      2011:    if (!transfer_encodings)
                   2012:       transfer_encodings = HTList_new ();
                   2013:    if (!content_encodings)
                   2014:       content_encodings = HTList_new ();
1.4       cvs      2015: 
1.169     cvs      2016:    /* inhibits libwww's automatic file_suffix_binding */
                   2017:    HTFile_doFileSuffixBinding (FALSE);
                   2018: 
1.4       cvs      2019:    /* Register the default set of transport protocols */
                   2020:    HTTransportInit ();
                   2021: 
                   2022:    /* Register the default set of application protocol modules */
                   2023:    AHTProtocolInit ();
                   2024: 
1.116     cvs      2025:    /* Register the default set of messages and dialog functions */
                   2026:    AHTAlertInit ();
                   2027:    HTAlert_setInteractive (YES);
                   2028: 
                   2029: #ifdef AMAYA_WWW_CACHE
                   2030:    /* Enable the persistent cache  */
                   2031:    CacheInit ();
                   2032: #else
                   2033:    HTCacheMode_setEnabled (NO);
                   2034: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      2035: 
                   2036:    /* Register the default set of BEFORE and AFTER filters */
                   2037:    AHTNetInit ();
                   2038: 
                   2039:    /* Set up the default set of Authentication schemes */
1.167     cvs      2040:    HTAA_newModule ("basic", HTBasic_generate, HTBasic_parse, NULL,
                   2041:                    HTBasic_delete);
1.158     cvs      2042:    /* activate MDA by defaul */
1.198     cvs      2043:    strptr = TtaGetEnvString (TEXT("ENABLE_MDA"));
                   2044:    if (!strptr || (strptr && *strptr && ustrcasecmp (strptr, TEXT("no"))))
1.158     cvs      2045:      HTAA_newModule ("digest", HTDigest_generate, HTDigest_parse, 
1.167     cvs      2046:                     HTDigest_updateInfo, HTDigest_delete);
1.4       cvs      2047: 
1.97      cvs      2048:    /* Get any proxy settings */
                   2049:    ProxyInit ();
1.4       cvs      2050: 
                   2051:    /* Register the default set of converters */
                   2052:    AHTConverterInit (converters);
1.151     cvs      2053:    HTFormat_setConversion (converters);
1.27      cvs      2054:    AHTAcceptTypesInit (acceptTypes);
1.193     cvs      2055:    AHTAcceptLanguagesInit (acceptLanguages);
1.4       cvs      2056: 
                   2057:    /* Register the default set of transfer encoders and decoders */
1.151     cvs      2058:    HTTransferEncoderInit (transfer_encodings);
1.190     cvs      2059:    HTFormat_setTransferCoding (transfer_encodings);
                   2060:    /* Register the default set of content encoders and decoders */
                   2061:    HTContentEncoderInit (content_encodings);
                   2062:    /* ignore all other encoding formats (or libwww will send them 
1.181     cvs      2063:       thru a blackhole otherwise */
                   2064:    HTCoding_add (content_encodings, "*", NULL, HTIdentityCoding, 1.0);
1.151     cvs      2065:    if (HTList_count(content_encodings) > 0)
                   2066:      HTFormat_setContentCoding(content_encodings);
                   2067:    else 
                   2068:      {
                   2069:        HTList_delete(content_encodings);
                   2070:        content_encodings = NULL;
                   2071:      }
1.4       cvs      2072: 
                   2073:    /* Register the default set of MIME header parsers */
1.74      cvs      2074:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      2075: 
                   2076:    /* Register the default set of Icons for directory listings */
1.27      cvs      2077:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      2078: }
                   2079: 
1.5       cvs      2080: /*----------------------------------------------------------------------
1.17      cvs      2081:   AHTProfile_delete
                   2082:   deletes the Amaya client profile.
1.5       cvs      2083:   ----------------------------------------------------------------------*/
1.4       cvs      2084: #ifdef __STDC__
                   2085: static void         AHTProfile_delete (void)
                   2086: #else
                   2087: static void         AHTProfile_delete ()
1.7       cvs      2088: #endif                         /* __STDC__ */
1.4       cvs      2089: {
1.22      cvs      2090:  
                   2091:   /* free the Amaya global context */
1.151     cvs      2092: 
                   2093:   /* Clean up all the registred converters */
                   2094:   HTFormat_deleteAll ();
                   2095:   if (acceptTypes)
1.74      cvs      2096:     HTConversion_deleteAll (acceptTypes);
1.193     cvs      2097:   if (acceptLanguages)
                   2098:     HTLanguage_deleteAll (acceptLanguages);
1.151     cvs      2099: 
1.74      cvs      2100:   HTList_delete (Amaya->docid_status);
                   2101:   HTList_delete (Amaya->reqlist);
                   2102:   TtaFreeMemory (Amaya);
1.61      cvs      2103: 
1.151     cvs      2104:   if (HTLib_isInitialized ())
1.74      cvs      2105:       
1.120     cvs      2106: #ifdef _WINDOWS
1.151     cvs      2107:     HTEventTerminate ();
1.198     cvs      2108: #endif /* _WINDOWS; */         
1.74      cvs      2109:     
1.151     cvs      2110:   /* Clean up the persistent cache (if any) */
1.116     cvs      2111: #ifdef AMAYA_WWW_CACHE
1.151     cvs      2112:   clear_cachelock ();
                   2113:   HTCacheTerminate ();
1.116     cvs      2114: #endif /* AMAYA_WWW_CACHE */
1.74      cvs      2115:     
1.151     cvs      2116:   /* Terminate libwww */
                   2117:   HTLibTerminate ();
1.4       cvs      2118: }
                   2119: 
1.5       cvs      2120: /*----------------------------------------------------------------------
1.116     cvs      2121:   AmayacontextInit
                   2122:   initializes an internal Amaya context for our libwww interface 
                   2123:   ----------------------------------------------------------------------*/
                   2124: #ifdef __STDC__
                   2125: static void                AmayaContextInit ()
                   2126: #else
                   2127: static void                AmayaContextInit ()
                   2128: #endif
                   2129: 
                   2130: {
1.140     cvs      2131:   AmayaAlive_flag = TRUE;
1.116     cvs      2132:   /* Initialization of the global context */
                   2133:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   2134:   Amaya->reqlist = HTList_new ();
                   2135:   Amaya->docid_status = HTList_new ();
                   2136:   Amaya->open_requests = 0;
                   2137: }
                   2138: 
                   2139: /*----------------------------------------------------------------------
1.17      cvs      2140:   QueryInit
                   2141:   initializes the libwww interface 
1.5       cvs      2142:   ----------------------------------------------------------------------*/
1.4       cvs      2143: #ifdef __STDC__
                   2144: void                QueryInit ()
                   2145: #else
                   2146: void                QueryInit ()
                   2147: #endif
                   2148: {
1.198     cvs      2149:   STRING strptr;
1.151     cvs      2150:   int tmp_i;
                   2151:   long tmp_l;
1.4       cvs      2152: 
1.116     cvs      2153:    AmayaContextInit ();
1.4       cvs      2154:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.140     cvs      2155:    CanDoStop_set (TRUE);
1.4       cvs      2156: 
1.116     cvs      2157: #ifdef _WINDOWS
1.125     cvs      2158:    HTEventInit ();
1.116     cvs      2159: #endif /* _WINDOWS */
1.72      cvs      2160: 
1.123     cvs      2161: #ifndef _WINDOWS
1.116     cvs      2162:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
                   2163:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.120     cvs      2164:    HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
                   2165:    HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.116     cvs      2166: #endif /* !_WINDOWS */
1.190     cvs      2167: 
                   2168:    /*** @@@@
1.176     cvs      2169:    WWW_TraceFlag = 0;
1.190     cvs      2170:    ***/
1.74      cvs      2171: #ifdef DEBUG_LIBWWW
1.116     cvs      2172:   /* forwards error messages to our own function */
                   2173:    WWW_TraceFlag = THD_TRACE;
1.138     cvs      2174:    HTTrace_setCallback(LineTrace);
1.4       cvs      2175:    /* Trace activation (for debugging) */
1.148     cvs      2176:    /***
1.138     cvs      2177:     WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | SHOW_PROTOCOL_TRACE;
                   2178:     WWW_TraceFlag |= 0xFFFFFFFFl;
                   2179:     ***/
1.152     cvs      2180: #endif
                   2181: 
1.148     cvs      2182:    /* Setting up different network parameters */
1.151     cvs      2183: 
1.148     cvs      2184:    /* Maximum number of simultaneous open sockets */
1.198     cvs      2185:    strptr = TtaGetEnvString (TEXT("MAX_SOCKET"));
1.151     cvs      2186:    if (strptr && *strptr) 
1.198     cvs      2187:      tmp_i = uctoi (strptr);
1.151     cvs      2188:    else
                   2189:      tmp_i = DEFAULT_MAX_SOCKET;
                   2190:    HTNet_setMaxSocket (tmp_i);
                   2191: 
1.148     cvs      2192:    /* different network services timeouts */
1.151     cvs      2193:    /* dns timeout */
1.198     cvs      2194:    strptr = TtaGetEnvString (TEXT("DNS_TIMEOUT"));
1.151     cvs      2195:    if (strptr && *strptr) 
1.198     cvs      2196:      tmp_i = uctoi (strptr);
1.151     cvs      2197:    else
                   2198:      tmp_i = DEFAULT_DNS_TIMEOUT;
                   2199:    HTDNS_setTimeout (tmp_i);
                   2200: 
                   2201:    /* persistent connections timeout */
1.198     cvs      2202:    strptr = TtaGetEnvString (TEXT("PERSIST_CX_TIMEOUT"));
1.151     cvs      2203:    if (strptr && *strptr) 
1.198     cvs      2204:      tmp_l = uctol (strptr);
1.151     cvs      2205:    else
                   2206:      tmp_l = DEFAULT_PERSIST_TIMEOUT;
                   2207:    HTHost_setPersistTimeout (tmp_l);
                   2208: 
1.148     cvs      2209:    /* default timeout in ms */
1.198     cvs      2210:    strptr = TtaGetEnvString (TEXT("NET_EVENT_TIMEOUT"));
1.151     cvs      2211:    if (strptr && *strptr) 
1.198     cvs      2212:      tmp_i = uctoi (strptr);
1.151     cvs      2213:    else
                   2214:      tmp_i = DEFAULT_NET_EVENT_TIMEOUT;
                   2215:    HTHost_setEventTimeout (tmp_i);
                   2216: 
1.173     cvs      2217:    HTRequest_setMaxRetry (8);
1.4       cvs      2218: #ifdef CATCH_SIG
1.148     cvs      2219:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      2220: #endif
1.15      cvs      2221: }
                   2222: 
                   2223: /*----------------------------------------------------------------------
1.17      cvs      2224:   LoopForStop
1.136     cvs      2225:   a copy of the Thop event loop so we can handle the stop button in Unix
                   2226:   and preemptive requests under Windows
1.15      cvs      2227:   ----------------------------------------------------------------------*/
                   2228: #ifdef __STDC__
                   2229: static int          LoopForStop (AHTReqContext * me)
                   2230: #else
                   2231: static int          LoopForStop (AHTReqContext * me)
                   2232: #endif
                   2233: {
1.136     cvs      2234: #ifdef _WINDOWS
                   2235:   MSG msg;
                   2236:   unsigned long libwww_msg;
                   2237:   HWND old_active_window, libwww_window;
                   2238:   int  status_req = HT_OK;
                   2239: 
                   2240:   old_active_window = GetActiveWindow ();
                   2241:   libwww_window = HTEventList_getWinHandle (&libwww_msg);
1.137     cvs      2242:  
1.149     cvs      2243:   while (me->reqStatus != HT_END && me->reqStatus != HT_ERR
                   2244:             && me->reqStatus != HT_ABORT && AmayaIsAlive () &&
                   2245:             GetMessage (&msg, NULL, 0, 0))
                   2246:                {
                   2247:          if (msg.message != WM_QUIT)
                   2248:                        {
                   2249:                      TranslateMessage (&msg);
                   2250:                      DispatchMessage (&msg);
                   2251:                        }
                   2252:          else
                   2253:                break;      
                   2254:                }
1.136     cvs      2255:   if (!AmayaIsAlive ())
                   2256:     /* Amaya was killed by one of the callback handlers */
                   2257:     exit (0);
                   2258: #else /* _WINDOWS */
1.25      cvs      2259:    extern ThotAppContext app_cont;
1.51      cvs      2260:    XEvent                ev;
                   2261:    XtInputMask           status;
1.17      cvs      2262:    int                 status_req = HT_OK;
1.15      cvs      2263: 
                   2264:    /* to test the async calls  */
1.17      cvs      2265:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      2266:    while (me->reqStatus != HT_ABORT &&
                   2267:          me->reqStatus != HT_END &&
1.69      cvs      2268:          me->reqStatus != HT_ERR) {
1.116     cvs      2269:         if (!AmayaIsAlive ())
1.69      cvs      2270:            /* Amaya was killed by one of the callback handlers */
                   2271:            exit (0);
                   2272: 
                   2273:         status = XtAppPending (app_cont);
1.144     cvs      2274:         if (status & XtIMXEvent)
                   2275:           {
                   2276:             XtAppNextEvent (app_cont, &ev);
                   2277:             TtaHandleOneEvent (&ev);
                   2278:           } 
                   2279:         else if (status != 0) 
                   2280:           XtAppProcessEvent (app_cont, XtIMAll);
1.69      cvs      2281:    }
1.136     cvs      2282: #endif /* _WINDOWS */
1.69      cvs      2283:    switch (me->reqStatus) {
                   2284:          case HT_ERR:
                   2285:           case HT_ABORT:
1.130     cvs      2286:               status_req = NO;
1.15      cvs      2287:               break;
                   2288: 
1.69      cvs      2289:          case HT_END:
1.130     cvs      2290:               status_req = YES;
1.15      cvs      2291:               break;
                   2292: 
1.69      cvs      2293:          default:
1.15      cvs      2294:               break;
1.69      cvs      2295:    }
1.15      cvs      2296:    return (status_req);
1.4       cvs      2297: }
                   2298: 
1.5       cvs      2299: /*----------------------------------------------------------------------
1.15      cvs      2300:   QueryClose
1.21      cvs      2301:   closes all existing threads, frees all non-automatically deallocated
                   2302:   memory and then ends libwww.
1.5       cvs      2303:   ----------------------------------------------------------------------*/
1.116     cvs      2304: void QueryClose ()
1.4       cvs      2305: {
1.24      cvs      2306: 
1.140     cvs      2307:   AmayaAlive_flag = FALSE;
1.24      cvs      2308: 
1.116     cvs      2309:   /* remove all the handlers and callbacks that may output a message to
                   2310:      a non-existent Amaya window */
1.130     cvs      2311: #ifndef _WINDOWS
1.116     cvs      2312:   HTNet_deleteAfter (AHTLoadTerminate_handler);
1.198     cvs      2313: #endif /* _WINDOWS */
1.116     cvs      2314:   HTNet_deleteAfter (redirection_handler);
                   2315:   HTAlertCall_deleteAll (HTAlert_global () );
                   2316:   HTAlert_setGlobal ((HTList *) NULL);
                   2317:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   2318:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   2319: #ifndef _WINDOWS
                   2320:   /** need to erase all existing timers too **/
                   2321:    HTTimer_registerSetTimerCallback (NULL);
                   2322:    HTTimer_registerDeleteTimerCallback (NULL);
                   2323: #endif /* !_WINDOWS */
                   2324:   HTHost_setActivateRequestCallback (NULL);
                   2325:   Thread_deleteAll ();
1.21      cvs      2326:  
1.116     cvs      2327:   HTProxy_deleteAll ();
                   2328:   HTNoProxy_deleteAll ();
                   2329:   HTGateway_deleteAll ();
                   2330:   AHTProfile_delete ();
                   2331: }
                   2332: 
                   2333: /*----------------------------------------------------------------------
                   2334:   NextNameValue
                   2335:   ---------------------------------------------------------------------*/
                   2336: #ifdef __STDC__
                   2337: static char * NextNameValue (char ** pstr, char **name, char **value)
                   2338: #else
                   2339: static char * NextNameValue (pstr, name, value);
                   2340: char ** pstr;
                   2341: char **name;
                   2342: char **value;
                   2343: #endif /* __STDC__ */
                   2344: {
                   2345:   char * p = *pstr;
                   2346:   char * start = NULL;
                   2347:   if (!pstr || !*pstr) return NULL;
                   2348:   
                   2349:     if (!*p) {
                   2350:       *pstr = p;
                   2351:       *name = NULL;
                   2352:       *value = NULL;
                   2353:       return NULL;                                      /* No field */
                   2354:     }
                   2355:     
                   2356:     /* Now search for the next '&' and ';' delimitators */
                   2357:     start = p;
                   2358:     while (*p && *p != '&' && *p != ';') p++;
                   2359:     if (*p) 
                   2360:       *p++ = '\0';
                   2361:     *pstr = p;
                   2362: 
                   2363:     /* Search for the name and value */
                   2364:     *name = start;
                   2365:     p = start;
                   2366:     
                   2367:     while(*p && *p != '=') 
                   2368:       p++;
                   2369:     if (*p) 
                   2370:       *p++ = '\0';
                   2371:     *value = p;
                   2372: 
                   2373:     return start;
                   2374: }
                   2375: 
                   2376: /*----------------------------------------------------------------------
                   2377:   PrepareFormdata
                   2378:   ---------------------------------------------------------------------*/
                   2379: #ifdef __STDC__
1.198     cvs      2380: static HTAssocList * PrepareFormdata (STRING string)
1.116     cvs      2381: #else
                   2382: static HTAssocList * PrepareFormdata (string)
1.198     cvs      2383: STRING string;
1.116     cvs      2384: #endif /* __STDC__ */
                   2385: {
1.198     cvs      2386:   char*        tmp_string, *tmp_string_ptr;
                   2387:   char*        name;
                   2388:   char*        value;
                   2389:   HTAssocList* formdata;
1.116     cvs      2390: 
                   2391:   if (!string)
                   2392:     return NULL;
                   2393: 
                   2394:   /* store the ptr in another variable, as the original address will
                   2395:      change
                   2396:      */
                   2397: 
1.198     cvs      2398:   tmp_string_ptr = tmp_string = WideChar2ISO (TtaStrdup (string));
1.116     cvs      2399:   formdata = HTAssocList_new();
                   2400:   
                   2401:   while (*tmp_string)
                   2402:     {
                   2403:       NextNameValue (&tmp_string, &name, &value);
                   2404:       HTAssocList_addObject(formdata,
                   2405:                            name, value);
                   2406:     }
                   2407: 
                   2408:   TtaFreeMemory (tmp_string_ptr);
                   2409:   return formdata;
1.4       cvs      2410: }
                   2411: 
1.99      cvs      2412: 
                   2413: /*----------------------------------------------------------------------
                   2414:   InvokeGetObjectWWW_callback
                   2415:   A simple function to invoke a callback function whenever there's an error
                   2416:   in GetObjectWWW
                   2417:   ---------------------------------------------------------------------*/
                   2418: 
1.116     cvs      2419: #ifdef __STDC__
1.198     cvs      2420: void      InvokeGetObjectWWW_callback (int docid, STRING urlName, STRING outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      2421: #else
1.111     cvs      2422: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      2423: int docid;
1.198     cvs      2424: STRING urlName;
                   2425: STRING outputfile;
1.99      cvs      2426: TTcbf *terminate_cbf;
                   2427: void *context_tcbf;
1.116     cvs      2428: #endif /* __STDC__ */
1.99      cvs      2429: {
                   2430:   if (!terminate_cbf)
                   2431:     return;
                   2432:   
1.116     cvs      2433:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      2434:                    NULL, context_tcbf);  
                   2435: }
                   2436: 
                   2437: 
                   2438: 
1.5       cvs      2439: /*----------------------------------------------------------------------
1.15      cvs      2440:    GetObjectWWW
1.17      cvs      2441:    this function requests a resource designated by a URLname into a
                   2442:    temporary filename. The download can come from a simple GET operation,
                   2443:    or can come from POSTING/GETTING a form. In the latter
                   2444:    case, the function receives a query string to send to the server.
                   2445: 
1.5       cvs      2446:    4  file retrieval modes are proposed:                              
                   2447:    AMAYA_SYNC : blocking mode                            
                   2448:    AMAYA_ISYNC : incremental, blocking mode              
                   2449:    AMAYA_ASYNC : non-blocking mode                       
                   2450:    AMAYA_IASYNC : incremental, non-blocking mode         
                   2451:    
                   2452:    In the incremental mode, each time a package arrives, it will be   
                   2453:    stored in the temporary file. In addition, if an                   
                   2454:    incremental_callback function is defined, this function will be    
                   2455:    called and handled a copy of the newly received data package.      
                   2456:    Finally, if a terminate_callback function is defined, it will be   
                   2457:    invoked when the request terminates. The caller of this function
1.4       cvs      2458:    can define two different contexts to be passed to the callback
                   2459:    functions.
                   2460: 
                   2461:    When the function is called with the SYNC mode, the function will
                   2462:    return only when the requested file has been loaded.
                   2463:    The ASYNC mode will immediately return after setting up the
                   2464:    call.
                   2465: 
                   2466:    Notes:
                   2467:    At the end of a succesful request, the urlName string contains the
                   2468:    name of the actually retrieved URL. As a URL can change over the time,
                   2469:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      2470:    caller verify the value of the urlName variable at the end of
1.4       cvs      2471:    a request.
                   2472: 
                   2473:    Inputs:
                   2474:    - docid  Document identifier for the set of objects being
                   2475:    retrieved.
                   2476:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   2477:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   2478:    - mode The retrieval mode.
                   2479:    - incremental_cbf 
                   2480:    - context_icbf
                   2481:    Callback and context for the incremental modes
                   2482:    - terminate_cbf 
                   2483:    - context_icbf
                   2484:    Callback and context for a terminate handler
1.17      cvs      2485:    -error_html if TRUE, then display any server error message as an
                   2486:    HTML document.
1.88      cvs      2487:    - content_type a string
                   2488:  
1.4       cvs      2489:    Outputs:
                   2490:    - urlName The URL that was retrieved
                   2491:    - outputfile The name of the temporary file which holds the
                   2492:    retrieved data. (Only in case of success)
1.88      cvs      2493:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   2494:      sent in the HTTP answer
1.4       cvs      2495:    Returns:
                   2496:    HT_ERROR
                   2497:    HT_OK
1.5       cvs      2498:  
                   2499:   ----------------------------------------------------------------------*/
1.4       cvs      2500: #ifdef __STDC__
1.198     cvs      2501: int GetObjectWWW (int docid, STRING urlName, STRING formdata,
                   2502:                  STRING outputfile, int mode, TIcbf* incremental_cbf, 
1.116     cvs      2503:                  void* context_icbf, TTcbf* terminate_cbf, 
1.198     cvs      2504:                  void* context_tcbf, ThotBool error_html, STRING content_type)
1.4       cvs      2505: #else
1.127     cvs      2506: int GetObjectWWW (docid, urlName, formdata, outputfile, mode, 
1.116     cvs      2507:                  incremental_cbf, context_icbf, 
1.88      cvs      2508:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      2509: int           docid;
1.198     cvs      2510: STRING        urlName;
                   2511: STRING        formdata;
                   2512: STRING        outputfile;
1.73      cvs      2513: int           mode;
                   2514: TIcbf        *incremental_cbf;
                   2515: void         *context_icbf;
                   2516: TTcbf        *terminate_cbf;
                   2517: void         *context_tcbf;
1.191     cvs      2518: ThotBool      error_html;
1.198     cvs      2519: STRING        content_type;
1.4       cvs      2520: #endif
                   2521: {
                   2522:    AHTReqContext      *me;
1.198     cvs      2523:    STRING              ref;
1.107     cvs      2524:    int                 status, l;
1.114     cvs      2525:    int                 tempsubdir;
1.191     cvs      2526:    ThotBool            bool_tmp;
1.7       cvs      2527: 
1.116     cvs      2528:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   2529:      {
                   2530:        /* no file to be loaded */
                   2531:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      2532:        
1.116     cvs      2533:        if (error_html)
1.69      cvs      2534:         /* so we can show the error message */
                   2535:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2536:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2537:                                    context_tcbf, HT_ERROR);
                   2538:        return HT_ERROR;
                   2539:      }
1.7       cvs      2540: 
1.159     cvs      2541:    /* if it's a 'docImage', we have already downloaded it */
1.198     cvs      2542:    if (!ustrncmp (TEXT("internal:"), urlName, 9)) 
1.159     cvs      2543:      {
1.198     cvs      2544:        ustrcpy (outputfile, urlName);
1.159     cvs      2545:        InvokeGetObjectWWW_callback (docid, urlName, outputfile,
                   2546:                                    terminate_cbf, context_tcbf, HT_OK);
                   2547:        return HT_OK;
                   2548:      }
                   2549: 
1.4       cvs      2550:    /* do we support this protocol? */
1.116     cvs      2551:    if (IsValidProtocol (urlName) == NO) 
                   2552:      {
                   2553:        /* return error */
                   2554:        outputfile[0] = EOS;    /* file could not be opened */
                   2555:        TtaSetStatus (docid, 1, 
                   2556:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   2557:                     urlName);
                   2558: 
                   2559:        if (error_html)
1.69      cvs      2560:         /* so we can show the error message */
                   2561:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2562:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2563:                                    context_tcbf, HT_ERROR);
                   2564:        return HT_ERROR;
                   2565:      }
1.4       cvs      2566: 
1.114     cvs      2567:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   2568:       named after their own docid */
                   2569:    
                   2570:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   2571: 
1.116     cvs      2572:    /* create a tempfilename */
1.198     cvs      2573:    usprintf (outputfile, TEXT("%s%c%d%c%04dAM"), TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
1.116     cvs      2574:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      2575:    object_counter++;
1.116     cvs      2576:    
1.4       cvs      2577:    /* normalize the URL */
1.198     cvs      2578:    ref = AmayaParseUrl (urlName, _EMPTYSTR_, AMAYA_PARSE_ALL);
1.4       cvs      2579:    /* should we abort the request if we could not normalize the url? */
1.198     cvs      2580:    if (ref == NULL || ref[0] == EOS) {
1.69      cvs      2581:       /*error */
                   2582:       outputfile[0] = EOS;
                   2583:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   2584:       
                   2585:       if (error_html)
1.116     cvs      2586:        /* so we can show the error message */
                   2587:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      2588:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      2589:                                   context_tcbf, HT_ERROR);
1.69      cvs      2590:       return HT_ERROR;
                   2591:    }
1.116     cvs      2592: 
1.4       cvs      2593:    /* verify if that file name existed */
1.9       cvs      2594:    if (TtaFileExist (outputfile))
1.77      cvs      2595:      TtaFileUnlink (outputfile);
1.116     cvs      2596:    
1.4       cvs      2597:    /* Initialize the request structure */
                   2598:    me = AHTReqContext_new (docid);
1.116     cvs      2599:    if (me == NULL) 
                   2600:      {
                   2601:        outputfile[0] = EOS;
                   2602:        /* need an error message here */
                   2603:        TtaFreeMemory (ref);
                   2604:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2605:                                   context_tcbf, HT_ERROR);
                   2606:        return HT_ERROR;
                   2607:      }
                   2608: 
1.4       cvs      2609:    /* Specific initializations for POST and GET */
1.116     cvs      2610:    if (mode & AMAYA_FORM_POST)
                   2611:      {
                   2612:        me->method = METHOD_POST;
                   2613:        HTRequest_setMethod (me->request, METHOD_POST);
                   2614:      }
                   2615:    else 
                   2616:      {
                   2617:        me->method = METHOD_GET;
                   2618:        if (!HasKnownFileSuffix (ref))
                   2619:         HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.193     cvs      2620:        HTRequest_setLanguage (me->request, acceptLanguages, TRUE);
1.116     cvs      2621:      }
1.93      cvs      2622: 
1.116     cvs      2623:    /* Common initialization for all HTML methods */
1.4       cvs      2624:    me->mode = mode;
                   2625:    me->error_html = error_html;
                   2626:    me->incremental_cbf = incremental_cbf;
                   2627:    me->context_icbf = context_icbf;
                   2628:    me->terminate_cbf = terminate_cbf;
                   2629:    me->context_tcbf = context_tcbf;
1.64      cvs      2630: 
1.69      cvs      2631:    /* for the async. request modes, we need to have our
1.4       cvs      2632:       own copy of outputfile and urlname
1.116     cvs      2633:       */
1.4       cvs      2634: 
1.116     cvs      2635:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   2636:      {
1.198     cvs      2637:        l = ustrlen (outputfile);
1.116     cvs      2638:        if (l > MAX_LENGTH)
                   2639:         me->outputfile = TtaGetMemory (l + 2);
                   2640:        else
                   2641:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
1.198     cvs      2642:        strcpy (me->outputfile, WideChar2ISO (outputfile));
                   2643:        l = ustrlen (urlName);
1.116     cvs      2644:        if (l > MAX_LENGTH)
                   2645:         me->urlName = TtaGetMemory (l + 2);
                   2646:        else
                   2647:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
1.198     cvs      2648:        strcpy (me->urlName, WideChar2ISO (urlName));
1.116     cvs      2649: #ifdef _WINDOWS
                   2650:      /* force windows ASYNC requests to always be non preemptive */
                   2651:      HTRequest_setPreemptive (me->request, NO);
                   2652: #endif /*_WINDOWS */
                   2653:      } /* AMAYA_ASYNC mode */ 
                   2654:    else 
                   2655: #ifdef _WINDOWS
                   2656:      {
1.198     cvs      2657:        me->outputfile = WideChar2ISO (outputfile);
                   2658:        me->urlName = WideChar2ISO (urlName);
1.116     cvs      2659:        /* force windows SYNC requests to always be non preemptive */
                   2660:        HTRequest_setPreemptive (me->request, YES);
                   2661:      }
                   2662: #else /* !_WINDOWS */
                   2663:      {
                   2664:        me->outputfile = outputfile;
                   2665:        me->urlName = urlName;
                   2666:      }
                   2667:    /***
1.136     cvs      2668:      In order to take into account the stop button, 
                   2669:      the requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1.57      cvs      2670:      we will loop until the document has been received or a stop signal
                   2671:      generated
1.77      cvs      2672:      ****/
1.116     cvs      2673:    HTRequest_setPreemptive (me->request, NO);
                   2674: #endif /* _WINDOWS */
1.61      cvs      2675: 
1.151     cvs      2676:    /*
                   2677:    ** Make sure that the first request is flushed immediately and not
                   2678:    ** buffered in the output buffer
                   2679:    */
                   2680:    if (mode & AMAYA_FLUSH_REQUEST)
                   2681:      HTRequest_setFlush(me->request, YES);
                   2682:    HTRequest_setFlush(me->request, YES);
                   2683: 
1.61      cvs      2684:    /* prepare the URLname that will be displayed in teh status bar */
                   2685:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      2686:    TtaSetStatus (me->docid, 1, 
                   2687:                 TtaGetMessage (AMAYA, AM_FETCHING),
1.198     cvs      2688:                 ISO2WideChar (me->status_urlName));
1.4       cvs      2689: 
1.198     cvs      2690:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (WideChar2ISO (ref));
1.45      cvs      2691:    TtaFreeMemory (ref);
1.177     cvs      2692:    
1.198     cvs      2693:    TtaGetEnvBoolean (TEXT("CACHE_DISCONNECTED_MODE"), &bool_tmp);
1.177     cvs      2694:    if (!bool_tmp && (mode & AMAYA_NOCACHE))
1.116     cvs      2695:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2696: 
                   2697:    /* prepare the query string and format for POST */
                   2698:    if (mode & AMAYA_FORM_POST)
1.114     cvs      2699:      {
1.116     cvs      2700:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   2701:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   2702:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   2703:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   2704:      } 
1.114     cvs      2705: 
1.127     cvs      2706:    /* create the formdata element for libwww */
                   2707:    if (formdata)
                   2708:      me->formdata = PrepareFormdata (formdata);
                   2709: 
1.116     cvs      2710:    /* do the request */
                   2711:    if (mode & AMAYA_FORM_POST)
1.119     cvs      2712:      {
1.191     cvs      2713:        /* this call doesn't give back a ThotBool */
1.119     cvs      2714:        HTParentAnchor * posted = NULL;
                   2715: 
                   2716:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   2717:                                    me->request);
                   2718:        status = posted ? YES : NO; 
                   2719:      }
1.127     cvs      2720:    else if (formdata)
                   2721:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   2722:                              me->request);
1.116     cvs      2723:    else
1.77      cvs      2724:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      2725: 
1.123     cvs      2726:    /* @@@ may need some special windows error msg here */
1.116     cvs      2727:    /* control the errors */
1.4       cvs      2728: 
1.130     cvs      2729:     if (status == NO)
1.116     cvs      2730:      /* the request invocation failed */
                   2731:      {
                   2732:        /* show an error message on the status bar */
                   2733:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   2734:        TtaSetStatus (docid, 1, 
                   2735:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   2736:                     urlName);
                   2737:        if (me->reqStatus == HT_NEW)
                   2738:         /* manually invoke the last processing that usually gets done
                   2739:            in a succesful request */
                   2740:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2741:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   2742:        /* terminate_handler wasn't called */
1.136     cvs      2743:        AHTReqContext_delete (me);
1.116     cvs      2744:      }
                   2745:    else
1.136     cvs      2746:      /* end treatment for SYNC requests */
1.120     cvs      2747:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2748:        {
1.136     cvs      2749:         /* wait here untilt the asynchronous request finishes */
1.130     cvs      2750:         status = LoopForStop (me);
1.136     cvs      2751:         /* if status returns HT_ERROR, should we invoke the callback? */
1.120     cvs      2752:         if (!HTRequest_kill (me->request))
                   2753:           AHTReqContext_delete (me);
                   2754:        }
1.130     cvs      2755: 
1.136     cvs      2756:     /* an interface problem!!! */
                   2757:     return (status == YES ? 0 : -1);
1.4       cvs      2758: }
                   2759: 
1.5       cvs      2760: /*----------------------------------------------------------------------
1.17      cvs      2761:    PutObjectWWW
                   2762:    frontend for uploading a resource to a URL. This function downloads
                   2763:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   2764:    finish the job.
                   2765: 
1.5       cvs      2766:    2 upload modes are proposed:                                       
                   2767:    AMAYA_SYNC : blocking mode                            
                   2768:    AMAYA_ASYNC : non-blocking mode                       
                   2769:    
1.4       cvs      2770:    When the function is called with the SYNC mode, the function will
                   2771:    return only when the file has been uploaded.
                   2772:    The ASYNC mode will immediately return after setting up the
                   2773:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   2774:    call back terminate_cbf, handling it the context defined in
                   2775:    context_tcbf.
                   2776: 
                   2777:    Notes:
                   2778:    At the end of a succesful request, the urlName string contains the
                   2779:    name of the actually uploaded URL. As a URL can change over the time,
                   2780:    (e.g., be redirected elsewhere), it is advised that the function
                   2781:    caller verifies the value of the urlName variable at the end of
                   2782:    a request.
                   2783: 
                   2784:    Inputs:
                   2785:    - docid  Document identifier for the set of objects being
                   2786:    retrieved.
                   2787:    - fileName A pointer to the local file to upload
                   2788:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   2789:    - mode The retrieval mode.
                   2790:    - terminate_cbf 
                   2791:    - context_icbf
                   2792:    Callback and context for a terminate handler
                   2793: 
                   2794:    Outputs:
                   2795:    - urlName The URL that was uploaded
                   2796: 
                   2797:    Returns:
                   2798:    HT_ERROR
                   2799:    HT_OK
1.5       cvs      2800:   ----------------------------------------------------------------------*/
1.4       cvs      2801: #ifdef __STDC__
1.198     cvs      2802: int                 PutObjectWWW (int docid, STRING fileName, STRING urlName, int mode, PicType contentType,
1.4       cvs      2803:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   2804: #else
1.26      cvs      2805: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      2806:                                  ,terminate_cbf, context_tcbf)
                   2807: int                 docid;
1.198     cvs      2808: STRING              urlName;
                   2809: STRING              fileName;
1.4       cvs      2810: int                 mode;
1.27      cvs      2811: PicType             contentType;
1.4       cvs      2812: TTcbf              *terminate_cbf;
                   2813: void               *context_tcbf;
                   2814: 
1.116     cvs      2815: #endif /* __STDC__ */
1.4       cvs      2816: {
1.116     cvs      2817:    AHTReqContext      *me;
1.4       cvs      2818:    int                 status;
                   2819:    int                 fd;
                   2820:    struct stat         file_stat;
1.116     cvs      2821:    char               *fileURL;
1.168     cvs      2822:    char               *etag = NULL;
1.169     cvs      2823:    HTParentAnchor     *dest_anc_parent;
1.198     cvs      2824:    STRING              tmp;
1.168     cvs      2825:    int                 UsePreconditions;
1.191     cvs      2826:    ThotBool            lost_update_check = TRUE;
1.172     cvs      2827: 
                   2828:    /* should we protect the PUT against lost updates? */
1.198     cvs      2829:    tmp = TtaGetEnvString (TEXT("ENABLE_LOST_UPDATE_CHECK"));
                   2830:    if (tmp && *tmp && ustrcasecmp (tmp, TEXT("yes")))
1.172     cvs      2831:      lost_update_check = FALSE;
1.168     cvs      2832: 
                   2833:    UsePreconditions = mode & AMAYA_USE_PRECONDITIONS;
1.4       cvs      2834: 
1.33      cvs      2835:    AmayaLastHTTPErrorMsg [0] = EOS;
                   2836:    
1.116     cvs      2837:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   2838:        || !TtaFileExist (fileName))
1.4       cvs      2839:       /* no file to be uploaded */
                   2840:       return HT_ERROR;
                   2841: 
                   2842:    /* do we support this protocol? */
1.7       cvs      2843:    if (IsValidProtocol (urlName) == NO)
                   2844:      {
                   2845:        /* return error */
1.198     cvs      2846:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL), urlName);
1.7       cvs      2847:        return HT_ERROR;
                   2848:      }
1.116     cvs      2849: 
                   2850:    /* verify the file's size */
                   2851: #ifndef _WINDOWS
1.7       cvs      2852:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.116     cvs      2853: #else 
1.198     cvs      2854:    if ((fd = uopen (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.116     cvs      2855: #endif /* _WINDOWS */
1.7       cvs      2856:      {
                   2857:        /* if we could not open the file, exit */
                   2858:        /*error msg here */
                   2859:        return (HT_ERROR);
                   2860:      }
1.4       cvs      2861: 
                   2862:    fstat (fd, &file_stat);
1.137     cvs      2863: #  ifdef _WINDOWS
                   2864:    _close (fd);
                   2865: #  else /* _WINDOWS */
1.116     cvs      2866:    close (fd);
1.137     cvs      2867: #  endif /* _WINDOWS */
1.4       cvs      2868: 
1.7       cvs      2869:    if (file_stat.st_size == 0)
                   2870:      {
                   2871:        /* file was empty */
                   2872:        /*errmsg here */
                   2873:        return (HT_ERROR);
                   2874:      }
1.116     cvs      2875: 
                   2876:    /* prepare the request context */
1.4       cvs      2877:    if (THD_TRACE)
1.116     cvs      2878:       fprintf (stderr, "file size == %u\n", (unsigned) file_stat.st_size);
1.4       cvs      2879: 
                   2880:    me = AHTReqContext_new (docid);
1.7       cvs      2881:    if (me == NULL)
                   2882:      {
1.168     cvs      2883:        /* @@ need an error message here */
1.151     cvs      2884:        TtaHandlePendingEvents (); 
1.7       cvs      2885:        return (HT_ERROR);
                   2886:      }
1.116     cvs      2887: 
1.4       cvs      2888:    me->mode = mode;
                   2889:    me->incremental_cbf = (TIcbf *) NULL;
                   2890:    me->context_icbf = (void *) NULL;
                   2891:    me->terminate_cbf = terminate_cbf;
                   2892:    me->context_tcbf = context_tcbf;
1.198     cvs      2893:    me->urlName = WideChar2ISO (urlName);
1.116     cvs      2894:    me->block_size =  file_stat.st_size;
1.17      cvs      2895:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      2896:    me->method = METHOD_PUT;
                   2897:    me->output = stdout;
1.17      cvs      2898:    /* we are not expecting to receive any input from the server */
                   2899:    me->outputfile = (char *) NULL; 
1.4       cvs      2900: 
1.134     cvs      2901: #ifdef _WINDOWS
                   2902:    /* libwww's HTParse function doesn't take into account the drive name;
1.168     cvs      2903:       so we sidestep it */
1.134     cvs      2904:    fileURL = NULL;
                   2905:    StrAllocCopy (fileURL, "file:");
1.198     cvs      2906:    StrAllocCat (fileURL, WideChar2ISO (fileName));
1.134     cvs      2907: #else
1.116     cvs      2908:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134     cvs      2909: #endif /* _WINDOWS */
1.168     cvs      2910:    me->source = HTAnchor_findAddress (fileURL);
1.116     cvs      2911:    HT_FREE (fileURL);
1.198     cvs      2912:    me->dest = HTAnchor_findAddress (WideChar2ISO (urlName));
1.169     cvs      2913:    /* we memorize the anchor's parent @ as we use it a number of times
                   2914:       in the following lines */
                   2915:    dest_anc_parent = HTAnchor_parent (me->dest);
1.168     cvs      2916: 
1.169     cvs      2917:    /*
                   2918:    **  Set the Content-Type of the file we are uploading 
                   2919:    */
                   2920:    /* we try to use any content-type previosuly associated
                   2921:       with the parent. If it doesn't exist, we try to guess it
                   2922:       from the URL */
1.198     cvs      2923:    tmp = ISO2WideChar (HTAtom_name (HTAnchor_format (dest_anc_parent)));
                   2924:    if (!tmp || !ustrcmp (tmp, TEXT("www/unknown")))
1.169     cvs      2925:      {
1.198     cvs      2926:        HTAnchor_setFormat (dest_anc_parent, AHTGuessAtom_for (ISO2WideChar (me->urlName), contentType));
                   2927:        tmp = ISO2WideChar (HTAtom_name (HTAnchor_format (dest_anc_parent)));
1.169     cvs      2928:      }
                   2929:    /* .. and we give the same type to the source anchor */
                   2930:    /* we go thru setOutputFormat, rather than change the parent's
                   2931:       anchor, as that's the place that libwww expects it to be */
                   2932: 
1.170     cvs      2933:    HTAnchor_setFormat (HTAnchor_parent (me->source),
1.198     cvs      2934:                       HTAtom_for (WideChar2ISO (tmp)));
1.169     cvs      2935:    HTRequest_setOutputFormat (me->request,
1.198     cvs      2936:                              HTAtom_for (WideChar2ISO (tmp)));
1.134     cvs      2937:    /* define other request characteristics */
1.116     cvs      2938: #ifdef _WINDOWS
1.136     cvs      2939:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2940: #else
1.134     cvs      2941:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2942: #endif /* _WINDOWS */
                   2943: 
1.151     cvs      2944:    /*
                   2945:    ** Make sure that the first request is flushed immediately and not
                   2946:    ** buffered in the output buffer
                   2947:    */
                   2948:    if (mode & AMAYA_FLUSH_REQUEST)
                   2949:      HTRequest_setFlush(me->request, YES);
1.168     cvs      2950:    
                   2951:    /* Should we use preconditions? */
1.172     cvs      2952:    if (lost_update_check)
1.168     cvs      2953:      {
1.172     cvs      2954:        if (UsePreconditions) 
                   2955:         etag = HTAnchor_etag (HTAnchor_parent (me->dest));
                   2956:        
                   2957:        if (etag) 
                   2958:         {
                   2959:           HTRequest_setPreconditions(me->request, HT_MATCH_THIS);
                   2960:         }
                   2961:        else
                   2962:         {
                   2963:           HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   2964:           HTRequest_addAfter(me->request, check_handler, NULL, NULL, HT_ALL,
                   2965:                              HT_FILTER_MIDDLE, YES);
1.175     cvs      2966:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL, 
                   2967:                               HT_NO_ACCESS, HT_FILTER_MIDDLE, YES);
                   2968:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL,
                   2969:                               HT_REAUTH, HT_FILTER_MIDDLE, YES);
                   2970:           HTRequest_addAfter (me->request, HTAuthInfoFilter, "http://*", NULL,
                   2971:                               HT_ALL, HT_FILTER_MIDDLE, YES);
                   2972:           HTRequest_addAfter (me->request, HTUseProxyFilter, "http://*", NULL,
                   2973:                               HT_USE_PROXY, HT_FILTER_MIDDLE, YES);
1.172     cvs      2974:         }
1.168     cvs      2975:      }
                   2976:    else
                   2977:      {
1.172     cvs      2978:        /* don't use preconditions */
1.168     cvs      2979:        HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   2980:      }
1.151     cvs      2981:    
1.136     cvs      2982:    /* don't use the cache while saving a document */
                   2983:    HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.116     cvs      2984: 
1.164     cvs      2985:    /* Throw away any reponse body */
                   2986:    /*
                   2987:    HTRequest_setOutputStream (me->request, HTBlackHole());        
                   2988:    */
                   2989: 
1.168     cvs      2990:    /* prepare the URLname that will be displayed in the status bar */
1.74      cvs      2991:    ChopURL (me->status_urlName, me->urlName);
1.198     cvs      2992:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING), ISO2WideChar (me->status_urlName));
1.114     cvs      2993: 
1.164     cvs      2994:    /* make the request */
1.172     cvs      2995:    if (lost_update_check && (!UsePreconditions || !etag))
1.168     cvs      2996:      status = HTHeadAnchor (me->dest, me->request);
                   2997:    else
                   2998:      status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
1.4       cvs      2999: 
1.130     cvs      3000:    if (status == YES && me->reqStatus != HT_ERR)
1.7       cvs      3001:      {
1.168     cvs      3002:        /* part of the stop button handler */
                   3003:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   3004:         status = LoopForStop (me);
1.15      cvs      3005:      }
1.136     cvs      3006:    if (!HTRequest_kill (me->request))
                   3007:      AHTReqContext_delete (me);
1.168     cvs      3008:    
1.116     cvs      3009:    TtaHandlePendingEvents ();
1.90      cvs      3010: 
1.130     cvs      3011:    return (status == YES ? 0 : -1);
1.28      cvs      3012: }
1.4       cvs      3013: 
1.5       cvs      3014: /*----------------------------------------------------------------------
1.138     cvs      3015:   StopRequest
1.17      cvs      3016:   stops (kills) all active requests associated with a docid 
1.5       cvs      3017:   ----------------------------------------------------------------------*/
1.4       cvs      3018: #ifdef __STDC__
                   3019: void                StopRequest (int docid)
                   3020: #else
                   3021: void                StopRequest (docid)
                   3022: int                 docid;
                   3023: #endif
                   3024: {
1.140     cvs      3025:    if (Amaya && CanDoStop ())
1.138     cvs      3026:      { 
1.139     cvs      3027: #if 0 /* for later */
1.140     cvs      3028:        AHTDocId_Status    *docid_status;
                   3029:         /* verify if there are any requests at all associated with docid */
1.138     cvs      3030:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   3031:                                                          Amaya->docid_status);
                   3032:        if (docid_status == (AHTDocId_Status *) NULL)
                   3033:         return;
1.139     cvs      3034: #endif /* 0 */
1.138     cvs      3035:        /* temporary call to stop all requests, as libwww changed its API */
                   3036:        StopAllRequests (docid);
                   3037:      }
                   3038: }
                   3039: 
1.190     cvs      3040: /* @@@ the docid parameter isn't used... clean it up */
1.138     cvs      3041: /*----------------------------------------------------------------------
                   3042:   StopAllRequests
                   3043:   stops (kills) all active requests. We use the docid 
                   3044:   ----------------------------------------------------------------------*/
                   3045: #ifdef __STDC__
                   3046: void                StopAllRequests (int docid)
                   3047: #else
1.140     cvs      3048: void                StopAllRequests (docid)
1.138     cvs      3049: int                 docid;
                   3050: #endif
                   3051: {
1.4       cvs      3052:    HTList             *cur;
                   3053:    AHTReqContext      *me;
1.191     cvs      3054:    static ThotBool     lock_stop = 0;
                   3055:    ThotBool            async_flag;
1.116     cvs      3056: 
1.138     cvs      3057:    /* only do the stop if we're not being called while processing a 
1.146     cvs      3058:       request, and if we're not already dealing with a stop */
                   3059:    if (Amaya && CanDoStop () && !lock_stop)
1.7       cvs      3060:      {
1.100     cvs      3061: #ifdef DEBUG_LIBWWW
1.138     cvs      3062:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3063:                "before kill: %d\n", Amaya->open_requests);
1.100     cvs      3064: #endif /* DEBUG_LIBWWW */
1.150     cvs      3065:        /* enter the critical section */
1.146     cvs      3066:        lock_stop = TRUE; 
1.164     cvs      3067:        /* expire all outstanding timers */
                   3068:        HTTimer_expireAll ();
1.160     cvs      3069:        /* HTNet_killAll (); */
1.116     cvs      3070:        cur = Amaya->reqlist;
                   3071:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   3072:         {
1.140     cvs      3073:           if (AmayaIsAlive ())
1.160     cvs      3074:             {
1.161     cvs      3075: #ifdef DEBUG_LIBWWW
1.163     cvs      3076:               fprintf (stderr,"StopRequest: killing req %p, url %s, status %d\n", me, me->urlName, me->reqStatus);
1.161     cvs      3077: #endif /* DEBUG_LIBWWW */
                   3078: 
1.165     cvs      3079:               if (me->reqStatus != HT_END && me->reqStatus != HT_ABORT)
1.163     cvs      3080:                 {
                   3081:                   if ((me->mode & AMAYA_ASYNC)
                   3082:                       || (me->mode & AMAYA_IASYNC))
                   3083:                     async_flag = TRUE;
                   3084:                   else
                   3085:                     async_flag = FALSE;
                   3086: 
1.165     cvs      3087:                   /* change the status to say that the request aborted */
                   3088:                   me->reqStatus = HT_ABORT;
                   3089: 
1.163     cvs      3090:                   /* kill the request, using the appropriate function */
                   3091:                   if (me->request->net)
                   3092:                       HTNet_killPipe (me->request->net);
                   3093:                   else
                   3094:                     {
1.160     cvs      3095:                       if (me->terminate_cbf)
1.198     cvs      3096:                         (*me->terminate_cbf) (me->docid, -1, ISO2WideChar (me->urlName),
                   3097:                                               ISO2WideChar (me->outputfile),
                   3098:                                               ISO2WideChar (me->content_type), 
1.160     cvs      3099:                                               me->context_tcbf);
1.164     cvs      3100: 
                   3101:                       if (async_flag) 
1.165     cvs      3102:                         /* explicitly free the request context for async
                   3103:                          requests. The sync requests context is freed by LoopForStop */
1.164     cvs      3104:                           AHTReqContext_delete (me);
1.160     cvs      3105:                     }
1.163     cvs      3106:                   cur = Amaya->reqlist;
                   3107:                 }
1.142     cvs      3108: #ifndef _WINDOWS
                   3109: #ifdef WWW_XWINDOWS
1.140     cvs      3110:           /* to be on the safe side, remove all outstanding X events */
1.160     cvs      3111:                   else 
                   3112:                     RequestKillAllXtevents (me);
1.142     cvs      3113: #endif /* WWW_XWINDOWS */
                   3114: #endif /* !_WINDOWS */
1.160     cvs      3115:             }
1.116     cvs      3116:         }
1.150     cvs      3117:        /* Delete remaining channels */
                   3118:        HTChannel_safeDeleteAll ();
                   3119:        /* exit the critical section */
1.148     cvs      3120:        lock_stop = FALSE; 
1.100     cvs      3121: #ifdef DEBUG_LIBWWW
1.138     cvs      3122:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3123:                "after kill: %d\n", Amaya->open_requests);
1.100     cvs      3124: #endif /* DEBUG_LIBWWW */
1.138     cvs      3125:      }
                   3126: } /* StopAllRequests */
1.17      cvs      3127: 
                   3128: 
1.105     cvs      3129: /*----------------------------------------------------------------------
                   3130:   AmayaIsAlive
1.140     cvs      3131:   returns the value of the AmayaAlive_flag
1.105     cvs      3132:   ----------------------------------------------------------------------*/
                   3133: #ifdef __STDC__
1.191     cvs      3134: ThotBool AmayaIsAlive (void)
1.105     cvs      3135: #else
1.191     cvs      3136: ThotBool AmayaIsAlive ()
1.105     cvs      3137: #endif /* _STDC_ */
                   3138: {
1.140     cvs      3139:   return AmayaAlive_flag;
                   3140: }
                   3141: 
                   3142: /*----------------------------------------------------------------------
                   3143:   CanDoStop
                   3144:   returns the value of the CanDoStop flag
                   3145:   ----------------------------------------------------------------------*/
                   3146: #ifdef __STDC__
1.191     cvs      3147: ThotBool CanDoStop (void)
1.140     cvs      3148: #else
1.191     cvs      3149: ThotBool CanDoStop ()
1.140     cvs      3150: #endif /* _STDC_ */
                   3151: {
                   3152:   return CanDoStop_flag;
                   3153: }
                   3154: 
                   3155: /*----------------------------------------------------------------------
                   3156:   CanDoStop_set
                   3157:   sets the value of the CanDoStop flag
                   3158:   ----------------------------------------------------------------------*/
                   3159: #ifdef __STDC__
1.191     cvs      3160: void CanDoStop_set (ThotBool value)
1.140     cvs      3161: #else
                   3162: void CanDoStop (value)
1.191     cvs      3163: ThotBool value;
1.140     cvs      3164: #endif /* _STDC_ */
                   3165: {
                   3166:   CanDoStop_flag = value;
1.176     cvs      3167: }
                   3168: 
                   3169: #ifdef __STDC__
                   3170: void libwww_updateNetworkConf (int status)
                   3171: #else
                   3172: void libwww_updateNetworkConf (status)
                   3173: int status;
                   3174: #endif /*__STDC__*/
                   3175: {
                   3176:   /* @@@ the docid parameter isn't used... clean it up */
                   3177:   int docid = 1;
                   3178: 
                   3179:   /* first, stop all current requests, as the network
                   3180:    may make some changes */
                   3181:   StopAllRequests (docid);
                   3182: 
                   3183:   if (status & AMAYA_PROXY_RESTART)
                   3184:     {
                   3185:       HTProxy_deleteAll ();
                   3186:       ProxyInit ();
                   3187:     }
                   3188: 
                   3189:   if (status & AMAYA_CACHE_RESTART)
                   3190:     {
                   3191:       clear_cachelock ();
                   3192:       HTCacheTerminate ();
                   3193:       HTCacheMode_setEnabled (NO);
                   3194:       CacheInit ();
                   3195:     }
1.193     cvs      3196: 
                   3197:   if (status & AMAYA_LANNEG_RESTART)
                   3198:     {
                   3199:       /* clear the current values */
                   3200:       if (acceptLanguages)
                   3201:        HTLanguage_deleteAll (acceptLanguages);
                   3202:       /* read in the new ones */
                   3203:       acceptLanguages = HTList_new ();
                   3204:       AHTAcceptLanguagesInit (acceptLanguages);
                   3205:     }
1.105     cvs      3206: }
1.116     cvs      3207: 
1.69      cvs      3208: #endif /* AMAYA_JAVA */
1.77      cvs      3209: 
1.116     cvs      3210: /*
                   3211:   end of Module query.c
                   3212: */

Webmaster