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

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

Webmaster