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

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

Webmaster