Annotation of libwww/Library/src/HTAccess.c, revision 1.144
1.135 frystyk 1: /*
1.61 frystyk 2: ** ACCESS MANAGER
3: **
1.75 frystyk 4: ** (c) COPYRIGHT MIT 1995.
1.61 frystyk 5: ** Please first read the full copyright statement in the file COPYRIGH.
1.144 ! frystyk 6: ** @(#) $Id: HTAccess.c,v 1.143 1998/07/09 16:51:30 frystyk Exp $
1.1 timbl 7: **
8: ** Authors
1.79 frystyk 9: ** TBL Tim Berners-Lee timbl@w3.org
1.4 timbl 10: ** JFG Jean-Francois Groff jfg@dxcern.cern.ch
1.1 timbl 11: ** DD Denis DeLaRoca (310) 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
1.122 frystyk 12: ** HFN Henrik Frystyk, frystyk@w3.org
1.1 timbl 13: ** History
14: ** 8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
15: ** 26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
1.42 frystyk 16: ** 6 Oct 92 Moved HTClientHost and HTlogfile into here. TBL
1.1 timbl 17: ** 17 Dec 92 Tn3270 added, bug fix. DD
1.2 timbl 18: ** 4 Feb 93 Access registration, Search escapes bad chars TBL
1.9 timbl 19: ** PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
20: ** 28 May 93 WAIS gateway explicit if no WAIS library linked in.
1.19 timbl 21: ** Dec 93 Bug change around, more reentrant, etc
1.42 frystyk 22: ** 09 May 94 logfile renamed to HTlogfile to avoid clash with WAIS
1.114 frystyk 23: ** 8 Jul 94 Insulate HT_FREE();
1.88 frystyk 24: ** Sep 95 Rewritten, HFN
1.1 timbl 25: */
26:
1.67 frystyk 27: /* Library include files */
1.122 frystyk 28: #include "WWWUtil.h"
29: #include "WWWCore.h"
30: #include "WWWStream.h"
1.132 frystyk 31: #include "HTProxy.h"
32: #include "HTRules.h"
1.93 frystyk 33: #include "HTReqMan.h"
34: #include "HTAccess.h" /* Implemented here */
1.88 frystyk 35:
1.111 frystyk 36: #define PUTBLOCK(b, l) (*target->isa->put_block)(target, b, l)
37:
38: struct _HTStream {
39: HTStreamClass * isa;
40: };
41:
1.124 frystyk 42: typedef enum _HTPutState {
1.125 frystyk 43: HT_LOAD_SOURCE = 0,
1.128 frystyk 44: HT_SAVE_DEST,
45: HT_ABORT_SAVE
1.124 frystyk 46: } HTPutState;
47:
48: typedef struct _HTPutContext {
49: HTParentAnchor * source;
50: HTAnchor * destination;
51: HTChunk * document;
52: HTFormat format;
53: HTStream * target; /* Any existing output stream */
54: void * placeholder; /* Any existing doc in anchor */
55: HTPutState state;
56: } HTPutContext;
57:
1.123 frystyk 58: /* --------------------------------------------------------------------------*/
59: /* THE GET METHOD */
60: /* --------------------------------------------------------------------------*/
1.33 luotonen 61:
1.90 frystyk 62: /* Request a document
63: ** -----------------
64: ** Private version that requests a document from the request manager
65: ** Returns YES if request accepted, else NO
1.88 frystyk 66: */
1.124 frystyk 67: PRIVATE BOOL launch_request (HTRequest * request, BOOL recursive)
1.88 frystyk 68: {
69: if (PROT_TRACE) {
1.90 frystyk 70: HTParentAnchor *anchor = HTRequest_anchor(request);
71: char * full_address = HTAnchor_address((HTAnchor *) anchor);
1.115 eric 72: HTTrace("HTAccess.... Accessing document %s\n", full_address);
1.114 frystyk 73: HT_FREE(full_address);
1.88 frystyk 74: }
1.96 frystyk 75: return HTLoad(request, recursive);
1.58 frystyk 76: }
1.1 timbl 77:
1.90 frystyk 78: /* Request a document from absolute name
79: ** -------------------------------------
80: ** Request a document referencd by an absolute URL.
81: ** Returns YES if request accepted, else NO
82: */
1.122 frystyk 83: PUBLIC BOOL HTLoadAbsolute (const char * url, HTRequest * request)
1.90 frystyk 84: {
85: if (url && request) {
86: HTAnchor * anchor = HTAnchor_findAddress(url);
87: HTRequest_setAnchor(request, anchor);
1.124 frystyk 88: return launch_request(request, NO);
1.90 frystyk 89: }
90: return NO;
91: }
92:
1.123 frystyk 93: /* Request a document from relative name
94: ** -------------------------------------
95: ** Request a document referenced by a relative URL. The relative URL is
96: ** made absolute by resolving it relative to the address of the 'base'
97: ** anchor.
98: ** Returns YES if request accepted, else NO
99: */
100: PUBLIC BOOL HTLoadRelative (const char * relative,
101: HTParentAnchor * base,
102: HTRequest * request)
103: {
104: BOOL status = NO;
105: if (relative && base && request) {
106: char * full_url = NULL;
107: char * base_url = HTAnchor_address((HTAnchor *) base);
108: full_url = HTParse(relative, base_url,
109: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
110: status = HTLoadAbsolute(full_url, request);
111: HT_FREE(full_url);
112: HT_FREE(base_url);
113: }
114: return status;
115: }
1.90 frystyk 116:
117: /* Request a document from absolute name to stream
118: ** -----------------------------------------------
119: ** Request a document referencd by an absolute URL and sending the data
1.123 frystyk 120: ** down a stream.
121: ** Returns YES if request accepted, else NO
122: */
123: PUBLIC BOOL HTLoadToStream (const char * url, HTStream * output,
124: HTRequest * request)
125: {
126: if (url && output && request) {
127: HTRequest_setOutputStream(request, output);
128: return HTLoadAbsolute(url, request);
129: }
130: return NO;
131: }
132:
133: /* Load a document and save it ASIS in a local file
134: ** ------------------------------------------------
1.90 frystyk 135: ** Returns YES if request accepted, else NO
136: */
1.123 frystyk 137: PUBLIC BOOL HTLoadToFile (const char * url, HTRequest * request,
138: const char * filename)
1.90 frystyk 139: {
1.123 frystyk 140: if (url && filename && request) {
141: FILE * fp = NULL;
142:
143: /* Check if file exists. If so then ask user if we can replace it */
144: if (access(filename, F_OK) != -1) {
145: HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
146: if (prompt) {
147: if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_FILE_REPLACE, NULL,
148: NULL, NULL) != YES)
149: return NO;
150: }
151: }
152:
153: /* If replace then open the file */
154: if ((fp = fopen(filename, "wb")) == NULL) {
155: HTRequest_addError(request, ERR_NON_FATAL, NO, HTERR_NO_FILE,
156: (char *) filename, strlen(filename),
157: "HTLoadToFile");
158: return NO;
159: }
160:
161: /* Set the output stream and start the request */
162: HTRequest_setOutputFormat(request, WWW_SOURCE);
163: HTRequest_setOutputStream(request, HTFWriter_new(request, fp, NO));
1.144 ! frystyk 164: if (HTLoadAbsolute(url, request) == NO) {
! 165: fclose(fp);
! 166: return NO;
! 167: } else
! 168: return YES;
1.123 frystyk 169: }
170: return NO;
1.90 frystyk 171: }
172:
1.122 frystyk 173: /*
174: ** Load a URL to a mem buffer
175: ** --------------------------
176: ** Load a request and store the result in a memory buffer.
177: ** Returns chunk if OK - else NULL
178: */
179: PUBLIC HTChunk * HTLoadToChunk (const char * url, HTRequest * request)
180: {
181: if (url && request) {
182: HTChunk * chunk = NULL;
183: HTStream * target = HTStreamToChunk(request, &chunk, 0);
184: HTAnchor * anchor = HTAnchor_findAddress(url);
185: HTRequest_setAnchor(request, anchor);
186: HTRequest_setOutputStream(request, target);
1.124 frystyk 187: if (launch_request(request, NO) == YES)
1.122 frystyk 188: return chunk;
189: else {
190: HTChunk_delete(chunk);
191: return NULL;
192: }
193: }
194: return NULL;
195: }
1.90 frystyk 196:
197: /* Request an anchor
198: ** -----------------
199: ** Request the document referenced by the anchor
200: ** Returns YES if request accepted, else NO
201: */
202: PUBLIC BOOL HTLoadAnchor (HTAnchor * anchor, HTRequest * request)
203: {
204: if (anchor && request) {
205: HTRequest_setAnchor(request, anchor);
1.124 frystyk 206: return launch_request(request, NO);
1.90 frystyk 207: }
208: return NO;
209: }
210:
211: /* Request an anchor
212: ** -----------------
213: ** Same as HTLoadAnchor but any information in the Error Stack in the
214: ** request object is kept, so that any error messages in one
1.52 frystyk 215: ** This function is almost identical to HTLoadAnchor, but it doesn't
216: ** clear the error stack so that the information in there is kept.
1.90 frystyk 217: ** Returns YES if request accepted, else NO
218: */
219: PUBLIC BOOL HTLoadAnchorRecursive (HTAnchor * anchor, HTRequest * request)
220: {
221: if (anchor && request) {
222: HTRequest_setAnchor(request, anchor);
1.124 frystyk 223: return launch_request(request, YES);
1.90 frystyk 224: }
225: return NO;
226: }
227:
1.122 frystyk 228: /*
229: ** Load a URL to a mem buffer
230: ** --------------------------
231: ** Load a request and store the result in a memory buffer.
232: ** Returns chunk if OK - else NULL
233: */
234: PUBLIC HTChunk * HTLoadAnchorToChunk (HTAnchor * anchor, HTRequest * request)
235: {
1.124 frystyk 236: HTChunk * chunk = NULL;
1.122 frystyk 237: if (anchor && request) {
238: HTStream * target = HTStreamToChunk(request, &chunk, 0);
239: HTRequest_setAnchor(request, anchor);
240: HTRequest_setOutputStream(request, target);
1.124 frystyk 241: if (launch_request(request, NO) == YES)
1.122 frystyk 242: return chunk;
243: else {
244: HTChunk_delete(chunk);
245: return NULL;
246: }
247: }
248: return NULL;
249: }
1.90 frystyk 250:
1.123 frystyk 251: /*
252: ** Load a Rule File
253: ** ----------------
254: ** Load a rule find with the URL specified and add the set of rules to
255: ** the existing set.
256: */
257: PUBLIC BOOL HTLoadRules (const char * url)
258: {
259: BOOL status = NO;
260: if (url) {
261: HTList * list = HTList_new();
262: HTRequest * request = HTRequest_new();
263: HTRequest_setPreemptive(request, YES);
1.137 frystyk 264:
265: /*
266: ** We do only accept a new rules files when we are in interactive
267: ** mode and can ask the user for it. If HT_AUTOMATIC_RULES is
268: ** defined then we accept new rules files without explicit ack
269: ** from the user
270: */
271: #ifdef HT_AUTOMATIC_RULES
1.123 frystyk 272: HTAlert_setInteractive(NO);
1.137 frystyk 273: #endif
274:
275: /*
276: ** Add the rules parsing stream for this particular request only.
277: ** That is, we only accept a rules file when we have explicitly
278: ** asked for it using this function.
279: */
1.123 frystyk 280: HTConversion_add(list, "application/x-www-rules", "*/*", HTRules,
281: 1.0, 0.0, 0.0);
282: HTRequest_setConversion(request, list, YES);
283: status = HTLoadAbsolute(url, request);
284: HTConversion_deleteAll(list);
285: HTRequest_delete(request);
286: }
287: return status;
288: }
289:
290: /* --------------------------------------------------------------------------*/
291: /* GET WITH KEYWORDS (SEARCH) */
292: /* --------------------------------------------------------------------------*/
293:
294: /*
295: ** This function creates a URL with a searh part as defined by RFC 1866
296: ** Both the baseurl and the keywords must be escaped.
297: **
298: ** 1. The form field names and values are escaped: space
299: ** characters are replaced by `+', and then reserved characters
300: ** are escaped as per [URL]; that is, non-alphanumeric
301: ** characters are replaced by `%HH', a percent sign and two
302: ** hexadecimal digits representing the ASCII code of the
303: ** character. Line breaks, as in multi-line text field values,
304: ** are represented as CR LF pairs, i.e. `%0D%0A'.
305: **
306: ** 2. The fields are listed in the order they appear in the
307: ** document with the name separated from the value by `=' and
308: ** the pairs separated from each other by `&'. Fields with null
309: ** values may be omitted. In particular, unselected radio
310: ** buttons and checkboxes should not appear in the encoded
311: ** data, but hidden fields with VALUE attributes present
312: ** should.
313: **
314: ** NOTE - The URI from a query form submission can be
315: ** used in a normal anchor style hyperlink.
316: ** Unfortunately, the use of the `&' character to
317: ** separate form fields interacts with its use in SGML
318: ** attribute values as an entity reference delimiter.
319: ** For example, the URI `http://host/?x=1&y=2' must be
320: ** written `<a href="http://host/?x=1&y=2"' or `<a
321: ** href="http://host/?x=1&y=2">'.
322: **
323: ** HTTP server implementors, and in particular, CGI
324: ** implementors are encouraged to support the use of
325: ** `;' in place of `&' to save users the trouble of
326: ** escaping `&' characters this way.
327: */
328: PRIVATE char * query_url_encode (const char * baseurl, HTChunk * keywords)
329: {
330: char * fullurl = NULL;
331: if (baseurl && keywords && HTChunk_size(keywords)) {
332: int len = strlen(baseurl);
333: fullurl = (char *) HT_MALLOC(len + HTChunk_size(keywords) + 2);
334: sprintf(fullurl, "%s?%s", baseurl, HTChunk_data(keywords));
335: {
336: char * ptr = fullurl+len;
337: while (*ptr) {
338: if (*ptr == ' ') *ptr = '+';
339: ptr++;
340: }
341: }
342: }
343: return fullurl;
344: }
345:
346: PRIVATE char * form_url_encode (const char * baseurl, HTAssocList * formdata)
347: {
348: if (formdata) {
349: BOOL first = YES;
350: int cnt = HTList_count((HTList *) formdata);
351: HTChunk * fullurl = HTChunk_new(128);
352: HTAssoc * pres;
1.124 frystyk 353: if (baseurl) {
354: HTChunk_puts(fullurl, baseurl);
355: HTChunk_putc(fullurl, '?');
356: }
1.123 frystyk 357: while (cnt > 0) {
358: pres = (HTAssoc *) HTList_objectAt((HTList *) formdata, --cnt);
359: if (first)
360: first = NO;
361: else
362: HTChunk_putc(fullurl, '&'); /* Could use ';' instead */
363: HTChunk_puts(fullurl, HTAssoc_name(pres));
364: HTChunk_putc(fullurl, '=');
365: HTChunk_puts(fullurl, HTAssoc_value(pres));
366: }
367: return HTChunk_toCString(fullurl);
368: }
369: return NULL;
370: }
371:
372: /* Search a document from absolute name
373: ** ------------------------------------
374: ** Request a document referencd by an absolute URL appended with the
375: ** keywords given. The URL can NOT contain any fragment identifier!
376: ** The list of keywords must be a space-separated list and spaces will
377: ** be converted to '+' before the request is issued.
378: ** Returns YES if request accepted, else NO
379: */
380: PUBLIC BOOL HTSearchAbsolute (HTChunk * keywords,
381: const char * base,
382: HTRequest * request)
383: {
384: if (keywords && base && request) {
385: char * full = query_url_encode(base, keywords);
386: if (full) {
387: HTAnchor * anchor = HTAnchor_findAddress(full);
388: HTRequest_setAnchor(request, anchor);
389: HT_FREE(full);
1.124 frystyk 390: return launch_request(request, NO);
1.123 frystyk 391: }
392: }
393: return NO;
394: }
395:
396: /* Search a document from relative name
397: ** -------------------------------------
398: ** Request a document referenced by a relative URL. The relative URL is
399: ** made absolute by resolving it relative to the address of the 'base'
400: ** anchor.
401: ** Returns YES if request accepted, else NO
402: */
403: PUBLIC BOOL HTSearchRelative (HTChunk * keywords,
404: const char * relative,
405: HTParentAnchor * base,
406: HTRequest * request)
407: {
408: BOOL status = NO;
409: if (keywords && relative && base && request) {
410: char * full_url = NULL;
411: char * base_url = HTAnchor_address((HTAnchor *) base);
412: full_url = HTParse(relative, base_url,
413: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
414: status = HTSearchAbsolute(keywords, full_url, request);
415: HT_FREE(full_url);
416: HT_FREE(base_url);
417: }
418: return status;
419: }
420:
421: /*
422: ** Search a string
423: ** ---------------
424: ** This is the same as HTSearchAbsolute but instead of using a chunk
425: ** you can pass a string.
426: */
427: PUBLIC BOOL HTSearchString (const char * keywords,
428: HTAnchor * anchor,
429: HTRequest * request)
430: {
431: BOOL status = NO;
432: if (keywords && anchor && request) {
433: char * base_url = HTAnchor_address((HTAnchor *) anchor);
434: HTChunk * chunk = HTChunk_new(strlen(keywords)+2);
435: HTChunk_puts(chunk, keywords);
436: status = HTSearchAbsolute(chunk, base_url, request);
437: HT_FREE(base_url);
438: HTChunk_delete(chunk);
439: }
440: return status;
441: }
442:
1.90 frystyk 443: /* Search an Anchor
444: ** ----------------
445: ** Performs a keyword search on word given by the user. Adds the keyword
446: ** to the end of the current address and attempts to open the new address.
447: ** The list of keywords must be a space-separated list and spaces will
448: ** be converted to '+' before the request is issued.
449: ** Search can also be performed by HTLoadAbsolute() etc.
450: ** Returns YES if request accepted, else NO
451: */
1.123 frystyk 452: PUBLIC BOOL HTSearchAnchor (HTChunk * keywords,
453: HTAnchor * anchor,
454: HTRequest * request)
1.90 frystyk 455: {
1.99 frystyk 456: BOOL status = NO;
1.123 frystyk 457: if (keywords && anchor && request) {
458: char * base_url = HTAnchor_address(anchor);
459: status = HTSearchAbsolute(keywords, base_url, request);
1.114 frystyk 460: HT_FREE(base_url);
1.90 frystyk 461: }
1.99 frystyk 462: return status;
1.2 timbl 463: }
464:
1.123 frystyk 465: /* --------------------------------------------------------------------------*/
466: /* HANDLING FORMS USING GET AND POST */
467: /* --------------------------------------------------------------------------*/
1.2 timbl 468:
1.123 frystyk 469: /* Send a Form request using GET from absolute name
470: ** ------------------------------------------------
1.90 frystyk 471: ** Request a document referencd by an absolute URL appended with the
1.123 frystyk 472: ** formdata given. The URL can NOT contain any fragment identifier!
473: ** The list of form data must be given as an association list where
474: ** the name is the field name and the value is the value of the field.
475: ** Returns YES if request accepted, else NO
476: */
477: PUBLIC BOOL HTGetFormAbsolute (HTAssocList * formdata,
478: const char * base,
479: HTRequest * request)
480: {
481: if (formdata && base && request) {
482: char * full = form_url_encode(base, formdata);
483: if (full) {
484: HTAnchor * anchor = HTAnchor_findAddress(full);
485: HTRequest_setAnchor(request, anchor);
486: HT_FREE(full);
1.124 frystyk 487: return launch_request(request, NO);
1.123 frystyk 488: }
489: }
490: return NO;
491: }
492:
493: /* Send a Form request using GET from relative name
494: ** ------------------------------------------------
495: ** Request a document referencd by a relative URL appended with the
496: ** formdata given. The URL can NOT contain any fragment identifier!
497: ** The list of form data must be given as an association list where
498: ** the name is the field name and the value is the value of the field.
499: ** Returns YES if request accepted, else NO
500: */
501: PUBLIC BOOL HTGetFormRelative (HTAssocList * formdata,
502: const char * relative,
503: HTParentAnchor * base,
504: HTRequest * request)
505: {
506: BOOL status = NO;
507: if (formdata && relative && base && request) {
508: char * full_url = NULL;
509: char * base_url = HTAnchor_address((HTAnchor *) base);
510: full_url=HTParse(relative, base_url,
511: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
512: status = HTGetFormAbsolute(formdata, full_url, request);
513: HT_FREE(full_url);
514: HT_FREE(base_url);
515: }
516: return status;
517: }
518:
519: /* Send a Form request using GET from an anchor
520: ** --------------------------------------------
521: ** Request a document referencd by an anchor object appended with the
522: ** formdata given. The URL can NOT contain any fragment identifier!
523: ** The list of form data must be given as an association list where
524: ** the name is the field name and the value is the value of the field.
1.90 frystyk 525: ** Returns YES if request accepted, else NO
526: */
1.123 frystyk 527: PUBLIC BOOL HTGetFormAnchor (HTAssocList * formdata,
528: HTAnchor * anchor,
529: HTRequest * request)
530: {
531: BOOL status = NO;
532: if (formdata && anchor && request) {
533: char * base_url = HTAnchor_address((HTAnchor *) anchor);
534: status = HTGetFormAbsolute(formdata, base_url, request);
535: HT_FREE(base_url);
536: }
537: return status;
538: }
539:
540: PRIVATE int HTEntity_callback (HTRequest * request, HTStream * target)
541: {
542: HTParentAnchor * entity = HTRequest_entityAnchor(request);
1.124 frystyk 543: if (WWWTRACE) HTTrace("Posting Data from callback function\n");
1.123 frystyk 544: if (!request || !entity || !target) return HT_ERROR;
545: {
1.126 frystyk 546: BOOL chunking = NO;
1.123 frystyk 547: int status;
548: char * document = (char *) HTAnchor_document(entity);
549: int len = HTAnchor_length(entity);
1.126 frystyk 550: if (!document) {
551: if (PROT_TRACE) HTTrace("Posting Data No document\n");
552: return HT_ERROR;
553: }
554:
555: /*
556: ** If the length is unknown (-1) then see if the document is a text
557: ** type and in that case take the strlen. If not then we don't know
558: ** how much data we can write and must stop
559: */
560: if (len < 0) {
561: HTFormat actual = HTAnchor_format(entity);
562: HTFormat tmplate = HTAtom_for("text/*");
563: if (HTMIMEMatch(tmplate, actual)) {
564: len = strlen(document); /* Naive! */
565: chunking = YES;
566: } else {
567: if (PROT_TRACE)
568: HTTrace("Posting Data Must know the length of document %p\n",
569: document);
570: return HT_ERROR;
571: }
572: }
573:
574: /* Send the data down the pipe */
1.123 frystyk 575: status = (*target->isa->put_block)(target, document, len);
576: if (status == HT_WOULD_BLOCK) {
1.124 frystyk 577: if (PROT_TRACE)HTTrace("Posting Data Target WOULD BLOCK\n");
1.123 frystyk 578: return HT_WOULD_BLOCK;
579: } else if (status == HT_PAUSE) {
1.126 frystyk 580: if (PROT_TRACE) HTTrace("Posting Data Target PAUSED\n");
1.123 frystyk 581: return HT_PAUSE;
1.126 frystyk 582: } else if (chunking && status == HT_OK) {
583: if (PROT_TRACE) HTTrace("Posting Data Target is SAVED using chunked\n");
584: return (*target->isa->put_block)(target, "", 0);
1.136 frystyk 585: } else if (status == HT_LOADED || status == HT_OK) {
586: if (PROT_TRACE) HTTrace("Posting Data Target is SAVED\n");
587: (*target->isa->flush)(target);
588: return HT_LOADED;
589: } else if (status > 0) { /* Stream specific return code */
590: if (PROT_TRACE) HTTrace("Posting Data. Target returns %d\n", status);
1.123 frystyk 591: return status;
592: } else { /* we have a real error */
1.129 frystyk 593: if (PROT_TRACE) HTTrace("Posting Data Target ERROR %d\n", status);
1.123 frystyk 594: return status;
595: }
596: }
597: }
598:
599: /* Send a Form request using POST from absolute name
600: ** -------------------------------------------------
601: ** Request a document referencd by an absolute URL appended with the
602: ** formdata given. The URL can NOT contain any fragment identifier!
603: ** The list of form data must be given as an association list where
604: ** the name is the field name and the value is the value of the field.
605: */
606: PUBLIC HTParentAnchor * HTPostFormAbsolute (HTAssocList * formdata,
607: const char * base,
608: HTRequest * request)
609: {
610: if (formdata && base && request) {
611: HTAnchor * anchor = HTAnchor_findAddress(base);
612: return HTPostFormAnchor(formdata, anchor, request);
613: }
614: return NULL;
615: }
616:
617: /* Send a Form request using POST from relative name
618: ** -------------------------------------------------
619: ** Request a document referencd by a relative URL appended with the
620: ** formdata given. The URL can NOT contain any fragment identifier!
621: ** The list of form data must be given as an association list where
622: ** the name is the field name and the value is the value of the field.
623: */
624: PUBLIC HTParentAnchor * HTPostFormRelative (HTAssocList * formdata,
625: const char * relative,
626: HTParentAnchor * base,
627: HTRequest * request)
1.90 frystyk 628: {
1.123 frystyk 629: HTParentAnchor * postanchor = NULL;
630: if (formdata && relative && base && request) {
631: char * full_url = NULL;
632: char * base_url = HTAnchor_address((HTAnchor *) base);
633: full_url=HTParse(relative, base_url,
634: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
635: postanchor = HTPostFormAbsolute(formdata, full_url, request);
636: HT_FREE(full_url);
637: HT_FREE(base_url);
638: }
639: return postanchor;
640: }
641:
642: /* Send a Form request using POST from an anchor
643: ** ---------------------------------------------
644: ** Request a document referencd by an anchor object appended with the
645: ** formdata given. The URL can NOT contain any fragment identifier!
646: ** The list of form data must be given as an association list where
647: ** the name is the field name and the value is the value of the field.
648: */
649: PUBLIC HTParentAnchor * HTPostFormAnchor (HTAssocList * formdata,
650: HTAnchor * anchor,
651: HTRequest * request)
652: {
653: HTParentAnchor * postanchor = NULL;
654: if (formdata && anchor && request) {
655: HTUserProfile * up = HTRequest_userProfile(request);
656: char * tmpfile = HTGetTmpFileName(HTUserProfile_tmp(up));
657: char * tmpurl = HTParse(tmpfile, "file:", PARSE_ALL);
658: char * form_encoded = form_url_encode(NULL, formdata);
659: if (form_encoded) {
660:
661: /*
662: ** Now create a new anchor for the post data and set up
663: ** the rest of the metainformation we know about this anchor. The
664: ** tmp anchor may actually already exist from previous postings.
665: */
666: postanchor = (HTParentAnchor *) HTAnchor_findAddress(tmpurl);
667: HTAnchor_clearHeader(postanchor);
668: HTAnchor_setDocument(postanchor, form_encoded);
669: HTAnchor_setLength(postanchor, strlen(form_encoded));
670: HTAnchor_setFormat(postanchor, WWW_FORM);
671:
672: /*
673: ** Bind the temporary anchor to the anchor that will contain the
674: ** response
675: */
676: HTLink_removeAll((HTAnchor *) postanchor);
677: HTLink_add((HTAnchor *) postanchor, (HTAnchor *) anchor,
678: NULL, METHOD_POST);
679:
680: /* Set up the request object */
681: HTRequest_addGnHd(request, HT_G_DATE); /* Send date header */
682: HTRequest_setAnchor(request, anchor);
683: HTRequest_setEntityAnchor(request, postanchor);
684: HTRequest_setMethod(request, METHOD_POST);
685:
686: /* Add the post form callback function to provide the form data */
687: HTRequest_setPostCallback(request, HTEntity_callback);
688:
689: /* Now start the load normally */
1.124 frystyk 690: launch_request(request, NO);
1.123 frystyk 691: }
692: HT_FREE(tmpfile);
693: HT_FREE(tmpurl);
1.90 frystyk 694: }
1.123 frystyk 695: return postanchor;
1.57 howcome 696: }
697:
1.70 frystyk 698: /* --------------------------------------------------------------------------*/
1.124 frystyk 699: /* PUT A DOCUMENT */
700: /* --------------------------------------------------------------------------*/
1.125 frystyk 701: PRIVATE BOOL setup_anchors (HTRequest * request,
1.131 frystyk 702: HTParentAnchor * source, HTParentAnchor * dest,
703: HTMethod method)
1.124 frystyk 704: {
1.131 frystyk 705: if (!(method & (METHOD_PUT | METHOD_POST))) {
706: if (APP_TRACE) HTTrace("Posting..... Bad method\n");
707: return NO;
708: }
709:
1.124 frystyk 710: /*
1.127 frystyk 711: ** Check whether we know if it is possible to PUT to this destination.
712: ** We both check the local set of allowed methods in the anchor and any
713: ** site information that we may have about the location of the origin
714: ** server.
1.124 frystyk 715: */
716: {
1.131 frystyk 717: char * addr = HTAnchor_address((HTAnchor *) source);
718: char * hostname = HTParse(addr, "", PARSE_HOST);
1.142 frystyk 719: #if 0
1.131 frystyk 720: HTHost * host = HTHost_find(hostname);
721: HTMethod public_methods = HTHost_publicMethods(host);
1.134 frystyk 722: HTMethod private_methods = HTAnchor_allow(dest);
1.141 frystyk 723: #endif
1.131 frystyk 724: HT_FREE(hostname);
725: HT_FREE(addr);
1.141 frystyk 726:
727: #if 0
728: /*
729: ** Again, this may be too cautios for normal operations
730: */
1.131 frystyk 731: if (!(method & (private_methods | public_methods))) {
1.124 frystyk 732: HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
733: if (prompt) {
734: if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_METHOD,
735: NULL, NULL, NULL) != YES)
736: return NO;
737: }
738: }
1.141 frystyk 739: #endif
1.124 frystyk 740: }
741:
742: /*
743: ** Bind the source anchor to the dest anchor that will contain the
744: ** response. If link already exists then ask is we should do it again.
745: ** If so then remove the old link and replace it with a new one.
746: */
747: {
748: HTLink *link = HTLink_find((HTAnchor *) source, (HTAnchor *) dest);
749: if (link && HTLink_method(link) == METHOD_PUT) {
750: HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
751: if (prompt) {
752: if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDO,
753: NULL, NULL, NULL) != YES)
754: return NO;
755: }
756: HTLink_remove((HTAnchor *) source, (HTAnchor *) dest);
757: }
758: HTLink_add((HTAnchor*) source, (HTAnchor*) dest, NULL, METHOD_PUT);
759: }
760: return YES;
761: }
762:
763: /* Send an Anchor using PUT from absolute name
764: ** -------------------------------------------
765: ** Upload a document referenced by an absolute URL appended.
766: ** The URL can NOT contain any fragment identifier!
767: ** The list of form data must be given as an association list where
768: ** the name is the field name and the value is the value of the field.
769: */
770: PUBLIC BOOL HTPutAbsolute (HTParentAnchor * source,
771: const char * destination,
772: HTRequest * request)
773: {
774: if (source && destination && request) {
775: HTAnchor * dest = HTAnchor_findAddress(destination);
776: return HTPutAnchor(source, dest, request);
777: }
778: return NO;
779: }
780:
781: /* Send an Anchor using PUT from relative name
782: ** -------------------------------------------
783: ** Upload a document referenced by a relative URL appended.
784: ** The URL can NOT contain any fragment identifier!
785: ** The list of form data must be given as an association list where
786: ** the name is the field name and the value is the value of the field.
787: */
788: PUBLIC BOOL HTPutRelative (HTParentAnchor * source,
789: const char * relative,
790: HTParentAnchor * destination_base,
791: HTRequest * request)
792: {
793: if (source && relative && destination_base && request) {
794: BOOL status;
795: char * full_url = NULL;
796: char * base_url = HTAnchor_address((HTAnchor *) destination_base);
797: full_url=HTParse(relative, base_url,
798: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
799: status = HTPutAbsolute(source, full_url, request);
800: HT_FREE(full_url);
801: HT_FREE(base_url);
802: return status;
803: }
804: return NO;
805: }
806:
807: /* Send an Anchor using PUT from an anchor
808: ** ---------------------------------------
809: ** Upload a document referenced by an anchor object appended
810: ** The URL can NOT contain any fragment identifier!
811: ** The list of form data must be given as an association list where
812: ** the name is the field name and the value is the value of the field.
813: */
814: PUBLIC BOOL HTPutAnchor (HTParentAnchor * source,
815: HTAnchor * destination,
816: HTRequest * request)
817: {
818: HTParentAnchor * dest = HTAnchor_parent(destination);
819: if (source && dest && request) {
1.131 frystyk 820: if (setup_anchors(request, source, dest, METHOD_PUT) == YES) {
1.124 frystyk 821:
822: /* Set up the request object */
823: HTRequest_addGnHd(request, HT_G_DATE);
1.144 ! frystyk 824: if (HTRequest_preconditions(request))
! 825: HTRequest_addRqHd(request, HT_C_IF_MATCH | HT_C_IF_UNMOD_SINCE);
1.124 frystyk 826: HTRequest_setEntityAnchor(request, source);
827: HTRequest_setMethod(request, METHOD_PUT);
828: HTRequest_setAnchor(request, destination);
829:
830: /* Add the entity callback function to provide the form data */
831: HTRequest_setPostCallback(request, HTEntity_callback);
832:
833: /* Now start the load normally */
834: return launch_request(request, NO);
835: }
836: }
837: return NO;
838: }
839:
1.125 frystyk 840: /* Send an Anchor using POST from absolute name
841: ** -------------------------------------------
842: ** Upload a document referenced by an absolute URL appended.
843: ** The URL can NOT contain any fragment identifier!
844: ** The list of form data must be given as an association list where
845: ** the name is the field name and the value is the value of the field.
846: */
847: PUBLIC BOOL HTPostAbsolute (HTParentAnchor * source,
848: const char * destination,
849: HTRequest * request)
850: {
851: if (source && destination && request) {
852: HTAnchor * dest = HTAnchor_findAddress(destination);
853: return HTPostAnchor(source, dest, request);
854: }
855: return NO;
856: }
857:
858: /* Send an Anchor using POST from relative name
859: ** -------------------------------------------
860: ** Upload a document referenced by a relative URL appended.
861: ** The URL can NOT contain any fragment identifier!
862: ** The list of form data must be given as an association list where
863: ** the name is the field name and the value is the value of the field.
864: */
865: PUBLIC BOOL HTPostRelative (HTParentAnchor * source,
866: const char * relative,
867: HTParentAnchor * destination_base,
868: HTRequest * request)
869: {
870: if (source && relative && destination_base && request) {
871: BOOL status;
872: char * full_url = NULL;
873: char * base_url = HTAnchor_address((HTAnchor *) destination_base);
874: full_url=HTParse(relative, base_url,
875: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
876: status = HTPostAbsolute(source, full_url, request);
877: HT_FREE(full_url);
878: HT_FREE(base_url);
879: return status;
880: }
881: return NO;
882: }
883:
884: /* Send an Anchor using POST from an anchor
885: ** ---------------------------------------
886: ** Upload a document referenced by an anchor object appended
887: ** The URL can NOT contain any fragment identifier!
888: ** The list of form data must be given as an association list where
889: ** the name is the field name and the value is the value of the field.
890: */
891: PUBLIC BOOL HTPostAnchor (HTParentAnchor * source,
892: HTAnchor * destination,
893: HTRequest * request)
894: {
895: HTParentAnchor * dest = HTAnchor_parent(destination);
896: if (source && dest && request) {
1.131 frystyk 897: if (setup_anchors(request, source, dest, METHOD_POST) == YES) {
1.125 frystyk 898:
899: /* Set up the request object */
900: HTRequest_addGnHd(request, HT_G_DATE);
901: HTRequest_setEntityAnchor(request, source);
902: HTRequest_setMethod(request, METHOD_POST);
903: HTRequest_setAnchor(request, destination);
904:
905: /* Add the entity callback function to provide the form data */
906: HTRequest_setPostCallback(request, HTEntity_callback);
907:
908: /* Now start the load normally */
909: return launch_request(request, NO);
910: }
911: }
912: return NO;
913: }
914:
1.124 frystyk 915: /* Send an Anchor using PUT from absolute name
916: ** -------------------------------------------
917: ** Upload a document referenced by an absolute URL appended.
918: ** The URL can NOT contain any fragment identifier!
919: ** The list of form data must be given as an association list where
920: ** the name is the field name and the value is the value of the field.
921: */
922: PUBLIC BOOL HTPutStructuredAbsolute (HTParentAnchor * source,
923: const char * destination,
924: HTRequest * request,
925: HTPostCallback * input)
926: {
927: if (source && destination && request && input) {
928: HTAnchor * dest = HTAnchor_findAddress(destination);
929: return HTPutStructuredAnchor(source, dest, request, input);
930: }
931: return NO;
932: }
933:
934: /* Send an Anchor using PUT from relative name
935: ** -------------------------------------------
936: ** Upload a document referenced by a relative URL appended.
937: ** The URL can NOT contain any fragment identifier!
938: ** The list of form data must be given as an association list where
939: ** the name is the field name and the value is the value of the field.
940: */
941: PUBLIC BOOL HTPutStructuredRelative (HTParentAnchor * source,
942: const char * relative,
943: HTParentAnchor * destination_base,
944: HTRequest * request,
945: HTPostCallback * input)
946: {
947: if (source && relative && destination_base && request && input) {
948: BOOL status;
949: char * full_url = NULL;
950: char * base_url = HTAnchor_address((HTAnchor *) destination_base);
951: full_url=HTParse(relative, base_url,
952: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
953: status = HTPutStructuredAbsolute(source, full_url, request, input);
954: HT_FREE(full_url);
955: HT_FREE(base_url);
956: return status;
957: }
958: return NO;
959: }
960:
961: /* Send an Anchor using PUT from an anchor
962: ** ---------------------------------------
963: ** Upload a document referenced by an anchor object appended
964: ** The URL can NOT contain any fragment identifier!
965: ** The list of form data must be given as an association list where
966: ** the name is the field name and the value is the value of the field.
967: */
968: PUBLIC BOOL HTPutStructuredAnchor (HTParentAnchor * source,
969: HTAnchor * destination,
970: HTRequest * request,
971: HTPostCallback * input)
972: {
973: HTParentAnchor * dest = HTAnchor_parent(destination);
974: if (source && dest && request) {
1.131 frystyk 975: if (setup_anchors(request, source, dest, METHOD_PUT) == YES) {
1.124 frystyk 976:
977: /* Set up the request object */
978: HTRequest_addGnHd(request, HT_G_DATE);
1.144 ! frystyk 979: if (HTRequest_preconditions(request))
! 980: HTRequest_addRqHd(request, HT_C_IF_MATCH | HT_C_IF_UNMOD_SINCE);
1.124 frystyk 981: HTRequest_setEntityAnchor(request, source);
982: HTRequest_setMethod(request, METHOD_PUT);
983: HTRequest_setAnchor(request, destination);
984:
985: /* Add the entity callback function to provide the form data */
986: HTRequest_setPostCallback(request, input);
987:
988: /* Now start the load normally */
989: return launch_request(request, NO);
990: }
991: }
992: return NO;
993: }
994:
995: /*
1.144 ! frystyk 996: ** After filter for handling PUT of document.
1.124 frystyk 997: */
1.134 frystyk 998: PRIVATE int HTSaveFilter (HTRequest * request, HTResponse * response,
999: void * param, int status)
1.124 frystyk 1000: {
1001: HTPutContext * me = (HTPutContext *) param;
1002: if (APP_TRACE)
1003: HTTrace("Save Filter. Using context %p with state %c\n",
1004: me, me->state+0x30);
1005:
1006: /*
1.125 frystyk 1007: ** Just ignore authentication in the hope that some other filter will
1008: ** handle this.
1009: */
1.138 frystyk 1010: if (status == HT_NO_ACCESS || status == HT_NO_PROXY_ACCESS ||
1011: status == HT_REAUTH || status == HT_PROXY_REAUTH) {
1.125 frystyk 1012: if (APP_TRACE) HTTrace("Save Filter. Waiting for authentication\n");
1013: return HT_OK;
1014: }
1015:
1016: /*
1.124 frystyk 1017: ** If either the source or the destination has moved then ask the user
1018: ** what to do. If there is no user then stop
1019: */
1.138 frystyk 1020: if (status == HT_TEMP_REDIRECT || status == HT_PERM_REDIRECT ||
1021: status == HT_FOUND || status == HT_SEE_OTHER) {
1.124 frystyk 1022: HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
1.134 frystyk 1023: HTAnchor * redirection = HTResponse_redirection(response);
1.124 frystyk 1024: if (prompt && redirection) {
1.125 frystyk 1025: if (me->state == HT_LOAD_SOURCE) {
1.124 frystyk 1026: if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_SOURCE_MOVED,
1027: NULL, NULL, NULL) == YES) {
1028: me->source = HTAnchor_parent(redirection);
1.128 frystyk 1029: } else {
1030: /*
1031: ** Make sure that the operation stops
1032: */
1033: me->state = HT_ABORT_SAVE;
1.124 frystyk 1034: }
1035: } else {
1.141 frystyk 1036: #if 0
1037: /*
1038: ** If you are very precautios then you can ask here whether
1039: ** we should continue or not in case of a redirection
1040: */
1.124 frystyk 1041: if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_DESTINATION_MOVED,
1042: NULL, NULL, NULL) == YES) {
1043: me->destination = redirection;
1.128 frystyk 1044: } else {
1045: /*
1046: ** Make sure that the operation stops
1047: */
1048: me->state = HT_ABORT_SAVE;
1.124 frystyk 1049: }
1.141 frystyk 1050: #else
1051: if (APP_TRACE) HTTrace("Save Filter. Destination hae moved!\n");
1052: me->destination = redirection;
1053: #endif
1.124 frystyk 1054: }
1055: }
1.128 frystyk 1056: return HT_OK;
1.124 frystyk 1057: }
1058:
1059: /*
1.125 frystyk 1060: ** If we succeeded getting the source then start the PUT itself. Otherwise
1061: ** cleanup the mess
1.124 frystyk 1062: */
1.125 frystyk 1063: if (me->state == HT_LOAD_SOURCE && status == HT_LOADED &&
1064: !HTError_hasSeverity(HTRequest_error(request), ERR_INFO)) {
1.124 frystyk 1065:
1066: /* Swap the document in the anchor with the new one */
1067: me->placeholder = HTAnchor_document(me->source);
1068: HTAnchor_setDocument(me->source, HTChunk_data(me->document));
1069:
1070: /* Set up the request object */
1071: HTRequest_addGnHd(request, HT_G_DATE);
1072: HTRequest_setEntityAnchor(request, me->source);
1073: HTRequest_setMethod(request, METHOD_PUT);
1074: HTRequest_setAnchor(request, me->destination);
1075: HTRequest_setOutputFormat(request, me->format);
1076: HTRequest_setOutputStream(request, me->target);
1.144 ! frystyk 1077:
! 1078: /*
! 1079: ** If we are to use preconditions then
! 1080: ** copy the last modified and etag information from
! 1081: ** the source to the destination. This is needed in
! 1082: ** order to avoid the lost update problem
! 1083: */
! 1084: if (HTRequest_preconditions(request)) {
! 1085: time_t src_time = HTAnchor_lastModified(me->source);
! 1086: char * src_etag = HTAnchor_etag(me->source);
! 1087: HTAnchor_setLastModified(HTAnchor_parent(me->destination), src_time);
! 1088: HTAnchor_setEtag(HTAnchor_parent(me->destination), src_etag);
! 1089: HTRequest_addRqHd(request, HT_C_IF_MATCH | HT_C_IF_UNMOD_SINCE);
! 1090: }
1.124 frystyk 1091:
1.139 frystyk 1092: /* Make sure we flush the output immediately */
1093: HTRequest_forceFlush(request);
1094:
1.125 frystyk 1095: /* Turn progress notifications back on */
1096: HTRequest_setInternal(request, NO);
1097:
1.124 frystyk 1098: /* Add the entity callback function to provide the form data */
1099: HTRequest_setPostCallback(request, HTEntity_callback);
1100:
1101: /* Now start the load normally */
1.143 frystyk 1102: if (launch_request(request, NO) == YES)
1103: me->state = HT_SAVE_DEST;
1104: else {
1105: HTAnchor_setDocument(me->source, me->placeholder);
1106: HTChunk_delete(me->document);
1107: HT_FREE(me);
1108: }
1109: #if 0
1.124 frystyk 1110: me->state = launch_request(request, NO) ?
1.125 frystyk 1111: HT_SAVE_DEST : HT_LOAD_SOURCE;
1.143 frystyk 1112: #endif
1.124 frystyk 1113: /*
1114: ** By returning HT_ERROR we make sure that this is the last handler to
1115: ** be called. We do this as we don't want any other filter to delete
1116: ** the request object now when we have just started a new one
1117: ** ourselves
1118: */
1119: return HT_ERROR;
1120:
1121: } else {
1122: HTAnchor_setDocument(me->source, me->placeholder);
1123: HTChunk_delete(me->document);
1124: HT_FREE(me);
1125: }
1126: return HT_OK;
1127: }
1128:
1129: /* Send an Anchor using PUT from absolute name
1130: ** -------------------------------------------
1131: ** Upload a document referenced by an absolute URL appended.
1132: ** The URL can NOT contain any fragment identifier!
1133: ** The list of form data must be given as an association list where
1134: ** the name is the field name and the value is the value of the field.
1135: */
1136: PUBLIC BOOL HTPutDocumentAbsolute (HTParentAnchor * source,
1137: const char * destination,
1138: HTRequest * request)
1139: {
1140: if (source && destination && request) {
1141: HTAnchor * dest = HTAnchor_findAddress(destination);
1142: return HTPutDocumentAnchor(source, dest, request);
1143: }
1144: return NO;
1145: }
1146:
1147: /* Send an Anchor using PUT from relative name
1148: ** -------------------------------------------
1149: ** Upload a document referenced by a relative URL appended.
1150: ** The URL can NOT contain any fragment identifier!
1151: ** The list of form data must be given as an association list where
1152: ** the name is the field name and the value is the value of the field.
1153: */
1154: PUBLIC BOOL HTPutDocumentRelative (HTParentAnchor * source,
1155: const char * relative,
1156: HTParentAnchor * destination_base,
1157: HTRequest * request)
1158: {
1159: if (source && relative && destination_base && request) {
1160: BOOL status;
1161: char * full_url = NULL;
1162: char * base_url = HTAnchor_address((HTAnchor *) destination_base);
1163: full_url=HTParse(relative, base_url,
1164: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1165: status = HTPutDocumentAbsolute(source, full_url, request);
1166: HT_FREE(full_url);
1167: HT_FREE(base_url);
1168: return status;
1169: }
1170: return NO;
1171: }
1172:
1173: /* Send an Anchor using PUT from an anchor
1174: ** ---------------------------------------
1175: ** Upload a document referenced by an anchor object appended
1176: ** The URL can NOT contain any fragment identifier!
1177: ** The source document is first loaded into memory and then the PUT
1178: ** to the remote server is done using the memory version
1179: */
1180: PUBLIC BOOL HTPutDocumentAnchor (HTParentAnchor * source,
1181: HTAnchor * destination,
1182: HTRequest * request)
1183: {
1184: HTParentAnchor * dest = HTAnchor_parent(destination);
1185: if (source && dest && request) {
1.131 frystyk 1186: if (setup_anchors(request, source, dest, METHOD_PUT) == YES) {
1.124 frystyk 1187: HTPutContext * context = NULL;
1188:
1189: /*
1190: ** First we register an AFTER filter that can check the result
1191: ** of the source load if success then it can start the PUT
1192: ** operation to the destination.
1193: */
1194: if (!(context=(HTPutContext *) HT_CALLOC(1, sizeof(HTPutContext))))
1195: HT_OUTOFMEM("HTPutDocumentAnchor");
1196: context->source = source;
1197: context->destination = destination;
1.134 frystyk 1198:
1199: /*
1200: ** We register the after filter with a NULL template as we
1201: ** don't know the source of the data.
1202: */
1203: HTRequest_addAfter(request, HTSaveFilter, NULL, context, HT_ALL,
1204: HT_FILTER_FIRST, NO);
1.125 frystyk 1205:
1206: /* Turn off progress notifications */
1207: HTRequest_setInternal(request, YES);
1.124 frystyk 1208:
1209: /*
1210: ** We make sure that we are not using a memory cached element.
1211: ** It's OK to use a file cached element!
1212: */
1.133 frystyk 1213: HTRequest_setReloadMode(request, HT_CACHE_FLUSH_MEM);
1.124 frystyk 1214:
1215: /*
1216: ** Now we load the source document into a chunk. We specify that
1217: ** we want the document ASIS from the source location.
1218: */
1219: context->format = HTRequest_outputFormat(request);
1220: context->target = HTRequest_outputStream(request);
1221: HTRequest_setOutputFormat(request, WWW_SOURCE);
1222: context->document = HTLoadAnchorToChunk((HTAnchor*)source,request);
1223: if (context->document == NULL) {
1224: if (APP_TRACE) HTTrace("Put Document No source\n");
1225: HT_FREE(context);
1226: return NO;
1227: }
1228: return YES;
1229: }
1230: }
1231: return NO;
1232: }
1233:
1234: /* ------------------------------------------------------------------------- */
1.70 frystyk 1235:
1.90 frystyk 1236: /* Copy an anchor
1.70 frystyk 1237: ** --------------
1.90 frystyk 1238: ** Fetch the URL (possibly local file URL) and send it using either PUT
1239: ** or POST to the remote destination using HTTP. The caller can decide the
1240: ** exact method used and which HTTP header fields to transmit by setting
1241: ** the user fields in the request structure.
1.92 frystyk 1242: ** If posting to NNTP then we can't dispatch at this level but must pass
1243: ** the source anchor to the news module that then takes all the refs
1244: ** to NNTP and puts into the "newsgroups" header
1.70 frystyk 1245: */
1.109 frystyk 1246: PUBLIC BOOL HTCopyAnchor (HTAnchor * src_anchor, HTRequest * main_dest)
1.80 frystyk 1247: {
1.106 frystyk 1248: HTRequest * src_req;
1249: HTList * cur;
1.109 frystyk 1250: if (!src_anchor || !main_dest) {
1.115 eric 1251: if (WWWTRACE) HTTrace("Copy........ BAD ARGUMENT\n");
1.90 frystyk 1252: return NO;
1.109 frystyk 1253: }
1.70 frystyk 1254:
1.112 frystyk 1255: /* Set the source anchor */
1256: main_dest->source_anchor = HTAnchor_parent(src_anchor);
1257:
1.80 frystyk 1258: /* Build the POST web if not already there */
1.109 frystyk 1259: if (!main_dest->source) {
1260: src_req = HTRequest_dupInternal(main_dest); /* Get a duplicate */
1.80 frystyk 1261: HTAnchor_clearHeader((HTParentAnchor *) src_anchor);
1.109 frystyk 1262: src_req->method = METHOD_GET;
1.133 frystyk 1263: src_req->reload = HT_CACHE_FLUSH_MEM;
1.104 frystyk 1264: src_req->output_stream = NULL;
1.80 frystyk 1265: src_req->output_format = WWW_SOURCE; /* We want source (for now) */
1266:
1267: /* Set up the main link in the source anchor */
1268: {
1.106 frystyk 1269: HTLink * main_link = HTAnchor_mainLink((HTAnchor *) src_anchor);
1270: HTAnchor *main_anchor = HTLink_destination(main_link);
1271: HTMethod method = HTLink_method(main_link);
1.85 frystyk 1272: if (!main_link || method==METHOD_INVALID) {
1.91 frystyk 1273: if (WWWTRACE)
1.115 eric 1274: HTTrace("Copy Anchor. No destination found or unspecified method\n");
1.80 frystyk 1275: HTRequest_delete(src_req);
1.90 frystyk 1276: return NO;
1.80 frystyk 1277: }
1.109 frystyk 1278: main_dest->GenMask |= HT_G_DATE; /* Send date header */
1.133 frystyk 1279: main_dest->reload = HT_CACHE_VALIDATE;
1.109 frystyk 1280: main_dest->method = method;
1281: main_dest->input_format = WWW_SOURCE;
1282: HTRequest_addDestination(src_req, main_dest);
1283: if (HTLoadAnchor(main_anchor, main_dest) == NO)
1284: return NO;
1.80 frystyk 1285: }
1.78 frystyk 1286:
1.80 frystyk 1287: /* For all other links in the source anchor */
1.106 frystyk 1288: if ((cur = HTAnchor_subLinks(src_anchor))) {
1289: HTLink * pres;
1.109 frystyk 1290: while ((pres = (HTLink *) HTList_nextObject(cur))) {
1.106 frystyk 1291: HTAnchor *dest = HTLink_destination(pres);
1292: HTMethod method = HTLink_method(pres);
1.80 frystyk 1293: HTRequest *dest_req;
1294: if (!dest || method==METHOD_INVALID) {
1.91 frystyk 1295: if (WWWTRACE)
1.115 eric 1296: HTTrace("Copy Anchor. Bad anchor setup %p\n",
1.80 frystyk 1297: dest);
1.90 frystyk 1298: return NO;
1.80 frystyk 1299: }
1.109 frystyk 1300: dest_req = HTRequest_dupInternal(main_dest);
1.107 frystyk 1301: dest_req->GenMask |= HT_G_DATE; /* Send date header */
1.133 frystyk 1302: dest_req->reload = HT_CACHE_VALIDATE;
1.80 frystyk 1303: dest_req->method = method;
1.104 frystyk 1304: dest_req->output_stream = NULL;
1305: dest_req->output_format = WWW_SOURCE;
1.109 frystyk 1306: HTRequest_addDestination(src_req, dest_req);
1.104 frystyk 1307:
1.90 frystyk 1308: if (HTLoadAnchor(dest, dest_req) == NO)
1309: return NO;
1.80 frystyk 1310: }
1311: }
1312: } else { /* Use the existing Post Web and restart it */
1.109 frystyk 1313: src_req = main_dest->source;
1.80 frystyk 1314: if (src_req->mainDestination)
1.124 frystyk 1315: if (launch_request(main_dest, NO) == NO)
1.90 frystyk 1316: return NO;
1.80 frystyk 1317: if (src_req->destinations) {
1.106 frystyk 1318: HTRequest * pres;
1319: cur = HTAnchor_subLinks(src_anchor);
1.80 frystyk 1320: while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) {
1.124 frystyk 1321: if (launch_request(pres, NO) == NO)
1.90 frystyk 1322: return NO;
1.80 frystyk 1323: }
1324: }
1.78 frystyk 1325: }
1326:
1.80 frystyk 1327: /* Now open the source */
1328: return HTLoadAnchor(src_anchor, src_req);
1.70 frystyk 1329: }
1330:
1.90 frystyk 1331: /* Upload an Anchor
1.70 frystyk 1332: ** ----------------
1.111 frystyk 1333: ** This function can be used to send data along with a request to a remote
1334: ** server. It can for example be used to POST form data to a remote HTTP
1335: ** server - or it can be used to post a newsletter to a NNTP server. In
1336: ** either case, you pass a callback function which the request calls when
1337: ** the remote destination is ready to accept data. In this callback
1338: ** you get the current request object and a stream into where you can
1339: ** write data. It is very important that you return the value returned
1340: ** by this stream to the Library so that it knows what to do next. The
1341: ** reason is that the outgoing stream might block or an error may
1342: ** occur and in that case the Library must know about it. The source
1343: ** anchor represents the data object in memory and it points to
1344: ** the destination anchor by using the POSTWeb method. The source anchor
1345: ** contains metainformation about the data object in memory and the
1346: ** destination anchor represents the reponse from the remote server.
1.90 frystyk 1347: ** Returns YES if request accepted, else NO
1348: */
1.111 frystyk 1349: PUBLIC BOOL HTUploadAnchor (HTAnchor * source_anchor,
1350: HTRequest * request,
1351: HTPostCallback * callback)
1352: {
1353: HTLink * link = HTAnchor_mainLink((HTAnchor *) source_anchor);
1354: HTAnchor * dest_anchor = HTLink_destination(link);
1355: HTMethod method = HTLink_method(link);
1356: if (!link || method==METHOD_INVALID || !callback) {
1357: if (WWWTRACE)
1.115 eric 1358: HTTrace("Upload...... No destination found or unspecified method\n");
1.90 frystyk 1359: return NO;
1.109 frystyk 1360: }
1.111 frystyk 1361: request->GenMask |= HT_G_DATE; /* Send date header */
1.133 frystyk 1362: request->reload = HT_CACHE_VALIDATE;
1.111 frystyk 1363: request->method = method;
1364: request->source_anchor = HTAnchor_parent(source_anchor);
1365: request->PostCallback = callback;
1366: return HTLoadAnchor(dest_anchor, request);
1367: }
1368:
1369: /* POST Callback Handler
1370: ** ---------------------
1371: ** If you do not want to handle the stream interface on your own, you
1372: ** can use this function which writes the source anchor hyperdoc to the
1373: ** target stream for the anchor upload and also handles the return value
1374: ** from the stream. If you don't want to write the source anchor hyperdoc
1375: ** then you can register your own callback function that can get the data
1376: ** you want.
1377: */
1378: PUBLIC int HTUpload_callback (HTRequest * request, HTStream * target)
1379: {
1.115 eric 1380: if (WWWTRACE) HTTrace("Uploading... from callback function\n");
1.111 frystyk 1381: if (!request || !request->source_anchor || !target) return HT_ERROR;
1382: {
1383: int status;
1384: HTParentAnchor * source = request->source_anchor;
1385: char * document = (char *) HTAnchor_document(request->source_anchor);
1386: int len = HTAnchor_length(source);
1387: if (len < 0) {
1388: len = strlen(document);
1389: HTAnchor_setLength(source, len);
1390: }
1391: status = (*target->isa->put_block)(target, document, len);
1392: if (status == HT_OK)
1393: return (*target->isa->flush)(target);
1394: if (status == HT_WOULD_BLOCK) {
1.115 eric 1395: if (PROT_TRACE)HTTrace("POST Anchor. Target WOULD BLOCK\n");
1.111 frystyk 1396: return HT_WOULD_BLOCK;
1397: } else if (status == HT_PAUSE) {
1.115 eric 1398: if (PROT_TRACE) HTTrace("POST Anchor. Target PAUSED\n");
1.111 frystyk 1399: return HT_PAUSE;
1400: } else if (status > 0) { /* Stream specific return code */
1401: if (PROT_TRACE)
1.115 eric 1402: HTTrace("POST Anchor. Target returns %d\n", status);
1.111 frystyk 1403: return status;
1.120 eric 1404: } else { /* we have a real error */
1.115 eric 1405: if (PROT_TRACE) HTTrace("POST Anchor. Target ERROR\n");
1.111 frystyk 1406: return status;
1407: }
1.70 frystyk 1408: }
1.123 frystyk 1409: }
1410:
1411: /* ------------------------------------------------------------------------- */
1412: /* HEAD METHOD */
1413: /* ------------------------------------------------------------------------- */
1414:
1415: /* Request metainformation about a document from absolute name
1416: ** -----------------------------------------------------------
1417: ** Request a document referencd by an absolute URL.
1418: ** Returns YES if request accepted, else NO
1419: */
1420: PUBLIC BOOL HTHeadAbsolute (const char * url, HTRequest * request)
1421: {
1422: if (url && request) {
1423: HTAnchor * anchor = HTAnchor_findAddress(url);
1.124 frystyk 1424: return HTHeadAnchor(anchor, request);
1.123 frystyk 1425: }
1426: return NO;
1427: }
1428:
1429: /* Request metainformation about a document from relative name
1430: ** -----------------------------------------------------------
1431: ** Request a document referenced by a relative URL. The relative URL is
1432: ** made absolute by resolving it relative to the address of the 'base'
1433: ** anchor.
1434: ** Returns YES if request accepted, else NO
1435: */
1436: PUBLIC BOOL HTHeadRelative (const char * relative,
1437: HTParentAnchor * base,
1438: HTRequest * request)
1439: {
1440: BOOL status = NO;
1441: if (relative && base && request) {
1442: char * rel = NULL;
1443: char * full_url = NULL;
1444: char * base_url = HTAnchor_address((HTAnchor *) base);
1445: StrAllocCopy(rel, relative);
1446: full_url = HTParse(HTStrip(rel), base_url,
1447: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.124 frystyk 1448: status = HTHeadAbsolute(full_url, request);
1.123 frystyk 1449: HT_FREE(rel);
1450: HT_FREE(full_url);
1451: HT_FREE(base_url);
1452: }
1453: return status;
1454: }
1455:
1456: /* Request metainformation about an anchor
1457: ** --------------------------------------
1458: ** Request the document referenced by the anchor
1459: ** Returns YES if request accepted, else NO
1460: */
1461: PUBLIC BOOL HTHeadAnchor (HTAnchor * anchor, HTRequest * request)
1462: {
1463: if (anchor && request) {
1464: HTRequest_setAnchor(request, anchor);
1465: HTRequest_setMethod(request, METHOD_HEAD);
1.124 frystyk 1466: return launch_request(request, NO);
1.123 frystyk 1467: }
1468: return NO;
1469: }
1470:
1471: /* ------------------------------------------------------------------------- */
1472: /* DELETE METHOD */
1473: /* ------------------------------------------------------------------------- */
1474:
1475: /* Delete a document on a remote server
1476: ** ------------------------------------
1477: ** Request a document referencd by an absolute URL.
1478: ** Returns YES if request accepted, else NO
1479: */
1480: PUBLIC BOOL HTDeleteAbsolute (const char * url, HTRequest * request)
1481: {
1482: if (url && request) {
1483: HTAnchor * anchor = HTAnchor_findAddress(url);
1.124 frystyk 1484: return HTDeleteAnchor(anchor, request);
1.123 frystyk 1485: }
1486: return NO;
1487: }
1488:
1489: /* Request metainformation about a document from relative name
1490: ** -----------------------------------------------------------
1491: ** Request a document referenced by a relative URL. The relative URL is
1492: ** made absolute by resolving it relative to the address of the 'base'
1493: ** anchor.
1494: ** Returns YES if request accepted, else NO
1495: */
1496: PUBLIC BOOL HTDeleteRelative (const char * relative,
1497: HTParentAnchor * base,
1498: HTRequest * request)
1499: {
1500: BOOL status = NO;
1501: if (relative && base && request) {
1502: char * rel = NULL;
1503: char * full_url = NULL;
1504: char * base_url = HTAnchor_address((HTAnchor *) base);
1505: StrAllocCopy(rel, relative);
1506: full_url = HTParse(HTStrip(rel), base_url,
1507: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.124 frystyk 1508: status = HTDeleteAbsolute(full_url, request);
1.123 frystyk 1509: HT_FREE(rel);
1510: HT_FREE(full_url);
1511: HT_FREE(base_url);
1512: }
1513: return status;
1514: }
1515:
1516: /* Request metainformation about an anchor
1517: ** --------------------------------------
1518: ** Request the document referenced by the anchor
1519: ** Returns YES if request accepted, else NO
1520: */
1521: PUBLIC BOOL HTDeleteAnchor (HTAnchor * anchor, HTRequest * request)
1522: {
1523: if (anchor && request) {
1524: HTRequest_setAnchor(request, anchor);
1525: HTRequest_setMethod(request, METHOD_DELETE);
1.124 frystyk 1526: return launch_request(request, NO);
1527: }
1528: return NO;
1529: }
1530:
1531: /* ------------------------------------------------------------------------- */
1532: /* OPTIONS METHOD */
1533: /* ------------------------------------------------------------------------- */
1534:
1535: /* Options availeble for document from absolute name
1536: ** -------------------------------------------------
1537: ** Request a document referencd by an absolute URL.
1538: ** Returns YES if request accepted, else NO
1539: */
1540: PUBLIC BOOL HTOptionsAbsolute (const char * url, HTRequest * request)
1541: {
1542: if (url && request) {
1543: HTAnchor * anchor = HTAnchor_findAddress(url);
1544: return HTOptionsAnchor(anchor, request);
1545: }
1546: return NO;
1547: }
1548:
1549: /* Options available for document from relative name
1550: ** -------------------------------------------------
1551: ** Request a document referenced by a relative URL. The relative URL is
1552: ** made absolute by resolving it relative to the address of the 'base'
1553: ** anchor.
1554: ** Returns YES if request accepted, else NO
1555: */
1556: PUBLIC BOOL HTOptionsRelative (const char * relative,
1557: HTParentAnchor * base,
1558: HTRequest * request)
1559: {
1560: BOOL status = NO;
1561: if (relative && base && request) {
1562: char * rel = NULL;
1563: char * full_url = NULL;
1564: char * base_url = HTAnchor_address((HTAnchor *) base);
1565: StrAllocCopy(rel, relative);
1566: full_url = HTParse(HTStrip(rel), base_url,
1567: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1568: status = HTOptionsAbsolute(full_url, request);
1569: HT_FREE(rel);
1570: HT_FREE(full_url);
1571: HT_FREE(base_url);
1572: }
1573: return status;
1574: }
1575:
1576: /* Options available for document using Anchor
1577: ** -------------------------------------------
1578: ** Request the document referenced by the anchor
1579: ** Returns YES if request accepted, else NO
1580: */
1581: PUBLIC BOOL HTOptionsAnchor (HTAnchor * anchor, HTRequest * request)
1582: {
1583: if (anchor && request) {
1584: HTRequest_setAnchor(request, anchor);
1585: HTRequest_setMethod(request, METHOD_OPTIONS);
1586: return launch_request(request, NO);
1.123 frystyk 1587: }
1588: return NO;
1.1 timbl 1589: }
1.127 frystyk 1590:
1591: /* ------------------------------------------------------------------------- */
1592: /* TRACE METHOD */
1593: /* ------------------------------------------------------------------------- */
1594:
1595: /* Traces available for document from absolute name
1596: ** ------------------------------------------------
1597: ** Request a document referencd by an absolute URL.
1598: ** Returns YES if request accepted, else NO
1599: */
1600: PUBLIC BOOL HTTraceAbsolute (const char * url, HTRequest * request)
1601: {
1602: if (url && request) {
1603: HTAnchor * anchor = HTAnchor_findAddress(url);
1604: return HTTraceAnchor(anchor, request);
1605: }
1606: return NO;
1607: }
1608:
1609: /* Traces available for document from relative name
1610: ** ------------------------------------------------
1611: ** Request a document referenced by a relative URL. The relative URL is
1612: ** made absolute by resolving it relative to the address of the 'base'
1613: ** anchor.
1614: ** Returns YES if request accepted, else NO
1615: */
1616: PUBLIC BOOL HTTraceRelative (const char * relative,
1617: HTParentAnchor * base,
1618: HTRequest * request)
1619: {
1620: BOOL status = NO;
1621: if (relative && base && request) {
1622: char * rel = NULL;
1623: char * full_url = NULL;
1624: char * base_url = HTAnchor_address((HTAnchor *) base);
1625: StrAllocCopy(rel, relative);
1626: full_url = HTParse(HTStrip(rel), base_url,
1627: PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1628: status = HTTraceAbsolute(full_url, request);
1629: HT_FREE(rel);
1630: HT_FREE(full_url);
1631: HT_FREE(base_url);
1632: }
1633: return status;
1634: }
1635:
1636: /* Trace available for document using Anchor
1637: ** -------------------------------------------
1638: ** Request the document referenced by the anchor
1639: ** Returns YES if request accepted, else NO
1640: */
1641: PUBLIC BOOL HTTraceAnchor (HTAnchor * anchor, HTRequest * request)
1642: {
1643: if (anchor && request) {
1644: HTRequest_setAnchor(request, anchor);
1645: HTRequest_setMethod(request, METHOD_TRACE);
1646: return launch_request(request, NO);
1647: }
1648: return NO;
1649: }
1650:
Webmaster