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

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

Webmaster