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

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
        !          1086:    HTAlert_add (AHTConfirm, HT_MSG_CACHE_LOCK);
        !          1087: #endif /* AMAYA_WWW_CACHE */
        !          1088: }
        !          1089: 
        !          1090: /*----------------------------------------------------------------------
        !          1091:   CacheInit
        !          1092:   Reads the cache settings from the thot.ini file.
        !          1093:   ----------------------------------------------------------------------*/
        !          1094: static void CacheInit (void)
        !          1095: {
        !          1096: #ifndef AMAYA_WWW_CACHE
        !          1097:    HTCacheMode_setEnabled (NO);
        !          1098: 
        !          1099: #else /* AMAYA_WWW_CACHE */
        !          1100:   char *strptr;
        !          1101:   int cache_size;
        !          1102:   char *cache_dir = NULL;
        !          1103:   boolean cache;
        !          1104: 
        !          1105:   /* activate cache? */
        !          1106:   strptr = (char *) TtaGetEnvString ("ENABLE_CACHE");
        !          1107:   if (strptr && *strptr && strcasecmp (strptr,"yes" ))
        !          1108:     cache = NO;
        !          1109:   else
        !          1110:     cache = YES;
        !          1111: 
        !          1112:   /* get the cache dir (or use a default one) */
        !          1113:   strptr = (char *) TtaGetEnvString ("CACHE_DIR");
        !          1114:   if (strptr && *strptr) 
        !          1115:     {
        !          1116:       cache_dir = TtaGetMemory (strlen (strptr) + strlen (CACHE_DIR_NAME) + 1);
        !          1117:       strcpy (cache_dir, strptr);
        !          1118:     }
        !          1119:   else
        !          1120:     {
        !          1121:       cache_dir = TtaGetMemory (strlen (DEFAULT_CACHE_DIR) 
        !          1122:                                + strlen (CACHE_DIR_NAME) + 1);
        !          1123:       strcpy (cache_dir, DEFAULT_CACHE_DIR);
        !          1124:     }
        !          1125:   strcat (cache_dir, CACHE_DIR_NAME);
        !          1126: 
        !          1127:   /* get the cache size (or use a default one) */
        !          1128:     strptr = (char *) TtaGetEnvString ("CACHE_SIZE");
        !          1129:   if (strptr && *strptr) 
        !          1130:       cache_size = atoi (strptr);
        !          1131:   else
        !          1132:     cache_size = DEFAULT_CACHE_SIZE;
        !          1133: 
        !          1134:   if (cache) 
        !          1135:     {
        !          1136:       /* how to remove the lock? force remove it? */
        !          1137:       /* @@@ugly hack to remove the .lock file ... */
        !          1138:       /* we would need something to remove the index, clear
        !          1139:         the directory, etc. attention to dir-sep  */
        !          1140:       strptr = TtaGetMemory (strlen (cache_dir) + 20);
        !          1141:       strcpy (strptr, cache_dir);
        !          1142: #ifndef _WINDOWS
        !          1143:       strcat (strptr, "/.lock");
        !          1144: #else
        !          1145:       strcat (strptr, "\.lock");
        !          1146: #endif /* !_WINDOWS */
        !          1147:       if (TtaFileExist (strptr))
        !          1148:        {
        !          1149:          /* remove the lock and clean the cache (the clean cache will remove
        !          1150:           all, making the following call unnecessary */
        !          1151:          TtaFileUnlink (strptr);
        !          1152:          /* CleanCache (cache_dir); */
        !          1153:        }
        !          1154:       TtaFreeMemory (strptr);
        !          1155: 
        !          1156:       /* store this in our libwww context */
        !          1157:       HTCacheInit (cache_dir, cache_size);
        !          1158:       HTCacheMode_setExpires(HT_EXPIRES_AUTO);
        !          1159:     }
        !          1160:   else
        !          1161:       HTCacheMode_setEnabled (NO);
        !          1162: 
        !          1163:   if (cache_dir)
        !          1164:     TtaFreeMemory (cache_dir);
        !          1165: #endif /* AMAYA_WWW_CACHE */
1.15      cvs      1166: }
                   1167: 
                   1168: /*----------------------------------------------------------------------
1.97      cvs      1169:   ProxyInit
                   1170:   Reads any proxies settings which may be declared as environmental
                   1171:   variables or in the thot.ini file. The former overrides the latter.
                   1172:   ----------------------------------------------------------------------*/
                   1173: static void ProxyInit (void)
                   1174: {
                   1175:   char *strptr;
                   1176:   char *str = NULL;
                   1177:   char *name;
                   1178: 
                   1179:   /* get the proxy settings from the thot.ini file */
                   1180:   strptr = (char *) TtaGetEnvString ("HTTP_PROXY");
                   1181:   if (strptr && *strptr)
                   1182:     HTProxy_add ("http", strptr);
                   1183:   /* get the no_proxy settings from the thot.ini file */
                   1184:   strptr = (char *) TtaGetEnvString ("NO_PROXY");
                   1185:   if (strptr && *strptr) 
                   1186:     {
                   1187:       str = TtaStrdup (strptr);          /* Get copy we can mutilate */
                   1188:       strptr = str;
                   1189:       while ((name = HTNextField (&strptr)) != NULL) {
                   1190:        char *portstr = strchr (name, ':');
                   1191:        unsigned port=0;
                   1192:        if (portstr) {
                   1193:          *portstr++ = '\0';
1.116   ! cvs      1194:          if (*portstr) port = (unsigned) atoi (portstr);
1.97      cvs      1195:        }
                   1196:        /* Register it for all access methods */
                   1197:        HTNoProxy_add (name, NULL, port);
                   1198:       }
                   1199:       TtaFreeMemory (str);
                   1200:     }
                   1201:   
                   1202:   /* use libw3's routine to get all proxy settings from the environment */
                   1203:    HTProxy_getEnvVar ();
                   1204: 
                   1205: }
                   1206: 
                   1207: 
                   1208: /*----------------------------------------------------------------------
1.17      cvs      1209:   AHTProfile_newAmaya
                   1210:   creates the Amaya client profile for libwww.
1.15      cvs      1211:   ----------------------------------------------------------------------*/
                   1212: #ifdef __STDC__
                   1213: static void         AHTProfile_newAmaya (char *AppName, char *AppVersion)
                   1214: #else  /* __STDC__ */
                   1215: static void         AHTProfile_newAmaya (AppName, AppVersion)
                   1216: char               *AppName;
                   1217: char               *AppVersion;
                   1218: #endif /* __STDC__ */
1.4       cvs      1219: {
                   1220:    /* If the Library is not already initialized then do it */
                   1221:    if (!HTLib_isInitialized ())
                   1222:       HTLibInit (AppName, AppVersion);
                   1223: 
                   1224:    if (!converters)
                   1225:       converters = HTList_new ();
1.27      cvs      1226:    if (!acceptTypes)
                   1227:       acceptTypes = HTList_new ();
1.4       cvs      1228:    if (!encodings)
                   1229:       encodings = HTList_new ();
                   1230: 
                   1231:    /* Register the default set of transport protocols */
                   1232:    HTTransportInit ();
                   1233: 
                   1234:    /* Register the default set of application protocol modules */
                   1235:    AHTProtocolInit ();
                   1236: 
1.116   ! cvs      1237:    /* Register the default set of messages and dialog functions */
        !          1238:    AHTAlertInit ();
        !          1239:    HTAlert_setInteractive (YES);
        !          1240: 
        !          1241: #ifdef AMAYA_WWW_CACHE
        !          1242:    /* Enable the persistent cache  */
        !          1243:    CacheInit ();
        !          1244: #else
        !          1245:    HTCacheMode_setEnabled (NO);
        !          1246: #endif /* AMAYA_WWW_CACHE */
1.4       cvs      1247: 
                   1248:    /* Register the default set of BEFORE and AFTER filters */
                   1249:    AHTNetInit ();
                   1250: 
                   1251:    /* Set up the default set of Authentication schemes */
                   1252:    HTAAInit ();
                   1253: 
1.97      cvs      1254:    /* Get any proxy settings */
                   1255:    ProxyInit ();
1.4       cvs      1256: 
                   1257:    /* Register the default set of converters */
                   1258:    AHTConverterInit (converters);
1.27      cvs      1259:    AHTAcceptTypesInit (acceptTypes);
1.4       cvs      1260:    HTFormat_setConversion (converters);
                   1261: 
                   1262:    /* Register the default set of transfer encoders and decoders */
1.116   ! cvs      1263:    HTTransferEncoderInit (encodings);  /* chunks ??? */
1.4       cvs      1264:    HTFormat_setTransferCoding (encodings);
                   1265: 
                   1266:    /* Register the default set of MIME header parsers */
1.74      cvs      1267:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      1268: 
                   1269:    /* Register the default set of Icons for directory listings */
1.27      cvs      1270:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      1271: 
                   1272: }
                   1273: 
1.5       cvs      1274: /*----------------------------------------------------------------------
1.17      cvs      1275:   AHTProfile_delete
                   1276:   deletes the Amaya client profile.
1.5       cvs      1277:   ----------------------------------------------------------------------*/
1.4       cvs      1278: #ifdef __STDC__
                   1279: static void         AHTProfile_delete (void)
                   1280: #else
                   1281: static void         AHTProfile_delete ()
1.7       cvs      1282: #endif                         /* __STDC__ */
1.4       cvs      1283: {
1.22      cvs      1284:  
                   1285:   /* free the Amaya global context */
1.74      cvs      1286:   if (!converters)
                   1287:     HTConversion_deleteAll (converters);
                   1288:   if (!acceptTypes)
                   1289:     HTConversion_deleteAll (acceptTypes);
                   1290:   if (!encodings)
                   1291:     HTCoding_deleteAll (encodings);
                   1292:   
                   1293:   HTList_delete (Amaya->docid_status);
                   1294:   HTList_delete (Amaya->reqlist);
                   1295:   TtaFreeMemory (Amaya);
                   1296:   {
1.61      cvs      1297: 
1.74      cvs      1298:     if (HTLib_isInitialized ())
                   1299:       
1.61      cvs      1300: #  ifdef _WINDOWS
                   1301:       HTEventTerminate ();
                   1302: #  endif _WINDOWS;             
1.74      cvs      1303:     
                   1304:     /* Clean up the persistent cache (if any) */
1.116   ! cvs      1305: #ifdef AMAYA_WWW_CACHE
        !          1306:     HTCacheTerminate ();
        !          1307: #endif /* AMAYA_WWW_CACHE */
1.74      cvs      1308:     
                   1309:     /* Clean up all the global preferences */
                   1310:     HTFormat_deleteAll ();
                   1311:     
                   1312:     /* Terminate libwww */
                   1313:     HTLibTerminate ();
                   1314:   }
1.4       cvs      1315: }
                   1316: 
1.5       cvs      1317: /*----------------------------------------------------------------------
1.116   ! cvs      1318:   AmayacontextInit
        !          1319:   initializes an internal Amaya context for our libwww interface 
        !          1320:   ----------------------------------------------------------------------*/
        !          1321: #ifdef __STDC__
        !          1322: static void                AmayaContextInit ()
        !          1323: #else
        !          1324: static void                AmayaContextInit ()
        !          1325: #endif
        !          1326: 
        !          1327: {
        !          1328:   AmayaAlive = TRUE;
        !          1329:   /* Initialization of the global context */
        !          1330:   Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
        !          1331:   Amaya->reqlist = HTList_new ();
        !          1332:   Amaya->docid_status = HTList_new ();
        !          1333:   Amaya->open_requests = 0;
        !          1334: }
        !          1335: 
        !          1336: /*----------------------------------------------------------------------
1.17      cvs      1337:   QueryInit
                   1338:   initializes the libwww interface 
1.5       cvs      1339:   ----------------------------------------------------------------------*/
1.4       cvs      1340: #ifdef __STDC__
                   1341: void                QueryInit ()
                   1342: #else
                   1343: void                QueryInit ()
                   1344: #endif
                   1345: {
                   1346: 
1.116   ! cvs      1347:    AmayaContextInit ();
        !          1348:    
1.4       cvs      1349:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
1.116   ! cvs      1350:    
1.69      cvs      1351:    /* New AHTBridge stuff */
1.4       cvs      1352: 
1.116   ! cvs      1353: #ifdef _WINDOWS
1.75      cvs      1354:    AHTEventInit ();
1.116   ! cvs      1355: #endif /* _WINDOWS */
1.72      cvs      1356: 
1.116   ! cvs      1357:    HTEvent_setRegisterCallback ((void *) AHTEvent_register);
        !          1358:    HTEvent_setUnregisterCallback ((void *) AHTEvent_unregister);
1.4       cvs      1359: 
1.116   ! cvs      1360: #ifndef _WINDOWS
        !          1361:    HTTimer_registerSetTimerCallback ((void *) SetTimer);
        !          1362:    HTTimer_registerDeleteTimerCallback ((void *) DeleteTimer);
        !          1363: #endif /* !_WINDOWS */
        !          1364: 
        !          1365: #ifdef AMAYA_WWW_CACHE
        !          1366:    /*** 
        !          1367:        WWW_TraceFlag = CACHE_TRACE;
        !          1368:    ***/
        !          1369: #endif
1.72      cvs      1370: 
1.74      cvs      1371: #ifdef DEBUG_LIBWWW
1.116   ! cvs      1372:   /* forwards error messages to our own function */
        !          1373:    WWW_TraceFlag = THD_TRACE;
        !          1374:   HTTrace_setCallback(LineTrace);
1.74      cvs      1375: #endif
1.4       cvs      1376: 
                   1377:    /* Trace activation (for debugging) */
1.7       cvs      1378:    /*
1.116   ! cvs      1379:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
        !          1380: 
1.4       cvs      1381:       WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
                   1382:       SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
                   1383:       SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
                   1384:       SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1385:       SHOW_CORE_TRACE;
                   1386: 
1.7       cvs      1387:     */
1.4       cvs      1388: 
                   1389:    /***
                   1390:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1391:      SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
                   1392:      ***/
                   1393: 
                   1394:    /* Setting up other user interfaces */
                   1395: 
                   1396:    /* Setting up different network parameters */
1.17      cvs      1397:    /* Maximum number of simultaneous open sockets */
1.4       cvs      1398:    HTNet_setMaxSocket (8);
1.75      cvs      1399:    /* different network services timeouts */
1.109     cvs      1400:    HTDNS_setTimeout (60);
1.75      cvs      1401: #ifdef _WINDOWS
                   1402:    /* under windows, the libwww persistent socket handling has
                   1403:    ** some bugs. The following line inhibits idle socket reusal.
                   1404:    ** this is a bit slower, but avoids crashes and gives us time
                   1405:    ** to distribute Amaya before having to patch up libwww.
                   1406:    */
1.76      cvs      1407:    HTHost_setPersistTimeout (-1L);
1.75      cvs      1408: #else
1.76      cvs      1409:    HTHost_setPersistTimeout (60L);
1.75      cvs      1410: #endif /* _WINDOWS */
1.116   ! cvs      1411:    HTHost_setEventTimeout (1000);
1.75      cvs      1412: 
1.4       cvs      1413: #ifdef CATCH_SIG
1.18      cvs      1414:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      1415: #endif
1.15      cvs      1416: }
                   1417: 
1.69      cvs      1418: #ifndef _WINDOWS
1.15      cvs      1419: /*----------------------------------------------------------------------
1.17      cvs      1420:   LoopForStop
                   1421:   a copy of the Thop event loop so we can handle the stop button.
1.69      cvs      1422:   Not useful for windows code (Ramzi).
1.15      cvs      1423:   ----------------------------------------------------------------------*/
                   1424: #ifdef __STDC__
                   1425: static int          LoopForStop (AHTReqContext * me)
                   1426: #else
                   1427: static int          LoopForStop (AHTReqContext * me)
                   1428: #endif
                   1429: {
                   1430: 
1.25      cvs      1431:    extern ThotAppContext app_cont;
1.51      cvs      1432:    XEvent                ev;
                   1433:    XtInputMask           status;
1.17      cvs      1434:    int                 status_req = HT_OK;
1.15      cvs      1435: 
                   1436:    /* to test the async calls  */
1.17      cvs      1437:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      1438:    while (me->reqStatus != HT_ABORT &&
                   1439:          me->reqStatus != HT_END &&
1.69      cvs      1440:          me->reqStatus != HT_ERR) {
1.116   ! cvs      1441:         if (!AmayaIsAlive ())
1.69      cvs      1442:            /* Amaya was killed by one of the callback handlers */
                   1443:            exit (0);
                   1444: 
                   1445:         status = XtAppPending (app_cont);
                   1446:         if (status & XtIMXEvent) {
                   1447:             XtAppNextEvent (app_cont, &ev);
                   1448:            TtaHandleOneEvent (&ev);
                   1449:         } else if (status & (XtIMAll & (~XtIMXEvent))) {
                   1450:                 XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
                   1451:         } else {
                   1452:                XtAppNextEvent (app_cont, &ev);
                   1453:               TtaHandleOneEvent (&ev);
                   1454:         }
                   1455:    }
1.4       cvs      1456: 
1.69      cvs      1457:    switch (me->reqStatus) {
                   1458:          case HT_ERR:
                   1459:           case HT_ABORT:
1.15      cvs      1460:               status_req = HT_ERROR;
                   1461:               break;
                   1462: 
1.69      cvs      1463:          case HT_END:
1.15      cvs      1464:               status_req = HT_OK;
                   1465:               break;
                   1466: 
1.69      cvs      1467:          default:
1.15      cvs      1468:               break;
1.69      cvs      1469:    }
1.15      cvs      1470:    return (status_req);
1.4       cvs      1471: }
1.69      cvs      1472: #endif /* _WINDOWS */
1.4       cvs      1473: 
1.5       cvs      1474: /*----------------------------------------------------------------------
1.15      cvs      1475:   QueryClose
1.21      cvs      1476:   closes all existing threads, frees all non-automatically deallocated
                   1477:   memory and then ends libwww.
1.5       cvs      1478:   ----------------------------------------------------------------------*/
1.116   ! cvs      1479: void QueryClose ()
1.4       cvs      1480: {
1.24      cvs      1481: 
1.116   ! cvs      1482:   AmayaAlive = FALSE;
1.24      cvs      1483: 
1.116   ! cvs      1484:   /* remove all the handlers and callbacks that may output a message to
        !          1485:      a non-existent Amaya window */
1.21      cvs      1486: 
1.116   ! cvs      1487:   HTNet_deleteAfter (AHTLoadTerminate_handler);
        !          1488:   HTNet_deleteAfter (redirection_handler);
        !          1489:   HTAlertCall_deleteAll (HTAlert_global () );
        !          1490:   HTAlert_setGlobal ((HTList *) NULL);
        !          1491:   HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
        !          1492:   HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
        !          1493: #ifndef _WINDOWS
        !          1494:   /** need to erase all existing timers too **/
        !          1495:    HTTimer_registerSetTimerCallback (NULL);
        !          1496:    HTTimer_registerDeleteTimerCallback (NULL);
        !          1497: #endif /* !_WINDOWS */
        !          1498:   HTHost_setActivateRequestCallback (NULL);
        !          1499:   Thread_deleteAll ();
1.21      cvs      1500:  
1.116   ! cvs      1501:   HTProxy_deleteAll ();
        !          1502:   HTNoProxy_deleteAll ();
        !          1503:   HTGateway_deleteAll ();
        !          1504:   AHTProfile_delete ();
        !          1505: }
        !          1506: 
        !          1507: /*----------------------------------------------------------------------
        !          1508:   NextNameValue
        !          1509:   ---------------------------------------------------------------------*/
        !          1510: #ifdef __STDC__
        !          1511: static char * NextNameValue (char ** pstr, char **name, char **value)
        !          1512: #else
        !          1513: static char * NextNameValue (pstr, name, value);
        !          1514: char ** pstr;
        !          1515: char **name;
        !          1516: char **value;
        !          1517: #endif /* __STDC__ */
        !          1518: {
        !          1519:   char * p = *pstr;
        !          1520:   char * start = NULL;
        !          1521:   if (!pstr || !*pstr) return NULL;
        !          1522:   
        !          1523:     if (!*p) {
        !          1524:       *pstr = p;
        !          1525:       *name = NULL;
        !          1526:       *value = NULL;
        !          1527:       return NULL;                                      /* No field */
        !          1528:     }
        !          1529:     
        !          1530:     /* Now search for the next '&' and ';' delimitators */
        !          1531:     start = p;
        !          1532:     while (*p && *p != '&' && *p != ';') p++;
        !          1533:     if (*p) 
        !          1534:       *p++ = '\0';
        !          1535:     *pstr = p;
        !          1536: 
        !          1537:     /* Search for the name and value */
        !          1538:     *name = start;
        !          1539:     p = start;
        !          1540:     
        !          1541:     while(*p && *p != '=') 
        !          1542:       p++;
        !          1543:     if (*p) 
        !          1544:       *p++ = '\0';
        !          1545:     *value = p;
        !          1546: 
        !          1547:     return start;
        !          1548: }
        !          1549: 
        !          1550: /*----------------------------------------------------------------------
        !          1551:   PrepareFormdata
        !          1552:   ---------------------------------------------------------------------*/
        !          1553: #ifdef __STDC__
        !          1554: static HTAssocList * PrepareFormdata (char *string)
        !          1555: #else
        !          1556: static HTAssocList * PrepareFormdata (string)
        !          1557: char *string;
        !          1558: #endif /* __STDC__ */
        !          1559: {
        !          1560:   char * tmp_string, * tmp_string_ptr;
        !          1561:   char * name;
        !          1562:   char * value;
        !          1563:   HTAssocList * formdata;
        !          1564: 
        !          1565:   if (!string)
        !          1566:     return NULL;
        !          1567: 
        !          1568:   /* store the ptr in another variable, as the original address will
        !          1569:      change
        !          1570:      */
        !          1571: 
        !          1572:   tmp_string_ptr = tmp_string = TtaStrdup (string);
        !          1573:   formdata = HTAssocList_new();
        !          1574:   
        !          1575:   while (*tmp_string)
        !          1576:     {
        !          1577:       NextNameValue (&tmp_string, &name, &value);
        !          1578:       HTAssocList_addObject(formdata,
        !          1579:                            name, value);
        !          1580:     }
        !          1581: 
        !          1582:   TtaFreeMemory (tmp_string_ptr);
        !          1583:   return formdata;
1.4       cvs      1584: }
                   1585: 
1.99      cvs      1586: 
                   1587: /*----------------------------------------------------------------------
                   1588:   InvokeGetObjectWWW_callback
                   1589:   A simple function to invoke a callback function whenever there's an error
                   1590:   in GetObjectWWW
                   1591:   ---------------------------------------------------------------------*/
                   1592: 
1.116   ! cvs      1593: #ifdef __STDC__
        !          1594: void      InvokeGetObjectWWW_callback (int docid, char *urlName, char *outputfile, TTcbf *terminate_cbf, void *context_tcbf, int status)
1.99      cvs      1595: #else
1.111     cvs      1596: void      InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf, context_tcbf, status)
1.99      cvs      1597: int docid;
                   1598: char *urlName;
                   1599: char *outputfile;
                   1600: TTcbf *terminate_cbf;
                   1601: void *context_tcbf;
1.116   ! cvs      1602: #endif /* __STDC__ */
1.99      cvs      1603: {
                   1604:   if (!terminate_cbf)
                   1605:     return;
                   1606:   
1.116   ! cvs      1607:   (*terminate_cbf) (docid, status, urlName, outputfile,
1.99      cvs      1608:                    NULL, context_tcbf);  
                   1609: }
                   1610: 
                   1611: 
                   1612: 
1.5       cvs      1613: /*----------------------------------------------------------------------
1.15      cvs      1614:    GetObjectWWW
1.17      cvs      1615:    this function requests a resource designated by a URLname into a
                   1616:    temporary filename. The download can come from a simple GET operation,
                   1617:    or can come from POSTING/GETTING a form. In the latter
                   1618:    case, the function receives a query string to send to the server.
                   1619: 
1.5       cvs      1620:    4  file retrieval modes are proposed:                              
                   1621:    AMAYA_SYNC : blocking mode                            
                   1622:    AMAYA_ISYNC : incremental, blocking mode              
                   1623:    AMAYA_ASYNC : non-blocking mode                       
                   1624:    AMAYA_IASYNC : incremental, non-blocking mode         
                   1625:    
                   1626:    In the incremental mode, each time a package arrives, it will be   
                   1627:    stored in the temporary file. In addition, if an                   
                   1628:    incremental_callback function is defined, this function will be    
                   1629:    called and handled a copy of the newly received data package.      
                   1630:    Finally, if a terminate_callback function is defined, it will be   
                   1631:    invoked when the request terminates. The caller of this function
1.4       cvs      1632:    can define two different contexts to be passed to the callback
                   1633:    functions.
                   1634: 
                   1635:    When the function is called with the SYNC mode, the function will
                   1636:    return only when the requested file has been loaded.
                   1637:    The ASYNC mode will immediately return after setting up the
                   1638:    call.
                   1639: 
                   1640:    Notes:
                   1641:    At the end of a succesful request, the urlName string contains the
                   1642:    name of the actually retrieved URL. As a URL can change over the time,
                   1643:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      1644:    caller verify the value of the urlName variable at the end of
1.4       cvs      1645:    a request.
                   1646: 
                   1647:    Inputs:
                   1648:    - docid  Document identifier for the set of objects being
                   1649:    retrieved.
                   1650:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   1651:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   1652:    - mode The retrieval mode.
                   1653:    - incremental_cbf 
                   1654:    - context_icbf
                   1655:    Callback and context for the incremental modes
                   1656:    - terminate_cbf 
                   1657:    - context_icbf
                   1658:    Callback and context for a terminate handler
1.17      cvs      1659:    -error_html if TRUE, then display any server error message as an
                   1660:    HTML document.
1.88      cvs      1661:    - content_type a string
                   1662:  
1.4       cvs      1663:    Outputs:
                   1664:    - urlName The URL that was retrieved
                   1665:    - outputfile The name of the temporary file which holds the
                   1666:    retrieved data. (Only in case of success)
1.88      cvs      1667:    - if content_type wasn't NULL, it will contain a copy of the parameter
                   1668:      sent in the HTTP answer
1.4       cvs      1669:    Returns:
                   1670:    HT_ERROR
                   1671:    HT_OK
1.5       cvs      1672:  
                   1673:   ----------------------------------------------------------------------*/
1.4       cvs      1674: #ifdef __STDC__
1.116   ! cvs      1675: int GetObjectWWW (int docid, char* urlName, char* postString,
        !          1676:                  char* outputfile, int mode, TIcbf* incremental_cbf, 
        !          1677:                  void* context_icbf, TTcbf* terminate_cbf, 
1.88      cvs      1678:                  void* context_tcbf, boolean error_html, char *content_type)
1.4       cvs      1679: #else
1.116   ! cvs      1680: int GetObjectWWW (docid, urlName, postString, outputfile, mode, 
        !          1681:                  incremental_cbf, context_icbf, 
1.88      cvs      1682:                  terminate_cbf, context_tcbf, error_html, content_type)
1.73      cvs      1683: int           docid;
                   1684: char         *urlName;
                   1685: char         *postString;
                   1686: char         *outputfile;
                   1687: int           mode;
                   1688: TIcbf        *incremental_cbf;
                   1689: void         *context_icbf;
                   1690: TTcbf        *terminate_cbf;
                   1691: void         *context_tcbf;
                   1692: boolean       error_html;
1.88      cvs      1693: char        *content_type;
1.4       cvs      1694: #endif
                   1695: {
                   1696:    AHTReqContext      *me;
                   1697:    char               *ref;
1.107     cvs      1698:    int                 status, l;
1.114     cvs      1699:    int                 tempsubdir;
1.7       cvs      1700: 
1.116   ! cvs      1701:    if (urlName == NULL || docid == 0 || outputfile == NULL) 
        !          1702:      {
        !          1703:        /* no file to be loaded */
        !          1704:        TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
1.69      cvs      1705:        
1.116   ! cvs      1706:        if (error_html)
1.69      cvs      1707:         /* so we can show the error message */
                   1708:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116   ! cvs      1709:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
        !          1710:                                    context_tcbf, HT_ERROR);
        !          1711:        return HT_ERROR;
        !          1712:      }
1.7       cvs      1713: 
1.4       cvs      1714:    /* do we support this protocol? */
1.116   ! cvs      1715:    if (IsValidProtocol (urlName) == NO) 
        !          1716:      {
        !          1717:        /* return error */
        !          1718:        outputfile[0] = EOS;    /* file could not be opened */
        !          1719:        TtaSetStatus (docid, 1, 
        !          1720:                     TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL),
        !          1721:                     urlName);
        !          1722: 
        !          1723:        if (error_html)
1.69      cvs      1724:         /* so we can show the error message */
                   1725:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.116   ! cvs      1726:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
        !          1727:                                    context_tcbf, HT_ERROR);
        !          1728:        return HT_ERROR;
        !          1729:      }
1.4       cvs      1730: 
1.114     cvs      1731:    /* we store CSS in subdir named 0; all the other files go to a subidr
                   1732:       named after their own docid */
                   1733:    
                   1734:    tempsubdir = (mode & AMAYA_LOAD_CSS) ? 0 : docid;
                   1735: 
1.116   ! cvs      1736:    /* create a tempfilename */
        !          1737:    sprintf (outputfile, "%s%c%d%c%04dAM", 
        !          1738:            TempFileDirectory, DIR_SEP, tempsubdir, DIR_SEP, object_counter);
        !          1739:    /* update the object_counter (used for the tempfilename) */
1.4       cvs      1740:    object_counter++;
1.116   ! cvs      1741:    
1.4       cvs      1742:    /* normalize the URL */
1.45      cvs      1743:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      1744:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      1745:    if (ref == (char*) NULL || ref[0] == EOS) {
                   1746:       /*error */
                   1747:       outputfile[0] = EOS;
                   1748:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   1749:       
                   1750:       if (error_html)
1.116   ! cvs      1751:        /* so we can show the error message */
        !          1752:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.99      cvs      1753:       InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
1.116   ! cvs      1754:                                   context_tcbf, HT_ERROR);
1.69      cvs      1755:       return HT_ERROR;
                   1756:    }
1.116   ! cvs      1757: 
1.4       cvs      1758:    /* verify if that file name existed */
1.9       cvs      1759:    if (TtaFileExist (outputfile))
1.77      cvs      1760:      TtaFileUnlink (outputfile);
1.116   ! cvs      1761:    
1.4       cvs      1762:    /* Initialize the request structure */
                   1763:    me = AHTReqContext_new (docid);
1.116   ! cvs      1764:    if (me == NULL) 
        !          1765:      {
        !          1766:        outputfile[0] = EOS;
        !          1767:        /* need an error message here */
        !          1768:        TtaFreeMemory (ref);
        !          1769:        InvokeGetObjectWWW_callback (docid, urlName, outputfile, terminate_cbf,
        !          1770:                                   context_tcbf, HT_ERROR);
        !          1771:        return HT_ERROR;
        !          1772:      }
        !          1773: 
1.4       cvs      1774:    /* Specific initializations for POST and GET */
1.116   ! cvs      1775:    if (mode & AMAYA_FORM_POST)
        !          1776:      {
        !          1777:        me->method = METHOD_POST;
        !          1778:        if (postString)
        !          1779:         {
        !          1780:           me->mem_ptr = TtaStrdup (postString);
        !          1781:           me->block_size = strlen (postString);
        !          1782:         }
        !          1783:        HTRequest_setMethod (me->request, METHOD_POST);
        !          1784:      }
        !          1785:    else 
        !          1786:      {
        !          1787:        me->method = METHOD_GET;
        !          1788:        if (!HasKnownFileSuffix (ref))
        !          1789:         HTRequest_setConversion(me->request, acceptTypes, TRUE);
        !          1790:      }
1.93      cvs      1791: 
1.116   ! cvs      1792:    /* Common initialization for all HTML methods */
1.4       cvs      1793:    me->mode = mode;
                   1794:    me->error_html = error_html;
                   1795:    me->incremental_cbf = incremental_cbf;
                   1796:    me->context_icbf = context_icbf;
                   1797:    me->terminate_cbf = terminate_cbf;
                   1798:    me->context_tcbf = context_tcbf;
1.64      cvs      1799: 
1.69      cvs      1800:    /* for the async. request modes, we need to have our
1.4       cvs      1801:       own copy of outputfile and urlname
1.116   ! cvs      1802:       */
1.4       cvs      1803: 
1.116   ! cvs      1804:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) 
        !          1805:      {
        !          1806:        l = strlen (outputfile);
        !          1807:        if (l > MAX_LENGTH)
        !          1808:         me->outputfile = TtaGetMemory (l + 2);
        !          1809:        else
        !          1810:         me->outputfile = TtaGetMemory (MAX_LENGTH + 2);
        !          1811:        strcpy (me->outputfile, outputfile);
        !          1812:        l = strlen (urlName);
        !          1813:        if (l > MAX_LENGTH)
        !          1814:         me->urlName = TtaGetMemory (l + 2);
        !          1815:        else
        !          1816:         me->urlName = TtaGetMemory (MAX_LENGTH + 2);
        !          1817:        strcpy (me->urlName, urlName);
        !          1818: #ifdef _WINDOWS
        !          1819:      /* force windows ASYNC requests to always be non preemptive */
        !          1820:      HTRequest_setPreemptive (me->request, NO);
        !          1821: #endif /*_WINDOWS */
        !          1822:      } /* AMAYA_ASYNC mode */ 
        !          1823:    else 
        !          1824: #ifdef _WINDOWS
        !          1825:      {
        !          1826:        me->outputfile = outputfile;
        !          1827:        me->urlName = urlName;
        !          1828:        /* force windows SYNC requests to always be non preemptive */
        !          1829:        HTRequest_setPreemptive (me->request, YES);
        !          1830:      }
        !          1831: #else /* !_WINDOWS */
        !          1832:      {
        !          1833:        me->outputfile = outputfile;
        !          1834:        me->urlName = urlName;
        !          1835:      }
        !          1836:    /***
1.57      cvs      1837:      Change for taking into account the stop button:
                   1838:      The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
                   1839:      we will loop until the document has been received or a stop signal
                   1840:      generated
1.77      cvs      1841:      ****/
1.116   ! cvs      1842:    HTRequest_setPreemptive (me->request, NO);
        !          1843: #endif /* _WINDOWS */
1.61      cvs      1844: 
                   1845:    /* prepare the URLname that will be displayed in teh status bar */
                   1846:    ChopURL (me->status_urlName, me->urlName);
1.77      cvs      1847:    TtaSetStatus (me->docid, 1, 
                   1848:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   1849:                 me->status_urlName);
1.4       cvs      1850: 
                   1851:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      1852:    TtaFreeMemory (ref);
1.4       cvs      1853: 
1.114     cvs      1854:    if (mode & AMAYA_NOCACHE) 
1.116   ! cvs      1855:       HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
        !          1856: 
        !          1857:    /* prepare the query string and format for POST */
        !          1858:    if (mode & AMAYA_FORM_POST)
1.114     cvs      1859:      {
1.116   ! cvs      1860:        HTAnchor_setFormat ((HTParentAnchor *) me->anchor, 
        !          1861:                           HTAtom_for ("application/x-www-form-urlencoded"));
        !          1862:        HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
        !          1863:        HTRequest_setEntityAnchor (me->request, me->anchor);
        !          1864:        HTRequest_setEntityAnchor (me->request, me->anchor);
        !          1865:        me->formdata = PrepareFormdata (postString);
        !          1866:      } 
1.114     cvs      1867: 
1.116   ! cvs      1868:    /* do the request */
        !          1869:    if (mode & AMAYA_FORM_POST)
        !          1870:      status = HTPostFormAnchor (me->formdata, (HTAnchor *) me->anchor, 
        !          1871:                                me->request);
        !          1872:    else
1.77      cvs      1873:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      1874: 
1.116   ! cvs      1875:    /* control the errors */
        !          1876:    if (status != HT_OK
        !          1877:        && HTError_hasSeverity (HTRequest_error (me->request), ERR_NON_FATAL))
        !          1878:      status = HT_ERROR;
1.82      cvs      1879:      
1.116   ! cvs      1880:      /* test the effect of HTRequest_kill () */
1.85      cvs      1881:      
1.116   ! cvs      1882: #if 0
        !          1883:      /** *this should go to term_d @@@@ */
        !          1884:      if (me->reqStatus == HT_CACHE)
1.93      cvs      1885:        {
1.116   ! cvs      1886:         AHTPrintPendingRequestStatus (me->docid, YES);
        !          1887:         /* free the memory allocated for async requests */
        !          1888:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
        !          1889:                                      terminate_cbf, context_tcbf, HT_OK);
        !          1890:         AHTReqContext_delete (me);
        !          1891:         return HT_OK;
1.93      cvs      1892:        }
1.116   ! cvs      1893: #endif 
1.4       cvs      1894: 
1.116   ! cvs      1895:    if (status == HT_ERROR)
        !          1896:      /* the request invocation failed */
        !          1897:      {
        !          1898:        /* show an error message on the status bar */
        !          1899:        DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
        !          1900:        TtaSetStatus (docid, 1, 
        !          1901:                     TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
        !          1902:                     urlName);
        !          1903:        if (me->reqStatus == HT_NEW)
        !          1904:         /* manually invoke the last processing that usually gets done
        !          1905:            in a succesful request */
        !          1906:         InvokeGetObjectWWW_callback (docid, urlName, outputfile, 
        !          1907:                                      terminate_cbf, context_tcbf, HT_ERROR);
        !          1908:        /* terminate_handler wasn't called */
        !          1909:        if (mode & AMAYA_SYNC || mode & AMAYA_ISYNC)
        !          1910:         AHTReqContext_delete (me);
        !          1911:      }
        !          1912:    else
        !          1913:    /* end treatment for SYNC requests */
1.72      cvs      1914: 
1.116   ! cvs      1915:    if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
        !          1916:      {
        !          1917: #ifndef _WINDOWS
        !          1918:        /* part of the UNIX stop button handler */
        !          1919:        if (status != HT_ERROR)
        !          1920:         status = LoopForStop (me);
        !          1921: #endif /* _!WINDOWS */
        !          1922:        
        !          1923:        if (!HTRequest_kill (me->request))
        !          1924:         AHTReqContext_delete (me);
        !          1925:      }
        !          1926:     return (status);
1.4       cvs      1927: }
                   1928: 
1.5       cvs      1929: /*----------------------------------------------------------------------
1.17      cvs      1930:    PutObjectWWW
                   1931:    frontend for uploading a resource to a URL. This function downloads
                   1932:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   1933:    finish the job.
                   1934: 
1.5       cvs      1935:    2 upload modes are proposed:                                       
                   1936:    AMAYA_SYNC : blocking mode                            
                   1937:    AMAYA_ASYNC : non-blocking mode                       
                   1938:    
1.4       cvs      1939:    When the function is called with the SYNC mode, the function will
                   1940:    return only when the file has been uploaded.
                   1941:    The ASYNC mode will immediately return after setting up the
                   1942:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   1943:    call back terminate_cbf, handling it the context defined in
                   1944:    context_tcbf.
                   1945: 
                   1946:    Notes:
                   1947:    At the end of a succesful request, the urlName string contains the
                   1948:    name of the actually uploaded URL. As a URL can change over the time,
                   1949:    (e.g., be redirected elsewhere), it is advised that the function
                   1950:    caller verifies the value of the urlName variable at the end of
                   1951:    a request.
                   1952: 
                   1953:    Inputs:
                   1954:    - docid  Document identifier for the set of objects being
                   1955:    retrieved.
                   1956:    - fileName A pointer to the local file to upload
                   1957:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   1958:    - mode The retrieval mode.
                   1959:    - terminate_cbf 
                   1960:    - context_icbf
                   1961:    Callback and context for a terminate handler
                   1962: 
                   1963:    Outputs:
                   1964:    - urlName The URL that was uploaded
                   1965: 
                   1966:    Returns:
                   1967:    HT_ERROR
                   1968:    HT_OK
1.5       cvs      1969:   ----------------------------------------------------------------------*/
1.4       cvs      1970: #ifdef __STDC__
1.27      cvs      1971: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      1972:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   1973: #else
1.26      cvs      1974: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      1975:                                  ,terminate_cbf, context_tcbf)
                   1976: int                 docid;
                   1977: char               *urlName;
                   1978: char               *fileName;
                   1979: int                 mode;
1.27      cvs      1980: PicType             contentType;
1.4       cvs      1981: TTcbf              *terminate_cbf;
                   1982: void               *context_tcbf;
                   1983: 
1.116   ! cvs      1984: #endif /* __STDC__ */
1.4       cvs      1985: {
1.116   ! cvs      1986:    AHTReqContext      *me;
1.4       cvs      1987:    int                 status;
                   1988:    int                 fd;
                   1989:    struct stat         file_stat;
1.116   ! cvs      1990:    char               *fileURL;
1.4       cvs      1991: 
1.33      cvs      1992:    AmayaLastHTTPErrorMsg [0] = EOS;
                   1993:    
1.116   ! cvs      1994:    if (urlName == NULL || docid == 0 || fileName == NULL 
        !          1995:        || !TtaFileExist (fileName))
1.4       cvs      1996:       /* no file to be uploaded */
                   1997:       return HT_ERROR;
                   1998: 
                   1999:    /* do we support this protocol? */
1.7       cvs      2000:    if (IsValidProtocol (urlName) == NO)
                   2001:      {
                   2002:        /* return error */
1.77      cvs      2003:        TtaSetStatus (docid, 1, 
                   2004:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      2005:                      urlName);
                   2006:        return HT_ERROR;
                   2007:      }
1.116   ! cvs      2008: 
        !          2009:    /* verify the file's size */
        !          2010: #ifndef _WINDOWS
1.7       cvs      2011:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.116   ! cvs      2012: #else 
1.74      cvs      2013:    if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.116   ! cvs      2014: #endif /* _WINDOWS */
1.7       cvs      2015:      {
                   2016:        /* if we could not open the file, exit */
                   2017:        /*error msg here */
                   2018:        return (HT_ERROR);
                   2019:      }
1.4       cvs      2020: 
                   2021:    fstat (fd, &file_stat);
1.116   ! cvs      2022:    close (fd);
1.4       cvs      2023: 
1.7       cvs      2024:    if (file_stat.st_size == 0)
                   2025:      {
                   2026:        /* file was empty */
                   2027:        /*errmsg here */
                   2028:        return (HT_ERROR);
                   2029:      }
1.116   ! cvs      2030: 
        !          2031:    /* prepare the request context */
1.4       cvs      2032: 
                   2033:    if (THD_TRACE)
1.116   ! cvs      2034:       fprintf (stderr, "file size == %u\n", (unsigned) file_stat.st_size);
1.4       cvs      2035: 
                   2036:    me = AHTReqContext_new (docid);
                   2037: 
1.7       cvs      2038:    if (me == NULL)
                   2039:      {
                   2040:        /* need an error message here */
                   2041:        TtaHandlePendingEvents ();
                   2042:        return (HT_ERROR);
                   2043:      }
1.116   ! cvs      2044: 
1.4       cvs      2045:    me->mode = mode;
                   2046:    me->incremental_cbf = (TIcbf *) NULL;
                   2047:    me->context_icbf = (void *) NULL;
                   2048:    me->terminate_cbf = terminate_cbf;
                   2049:    me->context_tcbf = context_tcbf;
                   2050:    me->urlName = urlName;
1.116   ! cvs      2051:    me->block_size =  file_stat.st_size;
1.17      cvs      2052:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      2053:    me->method = METHOD_PUT;
                   2054:    HTRequest_setMethod (me->request, METHOD_PUT);
                   2055:    me->output = stdout;
1.17      cvs      2056:    /* we are not expecting to receive any input from the server */
                   2057:    me->outputfile = (char *) NULL; 
1.4       cvs      2058: 
1.116   ! cvs      2059:    fileURL = HTParse (fileName, "file:/", PARSE_ALL);
        !          2060:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (fileURL);
        !          2061:    HT_FREE (fileURL);
1.114     cvs      2062: 
1.28      cvs      2063:    /* Set the Content-Type of the file we are uploading */
1.77      cvs      2064:    HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
                   2065:                       AHTGuessAtom_for (me->urlName, contentType));
1.116   ! cvs      2066:    HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.17      cvs      2067: 
1.4       cvs      2068:    HTRequest_setEntityAnchor (me->request, me->anchor);
1.116   ! cvs      2069: #ifdef _WINDOWS
        !          2070:    HTRequest_setPreemptive (me->request, YES);
        !          2071: #else
        !          2072:    HTRequest_setPreemptive (me->request, NO);
        !          2073: #endif /* _WINDOWS */
        !          2074: 
        !          2075:    if (mode & AMAYA_NOCACHE)
        !          2076:      HTRequest_setReloadMode (me->request, HT_CACHE_FLUSH);
        !          2077: 
1.74      cvs      2078:    /* prepare the URLname that will be displayed in teh status bar */
                   2079:    ChopURL (me->status_urlName, me->urlName);
                   2080:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
                   2081:                     me->status_urlName);
1.114     cvs      2082: 
1.116   ! cvs      2083:    status = HTPutDocumentAbsolute (me->anchor, urlName, me->request);
1.4       cvs      2084: 
1.116   ! cvs      2085:    if (status != HT_ERROR && me->reqStatus != HT_ERR)
1.7       cvs      2086:      {
                   2087:        /* part of the stop button handler */
                   2088:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   2089:          {
                   2090:             status = LoopForStop (me);
                   2091:          }
1.15      cvs      2092:      }
1.116   ! cvs      2093:    else
        !          2094:      {
        !          2095:       if (! HTRequest_kill (me->request))
        !          2096:         AHTReqContext_delete (me);
        !          2097:      }
        !          2098:  
        !          2099:    TtaHandlePendingEvents ();
1.90      cvs      2100: 
1.116   ! cvs      2101:    return status;
1.28      cvs      2102: }
1.4       cvs      2103: 
1.5       cvs      2104: /*----------------------------------------------------------------------
1.17      cvs      2105:   Stop Request
                   2106:   stops (kills) all active requests associated with a docid 
1.5       cvs      2107:   ----------------------------------------------------------------------*/
1.4       cvs      2108: #ifdef __STDC__
                   2109: void                StopRequest (int docid)
                   2110: #else
                   2111: void                StopRequest (docid)
                   2112: int                 docid;
                   2113: #endif
                   2114: {
                   2115:    HTList             *cur;
                   2116:    AHTDocId_Status    *docid_status;
                   2117:    AHTReqContext      *me;
1.108     cvs      2118: #ifdef DEBUG_LIBWWW
                   2119:    int                 open_requests;
                   2120: #endif /* DEBUG_LIBWWW */
1.116   ! cvs      2121: 
        !          2122:  
        !          2123:    if (Amaya && libDoStop)
1.7       cvs      2124:      {
1.100     cvs      2125: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2126:        fprintf (stderr, "StopRequest: number of Amaya requests : %d\n", 
        !          2127:                Amaya->open_requests);
1.100     cvs      2128: #endif /* DEBUG_LIBWWW */
                   2129: 
1.116   ! cvs      2130:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
        !          2131:                                                          Amaya->docid_status);
        !          2132:        /* verify if there are any requests at all associated with docid */
        !          2133:        
        !          2134:        if (docid_status == (AHTDocId_Status *) NULL)
        !          2135:         return;
1.108     cvs      2136: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2137:        open_requests = docid_status->counter;
1.108     cvs      2138: #endif /* DEBUG_LIBWWW */
1.4       cvs      2139: 
1.116   ! cvs      2140:        /* First, kill all pending requests */
        !          2141:        /* We first inhibit the activation of pending requests */
        !          2142:        HTHost_disable_PendingReqLaunch ();
        !          2143:        cur = Amaya->reqlist;
        !          2144:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
        !          2145:         {
        !          2146:           if (me->docid == docid && me->reqStatus == HT_NEW)
        !          2147:             {
        !          2148:               /* If we have an ASYNC request, we kill it.
        !          2149:               ** If it's a SYNC request, we just mark it as aborted
        !          2150:               */
        !          2151:               me->reqStatus = HT_ABORT;
        !          2152:               if (((me->mode & AMAYA_IASYNC)
        !          2153:                    || (me->mode & AMAYA_ASYNC))
        !          2154:                   && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
        !          2155:                 {
        !          2156:                   /* delete the amaya/libwww request context */
        !          2157:                   if (!HTRequest_kill (me->request))
        !          2158:                     /* if the above function returns NO (0), it means
        !          2159:                     ** that there was no network context and that 
        !          2160:                     ** terminate_handler wasn't called. So, we erase
        !          2161:                     ** this context ourselves
        !          2162:                     */
1.96      cvs      2163:                     AHTReqContext_delete (me);
1.116   ! cvs      2164:                   
        !          2165:                   cur = Amaya->reqlist;
1.108     cvs      2166: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2167:                   /* update the number of open requests */
        !          2168:                   open_requests--;                
1.108     cvs      2169: #endif /* DEBUG_LIBWWW */
1.116   ! cvs      2170:                 }
        !          2171:             }
        !          2172:         }
1.108     cvs      2173: 
1.85      cvs      2174:        /* enable the activation of pending requests */
1.116   ! cvs      2175:        HTHost_enable_PendingReqLaunch ();
1.85      cvs      2176: 
1.116   ! cvs      2177:        cur = Amaya->reqlist;
        !          2178:        while ((me = (AHTReqContext *) HTList_nextObject (cur)))
        !          2179:         {
        !          2180:           if (me->docid == docid)
        !          2181:             {
        !          2182:               /* kill this request */
        !          2183:               
        !          2184:               switch (me->reqStatus)
        !          2185:                 {
        !          2186:                 case HT_ABORT:
1.108     cvs      2187: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2188:                   fprintf (stderr, "Stop: url %s says abort", me->urlName);
1.108     cvs      2189: #endif /* DEBUG_LIBWWW */
1.116   ! cvs      2190:                   break;
1.100     cvs      2191:                  
1.116   ! cvs      2192:                 case HT_END:
        !          2193: #ifdef DEBUG_LIBWWW
        !          2194:                   fprintf (stderr, "Stop: url %s says end", me->urlName);
        !          2195: #endif /* DEBUG_LIBWWW */
        !          2196:                   break;
        !          2197: 
        !          2198:                 case HT_BUSY:
        !          2199:                   me->reqStatus = HT_ABORT;
1.108     cvs      2200: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2201:                   fprintf (stderr, "Stop: url %s going from busy to abort\n",
        !          2202:                            me->urlName);
1.108     cvs      2203: #endif /* DEBUG_LIBWWW */
1.116   ! cvs      2204:                   break;
1.100     cvs      2205: 
1.116   ! cvs      2206:                 case HT_NEW_PENDING:
        !          2207:                 case HT_WAITING:
        !          2208:                 default:
1.108     cvs      2209: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2210:                   fprintf (stderr, "Stop: url %s says NEW_PENDING, WAITING",
        !          2211:                            me->urlName);
1.108     cvs      2212: #endif /* DEBUG_LIBWWW */
1.116   ! cvs      2213:                   me->reqStatus = HT_ABORT;
        !          2214: 
        !          2215:                   if (((me->mode & AMAYA_IASYNC)
        !          2216:                        || (me->mode & AMAYA_ASYNC))
        !          2217:                       && !(me->mode & AMAYA_ASYNC_SAFE_STOP))
        !          2218:                     {
        !          2219:                       /* delete the amaya/libwww request context */
        !          2220:                       if (!HTRequest_kill (me->request))
        !          2221:                         /* if the above function returns NO (0), it means
        !          2222:                         ** that there was no network context and that 
        !          2223:                         ** terminate_handler wasn't called. So, we erase
        !          2224:                         ** this context ourselves
        !          2225:                         */
        !          2226:                         AHTReqContext_delete (me);
1.96      cvs      2227: 
1.116   ! cvs      2228:                       cur = Amaya->reqlist;
1.108     cvs      2229: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2230:                       /* update the number of open requests */
        !          2231:                       open_requests--;            
1.108     cvs      2232: #endif /* DEBUG_LIBWWW */
1.116   ! cvs      2233:                     }
        !          2234: 
1.108     cvs      2235: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2236:                   if (HTHost_isIdle (reqHost))
        !          2237:                     fprintf (stderr, "StopRequst: After killing, Host is "
        !          2238:                              "idle\n");
        !          2239:                   else
        !          2240:                     fprintf (stderr, "StopRequest: After killing, Host isn't "
        !          2241:                              "idle\n");
1.108     cvs      2242: #endif /* DEBUG_LIBWWW */
1.95      cvs      2243: 
1.108     cvs      2244: #ifdef DEBUG_LIBWWW                 
1.116   ! cvs      2245:               open_requests--;
1.108     cvs      2246: #endif /* DEBUG_LIBWWW */                   
1.93      cvs      2247:                     break;
                   2248:                     
1.116   ! cvs      2249:                 }      /* switch */
        !          2250:             }          /* if me docid */
        !          2251:         }                      /* while */
        !          2252:        
1.100     cvs      2253: #ifdef DEBUG_LIBWWW
1.116   ! cvs      2254:        fprintf (stderr, "StopRequest: number of Amaya requests : "
        !          2255:                "%d\n", Amaya->open_requests);
1.100     cvs      2256: #endif /* DEBUG_LIBWWW */
1.7       cvs      2257:      }                         /* if amaya open requests */
1.93      cvs      2258:    
1.85      cvs      2259: } /* StopRequest */
1.17      cvs      2260: 
                   2261: 
1.105     cvs      2262: /*----------------------------------------------------------------------
                   2263:   AmayaIsAlive
                   2264:   returns the status of the AmayaAlive flag
                   2265:   ----------------------------------------------------------------------*/
                   2266: #ifdef __STDC__
                   2267: boolean AmayaIsAlive (void)
                   2268: #else
                   2269: boolean AmayaIsAlive ()
                   2270: #endif /* _STDC_ */
                   2271: {
                   2272:   return AmayaAlive;
                   2273: }
1.116   ! cvs      2274: 
1.69      cvs      2275: #endif /* AMAYA_JAVA */
1.77      cvs      2276: 
1.116   ! cvs      2277: /*
        !          2278:   end of Module query.c
        !          2279: */
1.17      cvs      2280: 
                   2281: 

Webmaster