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

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

Webmaster