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

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

Webmaster