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

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

Webmaster