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

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

Webmaster