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

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

Webmaster