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

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

Webmaster