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

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

Webmaster