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