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

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

Webmaster