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

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

Webmaster