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

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

Webmaster