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

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

Webmaster