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