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

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

Webmaster