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

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);
                   1803:         if (status & XtIMXEvent) {
                   1804:             XtAppNextEvent (app_cont, &ev);
                   1805:            TtaHandleOneEvent (&ev);
                   1806:         } else if (status & (XtIMAll & (~XtIMXEvent))) {
                   1807:                 XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
                   1808:         } else {
                   1809:                XtAppNextEvent (app_cont, &ev);
                   1810:               TtaHandleOneEvent (&ev);
                   1811:         }
                   1812:    }
1.136     cvs      1813: #endif /* _WINDOWS */
1.69      cvs      1814:    switch (me->reqStatus) {
                   1815:          case HT_ERR:
                   1816:           case HT_ABORT:
1.130     cvs      1817:               status_req = NO;
1.15      cvs      1818:               break;
                   1819: 
1.69      cvs      1820:          case HT_END:
1.130     cvs      1821:               status_req = YES;
1.15      cvs      1822:               break;
                   1823: 
1.69      cvs      1824:          default:
1.15      cvs      1825:               break;
1.69      cvs      1826:    }
1.15      cvs      1827:    return (status_req);
1.4       cvs      1828: }
                   1829: 
1.5       cvs      1830: /*----------------------------------------------------------------------
1.15      cvs      1831:   QueryClose
1.21      cvs      1832:   closes all existing threads, frees all non-automatically deallocated
                   1833:   memory and then ends libwww.
1.5       cvs      1834:   ----------------------------------------------------------------------*/
1.116     cvs      1835: void QueryClose ()
1.4       cvs      1836: {
1.24      cvs      1837: 
1.140     cvs      1838:   AmayaAlive_flag = FALSE;
1.24      cvs      1839: 
1.116     cvs      1840:   /* remove all the handlers and callbacks that may output a message to
                   1841:      a non-existent Amaya window */
1.130     cvs      1842: #ifndef _WINDOWS
1.116     cvs      1843:   HTNet_deleteAfter (AHTLoadTerminate_handler);
1.130     cvs      1844: #endif _WINDOWS
1.116     cvs      1845:   HTNet_deleteAfter (redirection_handler);
                   1846:   HTAlertCall_deleteAll (HTAlert_global () );
                   1847:   HTAlert_setGlobal ((HTList *) NULL);
                   1848:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   1849:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   1850: #ifndef _WINDOWS
                   1851:   /** need to erase all existing timers too **/
                   1852:    HTTimer_registerSetTimerCallback (NULL);
                   1853:    HTTimer_registerDeleteTimerCallback (NULL);
                   1854: #endif /* !_WINDOWS */
                   1855:   HTHost_setActivateRequestCallback (NULL);
                   1856:   Thread_deleteAll ();
1.21      cvs      1857:  
1.116     cvs      1858:   HTProxy_deleteAll ();
                   1859:   HTNoProxy_deleteAll ();
                   1860:   HTGateway_deleteAll ();
                   1861:   AHTProfile_delete ();
                   1862: }
                   1863: 
                   1864: /*----------------------------------------------------------------------
                   1865:   NextNameValue
                   1866:   ---------------------------------------------------------------------*/
                   1867: #ifdef __STDC__
                   1868: static char * NextNameValue (char ** pstr, char **name, char **value)
                   1869: #else
                   1870: static char * NextNameValue (pstr, name, value);
                   1871: char ** pstr;
                   1872: char **name;
                   1873: char **value;
                   1874: #endif /* __STDC__ */
                   1875: {
                   1876:   char * p = *pstr;
                   1877:   char * start = NULL;
                   1878:   if (!pstr || !*pstr) return NULL;
                   1879:   
                   1880:     if (!*p) {
                   1881:       *pstr = p;
                   1882:       *name = NULL;
                   1883:       *value = NULL;
                   1884:       return NULL;                                      /* No field */
                   1885:     }
                   1886:     
                   1887:     /* Now search for the next '&' and ';' delimitators */
                   1888:     start = p;
                   1889:     while (*p && *p != '&' && *p != ';') p++;
                   1890:     if (*p) 
                   1891:       *p++ = '\0';
                   1892:     *pstr = p;
                   1893: 
                   1894:     /* Search for the name and value */
                   1895:     *name = start;
                   1896:     p = start;
                   1897:     
                   1898:     while(*p && *p != '=') 
                   1899:       p++;
                   1900:     if (*p) 
                   1901:       *p++ = '\0';
                   1902:     *value = p;
                   1903: 
                   1904:     return start;
                   1905: }
                   1906: 
                   1907: /*----------------------------------------------------------------------
                   1908:   PrepareFormdata
                   1909:   ---------------------------------------------------------------------*/
                   1910: #ifdef __STDC__
                   1911: static HTAssocList * PrepareFormdata (char *string)
                   1912: #else
                   1913: static HTAssocList * PrepareFormdata (string)
                   1914: char *string;
                   1915: #endif /* __STDC__ */
                   1916: {
                   1917:   char * tmp_string, * tmp_string_ptr;
                   1918:   char * name;
                   1919:   char * value;
                   1920:   HTAssocList * formdata;
                   1921: 
                   1922:   if (!string)
                   1923:     return NULL;
                   1924: 
                   1925:   /* store the ptr in another variable, as the original address will
                   1926:      change
                   1927:      */
                   1928: 
                   1929:   tmp_string_ptr = tmp_string = TtaStrdup (string);
                   1930:   formdata = HTAssocList_new();
                   1931:   
                   1932:   while (*tmp_string)
                   1933:     {
                   1934:       NextNameValue (&tmp_string, &name, &value);
                   1935:       HTAssocList_addObject(formdata,
                   1936:                            name, value);
                   1937:     }
                   1938: 
                   1939:   TtaFreeMemory (tmp_string_ptr);
                   1940:   return formdata;
1.4       cvs      1941: }
                   1942: 
1.99      cvs      1943: 
                   1944: /*----------------------------------------------------------------------
                   1945:   InvokeGetObjectWWW_callback
                   1946:   A simple function to invoke a callback function whenever there's an error
                   1947:   in GetObjectWWW
                   1948:   ---------------------------------------------------------------------*/
                   1949: 
1.116     cvs      1950: #ifdef __STDC__
                   1951: void      InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      1952: #else
1.111     cvs      1953: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      1954: int docid;
                   1955: char *urlName;
                   1956: char *outputfile;
                   1957: TTcbf *terminate_cbf;
                   1958: void *context_tcbf;
1.116     cvs      1959: #endif /* __STDC__ */
1.99      cvs      1960: {
                   1961:   if (!terminate_cbf)
                   1962:     return;
                   1963:   
1.116     cvs      1964:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      1965:                    NULL, context_tcbf);  
                   1966: }
                   1967: 
                   1968: 
                   1969: 
1.5       cvs      1970: /*----------------------------------------------------------------------
1.15      cvs      1971:    GetObjectWWW
1.17      cvs      1972:    this function requests a resource designated by a URLname into a
                   1973:    temporary filename. The download can come from a simple GET operation,
                   1974:    or can come from POSTING/GETTING a form. In the latter
                   1975:    case, the function receives a query string to send to the server.
                   1976: 
1.5       cvs      1977:    4  file retrieval modes are proposed:                              
                   1978:    AMAYA_SYNC : blocking mode                            
                   1979:    AMAYA_ISYNC : incremental, blocking mode              
                   1980:    AMAYA_ASYNC : non-blocking mode                       
                   1981:    AMAYA_IASYNC : incremental, non-blocking mode         
                   1982:    
                   1983:    In the incremental mode, each time a package arrives, it will be   
                   1984:    stored in the temporary file. In addition, if an                   
                   1985:    incremental_callback function is defined, this function will be    
                   1986:    called and handled a copy of the newly received data package.      
                   1987:    Finally, if a terminate_callback function is defined, it will be   
                   1988:    invoked when the request terminates. The caller of this function
1.4       cvs      1989:    can define two different contexts to be passed to the callback
                   1990:    functions.
                   1991: 
                   1992:    When the function is called with the SYNC mode, the function will
                   1993:    return only when the requested file has been loaded.
                   1994:    The ASYNC mode will immediately return after setting up the
                   1995:    call.
                   1996: 
                   1997:    Notes:
                   1998:    At the end of a succesful request, the urlName string contains the
                   1999:    name of the actually retrieved URL. As a URL can change over the time,
                   2000:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      2001:    caller verify the value of the urlName variable at the end of
1.4       cvs      2002:    a request.
                   2003: 
                   2004:    Inputs:
                   2005:    - docid  Document identifier for the set of objects being
                   2006:    retrieved.
                   2007:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   2008:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   2009:    - mode The retrieval mode.
                   2010:    - incremental_cbf 
                   2011:    - context_icbf
                   2012:    Callback and context for the incremental modes
                   2013:    - terminate_cbf 
                   2014:    - context_icbf
                   2015:    Callback and context for a terminate handler
1.17      cvs      2016:    -error_html if TRUE, then display any server error message as an
                   2017:    HTML document.
1.88      cvs      2018:    - content_type a string
                   2019:  
1.4       cvs      2020:    Outputs:
                   2021:    - urlName The URL that was retrieved
                   2022:    - outputfile The name of the temporary file which holds the
                   2023:    retrieved data. (Only in case of success)
1.88      cvs      2024:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   2025:      sent in the HTTP answer
1.4       cvs      2026:    Returns:
                   2027:    HT_ERROR
                   2028:    HT_OK
1.5       cvs      2029:  
                   2030:   ----------------------------------------------------------------------*/
1.4       cvs      2031: #ifdef __STDC__
1.127     cvs      2032: int GetObjectWWW (int docid, char* urlName, char* formdata,
1.116     cvs      2033:                  char* outputfile, int mode, TIcbf* incremental_cbf, 
                   2034:                  void* context_icbf, TTcbf* terminate_cbf, 
1.88      cvs      2035:                  void* context_tcbf, boolean error_html, char *content_type)
1.4       cvs      2036: #else
1.127     cvs      2037: int GetObjectWWW (docid, urlName, formdata, outputfile, mode, 
1.116     cvs      2038:                  incremental_cbf, context_icbf, 
1.88      cvs      2039:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      2040: int           docid;
                   2041: char         *urlName;
1.127     cvs      2042: char         *formdata;
1.73      cvs      2043: char         *outputfile;
                   2044: int           mode;
                   2045: TIcbf        *incremental_cbf;
                   2046: void         *context_icbf;
                   2047: TTcbf        *terminate_cbf;
                   2048: void         *context_tcbf;
                   2049: boolean       error_html;
1.88      cvs      2050: char        *content_type;
1.4       cvs      2051: #endif
                   2052: {
                   2053:    AHTReqContext      *me;
                   2054:    char               *ref;
1.107     cvs      2055:    int                 status, l;
1.114     cvs      2056:    int                 tempsubdir;
1.7       cvs      2057: 
1.116     cvs      2058:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   2059:      {
                   2060:        /* no file to be loaded */
                   2061:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      2062:        
1.116     cvs      2063:        if (error_html)
1.69      cvs      2064:         /* so we can show the error message */
                   2065:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2066:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2067:                                    context_tcbf, HT_ERROR);
                   2068:        return HT_ERROR;
                   2069:      }
1.7       cvs      2070: 
1.4       cvs      2071:    /* do we support this protocol? */
1.116     cvs      2072:    if (IsValidProtocol (urlName) == NO) 
                   2073:      {
                   2074:        /* return error */
                   2075:        outputfile[0] = EOS;    /* file could not be opened */
                   2076:        TtaSetStatus (docid, 1, 
                   2077:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   2078:                     urlName);
                   2079: 
                   2080:        if (error_html)
1.69      cvs      2081:         /* so we can show the error message */
                   2082:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2083:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2084:                                    context_tcbf, HT_ERROR);
                   2085:        return HT_ERROR;
                   2086:      }
1.4       cvs      2087: 
1.114     cvs      2088:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   2089:       named after their own docid */
                   2090:    
                   2091:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   2092: 
1.116     cvs      2093:    /* create a tempfilename */
                   2094:    sprintf (outputfile, "%s%c%d%c%04dAM", 
                   2095:            TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
                   2096:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      2097:    object_counter++;
1.116     cvs      2098:    
1.4       cvs      2099:    /* normalize the URL */
1.45      cvs      2100:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      2101:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      2102:    if (ref == (char*) NULL || ref[0] == EOS) {
                   2103:       /*error */
                   2104:       outputfile[0] = EOS;
                   2105:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   2106:       
                   2107:       if (error_html)
1.116     cvs      2108:        /* so we can show the error message */
                   2109:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      2110:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      2111:                                   context_tcbf, HT_ERROR);
1.69      cvs      2112:       return HT_ERROR;
                   2113:    }
1.116     cvs      2114: 
1.4       cvs      2115:    /* verify if that file name existed */
1.9       cvs      2116:    if (TtaFileExist (outputfile))
1.77      cvs      2117:      TtaFileUnlink (outputfile);
1.116     cvs      2118:    
1.4       cvs      2119:    /* Initialize the request structure */
                   2120:    me = AHTReqContext_new (docid);
1.116     cvs      2121:    if (me == NULL) 
                   2122:      {
                   2123:        outputfile[0] = EOS;
                   2124:        /* need an error message here */
                   2125:        TtaFreeMemory (ref);
                   2126:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2127:                                   context_tcbf, HT_ERROR);
                   2128:        return HT_ERROR;
                   2129:      }
                   2130: 
1.4       cvs      2131:    /* Specific initializations for POST and GET */
1.116     cvs      2132:    if (mode & AMAYA_FORM_POST)
                   2133:      {
                   2134:        me->method = METHOD_POST;
                   2135:        HTRequest_setMethod (me->request, METHOD_POST);
                   2136:      }
                   2137:    else 
                   2138:      {
                   2139:        me->method = METHOD_GET;
                   2140:        if (!HasKnownFileSuffix (ref))
                   2141:         HTRequest_setConversion(me->request, acceptTypes, TRUE);
                   2142:      }
1.93      cvs      2143: 
1.116     cvs      2144:    /* Common initialization for all HTML methods */
1.4       cvs      2145:    me->mode = mode;
                   2146:    me->error_html = error_html;
                   2147:    me->incremental_cbf = incremental_cbf;
                   2148:    me->context_icbf = context_icbf;
                   2149:    me->terminate_cbf = terminate_cbf;
                   2150:    me->context_tcbf = context_tcbf;
1.64      cvs      2151: 
1.69      cvs      2152:    /* for the async. request modes, we need to have our
1.4       cvs      2153:       own copy of outputfile and urlname
1.116     cvs      2154:       */
1.4       cvs      2155: 
1.116     cvs      2156:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   2157:      {
                   2158:        l = strlen (outputfile);
                   2159:        if (l > MAX_LENGTH)
                   2160:         me->outputfile = TtaGetMemory (l + 2);
                   2161:        else
                   2162:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
                   2163:        strcpy (me->outputfile, outputfile);
                   2164:        l = strlen (urlName);
                   2165:        if (l > MAX_LENGTH)
                   2166:         me->urlName = TtaGetMemory (l + 2);
                   2167:        else
                   2168:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
                   2169:        strcpy (me->urlName, urlName);
                   2170: #ifdef _WINDOWS
                   2171:      /* force windows ASYNC requests to always be non preemptive */
                   2172:      HTRequest_setPreemptive (me->request, NO);
                   2173: #endif /*_WINDOWS */
                   2174:      } /* AMAYA_ASYNC mode */ 
                   2175:    else 
                   2176: #ifdef _WINDOWS
                   2177:      {
                   2178:        me->outputfile = outputfile;
                   2179:        me->urlName = urlName;
                   2180:        /* force windows SYNC requests to always be non preemptive */
                   2181:        HTRequest_setPreemptive (me->request, YES);
                   2182:      }
                   2183: #else /* !_WINDOWS */
                   2184:      {
                   2185:        me->outputfile = outputfile;
                   2186:        me->urlName = urlName;
                   2187:      }
                   2188:    /***
1.136     cvs      2189:      In order to take into account the stop button, 
                   2190:      the requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1.57      cvs      2191:      we will loop until the document has been received or a stop signal
                   2192:      generated
1.77      cvs      2193:      ****/
1.116     cvs      2194:    HTRequest_setPreemptive (me->request, NO);
                   2195: #endif /* _WINDOWS */
1.61      cvs      2196: 
                   2197:    /* prepare the URLname that will be displayed in teh status bar */
                   2198:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      2199:    TtaSetStatus (me->docid, 1, 
                   2200:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   2201:                 me->status_urlName);
1.4       cvs      2202: 
                   2203:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      2204:    TtaFreeMemory (ref);
1.4       cvs      2205: 
1.114     cvs      2206:    if (mode & AMAYA_NOCACHE) 
1.116     cvs      2207:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.143   ! cvs      2208:    else
        !          2209:      HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH_MEM);
1.116     cvs      2210: 
                   2211:    /* prepare the query string and format for POST */
                   2212:    if (mode & AMAYA_FORM_POST)
1.114     cvs      2213:      {
1.116     cvs      2214:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   2215:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   2216:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   2217:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   2218:      } 
1.114     cvs      2219: 
1.127     cvs      2220:    /* create the formdata element for libwww */
                   2221:    if (formdata)
                   2222:      me->formdata = PrepareFormdata (formdata);
                   2223: 
1.116     cvs      2224:    /* do the request */
                   2225:    if (mode & AMAYA_FORM_POST)
1.119     cvs      2226:      {
                   2227:        /* this call doesn't give back a boolean */
                   2228:        HTParentAnchor * posted = NULL;
                   2229: 
                   2230:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   2231:                                    me->request);
                   2232:        status = posted ? YES : NO; 
                   2233:      }
1.127     cvs      2234:    else if (formdata)
                   2235:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   2236:                              me->request);
1.116     cvs      2237:    else
1.77      cvs      2238:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      2239: 
1.123     cvs      2240:    /* @@@ may need some special windows error msg here */
1.116     cvs      2241:    /* control the errors */
1.4       cvs      2242: 
1.130     cvs      2243:     if (status == NO)
1.116     cvs      2244:      /* the request invocation failed */
                   2245:      {
                   2246:        /* show an error message on the status bar */
                   2247:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   2248:        TtaSetStatus (docid, 1, 
                   2249:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   2250:                     urlName);
                   2251:        if (me->reqStatus == HT_NEW)
                   2252:         /* manually invoke the last processing that usually gets done
                   2253:            in a succesful request */
                   2254:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2255:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   2256:        /* terminate_handler wasn't called */
1.136     cvs      2257:        AHTReqContext_delete (me);
1.116     cvs      2258:      }
                   2259:    else
1.136     cvs      2260:      /* end treatment for SYNC requests */
1.120     cvs      2261:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2262:        {
1.136     cvs      2263:         /* wait here untilt the asynchronous request finishes */
1.130     cvs      2264:         status = LoopForStop (me);
1.136     cvs      2265:         /* if status returns HT_ERROR, should we invoke the callback? */
1.120     cvs      2266:         if (!HTRequest_kill (me->request))
                   2267:           AHTReqContext_delete (me);
                   2268:        }
1.130     cvs      2269: 
1.136     cvs      2270:     /* an interface problem!!! */
                   2271:     return (status == YES ? 0 : -1);
1.4       cvs      2272: }
                   2273: 
1.5       cvs      2274: /*----------------------------------------------------------------------
1.17      cvs      2275:    PutObjectWWW
                   2276:    frontend for uploading a resource to a URL. This function downloads
                   2277:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   2278:    finish the job.
                   2279: 
1.5       cvs      2280:    2 upload modes are proposed:                                       
                   2281:    AMAYA_SYNC : blocking mode                            
                   2282:    AMAYA_ASYNC : non-blocking mode                       
                   2283:    
1.4       cvs      2284:    When the function is called with the SYNC mode, the function will
                   2285:    return only when the file has been uploaded.
                   2286:    The ASYNC mode will immediately return after setting up the
                   2287:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   2288:    call back terminate_cbf, handling it the context defined in
                   2289:    context_tcbf.
                   2290: 
                   2291:    Notes:
                   2292:    At the end of a succesful request, the urlName string contains the
                   2293:    name of the actually uploaded URL. As a URL can change over the time,
                   2294:    (e.g., be redirected elsewhere), it is advised that the function
                   2295:    caller verifies the value of the urlName variable at the end of
                   2296:    a request.
                   2297: 
                   2298:    Inputs:
                   2299:    - docid  Document identifier for the set of objects being
                   2300:    retrieved.
                   2301:    - fileName A pointer to the local file to upload
                   2302:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   2303:    - mode The retrieval mode.
                   2304:    - terminate_cbf 
                   2305:    - context_icbf
                   2306:    Callback and context for a terminate handler
                   2307: 
                   2308:    Outputs:
                   2309:    - urlName The URL that was uploaded
                   2310: 
                   2311:    Returns:
                   2312:    HT_ERROR
                   2313:    HT_OK
1.5       cvs      2314:   ----------------------------------------------------------------------*/
1.4       cvs      2315: #ifdef __STDC__
1.27      cvs      2316: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      2317:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   2318: #else
1.26      cvs      2319: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      2320:                                  ,terminate_cbf, context_tcbf)
                   2321: int                 docid;
                   2322: char               *urlName;
                   2323: char               *fileName;
                   2324: int                 mode;
1.27      cvs      2325: PicType             contentType;
1.4       cvs      2326: TTcbf              *terminate_cbf;
                   2327: void               *context_tcbf;
                   2328: 
1.116     cvs      2329: #endif /* __STDC__ */
1.4       cvs      2330: {
1.116     cvs      2331:    AHTReqContext      *me;
1.4       cvs      2332:    int                 status;
                   2333:    int                 fd;
                   2334:    struct stat         file_stat;
1.116     cvs      2335:    char               *fileURL;
1.4       cvs      2336: 
1.33      cvs      2337:    AmayaLastHTTPErrorMsg [0] = EOS;
                   2338:    
1.116     cvs      2339:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   2340:        || !TtaFileExist (fileName))
1.4       cvs      2341:       /* no file to be uploaded */
                   2342:       return HT_ERROR;
                   2343: 
                   2344:    /* do we support this protocol? */
1.7       cvs      2345:    if (IsValidProtocol (urlName) == NO)
                   2346:      {
                   2347:        /* return error */
1.77      cvs      2348:        TtaSetStatus (docid, 1, 
                   2349:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      2350:                      urlName);
                   2351:        return HT_ERROR;
                   2352:      }
1.116     cvs      2353: 
                   2354:    /* verify the file's size */
                   2355: #ifndef _WINDOWS
1.7       cvs      2356:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.116     cvs      2357: #else 
1.137     cvs      2358:    if ((fd = _open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.116     cvs      2359: #endif /* _WINDOWS */
1.7       cvs      2360:      {
                   2361:        /* if we could not open the file, exit */
                   2362:        /*error msg here */
                   2363:        return (HT_ERROR);
                   2364:      }
1.4       cvs      2365: 
                   2366:    fstat (fd, &file_stat);
1.137     cvs      2367: #  ifdef _WINDOWS
                   2368:    _close (fd);
                   2369: #  else /* _WINDOWS */
1.116     cvs      2370:    close (fd);
1.137     cvs      2371: #  endif /* _WINDOWS */
1.4       cvs      2372: 
1.7       cvs      2373:    if (file_stat.st_size == 0)
                   2374:      {
                   2375:        /* file was empty */
                   2376:        /*errmsg here */
                   2377:        return (HT_ERROR);
                   2378:      }
1.116     cvs      2379: 
                   2380:    /* prepare the request context */
1.4       cvs      2381: 
                   2382:    if (THD_TRACE)
1.116     cvs      2383:       fprintf (stderr, "file size == %u\n", (unsigned) file_stat.st_size);
1.4       cvs      2384: 
                   2385:    me = AHTReqContext_new (docid);
                   2386: 
1.7       cvs      2387:    if (me == NULL)
                   2388:      {
1.131     cvs      2389:        /* @@ need an error message here */
1.7       cvs      2390:        TtaHandlePendingEvents ();
                   2391:        return (HT_ERROR);
                   2392:      }
1.116     cvs      2393: 
1.4       cvs      2394:    me->mode = mode;
                   2395:    me->incremental_cbf = (TIcbf *) NULL;
                   2396:    me->context_icbf = (void *) NULL;
                   2397:    me->terminate_cbf = terminate_cbf;
                   2398:    me->context_tcbf = context_tcbf;
                   2399:    me->urlName = urlName;
1.116     cvs      2400:    me->block_size =  file_stat.st_size;
1.17      cvs      2401:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      2402:    me->method = METHOD_PUT;
                   2403:    HTRequest_setMethod (me->request, METHOD_PUT);
                   2404:    me->output = stdout;
1.17      cvs      2405:    /* we are not expecting to receive any input from the server */
                   2406:    me->outputfile = (char *) NULL; 
1.4       cvs      2407: 
1.134     cvs      2408: #ifdef _WINDOWS
                   2409:    /* libwww's HTParse function doesn't take into account the drive name;
                   2410:    so we sidestep it */
                   2411:    fileURL = NULL;
                   2412:    StrAllocCopy (fileURL, "file:");
                   2413:    StrAllocCat (fileURL, fileName);
                   2414: #else
1.116     cvs      2415:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134     cvs      2416: #endif /* _WINDOWS */
1.116     cvs      2417:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (fileURL);
                   2418:    HT_FREE (fileURL);
1.114     cvs      2419: 
1.28      cvs      2420:    /* Set the Content-Type of the file we are uploading */
1.77      cvs      2421:    HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
                   2422:                       AHTGuessAtom_for (me->urlName, contentType));
1.17      cvs      2423: 
1.134     cvs      2424:    /* associate the anchor to the request */
1.4       cvs      2425:    HTRequest_setEntityAnchor (me->request, me->anchor);
1.134     cvs      2426: 
                   2427:    /* define other request characteristics */
1.116     cvs      2428: #ifdef _WINDOWS
1.136     cvs      2429:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2430: #else
1.134     cvs      2431:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      2432: #endif /* _WINDOWS */
                   2433: 
1.136     cvs      2434:    /* don't use the cache while saving a document */
                   2435:    HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.116     cvs      2436: 
1.74      cvs      2437:    /* prepare the URLname that will be displayed in teh status bar */
                   2438:    ChopURL (me->status_urlName, me->urlName);
                   2439:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
                   2440:                     me->status_urlName);
1.114     cvs      2441: 
1.116     cvs      2442:    status = HTPutDocumentAbsolute (me->anchor, urlName, me->request);
1.4       cvs      2443: 
1.130     cvs      2444:    if (status == YES && me->reqStatus != HT_ERR)
1.7       cvs      2445:      {
                   2446:        /* part of the stop button handler */
                   2447:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
1.136     cvs      2448:            status = LoopForStop (me);
1.15      cvs      2449:      }
1.136     cvs      2450:    if (!HTRequest_kill (me->request))
                   2451:      AHTReqContext_delete (me);
                   2452: 
1.116     cvs      2453:    TtaHandlePendingEvents ();
1.90      cvs      2454: 
1.130     cvs      2455:    return (status == YES ? 0 : -1);
1.28      cvs      2456: }
1.4       cvs      2457: 
1.5       cvs      2458: /*----------------------------------------------------------------------
1.138     cvs      2459:   StopRequest
1.17      cvs      2460:   stops (kills) all active requests associated with a docid 
1.5       cvs      2461:   ----------------------------------------------------------------------*/
1.4       cvs      2462: #ifdef __STDC__
                   2463: void                StopRequest (int docid)
                   2464: #else
                   2465: void                StopRequest (docid)
                   2466: int                 docid;
                   2467: #endif
                   2468: {
1.140     cvs      2469:    if (Amaya && CanDoStop ())
1.138     cvs      2470:      { 
1.139     cvs      2471: #if 0 /* for later */
1.140     cvs      2472:        AHTDocId_Status    *docid_status;
                   2473:         /* verify if there are any requests at all associated with docid */
1.138     cvs      2474:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   2475:                                                          Amaya->docid_status);
                   2476:        if (docid_status == (AHTDocId_Status *) NULL)
                   2477:         return;
1.139     cvs      2478: #endif /* 0 */
1.138     cvs      2479:        /* temporary call to stop all requests, as libwww changed its API */
                   2480:        StopAllRequests (docid);
                   2481:      }
                   2482: }
                   2483: 
                   2484: /*----------------------------------------------------------------------
                   2485:   StopAllRequests
                   2486:   stops (kills) all active requests. We use the docid 
                   2487:   ----------------------------------------------------------------------*/
                   2488: #ifdef __STDC__
                   2489: void                StopAllRequests (int docid)
                   2490: #else
1.140     cvs      2491: void                StopAllRequests (docid)
1.138     cvs      2492: int                 docid;
                   2493: #endif
                   2494: {
1.4       cvs      2495:    HTList             *cur;
                   2496:    AHTReqContext      *me;
1.116     cvs      2497: 
1.138     cvs      2498:    /* only do the stop if we're not being called while processing a 
                   2499:       request */
1.140     cvs      2500:    if (Amaya && CanDoStop ())
1.7       cvs      2501:      {
1.138     cvs      2502: 
1.100     cvs      2503: #ifdef DEBUG_LIBWWW
1.138     cvs      2504:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   2505:                "before kill: %d\n", Amaya->open_requests);
1.100     cvs      2506: #endif /* DEBUG_LIBWWW */
1.116     cvs      2507:        
1.142     cvs      2508:           /* delete all timers first */
                   2509:           HTTimer_deleteAll ();
1.138     cvs      2510:        HTNet_killAll();
                   2511:        EventOrder_deleteAll();
1.4       cvs      2512: 
1.116     cvs      2513:        cur = Amaya->reqlist;
                   2514:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   2515:         {
1.140     cvs      2516:           if (AmayaIsAlive ())
                   2517:             if (me->reqStatus != HT_END)
                   2518:               {
                   2519:                 if (me->terminate_cbf)
                   2520:                   (*me->terminate_cbf) (me->docid, -1, me->urlName,
                   2521:                                         me->outputfile,
                   2522:                                         me->content_type, me->context_tcbf);
                   2523:                 fprintf (stderr,"StopRequest: killing req %p, url %s, status %d\n", me, me->urlName, me->reqStatus);
                   2524:                 AHTReqContext_delete (me);
                   2525:               }
1.142     cvs      2526: #ifndef _WINDOWS
                   2527: #ifdef WWW_XWINDOWS
1.140     cvs      2528:           /* to be on the safe side, remove all outstanding X events */
1.142     cvs      2529:           else 
                   2530:                  RequestKillAllXtevents (me);
                   2531: #endif /* WWW_XWINDOWS */
                   2532: #endif /* !_WINDOWS */
1.116     cvs      2533:         }
1.138     cvs      2534:        /* Delete remaining channels */
1.140     cvs      2535:        HTChannel_deleteAll();
1.116     cvs      2536:        
1.100     cvs      2537: #ifdef DEBUG_LIBWWW
1.138     cvs      2538:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   2539:                "after kill: %d\n", Amaya->open_requests);
1.100     cvs      2540: #endif /* DEBUG_LIBWWW */
1.138     cvs      2541:      }
                   2542: } /* StopAllRequests */
1.17      cvs      2543: 
                   2544: 
1.105     cvs      2545: /*----------------------------------------------------------------------
                   2546:   AmayaIsAlive
1.140     cvs      2547:   returns the value of the AmayaAlive_flag
1.105     cvs      2548:   ----------------------------------------------------------------------*/
                   2549: #ifdef __STDC__
                   2550: boolean AmayaIsAlive (void)
                   2551: #else
                   2552: boolean AmayaIsAlive ()
                   2553: #endif /* _STDC_ */
                   2554: {
1.140     cvs      2555:   return AmayaAlive_flag;
                   2556: }
                   2557: 
                   2558: /*----------------------------------------------------------------------
                   2559:   CanDoStop
                   2560:   returns the value of the CanDoStop flag
                   2561:   ----------------------------------------------------------------------*/
                   2562: #ifdef __STDC__
                   2563: boolean CanDoStop (void)
                   2564: #else
                   2565: boolean CanDoStop ()
                   2566: #endif /* _STDC_ */
                   2567: {
                   2568:   return CanDoStop_flag;
                   2569: }
                   2570: 
                   2571: /*----------------------------------------------------------------------
                   2572:   CanDoStop_set
                   2573:   sets the value of the CanDoStop flag
                   2574:   ----------------------------------------------------------------------*/
                   2575: #ifdef __STDC__
                   2576: void CanDoStop_set (boolean value)
                   2577: #else
                   2578: void CanDoStop (value)
                   2579: boolean value;
                   2580: #endif /* _STDC_ */
                   2581: {
                   2582:   CanDoStop_flag = value;
1.105     cvs      2583: }
1.116     cvs      2584: 
1.69      cvs      2585: #endif /* AMAYA_JAVA */
1.77      cvs      2586: 
1.116     cvs      2587: /*
                   2588:   end of Module query.c
                   2589: */
1.140     cvs      2590: 
                   2591: 
                   2592: 
                   2593: 
                   2594: 
                   2595: 
1.17      cvs      2596: 
                   2597: 

Webmaster