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

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

Webmaster