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

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

Webmaster