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

1.14      cvs         1: /*
                      2:  *
                      3:  *  (c) COPYRIGHT MIT and INRIA, 1996.
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
1.4       cvs         7: 
1.19      cvs         8: /*
1.83      cvs         9:  * query.c : contains all the functions for requesting and publishing
1.19      cvs        10:  * URLs via libwww. It handles any eventual HTTP error code
                     11:  * (redirection, authentication needed, not found, etc.)
                     12:  *
1.69      cvs        13:  * Author: J. Kahan
1.92      cvs        14:  *         J. Kahan/R. Guetari Windows 95/NT routines
1.19      cvs        15:  */
1.17      cvs        16: 
1.44      cvs        17: #ifndef AMAYA_JAVA
                     18: 
1.116     cvs        19: /* defines to include elsewhere
                     20: *********************************/
                     21: 
1.124     cvs        22: #ifndef _WINDOWS
1.116     cvs        23: #define AMAYA_WWW_CACHE
1.124     cvs        24: #endif /* !_WINDOWS */
1.116     cvs        25: 
1.128   ! cvs        26: #define CACHE_DIR_NAME DIR_STR"libwww-cache"
1.116     cvs        27: #define DEFAULT_CACHE_SIZE 5
                     28: 
1.12      cvs        29: /* Amaya includes  */
1.25      cvs        30: #define THOT_EXPORT extern
1.4       cvs        31: #include "amaya.h"
1.70      cvs        32: #ifdef _WINDOWS 
                     33: #include <fcntl.h>
                     34: #endif /* _WINDOWS */
1.7       cvs        35: 
1.4       cvs        36: #if defined(__svr4__)
                     37: #define CATCH_SIG
                     38: #endif
                     39: 
                     40: /* local structures coming from libwww and which are
1.17      cvs        41:    not found in any .h file
1.7       cvs        42:  */
1.68      cvs        43: 
1.7       cvs        44: struct _HTStream
                     45:   {
1.15      cvs        46:      const HTStreamClass *isa;
                     47:      FILE               *fp;
1.45      cvs        48:      BOOL                leave_open;   /* Close file when TtaFreeMemory? */
1.15      cvs        49:      char               *end_command;  /* Command to execute       */
                     50:      BOOL                remove_on_close;      /* Remove file?             */
                     51:      char               *filename;     /* Name of file             */
                     52:      HTRequest          *request;      /* saved for callback       */
                     53:      HTRequestCallback  *callback;
1.7       cvs        54:   };
                     55: 
1.15      cvs        56: 
1.7       cvs        57: struct _HTError
                     58:   {
                     59:      HTErrorElement      element;      /* Index number into HTError */
                     60:      HTSeverity          severity;     /* A la VMS */
                     61:      BOOL                ignore;       /* YES if msg should not go to user */
                     62:      void               *par;  /* Explanation, e.g. filename  */
                     63:      int                 length;       /* For copying by generic routine */
                     64:      char               *where;        /* Which function */
                     65:   };
                     66: 
1.4       cvs        67: /* Type definitions and global variables etc. local to this module */
                     68: 
1.17      cvs        69: /*----------------------------------------------------------------------*/
                     70: 
1.4       cvs        71: /*** private variables ***/
1.17      cvs        72: 
1.4       cvs        73: static HTList      *converters = NULL; /* List of global converters */
1.27      cvs        74: static HTList      *acceptTypes = NULL; /* List of types for the Accept header */
1.4       cvs        75: static HTList      *encodings = NULL;
1.16      cvs        76: static int          object_counter = 0;        /* loaded objects counter */
1.116     cvs        77: static  boolean     AmayaAlive; /* set to 1 if the application is active;
                     78:                                   0 if we have killed */
1.128   ! cvs        79: #ifdef  AMAYA_WWW_CACHE
        !            80: static int          cache_size = 0;  /* size of the cache in megabytes */
        !            81: #endif /* WWW_AMAYACACHE */
1.4       cvs        82: 
1.15      cvs        83: #include "answer_f.h"
                     84: #include "query_f.h"
                     85: #include "AHTURLTools_f.h"
                     86: #include "AHTBridge_f.h"
                     87: #include "AHTMemConv_f.h"
                     88: #include "AHTFWrite_f.h"
1.4       cvs        89: 
1.116     cvs        90: /* prototypes */
                     91: 
                     92: #ifdef __STDC__
1.128   ! cvs        93: static void RecCleanCache (char *dirname);
1.123     cvs        94: #ifdef _WINDOWS
1.70      cvs        95: int WIN_Activate_Request (HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar*);
1.116     cvs        96: #endif /* _WINDOWS */
1.70      cvs        97: #else
1.128   ! cvs        98: static void RecCleanCache (/* char *dirname */);
1.123     cvs        99: #ifdef _WINDOWS
1.128   ! cvs       100: int WIN_Activate_Request (/* HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar* */);
1.116     cvs       101: #endif /* _WINDOWS */
1.70      cvs       102: #endif /* __STDC__ */
1.116     cvs       103: 
1.15      cvs       104: 
                    105: /*----------------------------------------------------------------------
1.17      cvs       106:   GetDocIdStatus
                    107:   gets the status associated to a docid                         
1.15      cvs       108:   ----------------------------------------------------------------------*/
                    109: #ifdef __STDC__
                    110: AHTDocId_Status    *GetDocIdStatus (int docid, HTList * documents)
                    111: #else
                    112: AHTDocID_Status    *GetDocIdStatus (docid, documents)
                    113: int                 docid;
                    114: HTList             *documents;
                    115: 
                    116: #endif
                    117: {
                    118:    AHTDocId_Status    *me;
                    119:    HTList             *cur;
                    120: 
                    121:    if (documents)
                    122:      {
                    123:        cur = documents;
                    124: 
                    125:        while ((me = (AHTDocId_Status *) HTList_nextObject (cur)))
                    126:          {
                    127:             if (me->docid == docid)
                    128:                return (me);
1.18      cvs       129:          }
                    130:      }
1.15      cvs       131:    return (AHTDocId_Status *) NULL;
                    132: 
                    133: }
                    134: 
1.5       cvs       135: /*----------------------------------------------------------------------
1.27      cvs       136:   AHTGuessAtom_for
                    137:   Converts an Amaya type descriptor into the equivalent MIME type.
                    138:   ----------------------------------------------------------------------*/
                    139: #ifdef __STDC__
                    140: static  HTAtom *AHTGuessAtom_for (char *urlName, PicType contentType)
                    141: #else
                    142: static  HTAtom *AHTGuessAtom_for (urlName, contentType)
                    143: char *urlName;
                    144: PicType contentType;
                    145: #endif
                    146: {
                    147:  HTAtom           *atom;
                    148:  char             *filename;
                    149:  HTEncoding        enc;
                    150:  HTEncoding        cte;
                    151:  HTLanguage        lang;
1.50      cvs       152:  double            quality = 1.0;
1.27      cvs       153: 
                    154:  switch (contentType)
                    155:    {
                    156:     case xbm_type:
                    157:       atom = HTAtom_for("image/xbm");
                    158:       break;
                    159:     case eps_type:
1.35      cvs       160:       atom = HTAtom_for("application/postscript");
1.27      cvs       161:       break;
                    162:    case xpm_type:
                    163:       atom = HTAtom_for("image/xpm");
                    164:      break;
                    165:     case gif_type:
                    166:       atom = HTAtom_for("image/gif");
                    167:       break;
                    168:     case jpeg_type:
                    169:       atom = HTAtom_for("image/jpeg");
                    170:       break;
                    171:     case png_type:
                    172:       atom = HTAtom_for("image/png");
                    173:       break;
                    174:    case unknown_type:
                    175:    default:
                    176:      /* 
                    177:      ** Amaya could not detect the type, so 
                    178:      ** we try to use the filename's suffix to do so.
                    179:      */
1.45      cvs       180:      filename = AmayaParseUrl (urlName, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.27      cvs       181:      HTBind_getFormat (filename, &atom, &enc, &cte, &lang, &quality);
1.45      cvs       182:      TtaFreeMemory (filename);
1.27      cvs       183:      if (atom ==  WWW_UNKNOWN)
                    184:         /*
                    185:         ** we could not identify the suffix, so we assign it
                    186:         ** a default type
                    187:         */
                    188:         atom = HTAtom_for ("text/html");
                    189:      break;
                    190:    }
                    191: 
                    192:  return atom;
                    193: }
                    194: 
                    195: /*----------------------------------------------------------------------
1.17      cvs       196:   AHTReqContext_new
                    197:   create a new Amaya Context Object and update the global Amaya
                    198:   request status.
1.5       cvs       199:   ----------------------------------------------------------------------*/
1.4       cvs       200: #ifdef __STDC__
                    201: static AHTReqContext *AHTReqContext_new (int docid)
                    202: #else
                    203: static AHTReqContext *AHTReqContext_new (docid)
                    204: int                 docid;
                    205: 
                    206: #endif
                    207: {
                    208:    AHTReqContext      *me;
                    209:    AHTDocId_Status    *docid_status;
                    210: 
                    211:    if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
1.116     cvs       212:      outofmem (__FILE__, "AHTReqContext_new");
                    213: 
                    214:    /* clear the structure */
                    215:    memset ((void *) me, 0, sizeof (AHTReqContext));
1.4       cvs       216: 
1.81      cvs       217:    /* Bind the Context object together with the Request Object */
                    218:    me->request = HTRequest_new ();
                    219:   
1.116     cvs       220:    /*
                    221:    ** Make sure that the first request is flushed immediately and not
                    222:    ** buffered in the output buffer
                    223:    */
                    224:    HTRequest_setFlush(me->request, YES);
                    225: 
1.80      cvs       226:    /* clean the associated file structure) */
1.79      cvs       227:    HTRequest_setOutputStream (me->request, NULL);
1.81      cvs       228:  
1.4       cvs       229:    /* Initialize the other members of the structure */
1.17      cvs       230:    me->reqStatus = HT_NEW; /* initial status of a request */
1.4       cvs       231:    me->docid = docid;
                    232:    HTRequest_setMethod (me->request, METHOD_GET);
                    233:    HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    234:    HTRequest_setContext (me->request, me);
1.116     cvs       235: 
1.36      cvs       236:    /* experimental */
                    237:    me->read_sock = INVSOC;
                    238:    me->write_sock = INVSOC;
                    239:    me->except_sock = INVSOC;
1.4       cvs       240: 
                    241:    /* Update the global context */
                    242:    HTList_appendObject (Amaya->reqlist, (void *) me);
                    243: 
                    244:    docid_status = GetDocIdStatus (docid, Amaya->docid_status);
                    245: 
1.7       cvs       246:    if (docid_status == NULL)
                    247:      {
1.116     cvs       248:        docid_status = (AHTDocId_Status *) 
                    249:          TtaGetMemory (sizeof (AHTDocId_Status));
1.7       cvs       250:        docid_status->docid = docid;
                    251:        docid_status->counter = 1;
                    252:        HTList_addObject (Amaya->docid_status, (void *) docid_status);
                    253:      }
                    254:    else
1.4       cvs       255:       docid_status->counter++;
                    256: 
                    257:    Amaya->open_requests++;
                    258: 
1.74      cvs       259: #ifdef DEBUG_LIBWWW
                    260:    fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
                    261: #endif   
1.116     cvs       262: 
1.4       cvs       263:    return me;
                    264: }
                    265: 
1.5       cvs       266: /*----------------------------------------------------------------------
1.17      cvs       267:   AHTReqContext_delete
                    268:   Delete an Amaya Context Object and update the global Amaya request
                    269:   status.
1.5       cvs       270:   ----------------------------------------------------------------------*/
1.4       cvs       271: 
                    272: #ifdef __STDC__
1.15      cvs       273: boolean   AHTReqContext_delete (AHTReqContext * me)
1.4       cvs       274: #else
1.15      cvs       275: boolean   AHTReqContext_delete (me)
1.4       cvs       276: AHTReqContext      *me;
                    277: 
                    278: #endif
                    279: {
                    280:    AHTDocId_Status    *docid_status;
                    281: 
1.7       cvs       282:    if (me)
                    283:      {
1.4       cvs       284: 
1.74      cvs       285: #ifdef DEBUG_LIBWWW
                    286:         fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
                    287: #endif   
                    288: 
1.7       cvs       289:        if (Amaya->reqlist)
                    290:           HTList_removeObject (Amaya->reqlist, (void *) me);
1.4       cvs       291: 
1.7       cvs       292:        docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
1.4       cvs       293: 
1.7       cvs       294:        if (docid_status)
                    295:          {
                    296:             docid_status->counter--;
                    297: 
                    298:             if (docid_status->counter == 0)
                    299:               {
                    300:                  HTList_removeObject (Amaya->docid_status, (void *) docid_status);
                    301:                  TtaFreeMemory ((void *) docid_status);
                    302:               }
                    303:          }
1.79      cvs       304:        if (HTRequest_outputStream (me->request))
                    305:          AHTFWriter_FREE (me->request->output_stream);
1.7       cvs       306:        HTRequest_delete (me->request);
1.85      cvs       307: 
1.89      cvs       308:        if (me->output && me->output != stdout)
1.85      cvs       309:          {     
                    310: #ifdef DEBUG_LIBWWW       
                    311:            fprintf (stderr, "AHTReqContext_delete: URL is  %s, closing "
                    312:                     "FILE %p\n", me->urlName, me->output); 
                    313: #endif
                    314:            fclose (me->output);
                    315:            me->output = NULL;
                    316:          }
1.79      cvs       317:          
1.7       cvs       318:        if (me->error_stream != (char *) NULL)
1.70      cvs       319:          HT_FREE (me->error_stream);
                    320: #     ifndef _WINDOWS
                    321: #     ifdef WWW_XWINDOWS       
1.21      cvs       322:        if (me->read_xtinput_id || me->write_xtinput_id ||
                    323:             me->except_xtinput_id)
                    324:           RequestKillAllXtevents(me);
1.70      cvs       325: #     endif /* WWW_XWINDOWS */
                    326: #     endif /* !_WINDOWS */
1.74      cvs       327: 
1.99      cvs       328:     if (me->reqStatus == HT_ABORT)
                    329:       {
                    330:       if (me->outputfile && me->outputfile[0] != EOS)
                    331:        {
                    332:          TtaFileUnlink (me->outputfile);
                    333:          me->outputfile[0] = EOS;
                    334:        }
                    335:       }
                    336: 
1.74      cvs       337:        if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
                    338:          /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
                    339:             or in PutObjectWWW */
                    340:          {
1.79      cvs       341:             if (me->urlName)
                    342:               TtaFreeMemory (me->urlName);
                    343:             if (me->outputfile)
                    344:               TtaFreeMemory (me->outputfile);
1.74      cvs       345:          }
1.113     cvs       346: 
                    347:        if (me->content_type)
                    348:          TtaFreeMemory (me->content_type);
1.116     cvs       349:        /* @@@ need to do this better */
                    350:        if (me->formdata)
                    351:          HTAssocList_delete (me->formdata);
                    352: 
                    353:        /* to trace bugs */
                    354:        memset ((void *) me, 0, sizeof (AHTReqContext));
1.99      cvs       355: 
1.7       cvs       356:        TtaFreeMemory ((void *) me);
1.116     cvs       357: 
1.7       cvs       358:        Amaya->open_requests--;
1.116     cvs       359: 
1.15      cvs       360:        return TRUE;
1.7       cvs       361:      }
1.15      cvs       362:    return FALSE;
1.4       cvs       363: }
                    364: 
                    365: 
1.15      cvs       366: /*----------------------------------------------------------------------
1.17      cvs       367:   Thread_deleteAll
                    368:   this function deletes the whole list of active threads.           
1.5       cvs       369:   ----------------------------------------------------------------------*/
1.4       cvs       370: #ifdef __STDC__
                    371: static void         Thread_deleteAll (void)
                    372: #else
                    373: static void         Thread_deleteAll ()
                    374: #endif
                    375: {
1.21      cvs       376:   HTList             *cur;
                    377:   AHTReqContext      *me;
                    378:   AHTDocId_Status    *docid_status;
                    379: 
1.74      cvs       380:   if (Amaya && Amaya->reqlist)
                    381:     {
                    382:       if (Amaya->open_requests > 0)
                    383: #ifdef DEBUG_LIBWWW
                    384:       fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
                    385:                       "requests\n", Amaya->open_requests);
                    386: #endif   
                    387:        {
                    388:          cur = Amaya->reqlist;
                    389:          
                    390:          /* erase the requests */
                    391:          while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
                    392:            {
                    393:              if (me->request)
                    394:                {
1.70      cvs       395: #              ifndef _WINDOWS 
1.74      cvs       396:                  RequestKillAllXtevents (me);
1.70      cvs       397: #              endif /* !_WINDOWS */
1.92      cvs       398: 
                    399:                  if (me->request->net)
                    400:                    HTRequest_kill (me->request);
1.96      cvs       401: #ifndef _WINDOWS
                    402:                  AHTReqContext_delete (me);
                    403: #endif /* _WINDOWS */
1.74      cvs       404:                }
                    405:            }           /* while */
                    406:          
                    407:          /* erase the docid_status entities */
                    408:          while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
                    409:            TtaFreeMemory ((void *) docid_status);
                    410:          
                    411:        }                       /* if */
1.84      cvs       412:        
1.74      cvs       413:     }
1.4       cvs       414: }
                    415: 
1.5       cvs       416: /*----------------------------------------------------------------------
1.83      cvs       417:   AHTOpen_file
                    418:   ----------------------------------------------------------------------*/
                    419: #ifdef __STDC__
1.85      cvs       420: int                 AHTOpen_file (HTRequest * request)
1.83      cvs       421: #else
1.85      cvs       422: int                 AHTOpen_file (request)
1.83      cvs       423: HTRequest           *request;
                    424: 
                    425: #endif /* __STDC__ */
                    426: {
                    427:   AHTReqContext      *me;      /* current request */
                    428: 
                    429:   me = HTRequest_context (request);
                    430: 
1.93      cvs       431: 
                    432:   if (!me)
                    433:       return HT_ERROR;
                    434: 
1.83      cvs       435: #ifdef DEBUG_LIBWWW
1.116     cvs       436:   fprintf(stderr, "AHTOpen_file: start for object : %p\n", me);
1.85      cvs       437: #endif /* DEBUG_LIBWWW */
                    438: 
                    439:   if (me->reqStatus == HT_ABORT) 
                    440:     {
                    441: #ifdef DEBUG_LIBWWW
                    442:       fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83      cvs       443: #endif /* DEBUG_LIBWWW */
                    444: 
1.85      cvs       445:       return HT_OK;
                    446:     }
                    447: 
                    448:   if (HTRequest_outputStream (me->request)) 
                    449:     {
                    450: 
                    451: #ifdef DEBUG_LIBWWW
                    452:       fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
                    453: #endif /* DEBUG_LIBWWW */      
                    454:       return HT_OK;
                    455:     }
                    456: 
                    457: #ifdef DEBUG_LIBWWW
                    458:       fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
                    459: #endif /* DEBUG_LIBWWW */      
                    460: 
1.83      cvs       461:   if (!(me->output) && 
                    462:       (me->output != stdout) && 
                    463: #ifndef _WINDOWS
                    464:       (me->output = fopen (me->outputfile, "w")) == NULL)
                    465:     {
1.116     cvs       466: #else /* !_WINDOWS */
1.83      cvs       467:     (me->output = fopen (me->outputfile, "wb")) == NULL) 
                    468:     {
                    469: #endif /* !_WINDOWS */
                    470: 
                    471:       me->outputfile[0] = '\0';        /* file could not be opened */
1.85      cvs       472: #ifdef DEBUG_LIBWWW
                    473:       fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
                    474: #endif
1.83      cvs       475:       TtaSetStatus (me->docid, 1, 
                    476:                    TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
                    477:                    me->outputfile);
                    478:       me->reqStatus = HT_ERR;
                    479:       return (HT_ERROR);
                    480:     }
                    481:          
                    482:   HTRequest_setOutputStream (me->request,
                    483:                             AHTFWriter_new (me->request, 
                    484:                                             me->output, YES));
                    485:   me->reqStatus = HT_WAITING;
                    486:   
                    487:   return HT_OK;
                    488: }
                    489: 
                    490: /*----------------------------------------------------------------------
1.17      cvs       491:   redirection_handler
                    492:   this function is registered to handle permanent and temporary
                    493:   redirections.
                    494:   ----------------------------------------------------------------------*/
1.4       cvs       495: #ifdef __STDC__
1.7       cvs       496: static int          redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       497: #else
                    498: static int          redirection_handler (request, context, status)
                    499: HTRequest          *request;
                    500: HTResponse         *response;
                    501: void               *param;
                    502: int                 status;
                    503: 
                    504: #endif
                    505: {
                    506: 
                    507:    HTAnchor           *new_anchor = HTResponse_redirection (response);
1.7       cvs       508:    AHTReqContext      *me = HTRequest_context (request);
1.4       cvs       509:    HTMethod            method = HTRequest_method (request);
1.98      cvs       510:    char               *ref;
                    511:    char               *tmp;
1.4       cvs       512: 
1.7       cvs       513:    if (!new_anchor)
                    514:      {
                    515:        if (PROT_TRACE)
                    516:           HTTrace ("Redirection. No destination\n");
                    517:        return HT_OK;
                    518:      }
1.4       cvs       519: 
                    520:    /*
                    521:       ** Only do redirect on GET and HEAD
                    522:     */
1.7       cvs       523:    if (!HTMethod_isSafe (method))
                    524:      {
                    525:        HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
                    526:        if (prompt)
                    527:          {
                    528:             if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    529:                            NULL, NULL, NULL) != YES)
                    530:                return HT_ERROR;
                    531:          }
                    532:      }
1.4       cvs       533: 
                    534:    /*
                    535:     **  Start new request with the redirect anchor found in the headers.
                    536:     **  Note that we reuse the same request object which means that we must
                    537:     **  keep this around until the redirected request has terminated. It also           
                    538:     **  allows us in an easy way to keep track of the number of redirections
                    539:     **  so that we can detect endless loops.
                    540:     */
1.17      cvs       541:    
1.7       cvs       542:    if (HTRequest_doRetry (request))
                    543:      {
                    544:        /* do we need to normalize the URL? */
                    545:        if (strncmp (new_anchor->parent->address, "http:", 5))
                    546:          {
                    547:             /* Yes, so we use the pre-redirection anchor as a base name */
1.74      cvs       548:             ref = AmayaParseUrl (new_anchor->parent->address, 
                    549:                                  me->urlName, AMAYA_PARSE_ALL);
1.7       cvs       550:             if (ref)
                    551:               {
1.98      cvs       552:                 HT_FREE (new_anchor->parent->address);
                    553:                 tmp = NULL;
                    554:                 HTSACopy (&tmp, ref);
                    555:                 new_anchor->parent->address = tmp;
                    556:                 TtaFreeMemory (ref);
1.7       cvs       557:               }
1.98      cvs       558:             else
                    559:               return HT_ERROR; /* We can't redirect anymore */
1.7       cvs       560:          }
                    561: 
                    562:        /* update the current file name */
1.116     cvs       563:        TtaFreeMemory (me->urlName);
                    564:        me->urlName = TtaStrdup (new_anchor->parent->address);
                    565: 
1.21      cvs       566:        if (strlen (new_anchor->parent->address) > (MAX_LENGTH - 2))
1.7       cvs       567:          {
1.116     cvs       568:             strncpy (me->urlName, new_anchor->parent->address, 
                    569:                      MAX_LENGTH - 1);
1.21      cvs       570:             me->urlName[MAX_LENGTH - 1] = EOS;
1.7       cvs       571:          }
                    572:        else
1.74      cvs       573:          strcpy (me->urlName, new_anchor->parent->address);
1.7       cvs       574: 
1.38      cvs       575:        ChopURL (me->status_urlName, me->urlName);
1.116     cvs       576: 
1.7       cvs       577:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.38      cvs       578:                      me->status_urlName);
1.7       cvs       579: 
                    580:        /* Start request with new credentials */
1.64      cvs       581: 
1.79      cvs       582:        if (HTRequest_outputStream (me->request) != NULL) {
1.85      cvs       583:          AHTFWriter_FREE (request->output_stream);
                    584:          if (me->output != stdout) { /* Are we writing to a file? */
1.74      cvs       585: #ifdef DEBUG_LIBWWW
1.79      cvs       586:          fprintf (stderr, "redirection_handler: New URL is  %s, closing "
1.74      cvs       587:                             "FILE %p\n", me->urlName, me->output); 
                    588: #endif 
1.79      cvs       589:            fclose (me->output);
                    590:            me->output = NULL;
                    591:          }
1.64      cvs       592:        }
1.68      cvs       593: 
1.105     cvs       594:        /* reset the status */
1.116     cvs       595:        me->reqStatus = HT_NEW; 
1.105     cvs       596:        /* clear the errors */
1.116     cvs       597:        HTError_deleteAll (HTRequest_error (request));
1.105     cvs       598:        HTRequest_setError (request, NULL);
1.116     cvs       599: 
1.7       cvs       600:        if (me->method == METHOD_PUT || me->method == METHOD_POST)      /* PUT, POST etc. */
1.74      cvs       601:          status = HTLoadAbsolute (me->urlName, request);
1.7       cvs       602:        else
1.74      cvs       603:          HTLoadAnchor (new_anchor, request);
1.7       cvs       604:      }
                    605:    else
1.68      cvs       606:      {
1.74      cvs       607:        HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    608:                           NULL, 0, "HTRedirectFilter");
                    609:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
                    610:                     NULL);
                    611:        if (me->error_html)
                    612:         DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR; 
                    613:        /* so that we can show the error message */
1.68      cvs       614:      }
                    615: 
1.4       cvs       616:    /*
1.74      cvs       617:    **  By returning HT_ERROR we make sure that this is the last handler to be
                    618:    **  called. We do this as we don't want any other filter to delete the 
                    619:    **  request object now when we have just started a new one ourselves
                    620:    */
1.4       cvs       621:    return HT_ERROR;
                    622: }
                    623: 
1.5       cvs       624: /*----------------------------------------------------------------------
1.17      cvs       625:   terminate_handler
                    626:   this function is registered to handle the result of the request
1.5       cvs       627:   ----------------------------------------------------------------------*/
1.4       cvs       628: #if __STDC__
1.7       cvs       629: static int          terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4       cvs       630: #else
                    631: static int          terminate_handler (request, response, context, status)
                    632: HTRequest          *request;
                    633: HTResponse         *response;
                    634: void               *context;
                    635: int                 status;
1.123     cvs       636: #endif /* __STDC__ */
1.4       cvs       637: {
1.116     cvs       638:   AHTReqContext      *me = (AHTReqContext *) HTRequest_context (request);
                    639:   boolean             error_flag;
                    640:   char               *content_type;
                    641: 
                    642:   if (!me)
                    643:     return HT_OK;              /* not an Amaya request */
                    644: 
                    645:   if (me->reqStatus == HT_END)
                    646:     /* we have already processed this request! */
                    647:     return HT_OK;
                    648: 
                    649:   /* if Amaya was killed, treat with this request as if it were
                    650:      issued by a Stop button event */
                    651: 
                    652:    if (!AmayaIsAlive ())           
1.80      cvs       653:       me->reqStatus = HT_ABORT; 
1.74      cvs       654:    
1.77      cvs       655:    if (status == HT_LOADED || 
                    656:        status == HT_CREATED || 
1.79      cvs       657:        status == HT_NO_DATA ||
1.116     cvs       658: #ifdef AMAYA_WWW_CACHE
                    659:        /* what status to use to know we're downloading from a cache? */
                    660:        status ==  HT_NOT_MODIFIED ||
                    661: #endif /* AMAYA_WWW_CACHE */
1.79      cvs       662:        me->reqStatus == HT_ABORT)
1.74      cvs       663:      error_flag = FALSE;
1.13      cvs       664:    else
1.74      cvs       665:      error_flag = TRUE;
                    666:    
1.4       cvs       667:    /* output any errors from the server */
1.74      cvs       668:    
1.77      cvs       669:    /*
1.74      cvs       670:    ** me->output = output file which will receive an html file
                    671:    ** me->error_html = yes, output HTML errors in the screen
                    672:    ** request->error_stack == if there are any errors, they will be here
                    673:    ** me->error_stream_size If it's != 0 means an error message has already
                    674:    **                       been written to the stack
                    675:    */
                    676:    
1.4       cvs       677:    /* First, we verify if there are any errors and if they are not
1.17      cvs       678:    ** yet written to the error stack. If no, then let's try to write them
                    679:    ** ourselves
                    680:    */
1.74      cvs       681:    
                    682: #ifdef DEBUG_LIBWWW
                    683:    fprintf (stderr, "terminate_handler: URL is "
                    684:            "%s, closing FILE %p status is %d\n", me->urlName, me->output, 
                    685:            status); 
1.69      cvs       686: #endif
1.74      cvs       687:    
1.69      cvs       688:    if (me->output && me->output != stdout)
                    689:      {
1.74      cvs       690:        /* we are writing to a file */
                    691:        if (me->reqStatus != HT_ABORT)
                    692:         {                      /* if the request was not aborted and */
1.80      cvs       693:           if (error_flag &&
                    694:               me->error_html == TRUE)
                    695:               /* there were some errors and we want to print them */
                    696:             {          
                    697:               if (me->error_stream_size == 0)/* and the stream is empty */
                    698:                 AHTError_MemPrint (request); /* copy errors from 
1.74      cvs       699:                                                  **the error stack 
1.77      cvs       700:                                                  ** into the error stream */
1.80      cvs       701:               if (me->error_stream)
                    702:                 {      /* if the stream is non-empty */
                    703:                   fprintf (me->output, me->error_stream);/* output the errors */
1.85      cvs       704:                   /* Clear the error context, so that we can deal with
                    705:                      this answer as if it were a normal reply */
                    706:                    HTError_deleteAll( HTRequest_error (request));
                    707:                    HTRequest_setError (request, NULL);
                    708:                    error_flag = 0;
1.74      cvs       709:                 }
                    710:             }                  /* if error_stack */
1.85      cvs       711:         }
                    712: 
                    713:        /* if != HT_ABORT */
                    714:        
                    715: #ifdef DEBUG_LIBWWW       
                    716:        fprintf (stderr, "terminate_handler: URL is  %s, closing "
                    717:                "FILE %p\n", me->urlName, me->output); 
                    718: #endif
1.74      cvs       719:        fclose (me->output);
                    720:        me->output = NULL;
1.69      cvs       721:      }
1.80      cvs       722: 
                    723:    if (error_flag)
                    724:      me->reqStatus = HT_ERR;
                    725:    else if (me->reqStatus != HT_ABORT)
                    726:      me->reqStatus = HT_END;
1.116     cvs       727:    
                    728:    /* copy the content_type */
                    729:    content_type = request->anchor->content_type->name;
1.114     cvs       730:    if (content_type && content_type [0] != EOS)
1.88      cvs       731:      {
1.114     cvs       732:        /* libwww gives www/unknown when it gets an error. As this is 
                    733:          an HTML test, we force the type to text/html */
                    734:        if (!strcmp (content_type, "www/unknown"))
                    735:         me->content_type = TtaStrdup ("text/html");
                    736:        else
                    737:         me->content_type = TtaStrdup (content_type);
                    738: 
                    739:        /* Content-Type can be specified by an httpd  server's admin. To be on
                    740:          the safe side, we normalize its case */
                    741:        ConvertToLowerCase (me->content_type);
                    742: 
1.88      cvs       743: #ifdef DEBUG_LIBWWW
                    744:         fprintf (stderr, "content type is: %s\n", me->content_type);
                    745: #endif /* DEBUG_LIBWWW */
1.114     cvs       746:      } 
                    747: 
1.115     cvs       748:    /* to avoid a hangup while downloading css files */
                    749:    if (AmayaAlive && (me->mode & AMAYA_LOAD_CSS))
1.116     cvs       750:      TtaSetStatus (me->docid, 1, 
                    751:                   TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
                    752:                   me->status_urlName);
                    753:    
                    754:   /* don't remove or Xt will hang up during the PUT */
                    755:    if (AmayaIsAlive ()  && ((me->method == METHOD_POST) ||
                    756:                            (me->method == METHOD_PUT)))
1.7       cvs       757:      {
1.80      cvs       758:        PrintTerminateStatus (me, status);
1.116     cvs       759:        
1.80      cvs       760:      } 
1.114     cvs       761: 
1.111     cvs       762:    ProcessTerminateRequest (request, response, context, status);
1.116     cvs       763:    
1.111     cvs       764:    return HT_OK;
1.4       cvs       765: }
                    766: 
1.5       cvs       767: /*----------------------------------------------------------------------
1.17      cvs       768:   AHTLoadTerminate_handler
1.74      cvs       769:   this is an application "AFTER" Callback. It's called by the library
                    770:   when a request has ended, so that we can setup the correct status.
1.5       cvs       771:   ----------------------------------------------------------------------*/
1.4       cvs       772: 
                    773: #ifdef __STDC__
1.111     cvs       774: int          AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       775: #else
1.111     cvs       776: int          AHTLoadTerminate_handler (request, response, param, status)
1.4       cvs       777: HTRequest          *request;
                    778: HTResponse         *response;
                    779: void               *param;
                    780: int                 status;
1.69      cvs       781: 
1.4       cvs       782: #endif
                    783: {
1.116     cvs       784: 
                    785:   /** @@@@ use this with printstatus ?? */
                    786: 
1.4       cvs       787:    AHTReqContext      *me = HTRequest_context (request);
                    788:    HTAlertCallback    *cbf;
                    789:    AHTDocId_Status    *docid_status;
                    790: 
1.123     cvs       791: #ifdef _WINDOWS
                    792:    /* @@@ I have problems with the trace variables under windows */
1.124     cvs       793:    return HT_OK;
1.123     cvs       794: #endif /* _WINDOWS */
                    795: 
1.7       cvs       796:    switch (status)
1.116     cvs       797:      {
                    798:         case HT_LOADED:
                    799:           if (PROT_TRACE)
                    800:             HTTrace ("Load End.... OK: `%s\' has been accessed\n",
                    801:                      me->status_urlName);
1.4       cvs       802: 
1.116     cvs       803:           docid_status = GetDocIdStatus (me->docid,
                    804:                                          Amaya->docid_status);
1.7       cvs       805: 
1.116     cvs       806:           if (docid_status != NULL && docid_status->counter > 1)
                    807:             TtaSetStatus (me->docid, 1, 
                    808:                           TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
1.74      cvs       809:                           me->status_urlName);
1.7       cvs       810:               break;
1.116     cvs       811:               
                    812:      case HT_NO_DATA:
                    813:        if (PROT_TRACE)
                    814:         HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", 
                    815:                  me->status_urlName);
                    816:        TtaSetStatus (me->docid, 1, 
                    817:                     TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
                    818:                     me->status_urlName);
                    819:        break;
                    820:        
                    821:      case HT_INTERRUPTED:
                    822:        if (PROT_TRACE)
                    823:         HTTrace ("Load End.... INTERRUPTED: `%s\'\n", 
                    824:                  me->status_urlName);
                    825:        TtaSetStatus (me->docid, 1, 
                    826:                     TtaGetMessage (AMAYA, AM_LOAD_ABORT), 
                    827:                     NULL);
                    828:        break;
1.7       cvs       829: 
1.116     cvs       830:      case HT_RETRY:
                    831:        if (PROT_TRACE)
                    832:         HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    833:                  HTResponse_retryTime (response));
                    834:        TtaSetStatus (me->docid, 1, 
                    835:                     TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
                    836:                     me->status_urlName);
                    837:        break;
1.7       cvs       838: 
1.116     cvs       839:      case HT_ERROR:
                    840:        cbf = HTAlert_find (HT_A_MESSAGE);
                    841:        if (cbf)
                    842:         (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    843:                 HTRequest_error (request), NULL);
                    844:        break;
                    845:        
                    846:        if (PROT_TRACE)
                    847:         HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
                    848:                  me->status_urlName ? me->status_urlName :"<UNKNOWN>");
                    849:        TtaSetStatus (me->docid, 1,
                    850:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                    851:                     me->status_urlName ? me->status_urlName : "<UNKNOWN>");
                    852:        break;
                    853:      default:
                    854:        if (PROT_TRACE)
                    855:         HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
                    856:        break;
                    857:      }
                    858:    
                    859:    return HT_OK;
                    860: }
1.7       cvs       861: 
1.116     cvs       862: #ifdef DEBUG_LIBWWW
                    863: static  int LineTrace (const char * fmt, va_list pArgs)
                    864: {
                    865:     return (vfprintf(stderr, fmt, pArgs));
1.4       cvs       866: }
1.116     cvs       867: #endif DEBUG_LIBWWW
1.4       cvs       868: 
1.27      cvs       869: /*----------------------------------------------------------------------
                    870:   AHTAcceptTypesInit
1.74      cvs       871:   This function prepares the Accept header used by Amaya during
                    872:   the HTTP content negotiation phase
1.27      cvs       873:   ----------------------------------------------------------------------*/
                    874: #ifdef __STDC__
                    875: static void           AHTAcceptTypesInit (HTList *c)
                    876: #else  /* __STDC__ */
                    877: static void           AHTAcceptTypesInit (c)
                    878: HTList             *c;
                    879: #endif /* __STDC__ */
                    880: {
                    881:   if (c == (HTList *) NULL) 
                    882:       return;
                    883: 
                    884:       /* define here all the mime types that Amaya can accept */
                    885: 
1.74      cvs       886:       HTConversion_add (c, "image/png",  "www/present", 
                    887:                        HTThroughLine, 1.0, 0.0, 0.0);
                    888:       HTConversion_add (c, "image/jpeg", "www/present", 
                    889:                        HTThroughLine, 1.0, 0.0, 0.0);
                    890:       HTConversion_add (c, "image/gif",  "www/present", 
                    891:                        HTThroughLine, 1.0, 0.0, 0.0);
                    892:       HTConversion_add (c, "image/xbm",  "www/present", 
                    893:                        HTThroughLine, 1.0, 0.0, 0.0);
                    894:       HTConversion_add (c, "image/xpm",  "www/present", 
                    895:                        HTThroughLine, 1.0, 0.0, 0.0);
                    896:       HTConversion_add (c, "application/postscript",  
                    897:                        "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.4       cvs       898: 
1.27      cvs       899:    /* Define here the equivalences between MIME types and file extensions for
                    900:     the types that Amaya can display */
                    901: 
                    902:    /* Register the default set of file suffix bindings */
                    903:    HTFileInit ();
                    904: 
                    905:    /* Don't do any case distinction */
                    906:    HTBind_caseSensitive (FALSE);
                    907: }
1.4       cvs       908: 
1.5       cvs       909: /*----------------------------------------------------------------------
1.17      cvs       910:   AHTConverterInit
                    911:   Bindings between a source media type and a destination media type
                    912:   (conversion).
1.5       cvs       913:   ----------------------------------------------------------------------*/
1.15      cvs       914: #ifdef __STDC__
                    915: static void         AHTConverterInit (HTList *c)
                    916: #else  /* __STDC__ */
                    917: static void         AHTConverterInit (c)
                    918: HTList             *c;
                    919: #endif /* __STDC__ */
1.4       cvs       920: {
                    921: 
                    922:    /* Handler for custom http error messages */
1.7       cvs       923:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs       924: 
                    925:    /*
                    926:     ** These are converters that converts to something other than www/present,
                    927:     ** that is not directly outputting someting to the user on the screen
                    928:     */
                    929: 
1.116     cvs       930:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 
                    931:                     1.0, 0.0, 0.0);
1.4       cvs       932:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
                    933:                     1.0, 0.0, 0.0);
                    934:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
                    935:                     1.0, 0.0, 0.0);
1.116     cvs       936:    HTConversion_add(c,"message/x-rfc822-cont", "*/*", HTMIMEContinue,  
                    937:                    1.0, 0.0, 0.0);
                    938:    HTConversion_add(c,"message/x-rfc822-partial","*/*",        HTMIMEPartial,
                    939:                    1.0, 0.0, 0.0);
1.4       cvs       940:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
                    941:                     1.0, 0.0, 0.0);
                    942:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
                    943:                     1.0, 0.0, 0.0);
                    944: 
                    945:    /*
1.116     cvs       946:    ** The following conversions are converting ASCII output from various
                    947:    ** protocols to HTML objects.
                    948:    */
1.4       cvs       949:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
                    950:                     1.0, 0.0, 0.0);
                    951: 
                    952:    /*
1.116     cvs       953:    ** We also register a special content type guess stream that can figure out
                    954:    ** the content type by reading the first bytes of the stream
                    955:    */
1.4       cvs       956:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
                    957:                     1.0, 0.0, 0.0);
                    958: 
1.116     cvs       959: #ifdef AMAYA_WWW_CACHE
1.4       cvs       960:    /*
1.116     cvs       961:    ** Register a persistent cache stream which can save an object to local
                    962:    ** file
                    963:    */
1.4       cvs       964:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
                    965:                     1.0, 0.0, 0.0);
1.116     cvs       966:    HTConversion_add(c,"www/cache-append", "*/*", HTCacheAppend,
                    967:                    1.0, 0.0, 0.0);
                    968: #endif AMAYA_WWW_CACHE
1.4       cvs       969: 
                    970:    /*
1.116     cvs       971:    ** This dumps all other formats to local disk without any further
                    972:    ** action taken
                    973:    */
1.4       cvs       974:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
                    975:                     0.3, 0.0, 0.0);
1.116     cvs       976:    
1.4       cvs       977: }
                    978: 
1.27      cvs       979: 
1.15      cvs       980: /*----------------------------------------------------------------------
1.17      cvs       981:   AHTProtocolInit
                    982:   Registers all amaya supported protocols.
1.15      cvs       983:   ----------------------------------------------------------------------*/
1.4       cvs       984: static void         AHTProtocolInit (void)
                    985: {
1.116     cvs       986:   char *strptr;
1.4       cvs       987: 
1.116     cvs       988:   /* 
                    989:      NB. Preemptive == YES means Blocking requests
                    990:      Non-preemptive == NO means Non-blocking requests
                    991:      */
                    992:   HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new);
                    993:   HTTransport_add("buffered_tcp", HT_TP_SINGLE, HTReader_new, 
                    994:                  HTBufferWriter_new);
                    995:   HTProtocol_add ("http", "buffered_tcp", HTTP_PORT, NO, HTLoadHTTP, NULL);
                    996:   HTProtocol_add ("file", "local", 0, NO, HTLoadFile, NULL);
                    997: #ifdef AMAYA_WWW_CACHE
                    998:    HTProtocol_add("cache",  "local", 0, YES, HTLoadCache, NULL);
                    999: #endif /* AMAYA_WWW_CACHE */
1.103     cvs      1000: #if 0 /* experimental code */
1.116     cvs      1001:    HTProtocol_add ("ftp", "tcp", FTP_PORT, NO, HTLoadFTP, NULL);
1.17      cvs      1002: #endif
1.116     cvs      1003: 
                   1004:    /* initialize pipelining */
1.121     cvs      1005:   strptr = (char *) TtaGetEnvString ("ENABLE_PIPELINING");
1.116     cvs      1006:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
                   1007:     HTTP_setConnectionMode (HTTP_11_NO_PIPELINING);
1.4       cvs      1008: }
                   1009: 
1.15      cvs      1010: /*----------------------------------------------------------------------
1.17      cvs      1011:   AHTNetInit
                   1012:   Reegisters "before" and "after" request filters.
1.15      cvs      1013:   ----------------------------------------------------------------------*/
1.4       cvs      1014: static void         AHTNetInit (void)
                   1015: {
                   1016: 
                   1017: /*      Register BEFORE filters
1.74      cvs      1018: **      The BEFORE filters handle proxies, caches, rule files etc.
                   1019: **      The filters are called in the order by which the are registered
                   1020: **      Not done automaticly - may be done by application!
                   1021: */
1.4       cvs      1022: 
1.116     cvs      1023: #ifdef AMAYA_WWW_CACHE  
                   1024:   HTNet_addBefore (HTCacheFilter, "http://*", NULL, HT_FILTER_MIDDLE);
                   1025: #endif /* AMAYA_WWW_CACHE */
                   1026:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, HT_FILTER_LATE);
                   1027:   HTNet_addBefore (HTProxyFilter, NULL, NULL, HT_FILTER_LATE);
1.85      cvs      1028:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1029: 
                   1030: /*      register AFTER filters
1.74      cvs      1031: **      The AFTER filters handle error messages, logging, redirection,
                   1032: **      authentication etc.
                   1033: **      The filters are called in the order by which the are registered
                   1034: **      Not done automaticly - may be done by application!
                   1035: */
1.4       cvs      1036: 
1.116     cvs      1037:   HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS,
                   1038:                  HT_FILTER_MIDDLE);
                   1039:   HTNet_addAfter(HTAuthFilter, "http://*", NULL, HT_REAUTH,
                   1040:                 HT_FILTER_MIDDLE);
                   1041:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT,
                   1042:                  HT_FILTER_MIDDLE);
                   1043:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_FOUND, 
                   1044:                  HT_FILTER_MIDDLE);
                   1045:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_SEE_OTHER,
                   1046:                  HT_FILTER_MIDDLE);
                   1047:   HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT,
                   1048:                  HT_FILTER_MIDDLE);
                   1049:   HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY,
                   1050:                  HT_FILTER_MIDDLE);
                   1051: #ifdef AMAYA_WWW_CACHE
                   1052:   HTNet_addAfter (HTCacheUpdateFilter, "http://*", NULL, HT_NOT_MODIFIED, 
                   1053:                  HT_FILTER_MIDDLE);
                   1054: #endif /* AMAYA_WWW_CACHE */
1.111     cvs      1055: #ifndef _WINDOWS
1.116     cvs      1056:   HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, 
                   1057:                   HT_FILTER_LAST);     
1.111     cvs      1058: #endif /* !_WINDOWS */
1.116     cvs      1059:    /**** for later ?? ****/
                   1060:    /*  HTNet_addAfter(HTInfoFilter,    NULL,           NULL, HT_ALL,           HT_FILTER_LATE); */
1.51      cvs      1061:    /* handles all errors */
1.116     cvs      1062:   HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1063: }
                   1064: 
1.15      cvs      1065: /*----------------------------------------------------------------------
1.17      cvs      1066:   AHTAlertInit
                   1067:   Register alert messages and their callbacks.
1.15      cvs      1068:   ----------------------------------------------------------------------*/
                   1069: #ifdef __STDC__
                   1070: static void         AHTAlertInit (void)
                   1071: #else
                   1072: static void         AHTAlertInit ()
                   1073: #endif
                   1074: {
                   1075:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.120     cvs      1076: #ifdef __WINDOWS
1.70      cvs      1077:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
1.116     cvs      1078: #endif /* _WINDOWS */
1.15      cvs      1079:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1080:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1081:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1082:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1083:    HTAlert_add (AHTPromptPassword, HT_A_SECRET);
                   1084:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
1.116     cvs      1085: #ifdef AMAYA_WWW_CACHE
1.117     cvs      1086:    /***
                   1087:      HTAlert_add (AHTConfirm, HT_MSG_CACHE_LOCK);
                   1088:      ***/
1.116     cvs      1089: #endif /* AMAYA_WWW_CACHE */
                   1090: }
                   1091: 
1.128   ! cvs      1092: /*----------------------------------------------------------------------
        !          1093:   libwww_CleanCache
        !          1094:   frontend to the recursive cache cleaning function
        !          1095:   ----------------------------------------------------------------------*/
        !          1096: #ifdef __STDC__
        !          1097: void libwww_CleanCache (void)
        !          1098: #else
        !          1099: void libwww_CleanCache ()
        !          1100: Document doc;
        !          1101: View view;
        !          1102: #endif /* __STDC__ */
        !          1103: {
        !          1104:   char *strptr;
        !          1105:   char *cache_dir;
        !          1106:   int cache_size;
        !          1107: #ifdef AMAYA_WWW_CACHE
        !          1108:   if (!HTCacheMode_enabled ())
        !          1109:     /* don't do anything if we're not using a cache */
        !          1110:     return;
        !          1111:   /* temporarily close down the cache, purge it, then restart */
        !          1112:   cache_dir = strdup (HTCacheMode_getRoot ());
        !          1113:   cache_size = HTCacheMode_maxSize ();
        !          1114:   HTCacheTerminate ();
        !          1115:   strptr = TtaGetMemory (strlen (cache_dir) + 2);
        !          1116:   strcpy (strptr, cache_dir);
        !          1117:   strcat (strptr, DIR_STR);
        !          1118: 
        !          1119:   RecCleanCache (strptr);
        !          1120: 
        !          1121:   TtaFreeMemory (strptr);
        !          1122:   HTCacheMode_setExpires(HT_EXPIRES_AUTO);
        !          1123:   HTCacheInit (cache_dir, cache_size);
        !          1124:   TtaFreeMemory (cache_dir);
        !          1125: #endif /* AMAYA_WWW_CACHE */
        !          1126: }
        !          1127: 
1.118     cvs      1128: #ifdef AMAYA_WWW_CACHE
                   1129: /*----------------------------------------------------------------------
1.128   ! cvs      1130:   RecCleanCache
1.118     cvs      1131:   Clears an existing cache directory
                   1132:   ----------------------------------------------------------------------*/
                   1133: #ifdef __STDC__
1.128   ! cvs      1134: static void RecCleanCache (char *dirname)
1.118     cvs      1135: #else
1.128   ! cvs      1136: static void RecCleanCache (char *dirname)
        !          1137: Document doc;
        !          1138: View view;
1.118     cvs      1139: #endif /* __STDC__ */
1.128   ! cvs      1140: 
        !          1141: #ifdef _WINDOWS
        !          1142: {
        !          1143:   HANDLE hFindFile;
        !          1144:   boolean status;
        !          1145:   WIN32_FIND_DATA ffd;
        !          1146:   
        !          1147:   char t_dir [MAX_LENGTH];
        !          1148: 
        !          1149:   /* creat a t_dir name to start searching for files */
        !          1150:   if ((strlen (dirname) + 10) > MAX_LENGTH)
        !          1151:     /* ERROR: directory name is too big */
        !          1152:     return;
        !          1153: 
        !          1154:   strcpy (t_dir, dirname);
        !          1155:   strcat (t_dir, "\\*");
        !          1156: 
        !          1157:   hFindFile = FindFirstFile ("t_dir", &ffd);
        !          1158:     
        !          1159:   if (hFindFile == INVALID_HANDLE_VALUE)
        !          1160:     /* nothing to erase? */
        !          1161:     return;
        !          1162: 
        !          1163:   status = TRUE;
        !          1164:   while (status) {
        !          1165:     if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        !          1166:       {
        !          1167:        /* it's a directory, erase it recursively */
        !          1168:        RecCleanCache (ffd.cFileName);
        !          1169:        rmdir (ffd.cFileName);
        !          1170:       }
        !          1171:     else if (ffd.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
        !          1172:       {
        !          1173:        /* it's a file, erase it */
        !          1174:        /*TtaUnlink (ffd.cFileName); */
        !          1175:        /* @@ false condition, for not doing anything */
        !          1176:        status = TRUE;
        !          1177:       }
        !          1178:     status = FindNextFile (hFindFile, &ffd);
        !          1179:   }
        !          1180:   FindClose (hFindFile);
        !          1181: }
        !          1182: 
        !          1183: #else /* _WINDOWS */
1.118     cvs      1184: {
                   1185:   DIR *dp;
                   1186:   struct stat st;
                   1187: #ifdef HAVE_DIRENT_H
                   1188:   struct dirent *d;
                   1189: #else
                   1190:   struct direct *d;
                   1191: #endif /* HAVE_DIRENT_H */
1.128   ! cvs      1192:   char filename[BUFSIZ+1];
1.118     cvs      1193: 
                   1194:   if ((dp = opendir (dirname)) == NULL) 
                   1195:     {
                   1196:       /* @@@ we couldn't open the directory ... we need some msg */
                   1197:       perror (dirname);
                   1198:       exit;
                   1199:     }
                   1200:   
                   1201:   while ((d = readdir (dp)) != NULL)
                   1202:     {
                   1203:       /* skip the UNIX . and .. links */
                   1204:       if (!strcmp (d->d_name, "..")
                   1205:          || !strcmp (d->d_name, "."))
                   1206:        continue;
                   1207: 
1.128   ! cvs      1208:       sprintf (filename, "%s"DIR_STR"%s", dirname, d->d_name);
1.118     cvs      1209:       if  (lstat (filename, &st) < 0 ) 
                   1210:        {
                   1211:          /* @@2 need some error message */
                   1212:          perror (filename);
                   1213:          continue;
                   1214:        }
                   1215:       
                   1216:       switch (st.st_mode & S_IFMT)
                   1217:        {
                   1218:        case S_IFDIR:
                   1219:          /* if we find a directory, we erase it, recursively */
1.128   ! cvs      1220:          strcat (filename, DIR_STR);
        !          1221:          RecCleanCache (filename);
        !          1222:          rmdir (filename);
1.118     cvs      1223:          break;
                   1224:        case S_IFLNK:
                   1225:          /* skip any links we find */
                   1226:          continue;
                   1227:          break;
                   1228:        default:
                   1229:          /* erase the filename */
                   1230:          TtaFileUnlink (filename);
                   1231:          break;
                   1232:        }
                   1233:     }
                   1234:   closedir (dp);
                   1235: }
1.128   ! cvs      1236: #endif /* _WINDOWS */
1.118     cvs      1237: #endif /* AMAYA_WWW_CACHE */
                   1238: 
1.116     cvs      1239: /*----------------------------------------------------------------------
                   1240:   CacheInit
                   1241:   Reads the cache settings from the thot.ini file.
                   1242:   ----------------------------------------------------------------------*/
1.118     cvs      1243: #ifdef __STDC__
1.116     cvs      1244: static void CacheInit (void)
1.118     cvs      1245: #else
                   1246: static void Cacheinit ()
                   1247: #endif
                   1248: 
1.116     cvs      1249: {
                   1250: #ifndef AMAYA_WWW_CACHE
                   1251:    HTCacheMode_setEnabled (NO);
                   1252: 
                   1253: #else /* AMAYA_WWW_CACHE */
                   1254:   char *strptr;
                   1255:   char *cache_dir = NULL;
                   1256:   boolean cache;
1.128   ! cvs      1257:   
1.116     cvs      1258:   /* activate cache? */
                   1259:   strptr = (char *) TtaGetEnvString ("ENABLE_CACHE");
                   1260:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
                   1261:     cache = NO;
                   1262:   else
                   1263:     cache = YES;
                   1264: 
                   1265:   /* get the cache dir (or use a default one) */
                   1266:   strptr = (char *) TtaGetEnvString ("CACHE_DIR");
                   1267:   if (strptr && *strptr) 
                   1268:     {
                   1269:       cache_dir = TtaGetMemory (strlen (strptr) + strlen (CACHE_DIR_NAME) + 1);
                   1270:       strcpy (cache_dir, strptr);
                   1271:     }
                   1272:   else
                   1273:     {
1.122     cvs      1274:       cache_dir = TtaGetMemory (strlen (TempFileDirectory) 
1.116     cvs      1275:                                + strlen (CACHE_DIR_NAME) + 1);
1.122     cvs      1276:       strcpy (cache_dir, TempFileDirectory);
1.116     cvs      1277:     }
                   1278:   strcat (cache_dir, CACHE_DIR_NAME);
                   1279: 
                   1280:   /* get the cache size (or use a default one) */
                   1281:     strptr = (char *) TtaGetEnvString ("CACHE_SIZE");
                   1282:   if (strptr && *strptr) 
                   1283:       cache_size = atoi (strptr);
                   1284:   else
                   1285:     cache_size = DEFAULT_CACHE_SIZE;
                   1286: 
                   1287:   if (cache) 
                   1288:     {
                   1289:       /* how to remove the lock? force remove it? */
                   1290:       /* @@@ugly hack to remove the .lock file ... */
                   1291:       /* we would need something to remove the index, clear
                   1292:         the directory, etc. attention to dir-sep  */
                   1293:       strptr = TtaGetMemory (strlen (cache_dir) + 20);
                   1294:       strcpy (strptr, cache_dir);
1.128   ! cvs      1295:       strcat (strptr, DIR_STR".lock");
1.116     cvs      1296:       if (TtaFileExist (strptr))
                   1297:        {
                   1298:          /* remove the lock and clean the cache (the clean cache will remove
                   1299:           all, making the following call unnecessary */
1.118     cvs      1300:          strcpy (strptr, cache_dir);
1.128   ! cvs      1301:          strcat (strptr, DIR_STR);
        !          1302:          RecCleanCache (strptr);
1.116     cvs      1303:        }
                   1304:       TtaFreeMemory (strptr);
                   1305: 
                   1306:       /* store this in our libwww context */
                   1307:       HTCacheInit (cache_dir, cache_size);
                   1308:       HTCacheMode_setExpires(HT_EXPIRES_AUTO);
                   1309:     }
                   1310:   else
                   1311:       HTCacheMode_setEnabled (NO);
                   1312: 
                   1313:   if (cache_dir)
                   1314:     TtaFreeMemory (cache_dir);
                   1315: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      1316: }
                   1317: 
                   1318: /*----------------------------------------------------------------------
1.97      cvs      1319:   ProxyInit
                   1320:   Reads any proxies settings which may be declared as environmental
                   1321:   variables or in the thot.ini file. The former overrides the latter.
                   1322:   ----------------------------------------------------------------------*/
1.118     cvs      1323: #ifdef __STDC__
1.97      cvs      1324: static void ProxyInit (void)
1.118     cvs      1325: #else
                   1326: static void ProxyInit ()
                   1327: #endif /* __STDC__ */
1.97      cvs      1328: {
                   1329:   char *strptr;
                   1330:   char *str = NULL;
                   1331:   char *name;
                   1332: 
                   1333:   /* get the proxy settings from the thot.ini file */
                   1334:   strptr = (char *) TtaGetEnvString ("HTTP_PROXY");
                   1335:   if (strptr && *strptr)
                   1336:     HTProxy_add ("http", strptr);
                   1337:   /* get the no_proxy settings from the thot.ini file */
                   1338:   strptr = (char *) TtaGetEnvString ("NO_PROXY");
                   1339:   if (strptr && *strptr) 
                   1340:     {
                   1341:       str = TtaStrdup (strptr);          /* Get copy we can mutilate */
                   1342:       strptr = str;
                   1343:       while ((name = HTNextField (&strptr)) != NULL) {
                   1344:        char *portstr = strchr (name, ':');
                   1345:        unsigned port=0;
                   1346:        if (portstr) {
                   1347:          *portstr++ = '\0';
1.116     cvs      1348:          if (*portstr) port = (unsigned) atoi (portstr);
1.97      cvs      1349:        }
                   1350:        /* Register it for all access methods */
                   1351:        HTNoProxy_add (name, NULL, port);
                   1352:       }
                   1353:       TtaFreeMemory (str);
                   1354:     }
                   1355:   
                   1356:   /* use libw3's routine to get all proxy settings from the environment */
                   1357:    HTProxy_getEnvVar ();
                   1358: }
                   1359: 
                   1360: 
                   1361: /*----------------------------------------------------------------------
1.17      cvs      1362:   AHTProfile_newAmaya
                   1363:   creates the Amaya client profile for libwww.
1.15      cvs      1364:   ----------------------------------------------------------------------*/
                   1365: #ifdef __STDC__
                   1366: static void         AHTProfile_newAmaya (char *AppName, char *AppVersion)
                   1367: #else  /* __STDC__ */
                   1368: static void         AHTProfile_newAmaya (AppName, AppVersion)
                   1369: char               *AppName;
                   1370: char               *AppVersion;
                   1371: #endif /* __STDC__ */
1.4       cvs      1372: {
                   1373:    /* If the Library is not already initialized then do it */
                   1374:    if (!HTLib_isInitialized ())
                   1375:       HTLibInit (AppName, AppVersion);
                   1376: 
                   1377:    if (!converters)
                   1378:       converters = HTList_new ();
1.27      cvs      1379:    if (!acceptTypes)
                   1380:       acceptTypes = HTList_new ();
1.4       cvs      1381:    if (!encodings)
                   1382:       encodings = HTList_new ();
                   1383: 
                   1384:    /* Register the default set of transport protocols */
                   1385:    HTTransportInit ();
                   1386: 
                   1387:    /* Register the default set of application protocol modules */
                   1388:    AHTProtocolInit ();
                   1389: 
1.116     cvs      1390:    /* Register the default set of messages and dialog functions */
                   1391:    AHTAlertInit ();
                   1392:    HTAlert_setInteractive (YES);
                   1393: 
                   1394: #ifdef AMAYA_WWW_CACHE
                   1395:    /* Enable the persistent cache  */
                   1396:    CacheInit ();
                   1397: #else
                   1398:    HTCacheMode_setEnabled (NO);
                   1399: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      1400: 
                   1401:    /* Register the default set of BEFORE and AFTER filters */
                   1402:    AHTNetInit ();
                   1403: 
                   1404:    /* Set up the default set of Authentication schemes */
                   1405:    HTAAInit ();
                   1406: 
1.97      cvs      1407:    /* Get any proxy settings */
                   1408:    ProxyInit ();
1.4       cvs      1409: 
                   1410:    /* Register the default set of converters */
                   1411:    AHTConverterInit (converters);
1.27      cvs      1412:    AHTAcceptTypesInit (acceptTypes);
1.4       cvs      1413:    HTFormat_setConversion (converters);
                   1414: 
                   1415:    /* Register the default set of transfer encoders and decoders */
1.116     cvs      1416:    HTTransferEncoderInit (encodings);  /* chunks ??? */
1.4       cvs      1417:    HTFormat_setTransferCoding (encodings);
                   1418: 
                   1419:    /* Register the default set of MIME header parsers */
1.74      cvs      1420:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      1421: 
                   1422:    /* Register the default set of Icons for directory listings */
1.27      cvs      1423:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      1424: 
                   1425: }
                   1426: 
1.5       cvs      1427: /*----------------------------------------------------------------------
1.17      cvs      1428:   AHTProfile_delete
                   1429:   deletes the Amaya client profile.
1.5       cvs      1430:   ----------------------------------------------------------------------*/
1.4       cvs      1431: #ifdef __STDC__
                   1432: static void         AHTProfile_delete (void)
                   1433: #else
                   1434: static void         AHTProfile_delete ()
1.7       cvs      1435: #endif                         /* __STDC__ */
1.4       cvs      1436: {
1.22      cvs      1437:  
                   1438:   /* free the Amaya global context */
1.74      cvs      1439:   if (!converters)
                   1440:     HTConversion_deleteAll (converters);
                   1441:   if (!acceptTypes)
                   1442:     HTConversion_deleteAll (acceptTypes);
                   1443:   if (!encodings)
                   1444:     HTCoding_deleteAll (encodings);
                   1445:   
                   1446:   HTList_delete (Amaya->docid_status);
                   1447:   HTList_delete (Amaya->reqlist);
                   1448:   TtaFreeMemory (Amaya);
                   1449:   {
1.61      cvs      1450: 
1.74      cvs      1451:     if (HTLib_isInitialized ())
                   1452:       
1.120     cvs      1453: #ifdef _WINDOWS
1.61      cvs      1454:       HTEventTerminate ();
1.120     cvs      1455: #endif _WINDOWS;               
1.74      cvs      1456:     
                   1457:     /* Clean up the persistent cache (if any) */
1.116     cvs      1458: #ifdef AMAYA_WWW_CACHE
                   1459:     HTCacheTerminate ();
                   1460: #endif /* AMAYA_WWW_CACHE */
1.74      cvs      1461:     
                   1462:     /* Clean up all the global preferences */
                   1463:     HTFormat_deleteAll ();
                   1464:     
                   1465:     /* Terminate libwww */
                   1466:     HTLibTerminate ();
                   1467:   }
1.4       cvs      1468: }
                   1469: 
1.5       cvs      1470: /*----------------------------------------------------------------------
1.116     cvs      1471:   AmayacontextInit
                   1472:   initializes an internal Amaya context for our libwww interface 
                   1473:   ----------------------------------------------------------------------*/
                   1474: #ifdef __STDC__
                   1475: static void                AmayaContextInit ()
                   1476: #else
                   1477: static void                AmayaContextInit ()
                   1478: #endif
                   1479: 
                   1480: {
                   1481:   AmayaAlive = TRUE;
                   1482:   /* Initialization of the global context */
                   1483:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   1484:   Amaya->reqlist = HTList_new ();
                   1485:   Amaya->docid_status = HTList_new ();
                   1486:   Amaya->open_requests = 0;
                   1487: }
                   1488: 
                   1489: /*----------------------------------------------------------------------
1.17      cvs      1490:   QueryInit
                   1491:   initializes the libwww interface 
1.5       cvs      1492:   ----------------------------------------------------------------------*/
1.4       cvs      1493: #ifdef __STDC__
                   1494: void                QueryInit ()
                   1495: #else
                   1496: void                QueryInit ()
                   1497: #endif
                   1498: {
                   1499: 
1.116     cvs      1500:    AmayaContextInit ();
                   1501:    
1.4       cvs      1502:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.116     cvs      1503:    
1.69      cvs      1504:    /* New AHTBridge stuff */
1.4       cvs      1505: 
1.116     cvs      1506: #ifdef _WINDOWS
1.120     cvs      1507:    /*** AHTEventInit (); this was the call to my AHTEvent module HTEvtLst today***/
1.125     cvs      1508:    HTEventInit ();
1.116     cvs      1509: #endif /* _WINDOWS */
1.72      cvs      1510: 
1.123     cvs      1511: #ifndef _WINDOWS
1.116     cvs      1512:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
                   1513:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.120     cvs      1514:    HTTimer_registerSetTimerCallback ((void *) AMAYA_SetTimer);
                   1515:    HTTimer_registerDeleteTimerCallback ((void *) AMAYA_DeleteTimer);
1.116     cvs      1516: #endif /* !_WINDOWS */
                   1517: 
                   1518: #ifdef AMAYA_WWW_CACHE
                   1519:    /*** 
                   1520:        WWW_TraceFlag = CACHE_TRACE;
                   1521:    ***/
                   1522: #endif
1.72      cvs      1523: 
1.74      cvs      1524: #ifdef DEBUG_LIBWWW
1.116     cvs      1525:   /* forwards error messages to our own function */
                   1526:    WWW_TraceFlag = THD_TRACE;
                   1527:   HTTrace_setCallback(LineTrace);
1.74      cvs      1528: #endif
1.4       cvs      1529: 
                   1530:    /* Trace activation (for debugging) */
1.7       cvs      1531:    /*
1.116     cvs      1532:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
                   1533: 
1.4       cvs      1534:       WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
                   1535:       SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
                   1536:       SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
                   1537:       SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1538:       SHOW_CORE_TRACE;
                   1539: 
1.7       cvs      1540:     */
1.4       cvs      1541: 
                   1542:    /***
                   1543:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1544:      SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
                   1545:      ***/
                   1546: 
                   1547:    /* Setting up other user interfaces */
                   1548: 
                   1549:    /* Setting up different network parameters */
1.17      cvs      1550:    /* Maximum number of simultaneous open sockets */
1.4       cvs      1551:    HTNet_setMaxSocket (8);
1.75      cvs      1552:    /* different network services timeouts */
1.109     cvs      1553:    HTDNS_setTimeout (60);
1.120     cvs      1554: #ifdef __WINDOWS
1.75      cvs      1555:    /* under windows, the libwww persistent socket handling has
                   1556:    ** some bugs. The following line inhibits idle socket reusal.
                   1557:    ** this is a bit slower, but avoids crashes and gives us time
                   1558:    ** to distribute Amaya before having to patch up libwww.
                   1559:    */
1.76      cvs      1560:    HTHost_setPersistTimeout (-1L);
1.75      cvs      1561: #else
1.76      cvs      1562:    HTHost_setPersistTimeout (60L);
1.75      cvs      1563: #endif /* _WINDOWS */
1.116     cvs      1564:    HTHost_setEventTimeout (1000);
1.75      cvs      1565: 
1.4       cvs      1566: #ifdef CATCH_SIG
1.18      cvs      1567:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      1568: #endif
1.15      cvs      1569: }
                   1570: 
1.69      cvs      1571: #ifndef _WINDOWS
1.15      cvs      1572: /*----------------------------------------------------------------------
1.17      cvs      1573:   LoopForStop
                   1574:   a copy of the Thop event loop so we can handle the stop button.
1.69      cvs      1575:   Not useful for windows code (Ramzi).
1.15      cvs      1576:   ----------------------------------------------------------------------*/
                   1577: #ifdef __STDC__
                   1578: static int          LoopForStop (AHTReqContext * me)
                   1579: #else
                   1580: static int          LoopForStop (AHTReqContext * me)
                   1581: #endif
                   1582: {
                   1583: 
1.25      cvs      1584:    extern ThotAppContext app_cont;
1.51      cvs      1585:    XEvent                ev;
                   1586:    XtInputMask           status;
1.17      cvs      1587:    int                 status_req = HT_OK;
1.15      cvs      1588: 
                   1589:    /* to test the async calls  */
1.17      cvs      1590:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      1591:    while (me->reqStatus != HT_ABORT &&
                   1592:          me->reqStatus != HT_END &&
1.69      cvs      1593:          me->reqStatus != HT_ERR) {
1.116     cvs      1594:         if (!AmayaIsAlive ())
1.69      cvs      1595:            /* Amaya was killed by one of the callback handlers */
                   1596:            exit (0);
                   1597: 
                   1598:         status = XtAppPending (app_cont);
                   1599:         if (status & XtIMXEvent) {
                   1600:             XtAppNextEvent (app_cont, &ev);
                   1601:            TtaHandleOneEvent (&ev);
                   1602:         } else if (status & (XtIMAll & (~XtIMXEvent))) {
                   1603:                 XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
                   1604:         } else {
                   1605:                XtAppNextEvent (app_cont, &ev);
                   1606:               TtaHandleOneEvent (&ev);
                   1607:         }
                   1608:    }
1.4       cvs      1609: 
1.69      cvs      1610:    switch (me->reqStatus) {
                   1611:          case HT_ERR:
                   1612:           case HT_ABORT:
1.15      cvs      1613:               status_req = HT_ERROR;
                   1614:               break;
                   1615: 
1.69      cvs      1616:          case HT_END:
1.15      cvs      1617:               status_req = HT_OK;
                   1618:               break;
                   1619: 
1.69      cvs      1620:          default:
1.15      cvs      1621:               break;
1.69      cvs      1622:    }
1.15      cvs      1623:    return (status_req);
1.4       cvs      1624: }
1.69      cvs      1625: #endif /* _WINDOWS */
1.4       cvs      1626: 
1.5       cvs      1627: /*----------------------------------------------------------------------
1.15      cvs      1628:   QueryClose
1.21      cvs      1629:   closes all existing threads, frees all non-automatically deallocated
                   1630:   memory and then ends libwww.
1.5       cvs      1631:   ----------------------------------------------------------------------*/
1.116     cvs      1632: void QueryClose ()
1.4       cvs      1633: {
1.24      cvs      1634: 
1.116     cvs      1635:   AmayaAlive = FALSE;
1.24      cvs      1636: 
1.116     cvs      1637:   /* remove all the handlers and callbacks that may output a message to
                   1638:      a non-existent Amaya window */
1.21      cvs      1639: 
1.116     cvs      1640:   HTNet_deleteAfter (AHTLoadTerminate_handler);
                   1641:   HTNet_deleteAfter (redirection_handler);
                   1642:   HTAlertCall_deleteAll (HTAlert_global () );
                   1643:   HTAlert_setGlobal ((HTList *) NULL);
                   1644:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
                   1645:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
                   1646: #ifndef _WINDOWS
                   1647:   /** need to erase all existing timers too **/
                   1648:    HTTimer_registerSetTimerCallback (NULL);
                   1649:    HTTimer_registerDeleteTimerCallback (NULL);
                   1650: #endif /* !_WINDOWS */
                   1651:   HTHost_setActivateRequestCallback (NULL);
                   1652:   Thread_deleteAll ();
1.21      cvs      1653:  
1.116     cvs      1654:   HTProxy_deleteAll ();
                   1655:   HTNoProxy_deleteAll ();
                   1656:   HTGateway_deleteAll ();
                   1657:   AHTProfile_delete ();
                   1658: }
                   1659: 
                   1660: /*----------------------------------------------------------------------
                   1661:   NextNameValue
                   1662:   ---------------------------------------------------------------------*/
                   1663: #ifdef __STDC__
                   1664: static char * NextNameValue (char ** pstr, char **name, char **value)
                   1665: #else
                   1666: static char * NextNameValue (pstr, name, value);
                   1667: char ** pstr;
                   1668: char **name;
                   1669: char **value;
                   1670: #endif /* __STDC__ */
                   1671: {
                   1672:   char * p = *pstr;
                   1673:   char * start = NULL;
                   1674:   if (!pstr || !*pstr) return NULL;
                   1675:   
                   1676:     if (!*p) {
                   1677:       *pstr = p;
                   1678:       *name = NULL;
                   1679:       *value = NULL;
                   1680:       return NULL;                                      /* No field */
                   1681:     }
                   1682:     
                   1683:     /* Now search for the next '&' and ';' delimitators */
                   1684:     start = p;
                   1685:     while (*p && *p != '&' && *p != ';') p++;
                   1686:     if (*p) 
                   1687:       *p++ = '\0';
                   1688:     *pstr = p;
                   1689: 
                   1690:     /* Search for the name and value */
                   1691:     *name = start;
                   1692:     p = start;
                   1693:     
                   1694:     while(*p && *p != '=') 
                   1695:       p++;
                   1696:     if (*p) 
                   1697:       *p++ = '\0';
                   1698:     *value = p;
                   1699: 
                   1700:     return start;
                   1701: }
                   1702: 
                   1703: /*----------------------------------------------------------------------
                   1704:   PrepareFormdata
                   1705:   ---------------------------------------------------------------------*/
                   1706: #ifdef __STDC__
                   1707: static HTAssocList * PrepareFormdata (char *string)
                   1708: #else
                   1709: static HTAssocList * PrepareFormdata (string)
                   1710: char *string;
                   1711: #endif /* __STDC__ */
                   1712: {
                   1713:   char * tmp_string, * tmp_string_ptr;
                   1714:   char * name;
                   1715:   char * value;
                   1716:   HTAssocList * formdata;
                   1717: 
                   1718:   if (!string)
                   1719:     return NULL;
                   1720: 
                   1721:   /* store the ptr in another variable, as the original address will
                   1722:      change
                   1723:      */
                   1724: 
                   1725:   tmp_string_ptr = tmp_string = TtaStrdup (string);
                   1726:   formdata = HTAssocList_new();
                   1727:   
                   1728:   while (*tmp_string)
                   1729:     {
                   1730:       NextNameValue (&tmp_string, &name, &value);
                   1731:       HTAssocList_addObject(formdata,
                   1732:                            name, value);
                   1733:     }
                   1734: 
                   1735:   TtaFreeMemory (tmp_string_ptr);
                   1736:   return formdata;
1.4       cvs      1737: }
                   1738: 
1.99      cvs      1739: 
                   1740: /*----------------------------------------------------------------------
                   1741:   InvokeGetObjectWWW_callback
                   1742:   A simple function to invoke a callback function whenever there's an error
                   1743:   in GetObjectWWW
                   1744:   ---------------------------------------------------------------------*/
                   1745: 
1.116     cvs      1746: #ifdef __STDC__
                   1747: void      InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      1748: #else
1.111     cvs      1749: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      1750: int docid;
                   1751: char *urlName;
                   1752: char *outputfile;
                   1753: TTcbf *terminate_cbf;
                   1754: void *context_tcbf;
1.116     cvs      1755: #endif /* __STDC__ */
1.99      cvs      1756: {
                   1757:   if (!terminate_cbf)
                   1758:     return;
                   1759:   
1.116     cvs      1760:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      1761:                    NULL, context_tcbf);  
                   1762: }
                   1763: 
                   1764: 
                   1765: 
1.5       cvs      1766: /*----------------------------------------------------------------------
1.15      cvs      1767:    GetObjectWWW
1.17      cvs      1768:    this function requests a resource designated by a URLname into a
                   1769:    temporary filename. The download can come from a simple GET operation,
                   1770:    or can come from POSTING/GETTING a form. In the latter
                   1771:    case, the function receives a query string to send to the server.
                   1772: 
1.5       cvs      1773:    4  file retrieval modes are proposed:                              
                   1774:    AMAYA_SYNC : blocking mode                            
                   1775:    AMAYA_ISYNC : incremental, blocking mode              
                   1776:    AMAYA_ASYNC : non-blocking mode                       
                   1777:    AMAYA_IASYNC : incremental, non-blocking mode         
                   1778:    
                   1779:    In the incremental mode, each time a package arrives, it will be   
                   1780:    stored in the temporary file. In addition, if an                   
                   1781:    incremental_callback function is defined, this function will be    
                   1782:    called and handled a copy of the newly received data package.      
                   1783:    Finally, if a terminate_callback function is defined, it will be   
                   1784:    invoked when the request terminates. The caller of this function
1.4       cvs      1785:    can define two different contexts to be passed to the callback
                   1786:    functions.
                   1787: 
                   1788:    When the function is called with the SYNC mode, the function will
                   1789:    return only when the requested file has been loaded.
                   1790:    The ASYNC mode will immediately return after setting up the
                   1791:    call.
                   1792: 
                   1793:    Notes:
                   1794:    At the end of a succesful request, the urlName string contains the
                   1795:    name of the actually retrieved URL. As a URL can change over the time,
                   1796:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      1797:    caller verify the value of the urlName variable at the end of
1.4       cvs      1798:    a request.
                   1799: 
                   1800:    Inputs:
                   1801:    - docid  Document identifier for the set of objects being
                   1802:    retrieved.
                   1803:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   1804:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   1805:    - mode The retrieval mode.
                   1806:    - incremental_cbf 
                   1807:    - context_icbf
                   1808:    Callback and context for the incremental modes
                   1809:    - terminate_cbf 
                   1810:    - context_icbf
                   1811:    Callback and context for a terminate handler
1.17      cvs      1812:    -error_html if TRUE, then display any server error message as an
                   1813:    HTML document.
1.88      cvs      1814:    - content_type a string
                   1815:  
1.4       cvs      1816:    Outputs:
                   1817:    - urlName The URL that was retrieved
                   1818:    - outputfile The name of the temporary file which holds the
                   1819:    retrieved data. (Only in case of success)
1.88      cvs      1820:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   1821:      sent in the HTTP answer
1.4       cvs      1822:    Returns:
                   1823:    HT_ERROR
                   1824:    HT_OK
1.5       cvs      1825:  
                   1826:   ----------------------------------------------------------------------*/
1.4       cvs      1827: #ifdef __STDC__
1.127     cvs      1828: int GetObjectWWW (int docid, char* urlName, char* formdata,
1.116     cvs      1829:                  char* outputfile, int mode, TIcbf* incremental_cbf, 
                   1830:                  void* context_icbf, TTcbf* terminate_cbf, 
1.88      cvs      1831:                  void* context_tcbf, boolean error_html, char *content_type)
1.4       cvs      1832: #else
1.127     cvs      1833: int GetObjectWWW (docid, urlName, formdata, outputfile, mode, 
1.116     cvs      1834:                  incremental_cbf, context_icbf, 
1.88      cvs      1835:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      1836: int           docid;
                   1837: char         *urlName;
1.127     cvs      1838: char         *formdata;
1.73      cvs      1839: char         *outputfile;
                   1840: int           mode;
                   1841: TIcbf        *incremental_cbf;
                   1842: void         *context_icbf;
                   1843: TTcbf        *terminate_cbf;
                   1844: void         *context_tcbf;
                   1845: boolean       error_html;
1.88      cvs      1846: char        *content_type;
1.4       cvs      1847: #endif
                   1848: {
                   1849:    AHTReqContext      *me;
                   1850:    char               *ref;
1.107     cvs      1851:    int                 status, l;
1.114     cvs      1852:    int                 tempsubdir;
1.7       cvs      1853: 
1.116     cvs      1854:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
                   1855:      {
                   1856:        /* no file to be loaded */
                   1857:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      1858:        
1.116     cvs      1859:        if (error_html)
1.69      cvs      1860:         /* so we can show the error message */
                   1861:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      1862:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   1863:                                    context_tcbf, HT_ERROR);
                   1864:        return HT_ERROR;
                   1865:      }
1.7       cvs      1866: 
1.4       cvs      1867:    /* do we support this protocol? */
1.116     cvs      1868:    if (IsValidProtocol (urlName) == NO) 
                   1869:      {
                   1870:        /* return error */
                   1871:        outputfile[0] = EOS;    /* file could not be opened */
                   1872:        TtaSetStatus (docid, 1, 
                   1873:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
                   1874:                     urlName);
                   1875: 
                   1876:        if (error_html)
1.69      cvs      1877:         /* so we can show the error message */
                   1878:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116     cvs      1879:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   1880:                                    context_tcbf, HT_ERROR);
                   1881:        return HT_ERROR;
                   1882:      }
1.4       cvs      1883: 
1.114     cvs      1884:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   1885:       named after their own docid */
                   1886:    
                   1887:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   1888: 
1.116     cvs      1889:    /* create a tempfilename */
                   1890:    sprintf (outputfile, "%s%c%d%c%04dAM", 
                   1891:            TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
                   1892:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      1893:    object_counter++;
1.116     cvs      1894:    
1.4       cvs      1895:    /* normalize the URL */
1.45      cvs      1896:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      1897:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      1898:    if (ref == (char*) NULL || ref[0] == EOS) {
                   1899:       /*error */
                   1900:       outputfile[0] = EOS;
                   1901:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   1902:       
                   1903:       if (error_html)
1.116     cvs      1904:        /* so we can show the error message */
                   1905:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      1906:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116     cvs      1907:                                   context_tcbf, HT_ERROR);
1.69      cvs      1908:       return HT_ERROR;
                   1909:    }
1.116     cvs      1910: 
1.4       cvs      1911:    /* verify if that file name existed */
1.9       cvs      1912:    if (TtaFileExist (outputfile))
1.77      cvs      1913:      TtaFileUnlink (outputfile);
1.116     cvs      1914:    
1.4       cvs      1915:    /* Initialize the request structure */
                   1916:    me = AHTReqContext_new (docid);
1.116     cvs      1917:    if (me == NULL) 
                   1918:      {
                   1919:        outputfile[0] = EOS;
                   1920:        /* need an error message here */
                   1921:        TtaFreeMemory (ref);
                   1922:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
                   1923:                                   context_tcbf, HT_ERROR);
                   1924:        return HT_ERROR;
                   1925:      }
                   1926: 
1.4       cvs      1927:    /* Specific initializations for POST and GET */
1.116     cvs      1928:    if (mode & AMAYA_FORM_POST)
                   1929:      {
                   1930:        me->method = METHOD_POST;
                   1931:        HTRequest_setMethod (me->request, METHOD_POST);
                   1932:      }
                   1933:    else 
                   1934:      {
                   1935:        me->method = METHOD_GET;
                   1936:        if (!HasKnownFileSuffix (ref))
                   1937:         HTRequest_setConversion(me->request, acceptTypes, TRUE);
                   1938:      }
1.93      cvs      1939: 
1.116     cvs      1940:    /* Common initialization for all HTML methods */
1.4       cvs      1941:    me->mode = mode;
                   1942:    me->error_html = error_html;
                   1943:    me->incremental_cbf = incremental_cbf;
                   1944:    me->context_icbf = context_icbf;
                   1945:    me->terminate_cbf = terminate_cbf;
                   1946:    me->context_tcbf = context_tcbf;
1.64      cvs      1947: 
1.69      cvs      1948:    /* for the async. request modes, we need to have our
1.4       cvs      1949:       own copy of outputfile and urlname
1.116     cvs      1950:       */
1.4       cvs      1951: 
1.116     cvs      1952:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
                   1953:      {
                   1954:        l = strlen (outputfile);
                   1955:        if (l > MAX_LENGTH)
                   1956:         me->outputfile = TtaGetMemory (l + 2);
                   1957:        else
                   1958:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
                   1959:        strcpy (me->outputfile, outputfile);
                   1960:        l = strlen (urlName);
                   1961:        if (l > MAX_LENGTH)
                   1962:         me->urlName = TtaGetMemory (l + 2);
                   1963:        else
                   1964:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
                   1965:        strcpy (me->urlName, urlName);
                   1966: #ifdef _WINDOWS
                   1967:      /* force windows ASYNC requests to always be non preemptive */
                   1968:      HTRequest_setPreemptive (me->request, NO);
                   1969: #endif /*_WINDOWS */
                   1970:      } /* AMAYA_ASYNC mode */ 
                   1971:    else 
                   1972: #ifdef _WINDOWS
                   1973:      {
                   1974:        me->outputfile = outputfile;
                   1975:        me->urlName = urlName;
                   1976:        /* force windows SYNC requests to always be non preemptive */
1.123     cvs      1977:        /* @@@ verify this */
1.116     cvs      1978:        HTRequest_setPreemptive (me->request, YES);
                   1979:      }
                   1980: #else /* !_WINDOWS */
                   1981:      {
                   1982:        me->outputfile = outputfile;
                   1983:        me->urlName = urlName;
                   1984:      }
                   1985:    /***
1.57      cvs      1986:      Change for taking into account the stop button:
                   1987:      The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
                   1988:      we will loop until the document has been received or a stop signal
                   1989:      generated
1.77      cvs      1990:      ****/
1.116     cvs      1991:    HTRequest_setPreemptive (me->request, NO);
                   1992: #endif /* _WINDOWS */
1.61      cvs      1993: 
                   1994:    /* prepare the URLname that will be displayed in teh status bar */
                   1995:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      1996:    TtaSetStatus (me->docid, 1, 
                   1997:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   1998:                 me->status_urlName);
1.4       cvs      1999: 
                   2000:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      2001:    TtaFreeMemory (ref);
1.4       cvs      2002: 
1.114     cvs      2003:    if (mode & AMAYA_NOCACHE) 
1.116     cvs      2004:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2005: 
                   2006:    /* prepare the query string and format for POST */
                   2007:    if (mode & AMAYA_FORM_POST)
1.114     cvs      2008:      {
1.116     cvs      2009:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
                   2010:                           HTAtom_for ("application/x-www-form-urlencoded"));
                   2011:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   2012:        HTRequest_setEntityAnchor (me->request, me->anchor);
                   2013:      } 
1.114     cvs      2014: 
1.127     cvs      2015:    /* create the formdata element for libwww */
                   2016:    if (formdata)
                   2017:      me->formdata = PrepareFormdata (formdata);
                   2018: 
1.116     cvs      2019:    /* do the request */
                   2020:    if (mode & AMAYA_FORM_POST)
1.119     cvs      2021:      {
                   2022:        /* this call doesn't give back a boolean */
                   2023:        HTParentAnchor * posted = NULL;
                   2024: 
                   2025:        posted = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
                   2026:                                    me->request);
                   2027:        status = posted ? YES : NO; 
                   2028:      }
1.127     cvs      2029:    else if (formdata)
                   2030:      status = HTGetFormAnchor(me->formdata, (HTAnchor *) me->anchor,
                   2031:                              me->request);
1.116     cvs      2032:    else
1.77      cvs      2033:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      2034: 
1.123     cvs      2035: #ifndef _WINDOWS
                   2036:    /* @@@ may need some special windows error msg here */
1.116     cvs      2037:    /* control the errors */
                   2038:    if (status != HT_OK
                   2039:        && HTError_hasSeverity (HTRequest_error (me->request), ERR_NON_FATAL))
                   2040:      status = HT_ERROR;
1.123     cvs      2041: #endif /* !_WINDOWS */      
                   2042:      /* @@@test the effect of HTRequest_kill () */
1.85      cvs      2043:      
1.116     cvs      2044: #if 0
                   2045:      /** *this should go to term_d @@@@ */
                   2046:      if (me->reqStatus == HT_CACHE)
1.93      cvs      2047:        {
1.116     cvs      2048:         AHTPrintPendingRequestStatus (me->docid, YES);
                   2049:         /* free the memory allocated for async requests */
                   2050:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2051:                                      terminate_cbf, context_tcbf, HT_OK);
                   2052:         AHTReqContext_delete (me);
                   2053:         return HT_OK;
1.93      cvs      2054:        }
1.116     cvs      2055: #endif 
1.4       cvs      2056: 
1.116     cvs      2057:    if (status == HT_ERROR)
                   2058:      /* the request invocation failed */
                   2059:      {
                   2060:        /* show an error message on the status bar */
                   2061:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   2062:        TtaSetStatus (docid, 1, 
                   2063:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   2064:                     urlName);
                   2065:        if (me->reqStatus == HT_NEW)
                   2066:         /* manually invoke the last processing that usually gets done
                   2067:            in a succesful request */
                   2068:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
                   2069:                                      terminate_cbf, context_tcbf, HT_ERROR);
                   2070:        /* terminate_handler wasn't called */
                   2071:        if (mode & AMAYA_SYNC || mode & AMAYA_ISYNC)
                   2072:         AHTReqContext_delete (me);
                   2073:      }
                   2074:    else
                   2075:    /* end treatment for SYNC requests */
1.120     cvs      2076:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2077:        {
1.116     cvs      2078: #ifndef _WINDOWS
1.120     cvs      2079:         /* part of the UNIX stop button handler */
                   2080:         if (status != HT_ERROR)
                   2081:           status = LoopForStop (me);
1.116     cvs      2082: #endif /* _!WINDOWS */
1.120     cvs      2083:         /* @@@ this doesn't seem correct ... me->request may not exist ... */
                   2084:         if (!HTRequest_kill (me->request))
                   2085:           AHTReqContext_delete (me);
                   2086:        }
                   2087:    return (status);
1.4       cvs      2088: }
                   2089: 
1.5       cvs      2090: /*----------------------------------------------------------------------
1.17      cvs      2091:    PutObjectWWW
                   2092:    frontend for uploading a resource to a URL. This function downloads
                   2093:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   2094:    finish the job.
                   2095: 
1.5       cvs      2096:    2 upload modes are proposed:                                       
                   2097:    AMAYA_SYNC : blocking mode                            
                   2098:    AMAYA_ASYNC : non-blocking mode                       
                   2099:    
1.4       cvs      2100:    When the function is called with the SYNC mode, the function will
                   2101:    return only when the file has been uploaded.
                   2102:    The ASYNC mode will immediately return after setting up the
                   2103:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   2104:    call back terminate_cbf, handling it the context defined in
                   2105:    context_tcbf.
                   2106: 
                   2107:    Notes:
                   2108:    At the end of a succesful request, the urlName string contains the
                   2109:    name of the actually uploaded URL. As a URL can change over the time,
                   2110:    (e.g., be redirected elsewhere), it is advised that the function
                   2111:    caller verifies the value of the urlName variable at the end of
                   2112:    a request.
                   2113: 
                   2114:    Inputs:
                   2115:    - docid  Document identifier for the set of objects being
                   2116:    retrieved.
                   2117:    - fileName A pointer to the local file to upload
                   2118:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   2119:    - mode The retrieval mode.
                   2120:    - terminate_cbf 
                   2121:    - context_icbf
                   2122:    Callback and context for a terminate handler
                   2123: 
                   2124:    Outputs:
                   2125:    - urlName The URL that was uploaded
                   2126: 
                   2127:    Returns:
                   2128:    HT_ERROR
                   2129:    HT_OK
1.5       cvs      2130:   ----------------------------------------------------------------------*/
1.4       cvs      2131: #ifdef __STDC__
1.27      cvs      2132: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      2133:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   2134: #else
1.26      cvs      2135: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      2136:                                  ,terminate_cbf, context_tcbf)
                   2137: int                 docid;
                   2138: char               *urlName;
                   2139: char               *fileName;
                   2140: int                 mode;
1.27      cvs      2141: PicType             contentType;
1.4       cvs      2142: TTcbf              *terminate_cbf;
                   2143: void               *context_tcbf;
                   2144: 
1.116     cvs      2145: #endif /* __STDC__ */
1.4       cvs      2146: {
1.116     cvs      2147:    AHTReqContext      *me;
1.4       cvs      2148:    int                 status;
                   2149:    int                 fd;
                   2150:    struct stat         file_stat;
1.116     cvs      2151:    char               *fileURL;
1.4       cvs      2152: 
1.33      cvs      2153:    AmayaLastHTTPErrorMsg [0] = EOS;
                   2154:    
1.116     cvs      2155:    if (urlName == NULL || docid == 0 || fileName == NULL 
                   2156:        || !TtaFileExist (fileName))
1.4       cvs      2157:       /* no file to be uploaded */
                   2158:       return HT_ERROR;
                   2159: 
                   2160:    /* do we support this protocol? */
1.7       cvs      2161:    if (IsValidProtocol (urlName) == NO)
                   2162:      {
                   2163:        /* return error */
1.77      cvs      2164:        TtaSetStatus (docid, 1, 
                   2165:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      2166:                      urlName);
                   2167:        return HT_ERROR;
                   2168:      }
1.116     cvs      2169: 
                   2170:    /* verify the file's size */
                   2171: #ifndef _WINDOWS
1.7       cvs      2172:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.116     cvs      2173: #else 
1.74      cvs      2174:    if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.116     cvs      2175: #endif /* _WINDOWS */
1.7       cvs      2176:      {
                   2177:        /* if we could not open the file, exit */
                   2178:        /*error msg here */
                   2179:        return (HT_ERROR);
                   2180:      }
1.4       cvs      2181: 
                   2182:    fstat (fd, &file_stat);
1.116     cvs      2183:    close (fd);
1.4       cvs      2184: 
1.7       cvs      2185:    if (file_stat.st_size == 0)
                   2186:      {
                   2187:        /* file was empty */
                   2188:        /*errmsg here */
                   2189:        return (HT_ERROR);
                   2190:      }
1.116     cvs      2191: 
                   2192:    /* prepare the request context */
1.4       cvs      2193: 
                   2194:    if (THD_TRACE)
1.116     cvs      2195:       fprintf (stderr, "file size == %u\n", (unsigned) file_stat.st_size);
1.4       cvs      2196: 
                   2197:    me = AHTReqContext_new (docid);
                   2198: 
1.7       cvs      2199:    if (me == NULL)
                   2200:      {
                   2201:        /* need an error message here */
                   2202:        TtaHandlePendingEvents ();
                   2203:        return (HT_ERROR);
                   2204:      }
1.116     cvs      2205: 
1.4       cvs      2206:    me->mode = mode;
                   2207:    me->incremental_cbf = (TIcbf *) NULL;
                   2208:    me->context_icbf = (void *) NULL;
                   2209:    me->terminate_cbf = terminate_cbf;
                   2210:    me->context_tcbf = context_tcbf;
                   2211:    me->urlName = urlName;
1.116     cvs      2212:    me->block_size =  file_stat.st_size;
1.17      cvs      2213:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      2214:    me->method = METHOD_PUT;
                   2215:    HTRequest_setMethod (me->request, METHOD_PUT);
                   2216:    me->output = stdout;
1.17      cvs      2217:    /* we are not expecting to receive any input from the server */
                   2218:    me->outputfile = (char *) NULL; 
1.4       cvs      2219: 
1.116     cvs      2220:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
                   2221:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (fileURL);
                   2222:    HT_FREE (fileURL);
1.114     cvs      2223: 
1.28      cvs      2224:    /* Set the Content-Type of the file we are uploading */
1.77      cvs      2225:    HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
                   2226:                       AHTGuessAtom_for (me->urlName, contentType));
1.116     cvs      2227:    HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.17      cvs      2228: 
1.4       cvs      2229:    HTRequest_setEntityAnchor (me->request, me->anchor);
1.116     cvs      2230: #ifdef _WINDOWS
                   2231:    HTRequest_setPreemptive (me->request, YES);
                   2232: #else
                   2233:    HTRequest_setPreemptive (me->request, NO);
                   2234: #endif /* _WINDOWS */
                   2235: 
                   2236:    if (mode & AMAYA_NOCACHE)
                   2237:      HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
                   2238: 
1.74      cvs      2239:    /* prepare the URLname that will be displayed in teh status bar */
                   2240:    ChopURL (me->status_urlName, me->urlName);
                   2241:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
                   2242:                     me->status_urlName);
1.114     cvs      2243: 
1.116     cvs      2244:    status = HTPutDocumentAbsolute (me->anchor, urlName, me->request);
1.4       cvs      2245: 
1.116     cvs      2246:    if (status != HT_ERROR && me->reqStatus != HT_ERR)
1.7       cvs      2247:      {
                   2248:        /* part of the stop button handler */
                   2249:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2250:          {
1.120     cvs      2251: #ifndef _WINDOWS
1.7       cvs      2252:             status = LoopForStop (me);
1.120     cvs      2253: #endif /* _WINDOWS */
                   2254:             if (!HTRequest_kill (me->request))
                   2255:               AHTReqContext_delete (me);
1.7       cvs      2256:          }
1.15      cvs      2257:      }
1.116     cvs      2258:    else
                   2259:      {
                   2260:       if (! HTRequest_kill (me->request))
                   2261:         AHTReqContext_delete (me);
                   2262:      }
                   2263:  
                   2264:    TtaHandlePendingEvents ();
1.90      cvs      2265: 
1.116     cvs      2266:    return status;
1.28      cvs      2267: }
1.4       cvs      2268: 
1.5       cvs      2269: /*----------------------------------------------------------------------
1.17      cvs      2270:   Stop Request
                   2271:   stops (kills) all active requests associated with a docid 
1.5       cvs      2272:   ----------------------------------------------------------------------*/
1.4       cvs      2273: #ifdef __STDC__
                   2274: void                StopRequest (int docid)
                   2275: #else
                   2276: void                StopRequest (docid)
                   2277: int                 docid;
                   2278: #endif
                   2279: {
                   2280:    HTList             *cur;
                   2281:    AHTDocId_Status    *docid_status;
                   2282:    AHTReqContext      *me;
1.108     cvs      2283: #ifdef DEBUG_LIBWWW
                   2284:    int                 open_requests;
                   2285: #endif /* DEBUG_LIBWWW */
1.116     cvs      2286: 
                   2287:  
                   2288:    if (Amaya && libDoStop)
1.7       cvs      2289:      {
1.100     cvs      2290: #ifdef DEBUG_LIBWWW
1.116     cvs      2291:        fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", 
                   2292:                Amaya->open_requests);
1.100     cvs      2293: #endif /* DEBUG_LIBWWW */
                   2294: 
1.116     cvs      2295:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
                   2296:                                                          Amaya->docid_status);
                   2297:        /* verify if there are any requests at all associated with docid */
                   2298:        
                   2299:        if (docid_status == (AHTDocId_Status *) NULL)
                   2300:         return;
1.108     cvs      2301: #ifdef DEBUG_LIBWWW
1.116     cvs      2302:        open_requests = docid_status->counter;
1.108     cvs      2303: #endif /* DEBUG_LIBWWW */
1.4       cvs      2304: 
1.116     cvs      2305:        /* First, kill all pending requests */
                   2306:        /* We first inhibit the activation of pending requests */
                   2307:        HTHost_disable_PendingReqLaunch ();
                   2308:        cur = Amaya->reqlist;
                   2309:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   2310:         {
                   2311:           if (me->docid == docid && me->reqStatus == HT_NEW)
                   2312:             {
                   2313:               /* If we have an ASYNC request, we kill it.
                   2314:               ** If it's a SYNC request, we just mark it as aborted
                   2315:               */
                   2316:               me->reqStatus = HT_ABORT;
                   2317:               if (((me->mode & AMAYA_IASYNC)
                   2318:                    || (me->mode & AMAYA_ASYNC))
                   2319:                   && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
                   2320:                 {
                   2321:                   /* delete the amaya/libwww request context */
                   2322:                   if (!HTRequest_kill (me->request))
                   2323:                     /* if the above function returns NO (0), it means
                   2324:                     ** that there was no network context and that 
                   2325:                     ** terminate_handler wasn't called. So, we erase
                   2326:                     ** this context ourselves
                   2327:                     */
1.96      cvs      2328:                     AHTReqContext_delete (me);
1.116     cvs      2329:                   
                   2330:                   cur = Amaya->reqlist;
1.108     cvs      2331: #ifdef DEBUG_LIBWWW
1.116     cvs      2332:                   /* update the number of open requests */
                   2333:                   open_requests--;                
1.108     cvs      2334: #endif /* DEBUG_LIBWWW */
1.116     cvs      2335:                 }
                   2336:             }
                   2337:         }
1.108     cvs      2338: 
1.85      cvs      2339:        /* enable the activation of pending requests */
1.116     cvs      2340:        HTHost_enable_PendingReqLaunch ();
1.85      cvs      2341: 
1.116     cvs      2342:        cur = Amaya->reqlist;
                   2343:        while ((me = (AHTReqContext *) HTList_nextObject (cur)))
                   2344:         {
                   2345:           if (me->docid == docid)
                   2346:             {
                   2347:               /* kill this request */
                   2348:               
                   2349:               switch (me->reqStatus)
                   2350:                 {
                   2351:                 case HT_ABORT:
1.108     cvs      2352: #ifdef DEBUG_LIBWWW
1.116     cvs      2353:                   fprintf (stderr, "Stop: url %s says abort", me->urlName);
1.108     cvs      2354: #endif /* DEBUG_LIBWWW */
1.116     cvs      2355:                   break;
1.100     cvs      2356:                  
1.116     cvs      2357:                 case HT_END:
                   2358: #ifdef DEBUG_LIBWWW
                   2359:                   fprintf (stderr, "Stop: url %s says end", me->urlName);
                   2360: #endif /* DEBUG_LIBWWW */
                   2361:                   break;
                   2362: 
                   2363:                 case HT_BUSY:
                   2364:                   me->reqStatus = HT_ABORT;
1.108     cvs      2365: #ifdef DEBUG_LIBWWW
1.116     cvs      2366:                   fprintf (stderr, "Stop: url %s going from busy to abort\n",
                   2367:                            me->urlName);
1.108     cvs      2368: #endif /* DEBUG_LIBWWW */
1.116     cvs      2369:                   break;
1.100     cvs      2370: 
1.116     cvs      2371:                 case HT_NEW_PENDING:
                   2372:                 case HT_WAITING:
                   2373:                 default:
1.108     cvs      2374: #ifdef DEBUG_LIBWWW
1.116     cvs      2375:                   fprintf (stderr, "Stop: url %s says NEW_PENDING, WAITING",
                   2376:                            me->urlName);
1.108     cvs      2377: #endif /* DEBUG_LIBWWW */
1.116     cvs      2378:                   me->reqStatus = HT_ABORT;
                   2379: 
                   2380:                   if (((me->mode & AMAYA_IASYNC)
                   2381:                        || (me->mode & AMAYA_ASYNC))
                   2382:                       && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
                   2383:                     {
                   2384:                       /* delete the amaya/libwww request context */
                   2385:                       if (!HTRequest_kill (me->request))
                   2386:                         /* if the above function returns NO (0), it means
                   2387:                         ** that there was no network context and that 
                   2388:                         ** terminate_handler wasn't called. So, we erase
                   2389:                         ** this context ourselves
                   2390:                         */
                   2391:                         AHTReqContext_delete (me);
1.96      cvs      2392: 
1.116     cvs      2393:                       cur = Amaya->reqlist;
1.108     cvs      2394: #ifdef DEBUG_LIBWWW
1.116     cvs      2395:                       /* update the number of open requests */
                   2396:                       open_requests--;            
1.108     cvs      2397: #endif /* DEBUG_LIBWWW */
1.116     cvs      2398:                     }
                   2399: 
1.108     cvs      2400: #ifdef DEBUG_LIBWWW
1.116     cvs      2401:                   if (HTHost_isIdle (reqHost))
                   2402:                     fprintf (stderr, "StopRequst: After killing, Host is "
                   2403:                              "idle\n");
                   2404:                   else
                   2405:                     fprintf (stderr, "StopRequest: After killing, Host isn't "
                   2406:                              "idle\n");
1.108     cvs      2407: #endif /* DEBUG_LIBWWW */
1.95      cvs      2408: 
1.108     cvs      2409: #ifdef DEBUG_LIBWWW                 
1.116     cvs      2410:               open_requests--;
1.108     cvs      2411: #endif /* DEBUG_LIBWWW */                   
1.93      cvs      2412:                     break;
                   2413:                     
1.116     cvs      2414:                 }      /* switch */
                   2415:             }          /* if me docid */
                   2416:         }                      /* while */
                   2417:        
1.100     cvs      2418: #ifdef DEBUG_LIBWWW
1.116     cvs      2419:        fprintf (stderr, "StopRequest: number of Amaya requests : "
                   2420:                "%d\n", Amaya->open_requests);
1.100     cvs      2421: #endif /* DEBUG_LIBWWW */
1.7       cvs      2422:      }                         /* if amaya open requests */
1.93      cvs      2423:    
1.85      cvs      2424: } /* StopRequest */
1.17      cvs      2425: 
                   2426: 
1.105     cvs      2427: /*----------------------------------------------------------------------
                   2428:   AmayaIsAlive
                   2429:   returns the status of the AmayaAlive flag
                   2430:   ----------------------------------------------------------------------*/
                   2431: #ifdef __STDC__
                   2432: boolean AmayaIsAlive (void)
                   2433: #else
                   2434: boolean AmayaIsAlive ()
                   2435: #endif /* _STDC_ */
                   2436: {
                   2437:   return AmayaAlive;
                   2438: }
1.116     cvs      2439: 
1.69      cvs      2440: #endif /* AMAYA_JAVA */
1.77      cvs      2441: 
1.116     cvs      2442: /*
                   2443:   end of Module query.c
                   2444: */
1.17      cvs      2445: 
                   2446: 

Webmaster