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

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.280     kahan      40: #define IMAGE_ACCEPT_NEGOTIATION "*/*;q=0.1,image/*,image/gif,image/jpeg,image/png,image/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.280     kahan    1487:    HTBind_add("svg", "image/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.265     cvs      2078:       InitInfo ("Cache", TtaGetMessage (AMAYA, AM_CANT_CREATE_CACHE));
1.116     cvs      2079: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      2080: }
                   2081: 
                   2082: /*----------------------------------------------------------------------
1.97      cvs      2083:   ProxyInit
                   2084:   Reads any proxies settings which may be declared as environmental
                   2085:   variables or in the thot.ini file. The former overrides the latter.
                   2086:   ----------------------------------------------------------------------*/
                   2087: static void ProxyInit (void)
                   2088: {
1.265     cvs      2089:   char     *strptr;
                   2090:   char     *name;
                   2091:   char     *tmp = NULL;
                   2092:   char     *strptrA;
1.223     cvs      2093:   ThotBool  proxy_is_onlyproxy;
1.97      cvs      2094: 
                   2095:   /* get the proxy settings from the thot.ini file */
1.203     cvs      2096:   strptr = TtaGetEnvString ("HTTP_PROXY");
1.228     cvs      2097:   if (strptr && *strptr)
                   2098:     {
1.265     cvs      2099:       tmp = (char *) TtaGetMemory (strlen (strptr) + 1);
                   2100:       strcpy (tmp, strptr);
1.223     cvs      2101: 
1.228     cvs      2102:       /* does the proxy env string has an "http://" prefix? */
1.265     cvs      2103:       if (!strncasecmp (strptr, "http://", 7)) 
1.223     cvs      2104:         HTProxy_add ("http", tmp);
1.228     cvs      2105:       else 
                   2106:        {
1.265     cvs      2107:          strptrA = (char *) TtaGetMemory (strlen (strptr) + 9);
1.228     cvs      2108:          strcpy (strptrA, "http://");
                   2109:          strcat (strptrA, tmp);
                   2110:          HTProxy_add ("http", strptrA);
                   2111:          TtaFreeMemory (strptrA);
                   2112:        }
                   2113:       TtaFreeMemory (tmp);
                   2114:     }
1.216     cvs      2115: 
1.97      cvs      2116:   /* get the no_proxy settings from the thot.ini file */
1.212     cvs      2117:   strptr = TtaGetEnvString ("PROXYDOMAIN");
1.228     cvs      2118:   if (strptr && *strptr) 
                   2119:     {
1.265     cvs      2120:       strptrA = (char *) TtaGetMemory (strlen (strptr) + 1);
                   2121:       tmp = strptrA;
                   2122:       strcpy (tmp, strptr);
1.228     cvs      2123:       /* as HTNextField changes the ptr we pass as an argument, we'll
1.265     cvs      2124:         work with another variable, so that we can free the tmp
1.228     cvs      2125:         block later on */
                   2126:       while ((name = HTNextField (&tmp)) != NULL) 
                   2127:        {
                   2128:          char* portstr = strchr (name, ':');
                   2129:          unsigned port=0;
1.265     cvs      2130:          if (portstr)
                   2131:            { 
                   2132:              *portstr++ = EOS;
                   2133:              if (*portstr) 
                   2134:                port = (unsigned) atoi (portstr);
                   2135:            }
1.228     cvs      2136:          /* Register it for all access methods */
                   2137:          HTNoProxy_add (name, NULL, port);
                   2138:        }
                   2139:       TtaFreeMemory (strptrA);
                   2140:     }
                   2141:   
1.212     cvs      2142:   /* how should we interpret the proxy domain list? */
                   2143:   TtaGetEnvBoolean ("PROXYDOMAIN_IS_ONLYPROXY", &proxy_is_onlyproxy);
                   2144:   HTProxy_setNoProxyIsOnlyProxy (proxy_is_onlyproxy);
                   2145: 
                   2146:   /* use libwww's routine to get all proxy settings from the environment */
1.97      cvs      2147:    HTProxy_getEnvVar ();
                   2148: }
                   2149: 
1.207     cvs      2150: /*----------------------------------------------------------------------
1.17      cvs      2151:   AHTProfile_newAmaya
                   2152:   creates the Amaya client profile for libwww.
1.15      cvs      2153:   ----------------------------------------------------------------------*/
1.265     cvs      2154: static void AHTProfile_newAmaya (char *AppName, char *AppVersion)
1.4       cvs      2155: {
1.265     cvs      2156:    char *strptr;
1.158     cvs      2157: 
1.4       cvs      2158:    /* If the Library is not already initialized then do it */
1.257     kahan    2159:    if (!HTLib_isInitialized ()) 
1.273     cvs      2160:        HTLibInit (AppName, AppVersion);
1.4       cvs      2161: 
                   2162:    if (!converters)
                   2163:       converters = HTList_new ();
1.27      cvs      2164:    if (!acceptTypes)
                   2165:       acceptTypes = HTList_new ();
1.193     cvs      2166:    if (!acceptLanguages)
                   2167:       acceptLanguages = HTList_new ();
1.151     cvs      2168:    if (!transfer_encodings)
                   2169:       transfer_encodings = HTList_new ();
                   2170:    if (!content_encodings)
                   2171:       content_encodings = HTList_new ();
1.4       cvs      2172: 
1.169     cvs      2173:    /* inhibits libwww's automatic file_suffix_binding */
                   2174:    HTFile_doFileSuffixBinding (FALSE);
                   2175: 
1.4       cvs      2176:    /* Register the default set of transport protocols */
                   2177:    HTTransportInit ();
                   2178: 
                   2179:    /* Register the default set of application protocol modules */
                   2180:    AHTProtocolInit ();
                   2181: 
1.116     cvs      2182:    /* Register the default set of messages and dialog functions */
                   2183:    AHTAlertInit ();
                   2184:    HTAlert_setInteractive (YES);
                   2185: 
                   2186: #ifdef AMAYA_WWW_CACHE
                   2187:    /* Enable the persistent cache  */
                   2188:    CacheInit ();
                   2189: #else
                   2190:    HTCacheMode_setEnabled (NO);
                   2191: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      2192: 
                   2193:    /* Register the default set of BEFORE and AFTER filters */
                   2194:    AHTNetInit ();
                   2195: 
                   2196:    /* Set up the default set of Authentication schemes */
1.223     cvs      2197:    HTAA_newModule ("basic", HTBasic_generate, HTBasic_parse, NULL, HTBasic_delete);
1.158     cvs      2198:    /* activate MDA by defaul */
1.203     cvs      2199:    strptr = TtaGetEnvString ("ENABLE_MDA");
1.265     cvs      2200:    if (!strptr || (strptr && *strptr && strcasecmp (strptr, "no")))
1.223     cvs      2201:      HTAA_newModule ("digest", HTDigest_generate, HTDigest_parse, HTDigest_updateInfo, HTDigest_delete);
1.257     kahan    2202:    
1.97      cvs      2203:    /* Get any proxy settings */
                   2204:    ProxyInit ();
1.4       cvs      2205: 
1.207     cvs      2206:    /* Set up the domains where we accept a redirect on PUT */
                   2207:    SafePut_init ();
                   2208: 
1.4       cvs      2209:    /* Register the default set of converters */
                   2210:    AHTConverterInit (converters);
1.151     cvs      2211:    HTFormat_setConversion (converters);
1.27      cvs      2212:    AHTAcceptTypesInit (acceptTypes);
1.193     cvs      2213:    AHTAcceptLanguagesInit (acceptLanguages);
1.4       cvs      2214: 
                   2215:    /* Register the default set of transfer encoders and decoders */
1.151     cvs      2216:    HTTransferEncoderInit (transfer_encodings);
1.190     cvs      2217:    HTFormat_setTransferCoding (transfer_encodings);
                   2218:    /* Register the default set of content encoders and decoders */
                   2219:    HTContentEncoderInit (content_encodings);
                   2220:    /* ignore all other encoding formats (or libwww will send them 
1.181     cvs      2221:       thru a blackhole otherwise */
                   2222:    HTCoding_add (content_encodings, "*", NULL, HTIdentityCoding, 1.0);
1.151     cvs      2223:    if (HTList_count(content_encodings) > 0)
                   2224:      HTFormat_setContentCoding(content_encodings);
                   2225:    else 
                   2226:      {
                   2227:        HTList_delete(content_encodings);
                   2228:        content_encodings = NULL;
                   2229:      }
1.4       cvs      2230: 
                   2231:    /* Register the default set of MIME header parsers */
1.74      cvs      2232:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      2233: 
                   2234:    /* Register the default set of Icons for directory listings */
1.27      cvs      2235:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      2236: }
                   2237: 
1.5       cvs      2238: /*----------------------------------------------------------------------
1.17      cvs      2239:   AHTProfile_delete
                   2240:   deletes the Amaya client profile.
1.5       cvs      2241:   ----------------------------------------------------------------------*/
1.4       cvs      2242: static void         AHTProfile_delete (void)
                   2243: {
1.22      cvs      2244:   /* free the Amaya global context */
1.151     cvs      2245:   /* Clean up all the registred converters */
                   2246:   HTFormat_deleteAll ();
                   2247:   if (acceptTypes)
1.74      cvs      2248:     HTConversion_deleteAll (acceptTypes);
1.193     cvs      2249:   if (acceptLanguages)
                   2250:     HTLanguage_deleteAll (acceptLanguages);
1.151     cvs      2251: 
1.209     cvs      2252:   StopAllRequests (1);
1.74      cvs      2253:   HTList_delete (Amaya->docid_status);
                   2254:   HTList_delete (Amaya->reqlist);
                   2255:   TtaFreeMemory (Amaya);
1.61      cvs      2256: 
1.120     cvs      2257: #ifdef _WINDOWS
1.209     cvs      2258:   if (HTLib_isInitialized ())      
1.151     cvs      2259:     HTEventTerminate ();
1.198     cvs      2260: #endif /* _WINDOWS; */         
1.74      cvs      2261:     
1.151     cvs      2262:   /* Clean up the persistent cache (if any) */
1.116     cvs      2263: #ifdef AMAYA_WWW_CACHE
1.151     cvs      2264:   clear_cachelock ();
                   2265:   HTCacheTerminate ();
1.116     cvs      2266: #endif /* AMAYA_WWW_CACHE */
1.217     cvs      2267:   
1.218     cvs      2268:   /* call remove functions that are not called automatically by libwww */
                   2269:   HTNetCall_deleteBeforeAll (HTNet_before ());
                   2270:   HTNetCall_deleteAfterAll (HTNet_after ());
                   2271:   HTAA_deleteAllModules ();
                   2272:   HTAlertCall_deleteAll (HTAlert_global () );
                   2273:   HTAlert_setGlobal ((HTList *) NULL);
1.259     cvs      2274:   HTTimer_deleteAll ();
1.249     kahan    2275:   HTEventList_unregisterAll ();
1.218     cvs      2276:   /* these two functions are broken, so we can't call them right now 
                   2277:   HTHeader_deleteAll ();
                   2278:   HTTimer_deleteAll ();
                   2279:   */
                   2280:   HTTransport_deleteAll ();
1.217     cvs      2281:   /* remove bindings between suffixes, media types*/
                   2282:   HTBind_deleteAll ();
1.151     cvs      2283:   /* Terminate libwww */
                   2284:   HTLibTerminate ();
1.4       cvs      2285: }
                   2286: 
1.5       cvs      2287: /*----------------------------------------------------------------------
1.116     cvs      2288:   AmayacontextInit
                   2289:   initializes an internal Amaya context for our libwww interface 
                   2290:   ----------------------------------------------------------------------*/
1.265     cvs      2291: static void    AmayaContextInit ()
1.116     cvs      2292: {
1.140     cvs      2293:   AmayaAlive_flag = TRUE;
1.116     cvs      2294:   /* Initialization of the global context */
                   2295:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   2296:   Amaya->reqlist = HTList_new ();
                   2297:   Amaya->docid_status = HTList_new ();
                   2298:   Amaya->open_requests = 0;
                   2299: }
                   2300: 
                   2301: /*----------------------------------------------------------------------
1.17      cvs      2302:   QueryInit
                   2303:   initializes the libwww interface 
1.5       cvs      2304:   ----------------------------------------------------------------------*/
1.265     cvs      2305: void         QueryInit ()
1.4       cvs      2306: {
1.265     cvs      2307:   char   *strptr;
                   2308:   int     tmp_i;
                   2309:   long    tmp_l;
1.4       cvs      2310: 
1.116     cvs      2311:    AmayaContextInit ();
1.4       cvs      2312:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.140     cvs      2313:    CanDoStop_set (TRUE);
1.214     cvs      2314:    UserAborted_flag = FALSE;
1.4       cvs      2315: 
1.116     cvs      2316: #ifdef _WINDOWS
1.125     cvs      2317:    HTEventInit ();
1.116     cvs      2318: #endif /* _WINDOWS */
1.72      cvs      2319: 
1.123     cvs      2320: #ifndef _WINDOWS
1.116     cvs      2321:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
                   2322:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.120     cvs      2323:    HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
                   2324:    HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.116     cvs      2325: #endif /* !_WINDOWS */
1.190     cvs      2326: 
1.213     cvs      2327: #ifdef HTDEBUG
1.247     kahan    2328:    /* an undocumented option for being able to generate an HTTP protocol
                   2329:       trace.  The flag can take values from 1-10, which are interpreted as
                   2330:       different kinds of debug traces. Value 99 means a complete trace.
                   2331:       0 or other values means No Debug.1 No Debug (default), up to Full debug, respectively. */
1.238     cvs      2332:    if (TtaGetEnvInt ("ENABLE_LIBWWW_DEBUG", &tmp_i))
1.247     kahan    2333:      {
                   2334:        switch (tmp_i)
                   2335:         {
                   2336:         case 1:
                   2337:           WWW_TraceFlag = SHOW_URI_TRACE;
                   2338:           break;
                   2339:         case 2:
                   2340:           WWW_TraceFlag = SHOW_BIND_TRACE;
                   2341:           break;
                   2342:         case 3:
                   2343:           WWW_TraceFlag = SHOW_THREAD_TRACE;
                   2344:           break;
                   2345:         case 4:
                   2346:           WWW_TraceFlag = SHOW_STREAM_TRACE;
                   2347:           break;
                   2348:         case 5:
                   2349:           WWW_TraceFlag = SHOW_PROTOCOL_TRACE;
                   2350:           break;
                   2351:         case 6:
                   2352:           WWW_TraceFlag = SHOW_MEM_TRACE;
                   2353:           break;
                   2354:         case 7:
                   2355:           WWW_TraceFlag = SHOW_URI_TRACE;
                   2356:           break;
                   2357:         case 8:
                   2358:           WWW_TraceFlag = SHOW_AUTH_TRACE;
                   2359:           break;
                   2360:         case 9:
                   2361:           WWW_TraceFlag = SHOW_ANCHOR_TRACE;
                   2362:           break;
                   2363:         case 10 :
                   2364:           WWW_TraceFlag = SHOW_CORE_TRACE; 
                   2365:           break;
1.251     kahan    2366:         case 11 :
                   2367:           WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_ANCHOR_TRACE | SHOW_AUTH_TRACE | SHOW_URI_TRACE | SHOW_THREAD_TRACE | SHOW_STREAM_TRACE;
                   2368:           break;
1.247     kahan    2369:         case 99 :
                   2370:           WWW_TraceFlag = SHOW_ALL_TRACE;
                   2371:           break;
                   2372:         default:
                   2373:           WWW_TraceFlag = 0;
                   2374:           break;
                   2375:         }
1.248     kahan    2376:        if (WWW_TraceFlag)
                   2377:         {
                   2378:           /* Trace activation (for debugging) */
1.265     cvs      2379:           char   *s, *tmp;
1.248     kahan    2380:           s = TtaGetEnvString ("APP_TMPDIR");
1.265     cvs      2381:           tmp = TtaGetMemory (strlen (s) + sizeof ("/libwww.log") + 1);
                   2382:           strcpy (tmp, s);
                   2383:           strcat (tmp, "/libwww.log");
                   2384:           trace_fp = fopen (tmp, "ab");
1.248     kahan    2385:           TtaFreeMemory (tmp);
                   2386:           if (trace_fp)
                   2387:             HTTrace_setCallback(LineTrace);
                   2388:         }
1.247     kahan    2389:      }
1.208     cvs      2390:    else
                   2391:      WWW_TraceFlag = 0;
1.213     cvs      2392: #endif /* HTDEBUG */
1.208     cvs      2393: 
1.74      cvs      2394: #ifdef DEBUG_LIBWWW
1.116     cvs      2395:   /* forwards error messages to our own function */
                   2396:    WWW_TraceFlag = THD_TRACE;
1.138     cvs      2397:    HTTrace_setCallback(LineTrace);
1.248     kahan    2398: 
1.148     cvs      2399:    /***
1.138     cvs      2400:     WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | SHOW_PROTOCOL_TRACE;
                   2401:     ***/
1.152     cvs      2402: #endif
1.247     kahan    2403: 
                   2404:    TtaGetEnvBoolean ("ENABLE_FTP", &FTPURL_flag);
1.152     cvs      2405: 
1.148     cvs      2406:    /* Setting up different network parameters */
1.151     cvs      2407: 
1.148     cvs      2408:    /* Maximum number of simultaneous open sockets */
1.203     cvs      2409:    strptr = TtaGetEnvString ("MAX_SOCKET");
1.151     cvs      2410:    if (strptr && *strptr) 
1.288     vatton   2411:      tmp_i = atoi (strptr);
1.151     cvs      2412:    else
                   2413:      tmp_i = DEFAULT_MAX_SOCKET;
                   2414:    HTNet_setMaxSocket (tmp_i);
                   2415: 
1.148     cvs      2416:    /* different network services timeouts */
1.151     cvs      2417:    /* dns timeout */
1.203     cvs      2418:    strptr = TtaGetEnvString ("DNS_TIMEOUT");
1.151     cvs      2419:    if (strptr && *strptr) 
1.288     vatton   2420:      tmp_i = atoi (strptr);
1.151     cvs      2421:    else
                   2422:      tmp_i = DEFAULT_DNS_TIMEOUT;
                   2423:    HTDNS_setTimeout (tmp_i);
                   2424: 
                   2425:    /* persistent connections timeout */
1.203     cvs      2426:    strptr = TtaGetEnvString ("PERSIST_CX_TIMEOUT");
1.151     cvs      2427:    if (strptr && *strptr) 
1.288     vatton   2428:      tmp_l = atol (strptr); 
1.151     cvs      2429:    else
                   2430:      tmp_l = DEFAULT_PERSIST_TIMEOUT;
                   2431:    HTHost_setPersistTimeout (tmp_l);
                   2432: 
1.148     cvs      2433:    /* default timeout in ms */
1.203     cvs      2434:    strptr = TtaGetEnvString ("NET_EVENT_TIMEOUT");
1.151     cvs      2435:    if (strptr && *strptr) 
1.288     vatton   2436:      tmp_i = atoi (strptr);
1.151     cvs      2437:    else
                   2438:      tmp_i = DEFAULT_NET_EVENT_TIMEOUT;
                   2439:    HTHost_setEventTimeout (tmp_i);
                   2440: 
1.173     cvs      2441:    HTRequest_setMaxRetry (8);
1.4       cvs      2442: #ifdef CATCH_SIG
1.148     cvs      2443:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      2444: #endif
1.15      cvs      2445: }
                   2446: 
                   2447: /*----------------------------------------------------------------------
1.17      cvs      2448:   LoopForStop
1.136     cvs      2449:   a copy of the Thop event loop so we can handle the stop button in Unix
                   2450:   and preemptive requests under Windows
1.15      cvs      2451:   ----------------------------------------------------------------------*/
                   2452: static int          LoopForStop (AHTReqContext * me)
                   2453: {
1.136     cvs      2454: #ifdef _WINDOWS
                   2455:   MSG msg;
                   2456:   unsigned long libwww_msg;
                   2457:   HWND old_active_window, libwww_window;
                   2458:   int  status_req = HT_OK;
                   2459: 
                   2460:   old_active_window = GetActiveWindow ();
                   2461:   libwww_window = HTEventList_getWinHandle (&libwww_msg);
1.137     cvs      2462:  
1.149     cvs      2463:   while (me->reqStatus != HT_END && me->reqStatus != HT_ERR
1.253     cvs      2464:         && me->reqStatus != HT_ABORT && AmayaIsAlive () &&
                   2465:         GetMessage (&msg, NULL, 0, 0))
                   2466:     {
                   2467:       if (msg.message != WM_QUIT)
                   2468:        TtaHandleOneEvent (&msg);
                   2469:       else
                   2470:        break;      
                   2471:     }
1.136     cvs      2472:   if (!AmayaIsAlive ())
                   2473:     /* Amaya was killed by one of the callback handlers */
                   2474:     exit (0);
                   2475: #else /* _WINDOWS */
1.277     kahan    2476:    ThotEvent                ev;
1.17      cvs      2477:    int                 status_req = HT_OK;
1.15      cvs      2478: 
                   2479:    /* to test the async calls  */
1.17      cvs      2480:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      2481:    while (me->reqStatus != HT_ABORT &&
                   2482:          me->reqStatus != HT_END &&
1.69      cvs      2483:          me->reqStatus != HT_ERR) {
1.116     cvs      2484:         if (!AmayaIsAlive ())
1.69      cvs      2485:            /* Amaya was killed by one of the callback handlers */
                   2486:            exit (0);
1.277     kahan    2487:         
                   2488:          if (TtaFetchOneAvailableEvent (&ev))
                   2489:           TtaHandleOneEvent (&ev);
1.69      cvs      2490:    }
1.136     cvs      2491: #endif /* _WINDOWS */
1.69      cvs      2492:    switch (me->reqStatus) {
                   2493:          case HT_ERR:
                   2494:           case HT_ABORT:
1.130     cvs      2495:               status_req = NO;
1.15      cvs      2496:               break;
                   2497: 
1.69      cvs      2498:          case HT_END:
1.130     cvs      2499:               status_req = YES;
1.15      cvs      2500:               break;
                   2501: 
1.69      cvs      2502:          default:
1.15      cvs      2503:               break;
1.69      cvs      2504:    }
1.15      cvs      2505:    return (status_req);
1.4       cvs      2506: }
                   2507: 
1.5       cvs      2508: /*----------------------------------------------------------------------
1.15      cvs      2509:   QueryClose
1.21      cvs      2510:   closes all existing threads, frees all non-automatically deallocated
                   2511:   memory and then ends libwww.
1.5       cvs      2512:   ----------------------------------------------------------------------*/
1.116     cvs      2513: void QueryClose ()
1.4       cvs      2514: {
1.24      cvs      2515: 
1.140     cvs      2516:   AmayaAlive_flag = FALSE;
1.24      cvs      2517: 
1.116     cvs      2518:   /* remove all the handlers and callbacks that may output a message to
                   2519:      a non-existent Amaya window */
                   2520:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   2521:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   2522: #ifndef _WINDOWS
                   2523:   /** need to erase all existing timers too **/
                   2524:    HTTimer_registerSetTimerCallback (NULL);
                   2525:    HTTimer_registerDeleteTimerCallback (NULL);
                   2526: #endif /* !_WINDOWS */
                   2527:   HTHost_setActivateRequestCallback (NULL);
                   2528:   Thread_deleteAll ();
1.21      cvs      2529:  
1.116     cvs      2530:   HTProxy_deleteAll ();
                   2531:   HTNoProxy_deleteAll ();
1.207     cvs      2532:   SafePut_delete ();
1.116     cvs      2533:   HTGateway_deleteAll ();
                   2534:   AHTProfile_delete ();
1.248     kahan    2535: 
                   2536:   /* close the trace file (if it exists) */
                   2537:   if (trace_fp)
                   2538:     fclose (trace_fp);
                   2539: 
1.116     cvs      2540: }
                   2541: 
                   2542: /*----------------------------------------------------------------------
                   2543:   NextNameValue
                   2544:   ---------------------------------------------------------------------*/
1.265     cvs      2545: static char * NextNameValue (char **pstr, char **name, char **value)
1.116     cvs      2546: {
                   2547:   char * p = *pstr;
                   2548:   char * start = NULL;
                   2549:   if (!pstr || !*pstr) return NULL;
                   2550:   
                   2551:     if (!*p) {
                   2552:       *pstr = p;
                   2553:       *name = NULL;
                   2554:       *value = NULL;
                   2555:       return NULL;                                      /* No field */
                   2556:     }
                   2557:     
                   2558:     /* Now search for the next '&' and ';' delimitators */
                   2559:     start = p;
                   2560:     while (*p && *p != '&' && *p != ';') p++;
                   2561:     if (*p) 
                   2562:       *p++ = '\0';
                   2563:     *pstr = p;
                   2564: 
                   2565:     /* Search for the name and value */
                   2566:     *name = start;
                   2567:     p = start;
                   2568:     
                   2569:     while(*p && *p != '=') 
                   2570:       p++;
                   2571:     if (*p) 
                   2572:       *p++ = '\0';
                   2573:     *value = p;
                   2574: 
                   2575:     return start;
                   2576: }
                   2577: 
                   2578: /*----------------------------------------------------------------------
                   2579:   PrepareFormdata
                   2580:   ---------------------------------------------------------------------*/
1.265     cvs      2581: static   HTAssocList * PrepareFormdata (char *string)
1.116     cvs      2582: {
1.266     cvs      2583:   char          *tmp_string, *tmp_string_ptr;
                   2584:   char          *name;
                   2585:   char          *value;
                   2586:   HTAssocList   *formdata;
1.116     cvs      2587: 
                   2588:   if (!string)
                   2589:     return NULL;
                   2590: 
                   2591:   /* store the ptr in another variable, as the original address will
                   2592:      change
                   2593:      */
1.236     cvs      2594:   
1.265     cvs      2595:   tmp_string_ptr = TtaGetMemory (strlen (string) + 1);
1.236     cvs      2596:   tmp_string = tmp_string_ptr;
1.273     cvs      2597:   strcpy (tmp_string_ptr, string);
1.116     cvs      2598:   formdata = HTAssocList_new();
                   2599:   
                   2600:   while (*tmp_string)
                   2601:     {
                   2602:       NextNameValue (&tmp_string, &name, &value);
                   2603:       HTAssocList_addObject(formdata,
                   2604:                            name, value);
                   2605:     }
                   2606: 
                   2607:   TtaFreeMemory (tmp_string_ptr);
                   2608:   return formdata;
1.4       cvs      2609: }
                   2610: 
1.236     cvs      2611: /*----------------------------------------------------------------------
                   2612:   ---------------------------------------------------------------------*/
1.216     cvs      2613: void AHTRequest_setCustomAcceptHeader (HTRequest *request, char *value)
                   2614: {                              
                   2615:   HTRqHd rqhd = HTRequest_rqHd (request);
                   2616:   rqhd = rqhd & (~HT_C_ACCEPT_TYPE);
                   2617:   HTRequest_setRqHd (request, rqhd);
1.221     cvs      2618:   HTRequest_addExtraHeader (request, "Accept", value);
1.216     cvs      2619: }
                   2620: 
1.228     cvs      2621: /*----------------------------------------------------------------------
1.99      cvs      2622:   InvokeGetObjectWWW_callback
                   2623:   A simple function to invoke a callback function whenever there's an error
                   2624:   in GetObjectWWW
                   2625:   ---------------------------------------------------------------------*/
1.265     cvs      2626: void InvokeGetObjectWWW_callback (int docid, char *urlName,
                   2627:                                  char *outputfile, TTcbf *terminate_cbf,
                   2628:                                  void *context_tcbf, int status)
1.99      cvs      2629: {
                   2630:   if (!terminate_cbf)
                   2631:     return;
                   2632:   
1.116     cvs      2633:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      2634:                    NULL, context_tcbf);  
                   2635: }
                   2636: 
                   2637: 
                   2638: 
1.5       cvs      2639: /*----------------------------------------------------------------------
1.15      cvs      2640:    GetObjectWWW
1.17      cvs      2641:    this function requests a resource designated by a URLname into a
                   2642:    temporary filename. The download can come from a simple GET operation,
                   2643:    or can come from POSTING/GETTING a form. In the latter
                   2644:    case, the function receives a query string to send to the server.
                   2645: 
1.5       cvs      2646:    4  file retrieval modes are proposed:                              
                   2647:    AMAYA_SYNC : blocking mode                            
                   2648:    AMAYA_ISYNC : incremental, blocking mode              
                   2649:    AMAYA_ASYNC : non-blocking mode                       
                   2650:    AMAYA_IASYNC : incremental, non-blocking mode         
                   2651:    
                   2652:    In the incremental mode, each time a package arrives, it will be   
                   2653:    stored in the temporary file. In addition, if an                   
                   2654:    incremental_callback function is defined, this function will be    
                   2655:    called and handled a copy of the newly received data package.      
                   2656:    Finally, if a terminate_callback function is defined, it will be   
                   2657:    invoked when the request terminates. The caller of this function
1.4       cvs      2658:    can define two different contexts to be passed to the callback
                   2659:    functions.
                   2660: 
                   2661:    When the function is called with the SYNC mode, the function will
                   2662:    return only when the requested file has been loaded.
                   2663:    The ASYNC mode will immediately return after setting up the
                   2664:    call.
                   2665: 
                   2666:    Notes:
                   2667:    At the end of a succesful request, the urlName string contains the
                   2668:    name of the actually retrieved URL. As a URL can change over the time,
                   2669:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      2670:    caller verify the value of the urlName variable at the end of
1.4       cvs      2671:    a request.
                   2672: 
                   2673:    Inputs:
                   2674:    - docid  Document identifier for the set of objects being
                   2675:    retrieved.
                   2676:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   2677:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   2678:    - mode The retrieval mode.
                   2679:    - incremental_cbf 
                   2680:    - context_icbf
                   2681:    Callback and context for the incremental modes
                   2682:    - terminate_cbf 
                   2683:    - context_icbf
                   2684:    Callback and context for a terminate handler
1.17      cvs      2685:    -error_html if TRUE, then display any server error message as an
                   2686:    HTML document.
1.88      cvs      2687:    - content_type a string
                   2688:  
1.4       cvs      2689:    Outputs:
                   2690:    - urlName The URL that was retrieved
                   2691:    - outputfile The name of the temporary file which holds the
                   2692:    retrieved data. (Only in case of success)
1.88      cvs      2693:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   2694:      sent in the HTTP answer
1.4       cvs      2695:    Returns:
                   2696:    HT_ERROR
                   2697:    HT_OK
1.5       cvs      2698:  
                   2699:   ----------------------------------------------------------------------*/
1.265     cvs      2700: int GetObjectWWW (int docid, char *urlName, char *formdata,
                   2701:                  char *outputfile, int mode, TIcbf *incremental_cbf, 
                   2702:                  void *context_icbf, TTcbf *terminate_cbf, 
                   2703:                  void *context_tcbf, ThotBool error_html, char *content_type)
1.4       cvs      2704: {
                   2705:    AHTReqContext      *me;
1.286     kahan    2706:    char *              ref;
                   2707:    char *              tmp;
                   2708:    char *              esc_url;
1.107     cvs      2709:    int                 status, l;
1.114     cvs      2710:    int                 tempsubdir;
1.191     cvs      2711:    ThotBool            bool_tmp;
1.7       cvs      2712: 
1.116     cvs      2713:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   2714:      {
                   2715:        /* no file to be loaded */
                   2716:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      2717:        
1.116     cvs      2718:        if (error_html)
1.69      cvs      2719:         /* so we can show the error message */
                   2720:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2721:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2722:                                    context_tcbf, HT_ERROR);
                   2723:        return HT_ERROR;
                   2724:      }
1.7       cvs      2725: 
1.159     cvs      2726:    /* if it's a 'docImage', we have already downloaded it */
1.265     cvs      2727:    if (!strncmp ("internal:", urlName, 9)) 
1.159     cvs      2728:      {
1.265     cvs      2729:        strcpy (outputfile, urlName);
1.159     cvs      2730:        InvokeGetObjectWWW_callback (docid, urlName, outputfile,
                   2731:                                    terminate_cbf, context_tcbf, HT_OK);
                   2732:        return HT_OK;
                   2733:      }
                   2734: 
1.4       cvs      2735:    /* do we support this protocol? */
1.116     cvs      2736:    if (IsValidProtocol (urlName) == NO) 
                   2737:      {
                   2738:        /* return error */
                   2739:        outputfile[0] = EOS;    /* file could not be opened */
                   2740:        TtaSetStatus (docid, 1, 
                   2741:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   2742:                     urlName);
                   2743: 
                   2744:        if (error_html)
1.69      cvs      2745:         /* so we can show the error message */
                   2746:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      2747:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2748:                                    context_tcbf, HT_ERROR);
                   2749:        return HT_ERROR;
                   2750:      }
1.4       cvs      2751: 
1.114     cvs      2752:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   2753:       named after their own docid */
                   2754:    
                   2755:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   2756: 
1.116     cvs      2757:    /* create a tempfilename */
1.272     cvs      2758:    sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
1.116     cvs      2759:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      2760:    object_counter++;
1.116     cvs      2761:    
1.4       cvs      2762:    /* normalize the URL */
1.286     kahan    2763:    tmp = URLToUTF8 (urlName, docid);
                   2764:    if (tmp)
                   2765:      {
                   2766:        esc_url = EscapeURL (tmp);
                   2767:        TtaFreeMemory (tmp);
                   2768:      }
                   2769:    else
                   2770:      esc_url = EscapeURL (urlName);
                   2771: 
1.206     cvs      2772:    if (esc_url) 
                   2773:      {
1.265     cvs      2774:        ref = AmayaParseUrl (esc_url, "", AMAYA_PARSE_ALL);
1.206     cvs      2775:        TtaFreeMemory (esc_url);
                   2776:      }
                   2777:    else
                   2778:      ref = NULL;
                   2779: 
1.4       cvs      2780:    /* should we abort the request if we could not normalize the url? */
1.198     cvs      2781:    if (ref == NULL || ref[0] == EOS) {
1.69      cvs      2782:       /*error */
                   2783:       outputfile[0] = EOS;
                   2784:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.214     cvs      2785:       if (ref)
                   2786:        TtaFreeMemory (ref); 
1.69      cvs      2787:       if (error_html)
1.116     cvs      2788:        /* so we can show the error message */
                   2789:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      2790:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      2791:                                   context_tcbf, HT_ERROR);
1.69      cvs      2792:       return HT_ERROR;
                   2793:    }
1.116     cvs      2794: 
1.4       cvs      2795:    /* verify if that file name existed */
1.9       cvs      2796:    if (TtaFileExist (outputfile))
1.77      cvs      2797:      TtaFileUnlink (outputfile);
1.116     cvs      2798:    
1.4       cvs      2799:    /* Initialize the request structure */
                   2800:    me = AHTReqContext_new (docid);
1.116     cvs      2801:    if (me == NULL) 
                   2802:      {
                   2803:        outputfile[0] = EOS;
                   2804:        /* need an error message here */
                   2805:        TtaFreeMemory (ref);
                   2806:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   2807:                                   context_tcbf, HT_ERROR);
                   2808:        return HT_ERROR;
                   2809:      }
                   2810: 
1.4       cvs      2811:    /* Specific initializations for POST and GET */
1.222     cvs      2812:    if (mode & AMAYA_FORM_POST
1.228     cvs      2813:        || mode & AMAYA_FILE_POST)
1.116     cvs      2814:      {
                   2815:        me->method = METHOD_POST;
                   2816:        HTRequest_setMethod (me->request, METHOD_POST);
                   2817:      }
                   2818:    else 
                   2819:      {
1.254     kahan    2820: #ifdef ANNOTATIONS
                   2821:        /* we support the DELETE method for deleting annotations.
                   2822:          the rest of the request is similar to the GET */
                   2823:        if (mode & AMAYA_DELETE)
                   2824:         {
                   2825:           me->method = METHOD_GET;
                   2826:           HTRequest_setMethod (me->request, METHOD_DELETE);
                   2827:         }
                   2828:         else
                   2829: #endif /* ANNOTATIONS */
                   2830:           me->method = METHOD_GET;
1.116     cvs      2831:        if (!HasKnownFileSuffix (ref))
1.216     cvs      2832:         {
                   2833:           /* try to adjust the Accept header in an netwise economical way */
                   2834:           if (mode & AMAYA_LOAD_IMAGE)
                   2835:             AHTRequest_setCustomAcceptHeader (me->request, IMAGE_ACCEPT_NEGOTIATION);
                   2836:           else if (mode & AMAYA_LOAD_CSS)
                   2837:             AHTRequest_setCustomAcceptHeader (me->request, "*/*;q=0.1,css/*");
1.244     kahan    2838: #ifdef ANNOTATIONS
1.272     cvs      2839:           else if (content_type && content_type[0] != EOS)
1.244     kahan    2840:             /* use the custom sent content_type */
                   2841:             AHTRequest_setCustomAcceptHeader (me->request, content_type);
                   2842: #endif /* ANNOTATIONS */
1.216     cvs      2843:           /*
                   2844:           HTRequest_setConversion(me->request, acceptTypes, TRUE);
                   2845:           */
                   2846:         }
                   2847:           HTRequest_setLanguage (me->request, acceptLanguages, TRUE);
1.116     cvs      2848:      }
1.93      cvs      2849: 
1.116     cvs      2850:    /* Common initialization for all HTML methods */
1.4       cvs      2851:    me->mode = mode;
                   2852:    me->error_html = error_html;
                   2853:    me->incremental_cbf = incremental_cbf;
                   2854:    me->context_icbf = context_icbf;
                   2855:    me->terminate_cbf = terminate_cbf;
                   2856:    me->context_tcbf = context_tcbf;
1.64      cvs      2857: 
1.69      cvs      2858:    /* for the async. request modes, we need to have our
1.4       cvs      2859:       own copy of outputfile and urlname
1.116     cvs      2860:       */
1.4       cvs      2861: 
1.116     cvs      2862:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   2863:      {
1.265     cvs      2864:        l = strlen (outputfile);
1.116     cvs      2865:        if (l > MAX_LENGTH)
1.265     cvs      2866:         me->outputfile = TtaGetMemory (l + 2);
1.116     cvs      2867:        else
1.265     cvs      2868:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
                   2869:        strcpy (me->outputfile, outputfile);
                   2870:        l = strlen (urlName);
1.116     cvs      2871:        if (l > MAX_LENGTH)
1.265     cvs      2872:         me->urlName = TtaGetMemory (l + 2);
1.116     cvs      2873:        else
1.265     cvs      2874:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
                   2875:        strcpy (me->urlName, urlName);
1.116     cvs      2876: #ifdef _WINDOWS
                   2877:      /* force windows ASYNC requests to always be non preemptive */
                   2878:      HTRequest_setPreemptive (me->request, NO);
                   2879: #endif /*_WINDOWS */
                   2880:      } /* AMAYA_ASYNC mode */ 
                   2881:    else 
                   2882: #ifdef _WINDOWS
                   2883:      {
1.233     cvs      2884:        me->outputfile = outputfile;
                   2885:        me->urlName = urlName;
1.116     cvs      2886:        /* force windows SYNC requests to always be non preemptive */
                   2887:        HTRequest_setPreemptive (me->request, YES);
                   2888:      }
                   2889: #else /* !_WINDOWS */
                   2890:      {
                   2891:        me->outputfile = outputfile;
                   2892:        me->urlName = urlName;
                   2893:      }
                   2894:    /***
1.136     cvs      2895:      In order to take into account the stop button, 
                   2896:      the requests will be always asynchronous, however, if mode=AMAYA_SYNC,
1.57      cvs      2897:      we will loop until the document has been received or a stop signal
                   2898:      generated
1.77      cvs      2899:      ****/
1.116     cvs      2900:    HTRequest_setPreemptive (me->request, NO);
                   2901: #endif /* _WINDOWS */
1.61      cvs      2902: 
1.151     cvs      2903:    /*
                   2904:    ** Make sure that the first request is flushed immediately and not
                   2905:    ** buffered in the output buffer
                   2906:    */
                   2907:    if (mode & AMAYA_FLUSH_REQUEST)
                   2908:      HTRequest_setFlush(me->request, YES);
                   2909:    HTRequest_setFlush(me->request, YES);
                   2910: 
1.61      cvs      2911:    /* prepare the URLname that will be displayed in teh status bar */
                   2912:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      2913:    TtaSetStatus (me->docid, 1, 
                   2914:                 TtaGetMessage (AMAYA, AM_FETCHING),
1.233     cvs      2915:                 me->status_urlName);
1.4       cvs      2916: 
1.265     cvs      2917:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      2918:    TtaFreeMemory (ref);
1.177     cvs      2919:    
1.223     cvs      2920:    TtaGetEnvBoolean ("CACHE_DISCONNECTED_MODE", &bool_tmp);
1.177     cvs      2921:    if (!bool_tmp && (mode & AMAYA_NOCACHE))
1.116     cvs      2922:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2923: 
                   2924:    /* prepare the query string and format for POST */
                   2925:    if (mode & AMAYA_FORM_POST)
1.114     cvs      2926:      {
1.116     cvs      2927:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   2928:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   2929:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   2930:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   2931:      } 
1.114     cvs      2932: 
1.127     cvs      2933:    /* create the formdata element for libwww */
1.236     cvs      2934:    if (formdata && ! (mode & AMAYA_FILE_POST))
                   2935:       me->formdata = PrepareFormdata (formdata);
1.127     cvs      2936: 
1.116     cvs      2937:    /* do the request */
                   2938:    if (mode & AMAYA_FORM_POST)
1.119     cvs      2939:      {
1.191     cvs      2940:        /* this call doesn't give back a ThotBool */
1.119     cvs      2941:        HTParentAnchor * posted = NULL;
                   2942: 
                   2943:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   2944:                                    me->request);
                   2945:        status = posted ? YES : NO; 
                   2946:      }
1.222     cvs      2947: #ifdef ANNOTATIONS
1.251     kahan    2948:    else if (mode & AMAYA_FILE_POST)
1.222     cvs      2949:      {
1.228     cvs      2950:        unsigned long filesize;
                   2951:        char *fileURL;
                   2952: 
                   2953:        /* @@@ a very ugly patch :)))
                   2954:        I'm copying here some of the functionality I use in the PUT
                   2955:        I need to put the common parts in another module */
1.252     kahan    2956:        AM_GetFileSize (formdata, &filesize);
1.228     cvs      2957:        me->block_size = filesize;
                   2958: 
1.233     cvs      2959:        fileURL = HTParse (formdata, "file:/", PARSE_ALL);
1.228     cvs      2960:        me->source = HTAnchor_findAddress (fileURL);
                   2961:        HT_FREE (fileURL);
1.222     cvs      2962: 
1.228     cvs      2963:        /* hardcoded ... */
                   2964:        AHTRequest_setCustomAcceptHeader (me->request, "application/xml");
1.222     cvs      2965:        HTAnchor_setFormat (HTAnchor_parent (me->source),
                   2966:                           HTAtom_for ("application/xml"));
1.228     cvs      2967:        HTAnchor_setFormat (me->anchor,
                   2968:                           HTAtom_for ("application/xml"));
                   2969:        HTAnchor_setLength ((HTParentAnchor *) me->source, me->block_size);
                   2970:        /* @@ here I need to actually read the file and put it in document,
                   2971:          then, when I kill the request, I need to kill it */
                   2972:        {
                   2973:         FILE *fp;
                   2974:         int i;
                   2975:         char c;
                   2976: 
1.255     swick    2977:         me->document = TtaGetMemory (me->block_size + 1);
1.233     cvs      2978:         fp = fopen (formdata, "r");
1.244     kahan    2979:         i = 0; 
1.228     cvs      2980:         c = getc (fp);
                   2981:         while (!feof (fp))
                   2982:           {
                   2983:             me->document[i++] = c;
                   2984:             c = getc (fp);
                   2985:           }
                   2986:         me->document[i] = '\0';
                   2987:         fclose (fp);
                   2988:        }
                   2989:        HTAnchor_setDocument ( (HTParentAnchor *) me->source,
                   2990:                              (void * ) me->document);
                   2991:        HTRequest_setEntityAnchor (me->request, HTAnchor_parent (me->source));
                   2992:  
                   2993:        status = HTPostAnchor (HTAnchor_parent (me->source), 
1.222     cvs      2994:                              (HTAnchor *) me->anchor, 
                   2995:                              me->request);
                   2996:      }
                   2997: #endif /* ANNOTATIONS */
1.127     cvs      2998:    else if (formdata)
                   2999:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   3000:                              me->request);
1.116     cvs      3001:    else
1.77      cvs      3002:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      3003: 
1.123     cvs      3004:    /* @@@ may need some special windows error msg here */
1.116     cvs      3005:    /* control the errors */
1.4       cvs      3006: 
1.130     cvs      3007:     if (status == NO)
1.116     cvs      3008:      /* the request invocation failed */
                   3009:      {
                   3010:        /* show an error message on the status bar */
                   3011:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   3012:        TtaSetStatus (docid, 1, 
                   3013:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   3014:                     urlName);
                   3015:        if (me->reqStatus == HT_NEW)
                   3016:         /* manually invoke the last processing that usually gets done
                   3017:            in a succesful request */
                   3018:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   3019:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   3020:        /* terminate_handler wasn't called */
1.136     cvs      3021:        AHTReqContext_delete (me);
1.116     cvs      3022:      }
                   3023:    else
1.136     cvs      3024:      /* end treatment for SYNC requests */
1.120     cvs      3025:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   3026:        {
1.136     cvs      3027:         /* wait here untilt the asynchronous request finishes */
1.130     cvs      3028:         status = LoopForStop (me);
1.136     cvs      3029:         /* if status returns HT_ERROR, should we invoke the callback? */
1.120     cvs      3030:         if (!HTRequest_kill (me->request))
                   3031:           AHTReqContext_delete (me);
                   3032:        }
1.130     cvs      3033: 
1.136     cvs      3034:     /* an interface problem!!! */
                   3035:     return (status == YES ? 0 : -1);
1.4       cvs      3036: }
                   3037: 
1.5       cvs      3038: /*----------------------------------------------------------------------
1.17      cvs      3039:    PutObjectWWW
                   3040:    frontend for uploading a resource to a URL. This function downloads
                   3041:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   3042:    finish the job.
                   3043: 
1.5       cvs      3044:    2 upload modes are proposed:                                       
                   3045:    AMAYA_SYNC : blocking mode                            
                   3046:    AMAYA_ASYNC : non-blocking mode                       
                   3047:    
1.4       cvs      3048:    When the function is called with the SYNC mode, the function will
                   3049:    return only when the file has been uploaded.
                   3050:    The ASYNC mode will immediately return after setting up the
                   3051:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   3052:    call back terminate_cbf, handling it the context defined in
                   3053:    context_tcbf.
                   3054: 
                   3055:    Notes:
                   3056:    At the end of a succesful request, the urlName string contains the
                   3057:    name of the actually uploaded URL. As a URL can change over the time,
                   3058:    (e.g., be redirected elsewhere), it is advised that the function
                   3059:    caller verifies the value of the urlName variable at the end of
                   3060:    a request.
                   3061: 
                   3062:    Inputs:
                   3063:    - docid  Document identifier for the set of objects being
                   3064:    retrieved.
                   3065:    - fileName A pointer to the local file to upload
                   3066:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   3067:    - mode The retrieval mode.
                   3068:    - terminate_cbf 
                   3069:    - context_icbf
                   3070:    Callback and context for a terminate handler
                   3071: 
                   3072:    Outputs:
                   3073:    - urlName The URL that was uploaded
                   3074: 
                   3075:    Returns:
                   3076:    HT_ERROR
                   3077:    HT_OK
1.5       cvs      3078:   ----------------------------------------------------------------------*/
1.276     kahan    3079: int PutObjectWWW (int docid, char *fileName, char *urlName, 
                   3080:                  char *contentType, int mode,
1.265     cvs      3081:                  TTcbf *terminate_cbf, void *context_tcbf)
1.4       cvs      3082: {
1.116     cvs      3083:    AHTReqContext      *me;
1.250     cvs      3084:    CHARSET             charset;
1.4       cvs      3085:    int                 status;
1.250     cvs      3086:    unsigned long       file_size;
1.116     cvs      3087:    char               *fileURL;
1.168     cvs      3088:    char               *etag = NULL;
1.169     cvs      3089:    HTParentAnchor     *dest_anc_parent;
1.265     cvs      3090:    char               *tmp;
                   3091:    char               *esc_url;
1.168     cvs      3092:    int                 UsePreconditions;
1.191     cvs      3093:    ThotBool            lost_update_check = TRUE;
1.235     cvs      3094:    char                url_name[MAX_LENGTH];
1.285     kahan    3095:    char               *resource_name;
1.265     cvs      3096:    char               *tmp2;
1.239     cvs      3097: #ifdef _WINDOWS
                   3098:    char                file_name[MAX_LENGTH];
                   3099: #endif /* _WINDOWS */
1.172     cvs      3100: 
                   3101:    /* should we protect the PUT against lost updates? */
1.203     cvs      3102:    tmp = TtaGetEnvString ("ENABLE_LOST_UPDATE_CHECK");
1.265     cvs      3103:    if (tmp && *tmp && strcasecmp (tmp, "yes"))
1.172     cvs      3104:      lost_update_check = FALSE;
1.168     cvs      3105: 
                   3106:    UsePreconditions = mode & AMAYA_USE_PRECONDITIONS;
1.4       cvs      3107: 
1.33      cvs      3108:    AmayaLastHTTPErrorMsg [0] = EOS;
1.256     kahan    3109:    AmayaLastHTTPErrorMsgR [0] = EOS;
                   3110: 
1.33      cvs      3111:    
1.116     cvs      3112:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   3113:        || !TtaFileExist (fileName))
1.4       cvs      3114:       /* no file to be uploaded */
                   3115:       return HT_ERROR;
                   3116: 
                   3117:    /* do we support this protocol? */
1.7       cvs      3118:    if (IsValidProtocol (urlName) == NO)
                   3119:      {
                   3120:        /* return error */
1.198     cvs      3121:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL), urlName);
1.7       cvs      3122:        return HT_ERROR;
                   3123:      }
1.116     cvs      3124: 
1.228     cvs      3125:    /* get the size of the file */
1.252     kahan    3126:    if (!AM_GetFileSize (fileName, &file_size) || file_size == 0L)
1.7       cvs      3127:      {
                   3128:        /* file was empty */
                   3129:        /*errmsg here */
                   3130:        return (HT_ERROR);
                   3131:      }
1.116     cvs      3132: 
                   3133:    /* prepare the request context */
1.4       cvs      3134:    if (THD_TRACE)
1.228     cvs      3135:       fprintf (stderr, "file size == %u\n", (unsigned) file_size);
1.4       cvs      3136: 
                   3137:    me = AHTReqContext_new (docid);
1.7       cvs      3138:    if (me == NULL)
                   3139:      {
1.168     cvs      3140:        /* @@ need an error message here */
1.151     cvs      3141:        TtaHandlePendingEvents (); 
1.7       cvs      3142:        return (HT_ERROR);
1.208     cvs      3143:      }
                   3144: 
                   3145:    /*
                   3146:    ** Set up the original URL name
                   3147:    */
1.285     kahan    3148: 
                   3149:    /* are we using content-location? */
                   3150:    if (DocumentMeta[docid]->content_location)
                   3151:      resource_name = DocumentMeta[docid]->content_location;
                   3152:    else
                   3153:      resource_name = NULL;
                   3154: 
                   3155:    /* prepare the target URL */
                   3156:    if (resource_name)
1.208     cvs      3157:      {
1.285     kahan    3158:        tmp = strstr (urlName, resource_name);     
                   3159:        if (!tmp)
1.208     cvs      3160:         {
1.285     kahan    3161:           /* urlName does not include the resource name */
                   3162:           me->default_put_name = TtaGetMemory (strlen (urlName)
                   3163:                                                + strlen (resource_name)
                   3164:                                                + sizeof (URL_SEP)
                   3165:                                                + 1);
                   3166:           strcpy (me->default_put_name, urlName);
                   3167:           tmp = strrchr (me->default_put_name, URL_SEP);
                   3168:           if (tmp)
1.208     cvs      3169:             {
1.285     kahan    3170:               /* it is a URL finishing in /. Only add the resrouce name */
                   3171:               tmp++;
                   3172:               strcpy (tmp, resource_name);
                   3173:             }
                   3174:           else
                   3175:             {
                   3176:               strcat (me->default_put_name, URL_STR);
                   3177:               strcat (me->default_put_name, resource_name);
1.208     cvs      3178:             }
                   3179:         }
1.7       cvs      3180:      }
1.285     kahan    3181:    
                   3182:    if (!me->default_put_name)
1.271     kahan    3183:      me->default_put_name = TtaStrdup (urlName);
                   3184:    
1.285     kahan    3185:    HTRequest_setDefaultPutName (me->request, me->default_put_name);
1.116     cvs      3186: 
1.4       cvs      3187:    me->mode = mode;
                   3188:    me->incremental_cbf = (TIcbf *) NULL;
                   3189:    me->context_icbf = (void *) NULL;
                   3190:    me->terminate_cbf = terminate_cbf;
                   3191:    me->context_tcbf = context_tcbf;
1.286     kahan    3192: 
                   3193:    /* normalize the URL */
                   3194:    tmp = URLToUTF8 (urlName, docid);
                   3195:    if (tmp)
                   3196:      {
                   3197:        esc_url = EscapeURL (tmp);
                   3198:        TtaFreeMemory (tmp);
                   3199:      }
                   3200:    else
                   3201:      esc_url = EscapeURL (urlName);
1.272     cvs      3202:    me->urlName = TtaStrdup (esc_url);
1.206     cvs      3203:    TtaFreeMemory (esc_url);
1.228     cvs      3204:    me->block_size =  file_size;
1.17      cvs      3205:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      3206:    me->method = METHOD_PUT;
                   3207:    me->output = stdout;
1.17      cvs      3208:    /* we are not expecting to receive any input from the server */
1.265     cvs      3209:    me->outputfile = (char  *) NULL; 
1.4       cvs      3210: 
1.134     cvs      3211: #ifdef _WINDOWS
                   3212:    /* libwww's HTParse function doesn't take into account the drive name;
1.168     cvs      3213:       so we sidestep it */
1.238     cvs      3214: 
1.134     cvs      3215:    fileURL = NULL;
                   3216:    StrAllocCopy (fileURL, "file:");
1.273     cvs      3217:    strcpy (file_name, fileName);
1.235     cvs      3218:    StrAllocCat (fileURL, file_name);
1.134     cvs      3219: #else
1.116     cvs      3220:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
1.134     cvs      3221: #endif /* _WINDOWS */
1.168     cvs      3222:    me->source = HTAnchor_findAddress (fileURL);
1.116     cvs      3223:    HT_FREE (fileURL);
1.273     cvs      3224:    strcpy (url_name, me->urlName);
1.235     cvs      3225:    me->dest = HTAnchor_findAddress (url_name);
1.169     cvs      3226:    /* we memorize the anchor's parent @ as we use it a number of times
                   3227:       in the following lines */
                   3228:    dest_anc_parent = HTAnchor_parent (me->dest);
1.168     cvs      3229: 
1.169     cvs      3230:    /*
                   3231:    **  Set the Content-Type of the file we are uploading 
                   3232:    */
                   3233:    /* we try to use any content-type previosuly associated
                   3234:       with the parent. If it doesn't exist, we try to guess it
                   3235:       from the URL */
1.276     kahan    3236:    /* @@ JK: trying to use the content type we stored */
                   3237:    /*
1.235     cvs      3238:    tmp2 = HTAtom_name (HTAnchor_format (dest_anc_parent));
1.276     kahan    3239:    */
                   3240:    tmp2 = NULL;
1.235     cvs      3241:    if (!tmp2 || !strcmp (tmp2, "www/unknown"))
1.169     cvs      3242:      {
1.260     kahan    3243:        HTAtom *tmp_atom;
1.276     kahan    3244:        
1.260     kahan    3245:        tmp_atom = AHTGuessAtom_for (me->urlName, contentType);
1.276     kahan    3246:        if (!tmp_atom || !strcmp (HTAtom_name (tmp_atom), "www/unknown"))
                   3247:         {
                   3248:           /* ask the user for a MIME type */
                   3249:         }
1.260     kahan    3250:        HTAnchor_setFormat (dest_anc_parent, tmp_atom);
1.235     cvs      3251:        tmp2 = HTAtom_name (HTAnchor_format (dest_anc_parent));
1.169     cvs      3252:      }
                   3253:    /* .. and we give the same type to the source anchor */
                   3254:    /* we go thru setOutputFormat, rather than change the parent's
                   3255:       anchor, as that's the place that libwww expects it to be */
1.235     cvs      3256:    HTAnchor_setFormat (HTAnchor_parent (me->source), HTAtom_for (tmp2));
1.219     cvs      3257: 
1.235     cvs      3258:    HTRequest_setOutputFormat (me->request, HTAtom_for (tmp2));
1.219     cvs      3259: 
                   3260:    /*
                   3261:    **  Set the Charset of the file we are uploading 
                   3262:    */
1.240     cvs      3263:    /* we set the charset as indicated in the document's metadata
                   3264:       structure (and only if it exists) */
1.250     cvs      3265:    charset = TtaGetDocumentCharset (docid);
                   3266:    if (charset != UNDEFINED_CHARSET)
1.240     cvs      3267:      {
1.250     cvs      3268:        tmp =  TtaGetCharsetName (charset);
1.272     cvs      3269:        if (tmp && *tmp != EOS)
1.250     cvs      3270:         {
1.273     cvs      3271:           tmp2 = TtaStrdup (tmp);
1.250     cvs      3272:           HTAnchor_setCharset (dest_anc_parent, HTAtom_for (tmp2));
                   3273:           TtaFreeMemory (tmp2);
                   3274:           tmp2 = HTAtom_name (HTAnchor_charset (dest_anc_parent));
                   3275:           /* .. and we give the same charset to the source anchor */
                   3276:           /* we go thru setCharSet, rather than change the parent's
                   3277:              anchor, as that's the place that libwww expects it to be */
                   3278:           HTAnchor_setCharset (HTAnchor_parent (me->source),
                   3279:                                HTAtom_for (tmp2));
                   3280:         }
1.219     cvs      3281:      }
                   3282: 
                   3283:    /*
                   3284:    ** define other request characteristics
                   3285:    */
1.116     cvs      3286: #ifdef _WINDOWS
1.136     cvs      3287:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      3288: #else
1.134     cvs      3289:    HTRequest_setPreemptive (me->request, NO);
1.116     cvs      3290: #endif /* _WINDOWS */
                   3291: 
1.151     cvs      3292:    /*
                   3293:    ** Make sure that the first request is flushed immediately and not
                   3294:    ** buffered in the output buffer
                   3295:    */
                   3296:    if (mode & AMAYA_FLUSH_REQUEST)
                   3297:      HTRequest_setFlush(me->request, YES);
1.168     cvs      3298:    
                   3299:    /* Should we use preconditions? */
1.172     cvs      3300:    if (lost_update_check)
1.168     cvs      3301:      {
1.172     cvs      3302:        if (UsePreconditions) 
                   3303:         etag = HTAnchor_etag (HTAnchor_parent (me->dest));
                   3304:        
                   3305:        if (etag) 
                   3306:         {
                   3307:           HTRequest_setPreconditions(me->request, HT_MATCH_THIS);
                   3308:         }
                   3309:        else
                   3310:         {
                   3311:           HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   3312:           HTRequest_addAfter(me->request, check_handler, NULL, NULL, HT_ALL,
                   3313:                              HT_FILTER_MIDDLE, YES);
1.175     cvs      3314:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL, 
                   3315:                               HT_NO_ACCESS, HT_FILTER_MIDDLE, YES);
                   3316:           HTRequest_addAfter (me->request, HTAuthFilter, "http://*", NULL,
                   3317:                               HT_REAUTH, HT_FILTER_MIDDLE, YES);
                   3318:           HTRequest_addAfter (me->request, HTAuthInfoFilter, "http://*", NULL,
                   3319:                               HT_ALL, HT_FILTER_MIDDLE, YES);
                   3320:           HTRequest_addAfter (me->request, HTUseProxyFilter, "http://*", NULL,
                   3321:                               HT_USE_PROXY, HT_FILTER_MIDDLE, YES);
1.172     cvs      3322:         }
1.168     cvs      3323:      }
                   3324:    else
                   3325:      {
1.172     cvs      3326:        /* don't use preconditions */
1.168     cvs      3327:        HTRequest_setPreconditions(me->request, HT_NO_MATCH);
                   3328:      }
1.151     cvs      3329:    
1.136     cvs      3330:    /* don't use the cache while saving a document */
                   3331:    HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
1.116     cvs      3332: 
1.164     cvs      3333:    /* Throw away any reponse body */
                   3334:    /*
                   3335:    HTRequest_setOutputStream (me->request, HTBlackHole());        
                   3336:    */
                   3337: 
1.168     cvs      3338:    /* prepare the URLname that will be displayed in the status bar */
1.74      cvs      3339:    ChopURL (me->status_urlName, me->urlName);
1.233     cvs      3340:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING), me->status_urlName);
1.114     cvs      3341: 
1.164     cvs      3342:    /* make the request */
1.172     cvs      3343:    if (lost_update_check && (!UsePreconditions || !etag))
1.168     cvs      3344:      status = HTHeadAnchor (me->dest, me->request);
                   3345:    else
                   3346:      status = HTPutDocumentAnchor (HTAnchor_parent (me->source), me->dest, me->request);
1.4       cvs      3347: 
1.130     cvs      3348:    if (status == YES && me->reqStatus != HT_ERR)
1.7       cvs      3349:      {
1.168     cvs      3350:        /* part of the stop button handler */
                   3351:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   3352:         status = LoopForStop (me);
1.15      cvs      3353:      }
1.136     cvs      3354:    if (!HTRequest_kill (me->request))
                   3355:      AHTReqContext_delete (me);
1.168     cvs      3356:    
1.116     cvs      3357:    TtaHandlePendingEvents ();
1.90      cvs      3358: 
1.130     cvs      3359:    return (status == YES ? 0 : -1);
1.28      cvs      3360: }
1.4       cvs      3361: 
1.5       cvs      3362: /*----------------------------------------------------------------------
1.138     cvs      3363:   StopRequest
1.17      cvs      3364:   stops (kills) all active requests associated with a docid 
1.5       cvs      3365:   ----------------------------------------------------------------------*/
1.4       cvs      3366: void                StopRequest (int docid)
                   3367: {
1.140     cvs      3368:    if (Amaya && CanDoStop ())
1.138     cvs      3369:      { 
1.139     cvs      3370: #if 0 /* for later */
1.140     cvs      3371:        AHTDocId_Status    *docid_status;
                   3372:         /* verify if there are any requests at all associated with docid */
1.138     cvs      3373:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   3374:                                                          Amaya->docid_status);
                   3375:        if (docid_status == (AHTDocId_Status *) NULL)
                   3376:         return;
1.139     cvs      3377: #endif /* 0 */
1.138     cvs      3378:        /* temporary call to stop all requests, as libwww changed its API */
                   3379:        StopAllRequests (docid);
                   3380:      }
                   3381: }
                   3382: 
1.190     cvs      3383: /* @@@ the docid parameter isn't used... clean it up */
1.138     cvs      3384: /*----------------------------------------------------------------------
                   3385:   StopAllRequests
                   3386:   stops (kills) all active requests. We use the docid 
                   3387:   ----------------------------------------------------------------------*/
                   3388: void                StopAllRequests (int docid)
                   3389: {
1.4       cvs      3390:    HTList             *cur;
                   3391:    AHTReqContext      *me;
1.191     cvs      3392:    static ThotBool     lock_stop = 0;
                   3393:    ThotBool            async_flag;
1.232     cvs      3394:    AHTReqStatus        old_reqStatus;
1.116     cvs      3395: 
1.138     cvs      3396:    /* only do the stop if we're not being called while processing a 
1.146     cvs      3397:       request, and if we're not already dealing with a stop */
                   3398:    if (Amaya && CanDoStop () && !lock_stop)
1.7       cvs      3399:      {
1.100     cvs      3400: #ifdef DEBUG_LIBWWW
1.138     cvs      3401:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3402:                "before kill: %d\n", Amaya->open_requests);
1.100     cvs      3403: #endif /* DEBUG_LIBWWW */
1.150     cvs      3404:        /* enter the critical section */
1.146     cvs      3405:        lock_stop = TRUE; 
1.214     cvs      3406:        /* set a module global variable so that we can do special
                   3407:          processing easier */
                   3408:        UserAborted_flag = TRUE;
1.232     cvs      3409:        /* abort all outstanding libwww UI dialogues */
1.265     cvs      3410:        CallbackDialogue (BaseDialog + FormAnswer,  STRING_DATA, NULL);
                   3411:        CallbackDialogue (BaseDialog + ConfirmForm, INTEGER_DATA, NULL);
1.164     cvs      3412:        /* expire all outstanding timers */
                   3413:        HTTimer_expireAll ();
1.160     cvs      3414:        /* HTNet_killAll (); */
1.245     kahan    3415:        if (Amaya->open_requests)
1.116     cvs      3416:         {
1.245     kahan    3417:           cur = Amaya->reqlist;
                   3418:           while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
1.160     cvs      3419:             {
1.245     kahan    3420:               if (AmayaIsAlive ())
                   3421:                 {
1.161     cvs      3422: #ifdef DEBUG_LIBWWW
1.245     kahan    3423:                   fprintf (stderr,"StopRequest: killing req %p, url %s, status %d\n", me, me->urlName, me->reqStatus);
1.161     cvs      3424: #endif /* DEBUG_LIBWWW */
1.245     kahan    3425:                   
                   3426:                   if (me->reqStatus != HT_END && me->reqStatus != HT_ABORT)
1.163     cvs      3427:                     {
1.245     kahan    3428:                       if ((me->mode & AMAYA_ASYNC)
                   3429:                           || (me->mode & AMAYA_IASYNC))
                   3430:                         async_flag = TRUE;
                   3431:                       else
                   3432:                         async_flag = FALSE;
                   3433:                       
                   3434:                       /* change the status to say that the request aborted */
                   3435:                       /* if the request was "busy", we just change a flag to say so and
                   3436:                          let the handler finish the processing itself */
                   3437:                       old_reqStatus = me->reqStatus;
                   3438:                       me->reqStatus = HT_ABORT;
                   3439:                       if (old_reqStatus == HT_BUSY)
                   3440:                         continue;
                   3441:                       
                   3442:                       /* kill the request, using the appropriate function */
                   3443:                       if (me->request->net)
                   3444:                         HTNet_killPipe (me->request->net);
                   3445:                       else
                   3446:                         {
                   3447:                           if (me->terminate_cbf)
                   3448:                             (*me->terminate_cbf) (me->docid, -1, me->urlName,
                   3449:                                                   me->outputfile,
                   3450:                                                   NULL,
                   3451:                                                   me->context_tcbf);
                   3452:                           
                   3453:                           if (async_flag) 
                   3454:                             /* explicitly free the request context for async
                   3455:                                requests. The sync requests context is freed
                   3456:                                by LoopForStop */
                   3457:                             AHTReqContext_delete (me);
                   3458:                         }
                   3459:                       cur = Amaya->reqlist;
1.160     cvs      3460:                     }
1.142     cvs      3461: #ifndef _WINDOWS
                   3462: #ifdef WWW_XWINDOWS
1.245     kahan    3463:                   /* to be on the safe side, remove all outstanding 
                   3464:                      X events */
1.160     cvs      3465:                   else 
                   3466:                     RequestKillAllXtevents (me);
1.142     cvs      3467: #endif /* WWW_XWINDOWS */
                   3468: #endif /* !_WINDOWS */
1.245     kahan    3469:                 }
1.160     cvs      3470:             }
1.245     kahan    3471:           /* Delete remaining channels */
                   3472:           HTChannel_safeDeleteAll ();
1.116     cvs      3473:         }
1.214     cvs      3474:        /* reset the stop status */
                   3475:        UserAborted_flag = FALSE;
1.150     cvs      3476:        /* exit the critical section */
1.148     cvs      3477:        lock_stop = FALSE; 
1.100     cvs      3478: #ifdef DEBUG_LIBWWW
1.138     cvs      3479:        fprintf (stderr, "StopRequest: number of Amaya requests "
                   3480:                "after kill: %d\n", Amaya->open_requests);
1.100     cvs      3481: #endif /* DEBUG_LIBWWW */
1.138     cvs      3482:      }
                   3483: } /* StopAllRequests */
1.17      cvs      3484: 
                   3485: 
1.105     cvs      3486: /*----------------------------------------------------------------------
                   3487:   AmayaIsAlive
1.140     cvs      3488:   returns the value of the AmayaAlive_flag
1.105     cvs      3489:   ----------------------------------------------------------------------*/
1.191     cvs      3490: ThotBool AmayaIsAlive (void)
1.105     cvs      3491: {
1.140     cvs      3492:   return AmayaAlive_flag;
                   3493: }
                   3494: 
                   3495: /*----------------------------------------------------------------------
                   3496:   CanDoStop
                   3497:   returns the value of the CanDoStop flag
                   3498:   ----------------------------------------------------------------------*/
1.191     cvs      3499: ThotBool CanDoStop (void)
1.140     cvs      3500: {
                   3501:   return CanDoStop_flag;
                   3502: }
                   3503: 
                   3504: /*----------------------------------------------------------------------
                   3505:   CanDoStop_set
                   3506:   sets the value of the CanDoStop flag
                   3507:   ----------------------------------------------------------------------*/
1.191     cvs      3508: void CanDoStop_set (ThotBool value)
1.140     cvs      3509: {
                   3510:   CanDoStop_flag = value;
1.176     cvs      3511: }
                   3512: 
1.265     cvs      3513: /*----------------------------------------------------------------------
                   3514:   ----------------------------------------------------------------------*/
1.176     cvs      3515: void libwww_updateNetworkConf (int status)
                   3516: {
                   3517:   /* @@@ the docid parameter isn't used... clean it up */
                   3518:   int docid = 1;
                   3519: 
                   3520:   /* first, stop all current requests, as the network
                   3521:    may make some changes */
                   3522:   StopAllRequests (docid);
1.207     cvs      3523: 
                   3524:   if (status & AMAYA_SAFEPUT_RESTART)
                   3525:     { 
                   3526:       SafePut_delete ();
                   3527:       SafePut_init ();
                   3528:     }
1.176     cvs      3529: 
                   3530:   if (status & AMAYA_PROXY_RESTART)
                   3531:     {
                   3532:       HTProxy_deleteAll ();
                   3533:       ProxyInit ();
                   3534:     }
                   3535: 
                   3536:   if (status & AMAYA_CACHE_RESTART)
                   3537:     {
                   3538:       clear_cachelock ();
                   3539:       HTCacheTerminate ();
                   3540:       HTCacheMode_setEnabled (NO);
                   3541:       CacheInit ();
                   3542:     }
1.193     cvs      3543: 
                   3544:   if (status & AMAYA_LANNEG_RESTART)
                   3545:     {
                   3546:       /* clear the current values */
                   3547:       if (acceptLanguages)
                   3548:        HTLanguage_deleteAll (acceptLanguages);
                   3549:       /* read in the new ones */
                   3550:       acceptLanguages = HTList_new ();
                   3551:       AHTAcceptLanguagesInit (acceptLanguages);
                   3552:     }
1.105     cvs      3553: }
1.246     kahan    3554: 
                   3555: /****************************************************************************
                   3556:  ** The following two functions allow to set and read the value of          *
                   3557:  ** the  FTPURL_flag. If this flag is true, it means we can browse FTP URLs *
                   3558:  ***************************************************************************/
                   3559: 
                   3560: /*----------------------------------------------------------------------
                   3561:   AHT_FTPURL_flag_set
                   3562:   ----------------------------------------------------------------------*/
                   3563: void AHTFTPURL_flag_set (ThotBool value)
                   3564: {
                   3565:   FTPURL_flag = value;
                   3566: }
                   3567: 
                   3568: /*----------------------------------------------------------------------
                   3569:   AHT_FTPURL_flag
                   3570:   ----------------------------------------------------------------------*/
                   3571: ThotBool AHTFTPURL_flag (void)
                   3572: {
                   3573:   return (FTPURL_flag);
                   3574: }
                   3575: 

Webmaster