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