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

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

Webmaster