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

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

Webmaster