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

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.75      cvs        14:  *         R. Guetari/J. Kahan  Windows 95/NT routines
1.19      cvs        15:  */
1.17      cvs        16: 
1.44      cvs        17: #ifndef AMAYA_JAVA
                     18: 
1.12      cvs        19: /* Amaya includes  */
1.25      cvs        20: #define THOT_EXPORT extern
1.4       cvs        21: #include "amaya.h"
1.70      cvs        22: #ifdef _WINDOWS 
                     23: #include <fcntl.h>
                     24: #endif /* _WINDOWS */
1.7       cvs        25: 
1.4       cvs        26: #if defined(__svr4__)
                     27: #define CATCH_SIG
                     28: #endif
                     29: 
                     30: /* local structures coming from libwww and which are
1.17      cvs        31:    not found in any .h file
1.7       cvs        32:  */
1.68      cvs        33: 
1.7       cvs        34: struct _HTStream
                     35:   {
1.15      cvs        36:      const HTStreamClass *isa;
                     37:      FILE               *fp;
1.45      cvs        38:      BOOL                leave_open;   /* Close file when TtaFreeMemory? */
1.15      cvs        39:      char               *end_command;  /* Command to execute       */
                     40:      BOOL                remove_on_close;      /* Remove file?             */
                     41:      char               *filename;     /* Name of file             */
                     42:      HTRequest          *request;      /* saved for callback       */
                     43:      HTRequestCallback  *callback;
1.7       cvs        44:   };
                     45: 
1.15      cvs        46: 
1.7       cvs        47: struct _HTError
                     48:   {
                     49:      HTErrorElement      element;      /* Index number into HTError */
                     50:      HTSeverity          severity;     /* A la VMS */
                     51:      BOOL                ignore;       /* YES if msg should not go to user */
                     52:      void               *par;  /* Explanation, e.g. filename  */
                     53:      int                 length;       /* For copying by generic routine */
                     54:      char               *where;        /* Which function */
                     55:   };
                     56: 
                     57: 
                     58: struct _HTHost
                     59:   {
                     60:      char               *hostname;     /* name of host + optional port */
                     61:      time_t              ntime;        /* Creation time */
                     62:      char               *type; /* Peer type */
                     63:      int                 version;      /* Peer version */
                     64:      HTMethod            methods;      /* Public methods (bit-flag) */
                     65:      char               *server;       /* Server name */
                     66:      char               *user_agent;   /* User Agent */
                     67:      char               *range_units;  /* Acceptable range units */
                     68:      HTTransportMode     mode; /* Supported mode */
                     69:      HTChannel          *channel;      /* Persistent channel */
                     70:      HTList             *pipeline;     /* Pipe line of net objects */
                     71:      HTList             *pending;      /* List of pending Net objects */
                     72:      time_t              expires;      /* Persistent channel expires time */
                     73:   };
1.4       cvs        74: 
                     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.24      cvs        85: static  boolean    AmayaIsAlive;
1.4       cvs        86: 
1.15      cvs        87: #include "answer_f.h"
                     88: #include "query_f.h"
                     89: #include "AHTURLTools_f.h"
                     90: #include "AHTBridge_f.h"
                     91: #include "AHTMemConv_f.h"
                     92: #include "AHTFWrite_f.h"
1.4       cvs        93: 
1.70      cvs        94: #ifdef _WINDOWS
                     95: #ifdef __STDC__
                     96: int WIN_Activate_Request (HTRequest* , HTAlertOpcode, int, const char*, void*, HTAlertPar*);
                     97: #else
                     98: int WIN_Activate_Request ();
                     99: #endif /* __STDC__ */
                    100: #endif /* _WINDOWS */
1.15      cvs       101: 
                    102: /*----------------------------------------------------------------------
1.17      cvs       103:   GetDocIdStatus
                    104:   gets the status associated to a docid                         
1.15      cvs       105:   ----------------------------------------------------------------------*/
                    106: #ifdef __STDC__
                    107: AHTDocId_Status    *GetDocIdStatus (int docid, HTList * documents)
                    108: #else
                    109: AHTDocID_Status    *GetDocIdStatus (docid, documents)
                    110: int                 docid;
                    111: HTList             *documents;
                    112: 
                    113: #endif
                    114: {
                    115:    AHTDocId_Status    *me;
                    116:    HTList             *cur;
                    117: 
                    118:    if (documents)
                    119:      {
                    120:        cur = documents;
                    121: 
                    122:        while ((me = (AHTDocId_Status *) HTList_nextObject (cur)))
                    123:          {
                    124:             if (me->docid == docid)
                    125:                return (me);
1.18      cvs       126:          }
                    127:      }
1.15      cvs       128:    return (AHTDocId_Status *) NULL;
                    129: 
                    130: }
                    131: 
1.5       cvs       132: /*----------------------------------------------------------------------
1.27      cvs       133:   AHTGuessAtom_for
                    134:   Converts an Amaya type descriptor into the equivalent MIME type.
                    135:   ----------------------------------------------------------------------*/
                    136: #ifdef __STDC__
                    137: static  HTAtom *AHTGuessAtom_for (char *urlName, PicType contentType)
                    138: #else
                    139: static  HTAtom *AHTGuessAtom_for (urlName, contentType)
                    140: char *urlName;
                    141: PicType contentType;
                    142: #endif
                    143: {
                    144:  HTAtom           *atom;
                    145:  char             *filename;
                    146:  HTEncoding        enc;
                    147:  HTEncoding        cte;
                    148:  HTLanguage        lang;
1.50      cvs       149:  double            quality = 1.0;
1.27      cvs       150: 
                    151:  switch (contentType)
                    152:    {
                    153:     case xbm_type:
                    154:       atom = HTAtom_for("image/xbm");
                    155:       break;
                    156:     case eps_type:
1.35      cvs       157:       atom = HTAtom_for("application/postscript");
1.27      cvs       158:       break;
                    159:    case xpm_type:
                    160:       atom = HTAtom_for("image/xpm");
                    161:      break;
                    162:     case gif_type:
                    163:       atom = HTAtom_for("image/gif");
                    164:       break;
                    165:     case jpeg_type:
                    166:       atom = HTAtom_for("image/jpeg");
                    167:       break;
                    168:     case png_type:
                    169:       atom = HTAtom_for("image/png");
                    170:       break;
                    171:    case unknown_type:
                    172:    default:
                    173:      /* 
                    174:      ** Amaya could not detect the type, so 
                    175:      ** we try to use the filename's suffix to do so.
                    176:      */
1.45      cvs       177:      filename = AmayaParseUrl (urlName, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.27      cvs       178:      HTBind_getFormat (filename, &atom, &enc, &cte, &lang, &quality);
1.45      cvs       179:      TtaFreeMemory (filename);
1.27      cvs       180:      if (atom ==  WWW_UNKNOWN)
                    181:         /*
                    182:         ** we could not identify the suffix, so we assign it
                    183:         ** a default type
                    184:         */
                    185:         atom = HTAtom_for ("text/html");
                    186:      break;
                    187:    }
                    188: 
                    189:  return atom;
                    190: }
                    191: 
                    192: /*----------------------------------------------------------------------
1.17      cvs       193:   AHTReqContext_new
                    194:   create a new Amaya Context Object and update the global Amaya
                    195:   request status.
1.5       cvs       196:   ----------------------------------------------------------------------*/
1.4       cvs       197: #ifdef __STDC__
                    198: static AHTReqContext *AHTReqContext_new (int docid)
                    199: #else
                    200: static AHTReqContext *AHTReqContext_new (docid)
                    201: int                 docid;
                    202: 
                    203: #endif
                    204: {
                    205:    AHTReqContext      *me;
                    206:    AHTDocId_Status    *docid_status;
                    207: 
                    208:    if ((me = (AHTReqContext *) TtaGetMemory (sizeof (AHTReqContext))) == NULL)
                    209:       outofmem (__FILE__, "Context_new");
                    210: 
1.81      cvs       211:    /* Bind the Context object together with the Request Object */
                    212:    me->request = HTRequest_new ();
                    213:   
1.80      cvs       214:    /* clean the associated file structure) */
1.79      cvs       215:    HTRequest_setOutputStream (me->request, NULL);
1.81      cvs       216:  
1.4       cvs       217:    /* Initialize the other members of the structure */
1.17      cvs       218:    me->reqStatus = HT_NEW; /* initial status of a request */
1.4       cvs       219:    me->output = NULL;
1.51      cvs       220: #  ifndef _WINDOWS
1.4       cvs       221:    me->read_xtinput_id = (XtInputId) NULL;
                    222:    me->write_xtinput_id = (XtInputId) NULL;
                    223:    me->except_xtinput_id = (XtInputId) NULL;
1.51      cvs       224: #  endif
1.4       cvs       225:    me->docid = docid;
                    226:    HTRequest_setMethod (me->request, METHOD_GET);
                    227:    HTRequest_setOutputFormat (me->request, WWW_SOURCE);
                    228:    HTRequest_setContext (me->request, me);
                    229:    me->read_ops = 0;
                    230:    me->write_ops = 0;
                    231:    me->except_ops = 0;
1.36      cvs       232:    /* experimental */
                    233:    me->read_sock = INVSOC;
                    234:    me->write_sock = INVSOC;
                    235:    me->except_sock = INVSOC;
1.4       cvs       236: 
                    237:    /* Update the global context */
                    238:    HTList_appendObject (Amaya->reqlist, (void *) me);
                    239: 
                    240:    docid_status = GetDocIdStatus (docid, Amaya->docid_status);
                    241: 
1.7       cvs       242:    if (docid_status == NULL)
                    243:      {
                    244:        docid_status = (AHTDocId_Status *) TtaGetMemory (sizeof (AHTDocId_Status));
                    245:        docid_status->docid = docid;
                    246:        docid_status->counter = 1;
                    247:        HTList_addObject (Amaya->docid_status, (void *) docid_status);
                    248:      }
                    249:    else
1.4       cvs       250:       docid_status->counter++;
                    251: 
                    252: 
                    253:    Amaya->open_requests++;
                    254: 
                    255:    /* error stream handling */
                    256:    me->error_stream = (char *) NULL;
                    257:    me->error_stream_size = 0;
1.74      cvs       258: 
                    259: #ifdef DEBUG_LIBWWW
                    260:    fprintf (stderr, "AHTReqContext_new: Created object %p\n", me);
                    261: #endif   
1.4       cvs       262:    return me;
1.74      cvs       263: 
1.4       cvs       264: }
                    265: 
1.5       cvs       266: /*----------------------------------------------------------------------
1.17      cvs       267:   AHTReqContext_delete
                    268:   Delete an Amaya Context Object and update the global Amaya request
                    269:   status.
1.5       cvs       270:   ----------------------------------------------------------------------*/
1.4       cvs       271: 
                    272: #ifdef __STDC__
1.15      cvs       273: boolean   AHTReqContext_delete (AHTReqContext * me)
1.4       cvs       274: #else
1.15      cvs       275: boolean   AHTReqContext_delete (me)
1.4       cvs       276: AHTReqContext      *me;
                    277: 
                    278: #endif
                    279: {
                    280:    AHTDocId_Status    *docid_status;
                    281: 
1.7       cvs       282:    if (me)
                    283:      {
1.4       cvs       284: 
1.74      cvs       285: #ifdef DEBUG_LIBWWW
                    286:         fprintf (stderr, "AHTReqContext_delete: Deleting object %p\n", me);
                    287: #endif   
                    288: 
1.7       cvs       289:        if (Amaya->reqlist)
                    290:           HTList_removeObject (Amaya->reqlist, (void *) me);
1.4       cvs       291: 
1.7       cvs       292:        docid_status = GetDocIdStatus (me->docid, Amaya->docid_status);
1.4       cvs       293: 
1.7       cvs       294:        if (docid_status)
                    295:          {
                    296:             docid_status->counter--;
                    297: 
                    298:             if (docid_status->counter == 0)
                    299:               {
                    300:                  HTList_removeObject (Amaya->docid_status, (void *) docid_status);
                    301:                  TtaFreeMemory ((void *) docid_status);
                    302:               }
                    303:          }
1.79      cvs       304: 
                    305:        if (HTRequest_outputStream (me->request))
                    306:          AHTFWriter_FREE (me->request->output_stream);
                    307:          
1.7       cvs       308:        HTRequest_delete (me->request);
1.85      cvs       309: 
                    310:        if (me->output)
                    311:          {     
                    312: #ifdef DEBUG_LIBWWW       
                    313:            fprintf (stderr, "AHTReqContext_delete: URL is  %s, closing "
                    314:                     "FILE %p\n", me->urlName, me->output); 
                    315: #endif
                    316:            fclose (me->output);
                    317:            me->output = NULL;
                    318:          }
1.79      cvs       319:          
1.7       cvs       320:        if (me->error_stream != (char *) NULL)
1.70      cvs       321:          HT_FREE (me->error_stream);
                    322: #     ifndef _WINDOWS
                    323: #     ifdef WWW_XWINDOWS       
1.21      cvs       324:        if (me->read_xtinput_id || me->write_xtinput_id ||
                    325:             me->except_xtinput_id)
                    326:           RequestKillAllXtevents(me);
1.70      cvs       327: #     endif /* WWW_XWINDOWS */
                    328: #     endif /* !_WINDOWS */
1.74      cvs       329: 
                    330:        if ((me->mode & AMAYA_ASYNC) || (me->mode & AMAYA_IASYNC))
                    331:          /* for the ASYNC mode, free the memory we allocated in GetObjectWWW
                    332:             or in PutObjectWWW */
                    333:          {
1.79      cvs       334:             if (me->urlName)
                    335:               TtaFreeMemory (me->urlName);
                    336:             if (me->outputfile)
                    337:               TtaFreeMemory (me->outputfile);
1.74      cvs       338:          }
                    339:    
1.7       cvs       340:        TtaFreeMemory ((void *) me);
1.4       cvs       341: 
1.7       cvs       342:        Amaya->open_requests--;
1.4       cvs       343: 
1.15      cvs       344:        return TRUE;
1.4       cvs       345: 
1.7       cvs       346:      }
1.15      cvs       347:    return FALSE;
1.4       cvs       348: }
                    349: 
                    350: 
1.15      cvs       351: /*----------------------------------------------------------------------
                    352:   AHTUpload_callback
1.17      cvs       353:   callback handler for executing the PUT command
1.15      cvs       354:   ----------------------------------------------------------------------*/
1.4       cvs       355: #ifdef __STDC__
                    356: static int          AHTUpload_callback (HTRequest * request, HTStream * target)
                    357: #else
                    358: static int          AHTUpload_callback (request, target)
                    359: HTRequest          *request;
                    360: HTStream           *target;
                    361: 
                    362: #endif
                    363: {
                    364:    AHTReqContext      *me = HTRequest_context (request);
1.7       cvs       365:    HTParentAnchor     *entity = HTRequest_entityAnchor (request);
                    366:    int                 len = HTAnchor_length (entity);
1.4       cvs       367:    int                 status;
                    368: 
                    369:    /* Send the data down the pipe */
1.7       cvs       370: 
1.4       cvs       371:    status = (*target->isa->put_block) (target, me->mem_ptr, len);
                    372: 
1.7       cvs       373:    if (status == HT_LOADED || status == HT_OK)
                    374:      {
1.13      cvs       375:        if (PROT_TRACE)
                    376:         HTTrace ("Posting Data Target is SAVED\n");
                    377:        (*target->isa->flush) (target);
                    378:        return (HT_LOADED);
1.7       cvs       379:      }
                    380:    if (status == HT_WOULD_BLOCK)
                    381:      {
                    382:        if (PROT_TRACE)
                    383:           HTTrace ("Posting Data Target WOULD BLOCK\n");
1.74      cvs       384: #ifdef _WINDOWS
                    385:        return HT_CONTINUE;
                    386: #else
                    387:        return HT_WOULD_BLOCK;
                    388: #endif /* _WINDOWS */
                    389: 
1.7       cvs       390:        return HT_WOULD_BLOCK;
                    391:      }
                    392:    else if (status == HT_PAUSE)
                    393:      {
                    394:        if (PROT_TRACE)
                    395:           HTTrace ("Posting Data Target PAUSED\n");
                    396:        return HT_PAUSE;
                    397:      }
                    398:    else if (status > 0)
                    399:      {                         /* Stream specific return code */
                    400:        if (PROT_TRACE)
                    401:           HTTrace ("Posting Data. Target returns %d\n", status);
                    402:        return status;
                    403:      }
                    404:    else
                    405:      {                         /* we have a real error */
                    406:        if (PROT_TRACE)
                    407:           HTTrace ("Posting Data Target ERROR %d\n", status);
                    408:        return status;
                    409:      }
1.4       cvs       410: }
                    411: 
1.5       cvs       412: /*----------------------------------------------------------------------
1.17      cvs       413:   Thread_deleteAll
                    414:   this function deletes the whole list of active threads.           
1.5       cvs       415:   ----------------------------------------------------------------------*/
1.4       cvs       416: #ifdef __STDC__
                    417: static void         Thread_deleteAll (void)
                    418: #else
                    419: static void         Thread_deleteAll ()
                    420: #endif
                    421: {
1.21      cvs       422:   HTList             *cur;
                    423:   AHTReqContext      *me;
                    424:   AHTDocId_Status    *docid_status;
                    425: 
1.74      cvs       426:   if (Amaya && Amaya->reqlist)
                    427:     {
                    428:       if (Amaya->open_requests > 0)
                    429: #ifdef DEBUG_LIBWWW
                    430:       fprintf (stderr, "Thread_deleteAll: Killing %d outstanding "
                    431:                       "requests\n", Amaya->open_requests);
                    432: #endif   
                    433:        {
                    434:          cur = Amaya->reqlist;
                    435:          
                    436:          /* erase the requests */
                    437:          while ((me = (AHTReqContext *) HTList_removeLastObject (cur)))
                    438:            {
                    439:              if (me->request)
                    440:                {
1.70      cvs       441: #              ifndef _WINDOWS 
1.74      cvs       442:                  RequestKillAllXtevents (me);
1.70      cvs       443: #              endif /* !_WINDOWS */
1.87    ! cvs       444:              HTRequest_kill (me->request);
1.74      cvs       445:                  AHTReqContext_delete (me);
                    446:                }
                    447:            }           /* while */
                    448:          
                    449:          /* erase the docid_status entities */
                    450:          while ((docid_status = (AHTDocId_Status *) HTList_removeLastObject ((void *) Amaya->docid_status)))
                    451:            TtaFreeMemory ((void *) docid_status);
                    452:          
                    453:        }                       /* if */
1.84      cvs       454:        
1.74      cvs       455:     }
1.4       cvs       456: }
                    457: 
1.5       cvs       458: /*----------------------------------------------------------------------
1.83      cvs       459:   AHTOpen_file
                    460:   ----------------------------------------------------------------------*/
                    461: #ifdef __STDC__
1.85      cvs       462: int                 AHTOpen_file (HTRequest * request)
1.83      cvs       463: #else
1.85      cvs       464: int                 AHTOpen_file (request)
1.83      cvs       465: HTRequest           *request;
                    466: 
                    467: #endif /* __STDC__ */
                    468: {
                    469:   AHTReqContext      *me;      /* current request */
                    470: 
                    471:   me = HTRequest_context (request);
                    472: 
                    473: #ifdef DEBUG_LIBWWW
1.85      cvs       474:   fprintf(stderr, "AHTOpen_file: start\n");
                    475: #endif /* DEBUG_LIBWWW */
                    476: 
                    477:   if (me->reqStatus == HT_ABORT) 
                    478:     {
                    479: #ifdef DEBUG_LIBWWW
                    480:       fprintf(stderr, "AHTOpen_file: caught an abort request, skipping it\n");
1.83      cvs       481: #endif /* DEBUG_LIBWWW */
                    482: 
1.85      cvs       483:       return HT_OK;
                    484:     }
                    485: 
                    486:   if (HTRequest_outputStream (me->request)) 
                    487:     {
                    488: 
                    489: #ifdef DEBUG_LIBWWW
                    490:       fprintf(stderr, "AHTOpen_file: output stream already existed for url %s\n", me->urlName);
                    491: #endif /* DEBUG_LIBWWW */      
                    492:       return HT_OK;
                    493:     }
                    494: 
                    495: #ifdef DEBUG_LIBWWW
                    496:       fprintf(stderr, "AHTOpen_file: opening output stream for url %s\n", me->urlName);
                    497: #endif /* DEBUG_LIBWWW */      
                    498: 
1.83      cvs       499:   if (!(me->output) && 
                    500:       (me->output != stdout) && 
                    501: #ifndef _WINDOWS
                    502:       (me->output = fopen (me->outputfile, "w")) == NULL)
                    503:     {
                    504: #else
                    505:     (me->output = fopen (me->outputfile, "wb")) == NULL) 
                    506:     {
                    507: #endif /* !_WINDOWS */
                    508: 
                    509:       me->outputfile[0] = '\0';        /* file could not be opened */
1.85      cvs       510: #ifdef DEBUG_LIBWWW
                    511:       fprintf(stderr, "AHTOpen_file: couldn't open output stream for url %s\n", me->urlName);
                    512: #endif
1.83      cvs       513:       TtaSetStatus (me->docid, 1, 
                    514:                    TtaGetMessage (AMAYA, AM_CANNOT_CREATE_FILE),
                    515:                    me->outputfile);
                    516:       me->reqStatus = HT_ERR;
                    517:       return (HT_ERROR);
                    518:     }
                    519:          
                    520:   HTRequest_setOutputStream (me->request,
                    521:                             AHTFWriter_new (me->request, 
                    522:                                             me->output, YES));
                    523:   me->reqStatus = HT_WAITING;
                    524:   
                    525:   return HT_OK;
                    526: }
                    527: 
                    528: /*----------------------------------------------------------------------
1.17      cvs       529:   redirection_handler
                    530:   this function is registered to handle permanent and temporary
                    531:   redirections.
                    532:   ----------------------------------------------------------------------*/
1.4       cvs       533: #ifdef __STDC__
1.7       cvs       534: static int          redirection_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       535: #else
                    536: static int          redirection_handler (request, context, status)
                    537: HTRequest          *request;
                    538: HTResponse         *response;
                    539: void               *param;
                    540: int                 status;
                    541: 
                    542: #endif
                    543: {
                    544: 
                    545:    char               *ref;
                    546:    HTAnchor           *new_anchor = HTResponse_redirection (response);
1.7       cvs       547:    AHTReqContext      *me = HTRequest_context (request);
1.4       cvs       548:    HTMethod            method = HTRequest_method (request);
                    549: 
                    550: 
1.7       cvs       551:    if (!new_anchor)
                    552:      {
                    553:        if (PROT_TRACE)
                    554:           HTTrace ("Redirection. No destination\n");
                    555:        return HT_OK;
                    556:      }
1.4       cvs       557: 
                    558:    /*
                    559:       ** Only do redirect on GET and HEAD
                    560:     */
1.7       cvs       561:    if (!HTMethod_isSafe (method))
                    562:      {
                    563:        HTAlertCallback    *prompt = HTAlert_find (HT_A_CONFIRM);
                    564: 
                    565:        if (prompt)
                    566:          {
                    567:             if ((*prompt) (request, HT_A_CONFIRM, HT_MSG_REDIRECTION,
                    568:                            NULL, NULL, NULL) != YES)
                    569:                return HT_ERROR;
                    570:          }
                    571:      }
1.4       cvs       572: 
                    573:    /*
                    574:     **  Start new request with the redirect anchor found in the headers.
                    575:     **  Note that we reuse the same request object which means that we must
                    576:     **  keep this around until the redirected request has terminated. It also           
                    577:     **  allows us in an easy way to keep track of the number of redirections
                    578:     **  so that we can detect endless loops.
                    579:     */
1.17      cvs       580:    
1.7       cvs       581:    if (HTRequest_doRetry (request))
                    582:      {
                    583:        /* do we need to normalize the URL? */
                    584:        if (strncmp (new_anchor->parent->address, "http:", 5))
                    585:          {
                    586:             /* Yes, so we use the pre-redirection anchor as a base name */
1.74      cvs       587:             ref = AmayaParseUrl (new_anchor->parent->address, 
                    588:                                  me->urlName, AMAYA_PARSE_ALL);
1.7       cvs       589:             if (ref)
                    590:               {
1.74      cvs       591:                 TtaFreeMemory (new_anchor->parent->address);
                    592:                 new_anchor->parent->address = ref;
1.7       cvs       593:               }
                    594:          }
                    595: 
                    596:        /* update the current file name */
1.21      cvs       597:        if (strlen (new_anchor->parent->address) > (MAX_LENGTH - 2))
1.7       cvs       598:          {
1.74      cvs       599:             strncpy (me->urlName, new_anchor->parent->address, 
                    600:                      MAX_LENGTH - 1);
1.21      cvs       601:             me->urlName[MAX_LENGTH - 1] = EOS;
1.7       cvs       602:          }
                    603:        else
1.74      cvs       604:          strcpy (me->urlName, new_anchor->parent->address);
1.7       cvs       605: 
1.38      cvs       606:        ChopURL (me->status_urlName, me->urlName);
                    607: 
1.7       cvs       608:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_RED_FETCHING),
1.38      cvs       609:                      me->status_urlName);
1.7       cvs       610: 
                    611:        /* Start request with new credentials */
1.64      cvs       612: 
1.79      cvs       613:        if (HTRequest_outputStream (me->request) != NULL) {
1.85      cvs       614:          AHTFWriter_FREE (request->output_stream);
                    615:          if (me->output != stdout) { /* Are we writing to a file? */
1.74      cvs       616: #ifdef DEBUG_LIBWWW
1.79      cvs       617:          fprintf (stderr, "redirection_handler: New URL is  %s, closing "
1.74      cvs       618:                             "FILE %p\n", me->urlName, me->output); 
                    619: #endif 
1.79      cvs       620:            fclose (me->output);
                    621:            me->output = NULL;
                    622:          }
1.64      cvs       623:        }
1.68      cvs       624: 
1.69      cvs       625:        me->reqStatus = HT_NEW; /* reset the status */
1.7       cvs       626:        if (me->method == METHOD_PUT || me->method == METHOD_POST)      /* PUT, POST etc. */
1.74      cvs       627:          status = HTLoadAbsolute (me->urlName, request);
1.7       cvs       628:        else
1.74      cvs       629:          HTLoadAnchor (new_anchor, request);
1.7       cvs       630:      }
                    631:    else
1.68      cvs       632:      {
1.74      cvs       633:        HTRequest_addError (request, ERR_FATAL, NO, HTERR_MAX_REDIRECT,
                    634:                           NULL, 0, "HTRedirectFilter");
                    635:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REDIRECTIONS_LIMIT),
                    636:                     NULL);
                    637:        if (me->error_html)
                    638:         DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR; 
                    639:        /* so that we can show the error message */
1.68      cvs       640:      }
                    641: 
1.4       cvs       642:    /*
1.74      cvs       643:    **  By returning HT_ERROR we make sure that this is the last handler to be
                    644:    **  called. We do this as we don't want any other filter to delete the 
                    645:    **  request object now when we have just started a new one ourselves
                    646:    */
1.4       cvs       647:    return HT_ERROR;
                    648: }
                    649: 
1.5       cvs       650: /*----------------------------------------------------------------------
1.17      cvs       651:   terminate_handler
                    652:   this function is registered to handle the result of the request
1.5       cvs       653:   ----------------------------------------------------------------------*/
1.4       cvs       654: #if __STDC__
1.7       cvs       655: static int          terminate_handler (HTRequest * request, HTResponse * response, void *context, int status)
1.4       cvs       656: #else
                    657: static int          terminate_handler (request, response, context, status)
                    658: HTRequest          *request;
                    659: HTResponse         *response;
                    660: void               *context;
                    661: int                 status;
                    662: #endif
                    663: {
                    664:    AHTReqContext      *me = (AHTReqContext *) HTRequest_context (request);
1.13      cvs       665:    boolean             error_flag;
1.4       cvs       666: 
                    667:    if (!me)
1.74      cvs       668:      return HT_OK;             /* not an Amaya request */
                    669:    
1.80      cvs       670:    /* if Amaya was killed, treat with this request as if it were
                    671:       issued by a Stop button event */
                    672:    if (!AmayaIsAlive)           
                    673:       me->reqStatus = HT_ABORT; 
1.74      cvs       674:    
1.77      cvs       675:    if (status == HT_LOADED || 
                    676:        status == HT_CREATED || 
1.79      cvs       677:        status == HT_NO_DATA ||
                    678:        me->reqStatus == HT_ABORT)
1.74      cvs       679:      error_flag = FALSE;
1.13      cvs       680:    else
1.74      cvs       681:      error_flag = TRUE;
                    682:    
1.4       cvs       683:    /* output any errors from the server */
1.74      cvs       684:    
1.77      cvs       685:    /*
1.74      cvs       686:    ** me->output = output file which will receive an html file
                    687:    ** me->error_html = yes, output HTML errors in the screen
                    688:    ** request->error_stack == if there are any errors, they will be here
                    689:    ** me->error_stream_size If it's != 0 means an error message has already
                    690:    **                       been written to the stack
                    691:    */
                    692:    
1.4       cvs       693:    /* First, we verify if there are any errors and if they are not
1.17      cvs       694:    ** yet written to the error stack. If no, then let's try to write them
                    695:    ** ourselves
                    696:    */
1.74      cvs       697:    
                    698: #ifdef DEBUG_LIBWWW
                    699:    fprintf (stderr, "terminate_handler: URL is "
                    700:            "%s, closing FILE %p status is %d\n", me->urlName, me->output, 
                    701:            status); 
1.69      cvs       702: #endif
1.74      cvs       703:    
1.69      cvs       704:    if (me->output && me->output != stdout)
                    705:      {
1.74      cvs       706:        /* we are writing to a file */
                    707:        if (me->reqStatus != HT_ABORT)
                    708:         {                      /* if the request was not aborted and */
1.80      cvs       709:           if (error_flag &&
                    710:               me->error_html == TRUE)
                    711:               /* there were some errors and we want to print them */
                    712:             {          
                    713:               if (me->error_stream_size == 0)/* and the stream is empty */
                    714:                 AHTError_MemPrint (request); /* copy errors from 
1.74      cvs       715:                                                  **the error stack 
1.77      cvs       716:                                                  ** into the error stream */
1.80      cvs       717:               if (me->error_stream)
                    718:                 {      /* if the stream is non-empty */
                    719:                   fprintf (me->output, me->error_stream);/* output the errors */
1.85      cvs       720:                   /* Clear the error context, so that we can deal with
                    721:                      this answer as if it were a normal reply */
                    722:                    HTError_deleteAll( HTRequest_error (request));
                    723:                    HTRequest_setError (request, NULL);
                    724:                    error_flag = 0;
1.74      cvs       725:                 }
                    726:             }                  /* if error_stack */
1.85      cvs       727:         }
                    728: 
                    729:        /* if != HT_ABORT */
                    730:        
                    731: #ifdef DEBUG_LIBWWW       
                    732:        fprintf (stderr, "terminate_handler: URL is  %s, closing "
                    733:                "FILE %p\n", me->urlName, me->output); 
                    734: #endif
1.74      cvs       735:        fclose (me->output);
                    736:        me->output = NULL;
1.69      cvs       737:      }
1.80      cvs       738: 
1.74      cvs       739:    
1.80      cvs       740:    if (error_flag)
                    741:      me->reqStatus = HT_ERR;
                    742:    else if (me->reqStatus != HT_ABORT)
                    743:      me->reqStatus = HT_END;
                    744: 
                    745:   /* don't remove or Xt will hang up during the PUT */
                    746: 
1.28      cvs       747:    if (AmayaIsAlive  && ((me->method == METHOD_POST) ||
                    748:                         (me->method == METHOD_PUT)))
1.7       cvs       749:      {
1.80      cvs       750:        PrintTerminateStatus (me, status);
                    751: 
                    752:      } 
                    753: 
1.72      cvs       754: #ifdef _WINDOWS
1.80      cvs       755:    /* Try to add this to AHTEventrg.c */
                    756:    ProcessTerminateRequest (me);
                    757: #endif /* WINDOWS */
1.71      cvs       758: 
                    759:   return HT_OK;
1.4       cvs       760: }
                    761: 
1.5       cvs       762: /*----------------------------------------------------------------------
1.17      cvs       763:   AHTLoadTerminate_handler
1.74      cvs       764:   this is an application "AFTER" Callback. It's called by the library
                    765:   when a request has ended, so that we can setup the correct status.
1.5       cvs       766:   ----------------------------------------------------------------------*/
1.4       cvs       767: 
                    768: #ifdef __STDC__
1.7       cvs       769: static int          AHTLoadTerminate_handler (HTRequest * request, HTResponse * response, void *param, int status)
1.4       cvs       770: #else
                    771: static int          AHTLoadTerminate_handler (request, response, param, status)
                    772: HTRequest          *request;
                    773: HTResponse         *response;
                    774: void               *param;
                    775: int                 status;
1.69      cvs       776: 
1.4       cvs       777: #endif
                    778: {
                    779:    AHTReqContext      *me = HTRequest_context (request);
                    780:    HTAlertCallback    *cbf;
                    781:    AHTDocId_Status    *docid_status;
                    782: 
1.7       cvs       783:    switch (status)
                    784:         {
                    785:            case HT_LOADED:
                    786:               if (PROT_TRACE)
                    787:                  HTTrace ("Load End.... OK: `%s\' has been accessed\n",
1.38      cvs       788:                           me->status_urlName);
1.4       cvs       789: 
1.7       cvs       790:               docid_status = GetDocIdStatus (me->docid,
                    791:                                              Amaya->docid_status);
                    792: 
                    793:               if (docid_status != NULL && docid_status->counter > 1)
1.74      cvs       794:                  TtaSetStatus (me->docid, 1, 
                    795:                                TtaGetMessage (AMAYA, AM_ELEMENT_LOADED),
                    796:                                me->status_urlName);
1.7       cvs       797:               break;
                    798: 
                    799:            case HT_NO_DATA:
                    800:               if (PROT_TRACE)
1.74      cvs       801:                  HTTrace ("Load End.... OK BUT NO DATA: `%s\'\n", 
                    802:                           me->status_urlName);
                    803:               TtaSetStatus (me->docid, 1, 
                    804:                             TtaGetMessage (AMAYA, AM_LOADED_NO_DATA),
1.38      cvs       805:                             me->status_urlName);
1.7       cvs       806:               break;
                    807: 
                    808:            case HT_INTERRUPTED:
                    809:               if (PROT_TRACE)
1.74      cvs       810:                  HTTrace ("Load End.... INTERRUPTED: `%s\'\n", 
                    811:                           me->status_urlName);
                    812:               TtaSetStatus (me->docid, 1, 
                    813:                             TtaGetMessage (AMAYA, AM_LOAD_ABORT), 
                    814:                             NULL);
1.7       cvs       815:               break;
                    816: 
                    817:            case HT_RETRY:
                    818:               if (PROT_TRACE)
                    819:                  HTTrace ("Load End.... NOT AVAILABLE, RETRY AT %ld\n",
                    820:                           HTResponse_retryTime (response));
1.74      cvs       821:               TtaSetStatus (me->docid, 1, 
                    822:                             TtaGetMessage (AMAYA, AM_NOT_AVAILABLE_RETRY),
1.38      cvs       823:                             me->status_urlName);
1.7       cvs       824:               break;
                    825: 
                    826:            case HT_ERROR:
                    827: 
                    828:               cbf = HTAlert_find (HT_A_MESSAGE);
                    829:               if (cbf)
                    830:                  (*cbf) (request, HT_A_MESSAGE, HT_MSG_NULL, NULL,
                    831:                          HTRequest_error (request), NULL);
                    832:               break;
                    833: 
                    834:               if (PROT_TRACE)
                    835:                  HTTrace ("Load End.... ERROR: Can't access `%s\'\n",
1.74      cvs       836:                           me->status_urlName ? me->status_urlName :"<UNKNOWN>");
                    837:               TtaSetStatus (me->docid, 1,
                    838:                             TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
1.38      cvs       839:                             me->status_urlName ? me->status_urlName : "<UNKNOWN>");
1.7       cvs       840:               break;
                    841:            default:
                    842:               if (PROT_TRACE)
                    843:                  HTTrace ("Load End.... UNKNOWN RETURN CODE %d\n", status);
                    844:               break;
                    845:         }
                    846: 
1.4       cvs       847:    return HT_OK;
                    848: }
                    849: 
1.27      cvs       850: /*----------------------------------------------------------------------
                    851:   AHTAcceptTypesInit
1.74      cvs       852:   This function prepares the Accept header used by Amaya during
                    853:   the HTTP content negotiation phase
1.27      cvs       854:   ----------------------------------------------------------------------*/
                    855: #ifdef __STDC__
                    856: static void           AHTAcceptTypesInit (HTList *c)
                    857: #else  /* __STDC__ */
                    858: static void           AHTAcceptTypesInit (c)
                    859: HTList             *c;
                    860: #endif /* __STDC__ */
                    861: {
                    862:   if (c == (HTList *) NULL) 
                    863:       return;
                    864: 
                    865:       /* define here all the mime types that Amaya can accept */
                    866: 
1.74      cvs       867:       HTConversion_add (c, "image/png",  "www/present", 
                    868:                        HTThroughLine, 1.0, 0.0, 0.0);
                    869:       HTConversion_add (c, "image/jpeg", "www/present", 
                    870:                        HTThroughLine, 1.0, 0.0, 0.0);
                    871:       HTConversion_add (c, "image/gif",  "www/present", 
                    872:                        HTThroughLine, 1.0, 0.0, 0.0);
                    873:       HTConversion_add (c, "image/xbm",  "www/present", 
                    874:                        HTThroughLine, 1.0, 0.0, 0.0);
                    875:       HTConversion_add (c, "image/xpm",  "www/present", 
                    876:                        HTThroughLine, 1.0, 0.0, 0.0);
                    877:       HTConversion_add (c, "application/postscript",  
                    878:                        "www/present", HTThroughLine, 1.0, 0.0, 0.0);
1.4       cvs       879: 
1.27      cvs       880:    /* Define here the equivalences between MIME types and file extensions for
                    881:     the types that Amaya can display */
                    882: 
                    883:    /* Register the default set of file suffix bindings */
                    884:    HTFileInit ();
                    885: 
                    886:    /* Don't do any case distinction */
                    887:    HTBind_caseSensitive (FALSE);
                    888: }
1.4       cvs       889: 
1.5       cvs       890: /*----------------------------------------------------------------------
1.17      cvs       891:   AHTConverterInit
                    892:   Bindings between a source media type and a destination media type
                    893:   (conversion).
1.5       cvs       894:   ----------------------------------------------------------------------*/
1.15      cvs       895: #ifdef __STDC__
                    896: static void         AHTConverterInit (HTList *c)
                    897: #else  /* __STDC__ */
                    898: static void         AHTConverterInit (c)
                    899: HTList             *c;
                    900: #endif /* __STDC__ */
1.4       cvs       901: {
                    902: 
                    903:    /* Handler for custom http error messages */
1.7       cvs       904:    HTConversion_add (c, "*/*", "www/debug", AHTMemConverter, 1.0, 0.0, 0.0);
1.4       cvs       905: 
                    906:    /*
                    907:     ** These are converters that converts to something other than www/present,
                    908:     ** that is not directly outputting someting to the user on the screen
                    909:     */
                    910: 
                    911:    HTConversion_add (c, "message/rfc822", "*/*", HTMIMEConvert, 1.0, 0.0, 0.0);
                    912:    HTConversion_add (c, "message/x-rfc822-foot", "*/*", HTMIMEFooter,
                    913:                     1.0, 0.0, 0.0);
                    914:    HTConversion_add (c, "message/x-rfc822-head", "*/*", HTMIMEHeader,
                    915:                     1.0, 0.0, 0.0);
                    916:    HTConversion_add (c, "multipart/*", "*/*", HTBoundary,
                    917:                     1.0, 0.0, 0.0);
                    918:    HTConversion_add (c, "text/plain", "text/html", HTPlainToHTML,
                    919:                     1.0, 0.0, 0.0);
                    920: 
                    921: 
                    922:    /*
                    923:       ** The following conversions are converting ASCII output from various
                    924:       ** protocols to HTML objects.
                    925:     */
                    926:    HTConversion_add (c, "text/x-http", "*/*", HTTPStatus_new,
                    927:                     1.0, 0.0, 0.0);
                    928:    HTConversion_add (c, "text/x-nntp-list", "*/*", HTNewsList,
                    929:                     1.0, 0.0, 0.0);
                    930:    HTConversion_add (c, "text/x-nntp-over", "*/*", HTNewsGroup,
                    931:                     1.0, 0.0, 0.0);
                    932: 
                    933: 
                    934:    /*
                    935:     ** We also register a special content type guess stream that can figure out
                    936:     ** the content type by reading the first bytes of the stream
                    937:     */
                    938:    HTConversion_add (c, "www/unknown", "*/*", HTGuess_new,
                    939:                     1.0, 0.0, 0.0);
                    940: 
                    941:    /*
                    942:       ** Register a persistent cache stream which can save an object to local
                    943:       ** file
                    944:     */
                    945:    HTConversion_add (c, "www/cache", "*/*", HTCacheWriter,
                    946:                     1.0, 0.0, 0.0);
                    947: 
                    948:    /*
                    949:       ** This dumps all other formats to local disk without any further
                    950:       ** action taken
                    951:     */
                    952:    HTConversion_add (c, "*/*", "www/present", HTSaveLocally,
                    953:                     0.3, 0.0, 0.0);
                    954: 
                    955: }
                    956: 
1.27      cvs       957: 
1.15      cvs       958: /*----------------------------------------------------------------------
1.17      cvs       959:   AHTProtocolInit
                    960:   Registers all amaya supported protocols.
1.15      cvs       961:   ----------------------------------------------------------------------*/
1.4       cvs       962: static void         AHTProtocolInit (void)
                    963: {
                    964: 
1.17      cvs       965:    /* 
1.74      cvs       966:       NB. Preemptive == YES means Blocking requests
                    967:       Non-preemptive == NO means Non-blocking requests
1.17      cvs       968:    */
1.4       cvs       969: 
1.63      cvs       970:    HTProtocol_add ("http", "buffered_tcp", NO, HTLoadHTTP, NULL);
1.4       cvs       971:    /*   HTProtocol_add ("http", "tcp", NO, HTLoadHTTP, NULL); */
                    972:    HTProtocol_add ("file", "local", NO, HTLoadFile, NULL);
                    973:    HTProtocol_add ("cache", "local", NO, HTLoadCache, NULL);
1.36      cvs       974:    HTProtocol_add ("ftp", "tcp", NO, HTLoadFTP, NULL);
1.17      cvs       975: #if 0 /* experimental code */
1.4       cvs       976:    HTProtocol_add ("telnet", "", YES, HTLoadTelnet, NULL);
                    977:    HTProtocol_add ("tn3270", "", YES, HTLoadTelnet, NULL);
                    978:    HTProtocol_add ("rlogin", "", YES, HTLoadTelnet, NULL);
                    979:    HTProtocol_add ("nntp", "tcp", NO, HTLoadNews, NULL);
                    980:    HTProtocol_add ("news", "tcp", NO, HTLoadNews, NULL);
1.17      cvs       981: #endif
1.4       cvs       982: }
                    983: 
1.15      cvs       984: /*----------------------------------------------------------------------
1.17      cvs       985:   AHTNetInit
                    986:   Reegisters "before" and "after" request filters.
1.15      cvs       987:   ----------------------------------------------------------------------*/
1.4       cvs       988: static void         AHTNetInit (void)
                    989: {
                    990: 
                    991: /*      Register BEFORE filters
1.74      cvs       992: **      The BEFORE filters handle proxies, caches, rule files etc.
                    993: **      The filters are called in the order by which the are registered
                    994: **      Not done automaticly - may be done by application!
                    995: */
                    996:   
1.4       cvs       997: 
1.74      cvs       998:   HTNet_addBefore (HTCredentialsFilter, "http://*", NULL, 6);
                    999:   HTNet_addBefore (HTProxyFilter, NULL, NULL, 10);
1.85      cvs      1000:   /*  HTNet_addBefore (AHTOpen_file, NULL, NULL, 11); */
                   1001:   HTHost_setActivateRequestCallback (AHTOpen_file);
1.4       cvs      1002: 
                   1003: /*      register AFTER filters
1.74      cvs      1004: **      The AFTER filters handle error messages, logging, redirection,
                   1005: **      authentication etc.
                   1006: **      The filters are called in the order by which the are registered
                   1007: **      Not done automaticly - may be done by application!
                   1008: */
1.4       cvs      1009: 
1.82      cvs      1010:    HTNet_addAfter (HTAuthFilter, "http://*", NULL, HT_NO_ACCESS, HT_FILTER_MIDDLE);
                   1011:    HTNet_addAfter (redirection_handler, "http://*", NULL, HT_TEMP_REDIRECT, HT_FILTER_MIDDLE);
                   1012:    HTNet_addAfter (redirection_handler, "http://*", NULL, HT_PERM_REDIRECT, HT_FILTER_MIDDLE);
                   1013:    HTNet_addAfter (HTUseProxyFilter, "http://*", NULL, HT_USE_PROXY, HT_FILTER_MIDDLE);
1.51      cvs      1014:    HTNet_addAfter (AHTLoadTerminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);      
                   1015:    /* handles all errors */
1.7       cvs      1016:    HTNet_addAfter (terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);
1.4       cvs      1017: }
                   1018: 
1.15      cvs      1019: /*----------------------------------------------------------------------
1.17      cvs      1020:   AHTAlertInit
                   1021:   Register alert messages and their callbacks.
1.15      cvs      1022:   ----------------------------------------------------------------------*/
                   1023: #ifdef __STDC__
                   1024: static void         AHTAlertInit (void)
                   1025: #else
                   1026: static void         AHTAlertInit ()
                   1027: #endif
                   1028: {
                   1029:    HTAlert_add (AHTProgress, HT_A_PROGRESS);
1.70      cvs      1030: #  ifndef _WINDOWS
1.83      cvs      1031:    /*   HTAlert_add ((HTAlertCallback *) Add_NewSocket_to_Loop, HT_PROG_CONNECT); */
1.70      cvs      1032: #  else  /* _WINDOWS */
                   1033:    HTAlert_add ((HTAlertCallback *) WIN_Activate_Request, HT_PROG_CONNECT);
                   1034: #  endif /* _WINDOWS */
1.15      cvs      1035:    HTAlert_add (AHTError_print, HT_A_MESSAGE);
1.48      cvs      1036:    HTError_setShow (~((unsigned int) 0 ) & ~((unsigned int) HT_ERR_SHOW_DEBUG));       /* process all messages except debug ones*/
1.15      cvs      1037:    HTAlert_add (AHTConfirm, HT_A_CONFIRM);
                   1038:    HTAlert_add (AHTPrompt, HT_A_PROMPT);
                   1039:    HTAlert_add (AHTPromptPassword, HT_A_SECRET);
                   1040:    HTAlert_add (AHTPromptUsernameAndPassword, HT_A_USER_PW);
                   1041: }
                   1042: 
                   1043: /*----------------------------------------------------------------------
1.17      cvs      1044:   AHTProfile_newAmaya
                   1045:   creates the Amaya client profile for libwww.
1.15      cvs      1046:   ----------------------------------------------------------------------*/
                   1047: #ifdef __STDC__
                   1048: static void         AHTProfile_newAmaya (char *AppName, char *AppVersion)
                   1049: #else  /* __STDC__ */
                   1050: static void         AHTProfile_newAmaya (AppName, AppVersion)
                   1051: char               *AppName;
                   1052: char               *AppVersion;
                   1053: #endif /* __STDC__ */
1.4       cvs      1054: {
                   1055:    /* If the Library is not already initialized then do it */
                   1056:    if (!HTLib_isInitialized ())
                   1057:       HTLibInit (AppName, AppVersion);
                   1058: 
                   1059:    if (!converters)
                   1060:       converters = HTList_new ();
1.27      cvs      1061:    if (!acceptTypes)
                   1062:       acceptTypes = HTList_new ();
1.4       cvs      1063:    if (!encodings)
                   1064:       encodings = HTList_new ();
                   1065: 
                   1066:    /* Register the default set of transport protocols */
                   1067:    HTTransportInit ();
                   1068: 
                   1069:    /* Register the default set of application protocol modules */
                   1070:    AHTProtocolInit ();
                   1071: 
                   1072:    /* Enable the persistent cache */
                   1073:    /*   HTCacheInit (NULL, 20); */
                   1074: 
                   1075:    /* Register the default set of BEFORE and AFTER filters */
                   1076:    AHTNetInit ();
                   1077: 
                   1078:    /* Set up the default set of Authentication schemes */
                   1079:    HTAAInit ();
                   1080: 
                   1081:    /* Get any proxy or gateway environment variables */
                   1082:    HTProxy_getEnvVar ();
                   1083: 
                   1084:    /* Register the default set of converters */
                   1085:    AHTConverterInit (converters);
1.27      cvs      1086:    AHTAcceptTypesInit (acceptTypes);
1.4       cvs      1087:    HTFormat_setConversion (converters);
                   1088: 
                   1089:    /* Register the default set of transfer encoders and decoders */
                   1090:    HTEncoderInit (encodings);  /* chunks ??? */
                   1091:    HTFormat_setTransferCoding (encodings);
                   1092: 
                   1093:    /* Register the default set of MIME header parsers */
1.74      cvs      1094:    HTMIMEInit ();   /* must be called again for language selector */
1.4       cvs      1095: 
                   1096:    /* Register the default set of Icons for directory listings */
1.27      cvs      1097:    /*HTIconInit(NULL); *//* experimental */
1.4       cvs      1098: 
                   1099:    /* Register the default set of messages and dialog functions */
                   1100:    AHTAlertInit ();
                   1101:    HTAlert_setInteractive (YES);
                   1102: }
                   1103: 
1.5       cvs      1104: /*----------------------------------------------------------------------
1.17      cvs      1105:   AHTProfile_delete
                   1106:   deletes the Amaya client profile.
1.5       cvs      1107:   ----------------------------------------------------------------------*/
1.4       cvs      1108: #ifdef __STDC__
                   1109: static void         AHTProfile_delete (void)
                   1110: #else
                   1111: static void         AHTProfile_delete ()
1.7       cvs      1112: #endif                         /* __STDC__ */
1.4       cvs      1113: {
1.22      cvs      1114:  
                   1115:   /* free the Amaya global context */
1.74      cvs      1116:   if (!converters)
                   1117:     HTConversion_deleteAll (converters);
                   1118:   if (!acceptTypes)
                   1119:     HTConversion_deleteAll (acceptTypes);
                   1120:   if (!encodings)
                   1121:     HTCoding_deleteAll (encodings);
                   1122:   
                   1123:   HTList_delete (Amaya->docid_status);
                   1124:   HTList_delete (Amaya->reqlist);
                   1125:   TtaFreeMemory (Amaya);
                   1126:   {
1.61      cvs      1127: 
1.74      cvs      1128:     if (HTLib_isInitialized ())
                   1129:       
1.61      cvs      1130: #  ifdef _WINDOWS
                   1131:       HTEventTerminate ();
                   1132: #  endif _WINDOWS;             
1.74      cvs      1133:     
                   1134:     /* Clean up the persistent cache (if any) */
                   1135:     HTCacheTerminate ();
                   1136:     
                   1137:     /* Clean up all the global preferences */
                   1138:     HTFormat_deleteAll ();
                   1139:     
                   1140:     /* Terminate libwww */
                   1141:     HTLibTerminate ();
                   1142:   }
1.4       cvs      1143: }
                   1144: 
1.5       cvs      1145: /*----------------------------------------------------------------------
1.17      cvs      1146:   QueryInit
                   1147:   initializes the libwww interface 
1.5       cvs      1148:   ----------------------------------------------------------------------*/
1.4       cvs      1149: #ifdef __STDC__
                   1150: void                QueryInit ()
                   1151: #else
                   1152: void                QueryInit ()
                   1153: #endif
                   1154: {
                   1155: 
1.24      cvs      1156:    AmayaIsAlive = TRUE;
1.4       cvs      1157:    AHTProfile_newAmaya (HTAppName, HTAppVersion);
                   1158: 
1.69      cvs      1159:    /* New AHTBridge stuff */
1.4       cvs      1160: 
1.54      cvs      1161: #  ifdef _WINDOWS
1.75      cvs      1162:    AHTEventInit ();
1.54      cvs      1163: #  endif _WINDOWS;
1.72      cvs      1164: 
1.70      cvs      1165:    HTEvent_setRegisterCallback (AHTEvent_register);
1.78      cvs      1166:    /*** a effacer ***/
                   1167:    HTEvent_setUnregisterCallback (AHTEvent_unregister);
                   1168:        /***  ***/
1.4       cvs      1169: 
1.72      cvs      1170: #  ifndef _WINDOWS
                   1171:    HTEvent_setUnregisterCallback (AHTEvent_unregister);
                   1172: #  endif /* _WINDOWS */
                   1173: 
1.74      cvs      1174: #ifdef DEBUG_LIBWWW
                   1175:   WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_THREAD_TRACE | PROT_TRACE;
                   1176: #endif
1.4       cvs      1177: 
                   1178:    /* Trace activation (for debugging) */
1.7       cvs      1179:    /*
1.4       cvs      1180:       WWW_TraceFlag = SHOW_APP_TRACE | SHOW_UTIL_TRACE |
                   1181:       SHOW_BIND_TRACE | SHOW_THREAD_TRACE |
                   1182:       SHOW_STREAM_TRACE | SHOW_PROTOCOL_TRACE |
                   1183:       SHOW_URI_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1184:       SHOW_CORE_TRACE;
                   1185: 
1.7       cvs      1186:     */
1.4       cvs      1187: 
                   1188:    /***
                   1189:      WWW_TraceFlag = SHOW_CORE_TRACE | SHOW_AUTH_TRACE | SHOW_ANCHOR_TRACE |
                   1190:      SHOW_PROTOCOL_TRACE| SHOW_APP_TRACE | SHOW_UTIL_TRACE;
                   1191:      ***/
                   1192: 
                   1193:    /* Setting up other user interfaces */
                   1194: 
                   1195:    /* Setting up different network parameters */
1.17      cvs      1196:    /* Maximum number of simultaneous open sockets */
1.4       cvs      1197:    HTNet_setMaxSocket (8);
1.75      cvs      1198:    /* different network services timeouts */
1.4       cvs      1199:    HTDNS_setTimeout (3600);
1.75      cvs      1200: #ifdef _WINDOWS
                   1201:    /* under windows, the libwww persistent socket handling has
                   1202:    ** some bugs. The following line inhibits idle socket reusal.
                   1203:    ** this is a bit slower, but avoids crashes and gives us time
                   1204:    ** to distribute Amaya before having to patch up libwww.
                   1205:    */
1.76      cvs      1206:    HTHost_setPersistTimeout (-1L);
1.75      cvs      1207: #else
1.76      cvs      1208:    HTHost_setPersistTimeout (60L);
1.75      cvs      1209: #endif /* _WINDOWS */
                   1210: 
1.17      cvs      1211:    /* Cache is disabled in this version */
1.4       cvs      1212:    HTCacheMode_setEnabled (0);
                   1213: 
                   1214:    /* Initialization of the global context */
                   1215:    Amaya = (AmayaContext *) TtaGetMemory (sizeof (AmayaContext));
                   1216:    Amaya->reqlist = HTList_new ();
                   1217:    Amaya->docid_status = HTList_new ();
                   1218:    Amaya->open_requests = 0;
1.74      cvs      1219:    
1.4       cvs      1220: #ifdef CATCH_SIG
1.18      cvs      1221:    signal (SIGPIPE, SIG_IGN);
1.4       cvs      1222: #endif
1.15      cvs      1223: }
                   1224: 
1.69      cvs      1225: #ifndef _WINDOWS
1.15      cvs      1226: /*----------------------------------------------------------------------
1.17      cvs      1227:   LoopForStop
                   1228:   a copy of the Thop event loop so we can handle the stop button.
1.69      cvs      1229:   Not useful for windows code (Ramzi).
1.15      cvs      1230:   ----------------------------------------------------------------------*/
                   1231: #ifdef __STDC__
                   1232: static int          LoopForStop (AHTReqContext * me)
                   1233: #else
                   1234: static int          LoopForStop (AHTReqContext * me)
                   1235: #endif
                   1236: {
                   1237: 
1.25      cvs      1238:    extern ThotAppContext app_cont;
1.51      cvs      1239:    XEvent                ev;
                   1240:    XtInputMask           status;
1.17      cvs      1241:    int                 status_req = HT_OK;
1.15      cvs      1242: 
                   1243:    /* to test the async calls  */
1.17      cvs      1244:    /* Loop while waiting for new events, exists when the request is over */
1.15      cvs      1245:    while (me->reqStatus != HT_ABORT &&
                   1246:          me->reqStatus != HT_END &&
1.69      cvs      1247:          me->reqStatus != HT_ERR) {
                   1248:         if (!AmayaIsAlive)
                   1249:            /* Amaya was killed by one of the callback handlers */
                   1250:            exit (0);
                   1251: 
                   1252:         status = XtAppPending (app_cont);
                   1253:         if (status & XtIMXEvent) {
                   1254:             XtAppNextEvent (app_cont, &ev);
                   1255:            TtaHandleOneEvent (&ev);
                   1256:         } else if (status & (XtIMAll & (~XtIMXEvent))) {
                   1257:                 XtAppProcessEvent (app_cont, (XtIMAll & (~XtIMXEvent)));
                   1258:         } else {
                   1259:                XtAppNextEvent (app_cont, &ev);
                   1260:               TtaHandleOneEvent (&ev);
                   1261:         }
                   1262:    }
1.4       cvs      1263: 
1.69      cvs      1264:    switch (me->reqStatus) {
                   1265:          case HT_ERR:
                   1266:           case HT_ABORT:
1.15      cvs      1267:               status_req = HT_ERROR;
                   1268:               break;
                   1269: 
1.69      cvs      1270:          case HT_END:
1.15      cvs      1271:               status_req = HT_OK;
                   1272:               break;
                   1273: 
1.69      cvs      1274:          default:
1.15      cvs      1275:               break;
1.69      cvs      1276:    }
1.15      cvs      1277:    return (status_req);
1.4       cvs      1278: }
1.69      cvs      1279: #endif /* _WINDOWS */
1.4       cvs      1280: 
1.5       cvs      1281: /*----------------------------------------------------------------------
1.15      cvs      1282:   QueryClose
1.21      cvs      1283:   closes all existing threads, frees all non-automatically deallocated
                   1284:   memory and then ends libwww.
1.5       cvs      1285:   ----------------------------------------------------------------------*/
1.4       cvs      1286: void                QueryClose ()
                   1287: {
1.24      cvs      1288: 
                   1289:    AmayaIsAlive = FALSE;
                   1290: 
1.21      cvs      1291:    /* remove all the handlers and callbacks that may output a message to
                   1292:       a non-existent Amaya window */
                   1293: 
                   1294:    HTNet_deleteAfter (AHTLoadTerminate_handler);
                   1295:    HTNet_deleteAfter (redirection_handler);
                   1296:    HTAlertCall_deleteAll (HTAlert_global () );
1.23      cvs      1297:    HTAlert_setGlobal ((HTList *) NULL);
1.24      cvs      1298:    HTEvent_setRegisterCallback ((HTEvent_registerCallback *) NULL);
1.27      cvs      1299:    HTEvent_setUnregisterCallback ((HTEvent_unregisterCallback *) NULL);
1.87    ! cvs      1300:     HTHost_setActivateRequestCallback (NULL);
1.4       cvs      1301:    Thread_deleteAll ();
1.21      cvs      1302:  
1.4       cvs      1303:    HTProxy_deleteAll ();
                   1304:    HTNoProxy_deleteAll ();
                   1305:    HTGateway_deleteAll ();
                   1306:    AHTProfile_delete ();
                   1307: }
                   1308: 
1.5       cvs      1309: /*----------------------------------------------------------------------
1.15      cvs      1310:    GetObjectWWW
1.17      cvs      1311:    this function requests a resource designated by a URLname into a
                   1312:    temporary filename. The download can come from a simple GET operation,
                   1313:    or can come from POSTING/GETTING a form. In the latter
                   1314:    case, the function receives a query string to send to the server.
                   1315: 
1.5       cvs      1316:    4  file retrieval modes are proposed:                              
                   1317:    AMAYA_SYNC : blocking mode                            
                   1318:    AMAYA_ISYNC : incremental, blocking mode              
                   1319:    AMAYA_ASYNC : non-blocking mode                       
                   1320:    AMAYA_IASYNC : incremental, non-blocking mode         
                   1321:    
                   1322:    In the incremental mode, each time a package arrives, it will be   
                   1323:    stored in the temporary file. In addition, if an                   
                   1324:    incremental_callback function is defined, this function will be    
                   1325:    called and handled a copy of the newly received data package.      
                   1326:    Finally, if a terminate_callback function is defined, it will be   
                   1327:    invoked when the request terminates. The caller of this function
1.4       cvs      1328:    can define two different contexts to be passed to the callback
                   1329:    functions.
                   1330: 
                   1331:    When the function is called with the SYNC mode, the function will
                   1332:    return only when the requested file has been loaded.
                   1333:    The ASYNC mode will immediately return after setting up the
                   1334:    call.
                   1335: 
                   1336:    Notes:
                   1337:    At the end of a succesful request, the urlName string contains the
                   1338:    name of the actually retrieved URL. As a URL can change over the time,
                   1339:    (e.g., be redirected elsewhere), it is advised that the function
1.17      cvs      1340:    caller verify the value of the urlName variable at the end of
1.4       cvs      1341:    a request.
                   1342: 
                   1343:    Inputs:
                   1344:    - docid  Document identifier for the set of objects being
                   1345:    retrieved.
                   1346:    - urlName The URL to be retrieved (MAX_URL_LENGTH chars length)
                   1347:    - outputfile A pointer to an empty string of MAX_URL_LENGTH.
                   1348:    - mode The retrieval mode.
                   1349:    - incremental_cbf 
                   1350:    - context_icbf
                   1351:    Callback and context for the incremental modes
                   1352:    - terminate_cbf 
                   1353:    - context_icbf
                   1354:    Callback and context for a terminate handler
1.17      cvs      1355:    -error_html if TRUE, then display any server error message as an
                   1356:    HTML document.
1.4       cvs      1357: 
                   1358:    Outputs:
                   1359:    - urlName The URL that was retrieved
                   1360:    - outputfile The name of the temporary file which holds the
                   1361:    retrieved data. (Only in case of success)
                   1362:    Returns:
                   1363:    HT_ERROR
                   1364:    HT_OK
1.5       cvs      1365:  
                   1366:   ----------------------------------------------------------------------*/
1.4       cvs      1367: #ifdef __STDC__
1.52      cvs      1368: int GetObjectWWW (int docid, char* urlName, char* postString, char* outputfile, int mode,
                   1369:                  TIcbf* incremental_cbf, void* context_icbf, TTcbf* terminate_cbf, 
                   1370:                  void* context_tcbf, boolean error_html)
1.4       cvs      1371: #else
1.52      cvs      1372: int GetObjectWWW (docid, urlName, postString, outputfile, mode, incremental_cbf, context_icbf, 
                   1373:                  terminate_cbf, context_tcbf, error_html)
1.73      cvs      1374: int           docid;
                   1375: char         *urlName;
                   1376: char         *postString;
                   1377: char         *outputfile;
                   1378: int           mode;
                   1379: TIcbf        *incremental_cbf;
                   1380: void         *context_icbf;
                   1381: TTcbf        *terminate_cbf;
                   1382: void         *context_tcbf;
                   1383: boolean       error_html;
1.4       cvs      1384: #endif
                   1385: {
                   1386:    AHTReqContext      *me;
                   1387:    char               *ref;
                   1388:    int                 status;
1.7       cvs      1389: 
1.69      cvs      1390:    if (urlName == NULL || docid == 0 || outputfile == NULL) {
                   1391:       /* no file to be loaded */
                   1392:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   1393:        
                   1394:       if (error_html)
                   1395:         /* so we can show the error message */
                   1396:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   1397:       return HT_ERROR;
                   1398:       
                   1399:    }
1.7       cvs      1400: 
1.4       cvs      1401:    /* do we support this protocol? */
1.69      cvs      1402:    if (IsValidProtocol (urlName) == NO) {
                   1403:       /* return error */
                   1404:       outputfile[0] = EOS;     /* file could not be opened */
                   1405:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_GET_UNSUPPORTED_PROTOCOL), urlName);
                   1406:       
                   1407:       if (error_html)
                   1408:         /* so we can show the error message */
                   1409:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
                   1410:       return HT_ERROR;
                   1411:    }
1.4       cvs      1412: 
1.58      cvs      1413:    /*create a tempfilename */
1.59      cvs      1414:    sprintf (outputfile, "%s%c%d%c%04dAM", TempFileDirectory, DIR_SEP, docid, DIR_SEP, object_counter);
1.4       cvs      1415: 
                   1416:    /* update the object_counter */
                   1417:    object_counter++;
                   1418:    /* normalize the URL */
1.45      cvs      1419:    ref = AmayaParseUrl (urlName, "", AMAYA_PARSE_ALL);
1.4       cvs      1420:    /* should we abort the request if we could not normalize the url? */
1.69      cvs      1421:    if (ref == (char*) NULL || ref[0] == EOS) {
                   1422:       /*error */
                   1423:       outputfile[0] = EOS;
                   1424:       TtaSetStatus (docid, 1, TtaGetMessage (AMAYA, AM_BAD_URL), urlName);
                   1425:       
                   1426:       if (error_html)
                   1427:         /* so we can show the error message */
                   1428:         DocNetworkStatus[docid] |= AMAYA_NET_ERROR;
1.4       cvs      1429: 
1.69      cvs      1430:       return HT_ERROR;
                   1431:    }
1.4       cvs      1432:    /* verify if that file name existed */
1.9       cvs      1433:    if (TtaFileExist (outputfile))
1.77      cvs      1434:      TtaFileUnlink (outputfile);
1.4       cvs      1435: 
                   1436:    /* Initialize the request structure */
                   1437:    me = AHTReqContext_new (docid);
1.69      cvs      1438:    if (me == NULL) {
1.77      cvs      1439:      outputfile[0] = EOS;
                   1440:      /* need an error message here */
                   1441:      TtaFreeMemory (ref);
                   1442:      return (HT_ERROR);
1.69      cvs      1443:    }
1.77      cvs      1444:    
1.4       cvs      1445:    /* Specific initializations for POST and GET */
1.69      cvs      1446:    if (mode & AMAYA_FORM_POST) {
1.77      cvs      1447:      me->method = METHOD_POST;
                   1448:      if (postString) {
                   1449:        me->mem_ptr = postString;
                   1450:        me->block_size = strlen (postString);
                   1451:      } else {
                   1452:        me->mem_ptr = "";
                   1453:        me->block_size = 0;
                   1454:      }
                   1455:      HTRequest_setMethod (me->request, METHOD_POST);
1.69      cvs      1456:       HTRequest_setPostCallback (me->request, AHTUpload_callback);
                   1457:    } else {
1.77      cvs      1458:      me->method = METHOD_GET;
                   1459:      me->dest = (HTParentAnchor *) NULL;       /*useful only for PUT and POST methods */
                   1460:      if (!HasKnownFileSuffix (ref))
                   1461:        HTRequest_setConversion(me->request, acceptTypes, TRUE);
1.69      cvs      1462:    }
1.4       cvs      1463: 
                   1464:    /* Common initialization */
                   1465:    me->mode = mode;
                   1466:    me->error_html = error_html;
                   1467:    me->incremental_cbf = incremental_cbf;
                   1468:    me->context_icbf = context_icbf;
                   1469:    me->terminate_cbf = terminate_cbf;
                   1470:    me->context_tcbf = context_tcbf;
1.64      cvs      1471: 
1.69      cvs      1472:    /* for the async. request modes, we need to have our
1.4       cvs      1473:       own copy of outputfile and urlname
                   1474:     */
                   1475: 
1.69      cvs      1476:    if ((mode & AMAYA_ASYNC) || (mode & AMAYA_IASYNC)) {
1.77      cvs      1477:      char* tmp;
                   1478:      
1.69      cvs      1479:       tmp = TtaGetMemory (strlen (outputfile) + 1);
                   1480:       strcpy (tmp, outputfile);
                   1481:       me->outputfile = tmp;
                   1482:       
                   1483:       tmp = TtaGetMemory (MAX_LENGTH + 1);
                   1484:       strncpy (tmp, urlName, MAX_LENGTH);
                   1485:       tmp[MAX_LENGTH] = EOS;
                   1486:       me->urlName = tmp;
                   1487: #  ifdef _WINDOWS
                   1488:       HTRequest_setPreemptive (me->request, NO);
                   1489:    } else {
1.77      cvs      1490:      me->outputfile = outputfile;
                   1491:      me->urlName = urlName;
                   1492:      HTRequest_setPreemptive (me->request, YES);
1.61      cvs      1493:    }
1.69      cvs      1494: #  else /* _WINDOWS */
                   1495:    } else {
1.77      cvs      1496:      me->outputfile = outputfile;
                   1497:      me->urlName = urlName;
1.61      cvs      1498:    }
1.77      cvs      1499:      /***
1.57      cvs      1500:      Change for taking into account the stop button:
                   1501:      The requests will be always asynchronous, however, if mode=AMAYA_SYNC,
                   1502:      we will loop until the document has been received or a stop signal
                   1503:      generated
1.77      cvs      1504:      ****/
                   1505:      HTRequest_setPreemptive (me->request, NO);
1.69      cvs      1506: #  endif /* _WINDOWS */
1.61      cvs      1507: 
                   1508:    /* prepare the URLname that will be displayed in teh status bar */
                   1509:    ChopURL (me->status_urlName, me->urlName);
                   1510: 
1.77      cvs      1511:    TtaSetStatus (me->docid, 1, 
                   1512:                 TtaGetMessage (AMAYA, AM_FETCHING),
                   1513:                 me->status_urlName);
1.4       cvs      1514: 
                   1515:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (ref);
1.45      cvs      1516:    TtaFreeMemory (ref);
1.4       cvs      1517: 
1.69      cvs      1518:    if (mode & AMAYA_FORM_POST) {
                   1519:       HTAnchor_setFormat ((HTParentAnchor *) me->anchor, HTAtom_for ("application/x-www-form-urlencoded"));
                   1520:       HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
                   1521:       HTRequest_setEntityAnchor (me->request, me->anchor);
                   1522:       
                   1523:       status = HTLoadAbsolute (urlName, me->request);
                   1524:    } else
1.77      cvs      1525:      status = HTLoadAnchor ((HTAnchor *) me->anchor, me->request);
1.69      cvs      1526: 
1.72      cvs      1527: #ifndef _WINDOWS
1.77      cvs      1528:    if (status == HT_ERROR ||
                   1529:     me->reqStatus == HT_END ||
                   1530:     me->reqStatus == HT_ERR)
                   1531:    {
                   1532:      /* in case of error, free all allocated memory and exit */
1.82      cvs      1533:      if (me->output) {
1.85      cvs      1534: #ifdef DEBUG_LIBWWW      
                   1535:        fprintf (stderr, "tGetObjectWWW:: URL is  %s, closing "
                   1536:                "FILE %p\n", me->urlName, me->output); 
                   1537: #endif
1.77      cvs      1538:        fclose (me->output);
1.82      cvs      1539:        me->output = NULL;
                   1540:      }
                   1541:      
1.72      cvs      1542:       if (me->reqStatus == HT_ERR) {
1.77      cvs      1543:        status = HT_ERROR;
                   1544:        /* show an error message on the status bar */
                   1545:        DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
                   1546:        TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_CANNOT_LOAD), me->
                   1547:                      status_urlName);
1.72      cvs      1548:       } else
1.77      cvs      1549:        status = HT_OK;
1.72      cvs      1550: 
                   1551:       AHTReqContext_delete (me);
1.77      cvs      1552:  } else {
1.72      cvs      1553:          /* part of the stop button handler */
                   1554:          if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
                   1555:             status = LoopForStop (me);
                   1556:            AHTReqContext_delete (me);
                   1557:          }
                   1558:    }
1.79      cvs      1559: /* TtaHandlePendingEvents (); */
1.72      cvs      1560: 
                   1561: #else  /* !_WINDOWS */
                   1562: 
1.71      cvs      1563:    if (status == HT_ERROR) {
                   1564: 
1.77      cvs      1565:       /* in case of error, close any open files, free all allocated
                   1566:         memory and exit */
1.85      cvs      1567:      if (me->output && me->output != stdout) {
                   1568: #ifdef DEBUG_LIBWWW      
                   1569:        fprintf (stderr, "GetObjectWWW: URL is  %s, closing "
                   1570:                "FILE %p\n", me->urlName, me->output); 
                   1571: #endif
                   1572:        fclose (me->output);
                   1573:        me->output = NULL;
                   1574:      }
                   1575:      
1.69      cvs      1576:       if (me->reqStatus == HT_ERR) {
1.77      cvs      1577:        status = HT_ERROR;
                   1578:        /* show an error message on the status bar */
                   1579:        DocNetworkStatus[me->docid] |= AMAYA_NET_ERROR;
                   1580:        TtaSetStatus (me->docid, 1, 
                   1581:                      TtaGetMessage (AMAYA, AM_CANNOT_LOAD),
                   1582:                      me->status_urlName);
1.69      cvs      1583:       } else
1.77      cvs      1584:        status = HT_OK;
1.69      cvs      1585: 
                   1586:       AHTReqContext_delete (me);
                   1587:    } else {
1.77      cvs      1588:      /* part of the stop button handler */
                   1589:      if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC)) {
                   1590:        AHTReqContext_delete (me);
1.69      cvs      1591:         }
1.64      cvs      1592:    }
1.79      cvs      1593:   /*TtaHandlePendingEvents (); */
1.4       cvs      1594: 
1.72      cvs      1595: #endif /* !_WINDOWS */
                   1596: 
1.4       cvs      1597:    return (status);
                   1598: }
                   1599: 
1.5       cvs      1600: /*----------------------------------------------------------------------
1.17      cvs      1601:    PutObjectWWW
                   1602:    frontend for uploading a resource to a URL. This function downloads
                   1603:    a file to be uploaded into memory, it then calls UploadMemWWW to
                   1604:    finish the job.
                   1605: 
1.5       cvs      1606:    2 upload modes are proposed:                                       
                   1607:    AMAYA_SYNC : blocking mode                            
                   1608:    AMAYA_ASYNC : non-blocking mode                       
                   1609:    
1.4       cvs      1610:    When the function is called with the SYNC mode, the function will
                   1611:    return only when the file has been uploaded.
                   1612:    The ASYNC mode will immediately return after setting up the
                   1613:    call. Furthermore, at the end of an upload, the ASYNC mode will 
                   1614:    call back terminate_cbf, handling it the context defined in
                   1615:    context_tcbf.
                   1616: 
                   1617:    Notes:
                   1618:    At the end of a succesful request, the urlName string contains the
                   1619:    name of the actually uploaded URL. As a URL can change over the time,
                   1620:    (e.g., be redirected elsewhere), it is advised that the function
                   1621:    caller verifies the value of the urlName variable at the end of
                   1622:    a request.
                   1623: 
                   1624:    Inputs:
                   1625:    - docid  Document identifier for the set of objects being
                   1626:    retrieved.
                   1627:    - fileName A pointer to the local file to upload
                   1628:    - urlName The URL to be uploaded (MAX_URL_LENGTH chars length)
                   1629:    - mode The retrieval mode.
                   1630:    - terminate_cbf 
                   1631:    - context_icbf
                   1632:    Callback and context for a terminate handler
                   1633: 
                   1634:    Outputs:
                   1635:    - urlName The URL that was uploaded
                   1636: 
                   1637:    Returns:
                   1638:    HT_ERROR
                   1639:    HT_OK
1.5       cvs      1640:   ----------------------------------------------------------------------*/
1.4       cvs      1641: #ifdef __STDC__
1.27      cvs      1642: int                 PutObjectWWW (int docid, char *fileName, char *urlName, int mode, PicType contentType,
1.4       cvs      1643:                                  TTcbf * terminate_cbf, void *context_tcbf)
                   1644: #else
1.26      cvs      1645: int                 PutObjectWWW (docid, urlName, fileName, mode, contentType,
1.4       cvs      1646:                                  ,terminate_cbf, context_tcbf)
                   1647: int                 docid;
                   1648: char               *urlName;
                   1649: char               *fileName;
                   1650: int                 mode;
1.27      cvs      1651: PicType             contentType;
1.4       cvs      1652: TTcbf              *terminate_cbf;
                   1653: void               *context_tcbf;
                   1654: 
                   1655: #endif
                   1656: {
1.7       cvs      1657:    /*AHTReqContext      *me; */
1.4       cvs      1658:    int                 status;
                   1659: 
                   1660:    int                 fd;
                   1661:    struct stat         file_stat;
                   1662:    char               *mem_ptr;
                   1663:    unsigned long       block_size;
                   1664: 
1.33      cvs      1665:    AmayaLastHTTPErrorMsg [0] = EOS;
                   1666:    
1.4       cvs      1667:    if (urlName == NULL || docid == 0 || fileName == NULL ||
1.9       cvs      1668:        !TtaFileExist (fileName))
1.4       cvs      1669:       /* no file to be uploaded */
                   1670:       return HT_ERROR;
                   1671: 
                   1672:    /* do we support this protocol? */
1.7       cvs      1673:    if (IsValidProtocol (urlName) == NO)
                   1674:      {
                   1675:        /* return error */
1.77      cvs      1676:        TtaSetStatus (docid, 1, 
                   1677:                       TtaGetMessage (AMAYA, AM_PUT_UNSUPPORTED_PROTOCOL),
1.7       cvs      1678:                      urlName);
                   1679:        return HT_ERROR;
                   1680:      }
1.4       cvs      1681:    /* read the file into memory */
1.70      cvs      1682: #  ifndef _WINDOWS
1.7       cvs      1683:    if ((fd = open (fileName, O_RDONLY)) == -1)
1.70      cvs      1684: #  else /* _WINDOWS */
1.74      cvs      1685:    if ((fd = open (fileName, _O_RDONLY | _O_BINARY)) == -1)
1.70      cvs      1686: #  endif /* _WINDOWS */
1.7       cvs      1687:      {
                   1688:        /* if we could not open the file, exit */
                   1689:        /*error msg here */
                   1690:        return (HT_ERROR);
                   1691:      }
1.4       cvs      1692: 
                   1693:    fstat (fd, &file_stat);
                   1694: 
1.7       cvs      1695:    if (file_stat.st_size == 0)
                   1696:      {
                   1697:        /* file was empty */
                   1698:        /*errmsg here */
                   1699:        close (fd);
                   1700:        return (HT_ERROR);
                   1701:      }
1.4       cvs      1702:    block_size = file_stat.st_size;
                   1703: 
                   1704:    if (THD_TRACE)
                   1705:       fprintf (stderr, "file size == %u\n", (unsigned) block_size);
                   1706: 
                   1707:    mem_ptr = (char *) TtaGetMemory (block_size);
                   1708: 
1.7       cvs      1709:    if (mem_ptr == (char *) NULL)
                   1710:      {
                   1711:        /* could not allocate enough memory */
                   1712:        /*errmsg here */
                   1713:        close (fd);
                   1714:        return (HT_ERROR);
                   1715:      }
1.4       cvs      1716:    read (fd, mem_ptr, block_size);
                   1717: 
                   1718:    close (fd);
                   1719: 
1.27      cvs      1720:    status = UploadMemWWW (docid, METHOD_PUT, urlName, contentType, mem_ptr,
1.4       cvs      1721:                          block_size, mode, terminate_cbf,
                   1722:                          context_tcbf, (char *) NULL);
                   1723: 
                   1724:    TtaFreeMemory (mem_ptr);
1.28      cvs      1725:    TtaHandlePendingEvents ();
1.4       cvs      1726:    return (status);
                   1727: }
                   1728: 
1.5       cvs      1729: /*----------------------------------------------------------------------
1.17      cvs      1730:   UploadMemWWW
                   1731:   low level interface function to libwww for uploading a block of
                   1732:   memory to a URL.
1.5       cvs      1733:   ----------------------------------------------------------------------*/
1.4       cvs      1734: #ifdef __STDC__
                   1735: int                 UploadMemWWW (int docid, HTMethod method,
1.27      cvs      1736:                     char *urlName, PicType contentType, char *mem_ptr, unsigned long block_size,
1.4       cvs      1737:                        int mode, TTcbf * terminate_cbf, void *context_tcbf,
                   1738:                                  char *outputfile)
                   1739: #else
1.27      cvs      1740: int                 UploadMemWWW (docid, method, urlName, contentType, mem_ptr, block_size, mode,
1.4       cvs      1741:                                  terminate_cbf, context_tcbf, outputfile)
                   1742: int                 docid;
                   1743: HTMethod            method;
                   1744: char               *urlName;
1.27      cvs      1745: PicType             contentType;
1.4       cvs      1746: char               *mem_ptr;
                   1747: usigned long        block_size;
                   1748: int                 mode;
                   1749: TTcbf              *terminate_cbf;
                   1750: void               *context_tcbf;
                   1751: char               *outputfile;
1.7       cvs      1752: 
1.4       cvs      1753: #endif
                   1754: {
                   1755:    AHTReqContext      *me;
                   1756:    int                 status;
                   1757: 
                   1758:    if (mem_ptr == (char *) NULL ||
                   1759:        block_size == 0 ||
                   1760:        docid == 0 ||
1.7       cvs      1761:        urlName == (char *) NULL)
                   1762:      {
                   1763:        /* nothing to be uploaded */
                   1764:        return HT_ERROR;
                   1765:      }
1.4       cvs      1766: 
                   1767:    /* Initialize the request structure */
                   1768:    me = AHTReqContext_new (docid);
                   1769: 
1.7       cvs      1770:    if (me == NULL)
                   1771:      {
                   1772:        /* need an error message here */
                   1773:        TtaHandlePendingEvents ();
                   1774:        return (HT_ERROR);
                   1775:      }
1.4       cvs      1776:    me->mode = mode;
                   1777: 
                   1778:    me->incremental_cbf = (TIcbf *) NULL;
                   1779:    me->context_icbf = (void *) NULL;
                   1780:    me->terminate_cbf = terminate_cbf;
                   1781:    me->context_tcbf = context_tcbf;
                   1782: 
                   1783:    me->output = stdout;
                   1784:    me->outputfile = (char *) NULL;
                   1785:    me->urlName = urlName;
                   1786: 
1.70      cvs      1787: #ifdef _WINDOWS
                   1788:    HTRequest_setPreemptive (me->request, YES);
                   1789: #else
1.4       cvs      1790:    HTRequest_setPreemptive (me->request, NO);
1.70      cvs      1791: #endif /* _WINDOWS */
1.74      cvs      1792: 
1.17      cvs      1793:    /* select the parameters that distinguish a PUT from a GET/POST */
1.4       cvs      1794:    me->method = METHOD_PUT;
                   1795:    HTRequest_setMethod (me->request, METHOD_PUT);
                   1796:    me->output = stdout;
1.17      cvs      1797:    /* we are not expecting to receive any input from the server */
                   1798:    me->outputfile = (char *) NULL; 
1.4       cvs      1799: 
                   1800:    me->mem_ptr = mem_ptr;
                   1801:    me->block_size = block_size;
1.17      cvs      1802: 
                   1803:    /* set the callback which will actually copy data into the
                   1804:       output stream */
                   1805: 
1.4       cvs      1806:    HTRequest_setPostCallback (me->request, AHTUpload_callback);
1.72      cvs      1807: 
1.4       cvs      1808:    me->anchor = (HTParentAnchor *) HTAnchor_findAddress (urlName);
1.7       cvs      1809: 
1.28      cvs      1810:    /* Set the Content-Type of the file we are uploading */
1.77      cvs      1811:    HTAnchor_setFormat ((HTParentAnchor *) me->anchor,
                   1812:                       AHTGuessAtom_for (me->urlName, contentType));
1.17      cvs      1813: 
1.7       cvs      1814:    HTAnchor_setLength ((HTParentAnchor *) me->anchor, me->block_size);
1.4       cvs      1815:    HTRequest_setEntityAnchor (me->request, me->anchor);
1.74      cvs      1816:    /* prepare the URLname that will be displayed in teh status bar */
                   1817:    ChopURL (me->status_urlName, me->urlName);
                   1818:    TtaSetStatus (me->docid, 1, TtaGetMessage (AMAYA, AM_REMOTE_SAVING),
                   1819:                     me->status_urlName);
1.4       cvs      1820:    status = HTLoadAbsolute (urlName, me->request);
                   1821: 
1.74      cvs      1822: #ifndef _WINDOWS
1.77      cvs      1823:    if (status == HT_ERROR || 
                   1824:        me->reqStatus == HT_END || 
                   1825:        me->reqStatus == HT_ERR || 
                   1826:        HTError_hasSeverity (HTRequest_error (me->request), ERR_INFO))
1.74      cvs      1827: #else
                   1828:      if (status == HT_ERROR)
                   1829: #endif /* !_WINDOWS */
1.7       cvs      1830:      {
1.17      cvs      1831:        status = HT_ERROR;
1.28      cvs      1832:      }     
1.7       cvs      1833:    else
                   1834:      {
                   1835:        /* part of the stop button handler */
1.4       cvs      1836: 
1.7       cvs      1837:        if ((mode & AMAYA_SYNC) || (mode & AMAYA_ISYNC))
                   1838:          {
1.70      cvs      1839: #        ifndef _WINDOWS
1.7       cvs      1840:             status = LoopForStop (me);
1.70      cvs      1841: #        endif /* !_WINDOWS */
1.7       cvs      1842:          }
1.15      cvs      1843:      }
1.77      cvs      1844:    AHTReqContext_delete (me);
1.4       cvs      1845:    return (status);
1.28      cvs      1846: }
1.4       cvs      1847: 
                   1848: 
                   1849: 
1.5       cvs      1850: /*----------------------------------------------------------------------
1.17      cvs      1851:   Stop Request
                   1852:   stops (kills) all active requests associated with a docid 
1.5       cvs      1853:   ----------------------------------------------------------------------*/
1.4       cvs      1854: #ifdef __STDC__
                   1855: void                StopRequest (int docid)
                   1856: #else
                   1857: void                StopRequest (docid)
                   1858: int                 docid;
                   1859: #endif
                   1860: {
                   1861:    HTList             *cur;
                   1862:    AHTDocId_Status    *docid_status;
                   1863:    AHTReqContext      *me;
                   1864:    int                 open_requests;
                   1865: 
1.84      cvs      1866:    HTNet              *reqNet;
                   1867:    HTHost             *reqHost;
1.85      cvs      1868:    HTChannel          *reqChannel;
                   1869:    int                 reqSock;
                   1870:    
1.7       cvs      1871:    if (Amaya)
                   1872:      {
                   1873:        docid_status = (AHTDocId_Status *) GetDocIdStatus (docid,
1.4       cvs      1874:                                                       Amaya->docid_status);
1.7       cvs      1875:        /* verify if there are any requests at all associated with docid */
1.4       cvs      1876: 
1.7       cvs      1877:        if (docid_status == (AHTDocId_Status *) NULL)
                   1878:           return;
1.4       cvs      1879: 
1.7       cvs      1880:        open_requests = docid_status->counter;
1.4       cvs      1881: 
1.85      cvs      1882:        /* First, kill all pending requests */
                   1883:        /* We first inhibit the activation of pending requests */
                   1884:        HTHost_disable_PendingReqLaunch ();
                   1885:        cur = Amaya->reqlist;
                   1886:        while ((me = (AHTReqContext *) HTList_nextObject (cur))) 
                   1887:          {
                   1888:             if (me->docid == docid && me->reqStatus == HT_NEW)
                   1889:               {
                   1890:                 reqNet = HTRequest_net (me->request);
                   1891:                 reqSock = HTNet_socket (reqNet);
                   1892:                 reqChannel = HTChannel_find(reqSock);
                   1893:                 reqHost = HTChannel_host (reqChannel);
                   1894: 
                   1895: 
                   1896:                   HTRequest_kill (me->request); 
                   1897:                   /* HTRequest_setNet (me->request, NULL);
                   1898:                   */
                   1899: 
                   1900:                 if ((me->mode & AMAYA_ASYNC) ||
                   1901:                     (me->mode & AMAYA_IASYNC))
                   1902:                   {
                   1903:                     AHTReqContext_delete (me);
                   1904:                   }
                   1905:                 
                   1906:                 if (HTHost_isIdle (reqHost) ) {
                   1907: #ifdef DEBUG_LIBWWW
                   1908:                   fprintf (stderr, "Host is idle, killing socket %d\n",
                   1909:                            reqSock);;
                   1910: #endif /* DEBUG_LIBWWW */
                   1911: 
                   1912:                   HTEvent_unregister (reqSock, FD_ALL);
                   1913:                   HTEvent_register(reqSock, NULL, (SockOps) FD_READ,
                   1914:                                    HTHost_catchClose,  HT_PRIORITY_MAX);
                   1915:                   close (reqSock);
                   1916:                   /*   
                   1917:                   if (reqChannel && reqHost)
                   1918:                   HTHost_clearChannel(reqHost, HT_OK);
                   1919:                   HTHost_catchClose (reqSock, NULL, FD_CLOSE);
                   1920:                   */
                   1921:                 }
                   1922: 
                   1923:                 cur = Amaya->reqlist;
                   1924:                 open_requests--;                  
                   1925:               }
                   1926:          }
                   1927:        /* enable the activation of pending requests */
                   1928:        HTHost_enable_PendingReqLaunch ();
                   1929: 
                   1930:        cur = Amaya->reqlist;
1.7       cvs      1931:        while ((me = (AHTReqContext *) HTList_nextObject (cur)))
                   1932:          {
                   1933:             if (me->docid == docid)
                   1934:               {
                   1935:                  /* kill this request */
1.4       cvs      1936: 
1.7       cvs      1937:                  switch (me->reqStatus)
                   1938:                        {
                   1939:                           case HT_ABORT:
                   1940:                              break;
1.4       cvs      1941: 
1.7       cvs      1942:                           case HT_BUSY:
                   1943:                              me->reqStatus = HT_ABORT;
                   1944:                              break;
                   1945:                           case HT_NEW_PENDING:
                   1946:                           case HT_WAITING:
                   1947:                           default:
1.74      cvs      1948:                              me->reqStatus = HT_ABORT;
1.85      cvs      1949: 
1.70      cvs      1950: #                 ifndef _WINDOWS
1.7       cvs      1951:                              RequestKillAllXtevents (me);
1.74      cvs      1952: #                 endif _WINDOWS
1.85      cvs      1953:  
1.84      cvs      1954:                              reqNet = HTRequest_net (me->request);
                   1955:                              reqSock = HTNet_socket (reqNet);
1.85      cvs      1956:                              reqChannel = HTChannel_find(reqSock);
                   1957:                              reqHost = HTChannel_host (reqChannel);
                   1958: 
                   1959:                               HTRequest_kill (me->request); 
                   1960:                              /*                              HTRequest_setNet (me->request, NULL); */
1.84      cvs      1961: 
1.71      cvs      1962:                              if ((me->mode & AMAYA_ASYNC) ||
                   1963:                                  (me->mode & AMAYA_IASYNC))
1.7       cvs      1964:                                {
                   1965:                                   AHTReqContext_delete (me);
1.84      cvs      1966:                                     
1.7       cvs      1967:                                }
1.84      cvs      1968: 
1.85      cvs      1969:                              /* if there are no more requests, then close
                   1970:                                 the connection */
                   1971: 
1.84      cvs      1972:                              if (HTHost_isIdle (reqHost) ) {
1.85      cvs      1973: #ifdef                        DEBUG_LIBWWW
                   1974:                                fprintf (stderr, "Host is idle, "
                   1975:                                         "killing socket %d\n",
                   1976:                                         reqSock);
                   1977: #endif                        /* DEBUG_LIBWWW */
                   1978:                                 HTEvent_unregister (reqSock, FD_ALL);
                   1979:                                HTEvent_register(reqSock,
                   1980:                                                 NULL,
                   1981:                                                 (SockOps) FD_READ,
                   1982:                                                 HTHost_catchClose,
                   1983:                                                 HT_PRIORITY_MAX);
                   1984:                                close (reqSock);                                
                   1985:                                HTHost_clearChannel (reqHost, HT_ERROR);
                   1986:                                /*
                   1987:                                if (reqChannel && reqHost)
                   1988:                                  HTHost_clearChannel(reqHost, HT_OK);
                   1989:                                HTHost_catchClose (reqSock, NULL, FD_CLOSE);
                   1990:                                */
                   1991: #ifdef                          DEBUG_LIBWWW                           
                   1992:                                fprintf (stderr, "After killing, "
                   1993:                                         "HTHost_isIdle gives %d\n",
                   1994:                                         HTHost_isIdle (reqHost));
                   1995: #endif                          /* DEBUG_LIBWWW */
1.84      cvs      1996:                              }
                   1997:                                     
1.74      cvs      1998:                              cur = Amaya->reqlist;
1.7       cvs      1999:                              open_requests--;
1.4       cvs      2000: 
1.7       cvs      2001:                              break;
1.4       cvs      2002: 
1.7       cvs      2003:                        }       /* switch */
                   2004:               }                /* if me docid */
                   2005:          }                     /* while */
                   2006:      }                         /* if amaya open requests */
1.85      cvs      2007: 
                   2008: } /* StopRequest */
1.17      cvs      2009: 
                   2010: /*
                   2011:   end of Module query.c
                   2012: */
                   2013: 
1.69      cvs      2014: #endif /* AMAYA_JAVA */
1.77      cvs      2015: 
                   2016: 
1.17      cvs      2017: 
                   2018: 

Webmaster