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

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

Webmaster