Annotation of libwww/Library/src/HTAccess.c, revision 1.126

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.126   ! frystyk     6: **     @(#) $Id: HTAccess.c,v 1.125 1996/08/05 17:22:05 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&#38;y=2"' or `<a
                    300: **         href="http://host/?x=1&amp;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:     {
1.126   ! frystyk   525:        BOOL chunking = NO;
1.123     frystyk   526:        int status;
                    527:        char * document = (char *) HTAnchor_document(entity);
                    528:        int len = HTAnchor_length(entity);
1.126   ! frystyk   529:        if (!document) {
        !           530:            if (PROT_TRACE) HTTrace("Posting Data No document\n");
        !           531:            return HT_ERROR;
        !           532:        }
        !           533: 
        !           534:        /*
        !           535:        ** If the length is unknown (-1) then see if the document is a text
        !           536:        ** type and in that case take the strlen. If not then we don't know
        !           537:        ** how much data we can write and must stop
        !           538:        */
        !           539:        if (len < 0) {
        !           540:            HTFormat actual = HTAnchor_format(entity);
        !           541:            HTFormat tmplate = HTAtom_for("text/*");
        !           542:            if (HTMIMEMatch(tmplate, actual)) {
        !           543:                len = strlen(document);                 /* Naive! */
        !           544:                chunking = YES;
        !           545:            } else {
        !           546:                if (PROT_TRACE)
        !           547:                    HTTrace("Posting Data Must know the length of document %p\n",
        !           548:                            document);
        !           549:                return HT_ERROR;
        !           550:            }
        !           551:        }
        !           552: 
        !           553:        /* Send the data down the pipe */
1.123     frystyk   554:        status = (*target->isa->put_block)(target, document, len);
1.125     frystyk   555:        if (status == HT_LOADED) {
                    556:            if (PROT_TRACE) HTTrace("Posting Data Target is SAVED\n");
                    557:            (*target->isa->flush)(target);
                    558:            return HT_LOADED;
                    559:        }
1.123     frystyk   560:        if (status == HT_WOULD_BLOCK) {
1.124     frystyk   561:            if (PROT_TRACE)HTTrace("Posting Data Target WOULD BLOCK\n");
1.123     frystyk   562:            return HT_WOULD_BLOCK;
                    563:        } else if (status == HT_PAUSE) {
1.126   ! frystyk   564:            if (PROT_TRACE) HTTrace("Posting Data Target PAUSED\n");
1.123     frystyk   565:            return HT_PAUSE;
1.126   ! frystyk   566:        } else if (chunking && status == HT_OK) {
        !           567:            if (PROT_TRACE) HTTrace("Posting Data Target is SAVED using chunked\n");
        !           568:            return (*target->isa->put_block)(target, "", 0);
        !           569:        } else if (status > 0) {              /* Stream specific return code */
1.123     frystyk   570:            if (PROT_TRACE)
1.124     frystyk   571:                HTTrace("Posting Data. Target returns %d\n", status);
1.123     frystyk   572:            return status;
                    573:        } else {                                     /* we have a real error */
1.124     frystyk   574:            if (PROT_TRACE) HTTrace("Posting Data Target ERROR\n");
1.123     frystyk   575:            return status;
                    576:        }
                    577:     }
                    578: }
                    579: 
                    580: /*     Send a Form request using POST from absolute name
                    581: **     -------------------------------------------------
                    582: **     Request a document referencd by an absolute URL appended with the
                    583: **     formdata given. The URL can NOT contain any fragment identifier!
                    584: **     The list of form data must be given as an association list where 
                    585: **     the name is the field name and the value is the value of the field.
                    586: */
                    587: PUBLIC HTParentAnchor * HTPostFormAbsolute (HTAssocList *      formdata,
                    588:                                            const char *        base,
                    589:                                            HTRequest *         request)
                    590: {
                    591:     if (formdata && base && request) {
                    592:        HTAnchor * anchor = HTAnchor_findAddress(base);
                    593:        return HTPostFormAnchor(formdata, anchor, request);
                    594:     }
                    595:     return NULL;
                    596: }
                    597: 
                    598: /*     Send a Form request using POST from relative name
                    599: **     -------------------------------------------------
                    600: **     Request a document referencd by a relative URL appended with the
                    601: **     formdata given. The URL can NOT contain any fragment identifier!
                    602: **     The list of form data must be given as an association list where 
                    603: **     the name is the field name and the value is the value of the field.
                    604: */
                    605: PUBLIC HTParentAnchor * HTPostFormRelative (HTAssocList *      formdata,
                    606:                                            const char *        relative,
                    607:                                            HTParentAnchor *    base,
                    608:                                            HTRequest *         request)
1.90      frystyk   609: {
1.123     frystyk   610:     HTParentAnchor * postanchor = NULL;
                    611:     if (formdata && relative && base && request) {
                    612:        char * full_url = NULL;
                    613:        char * base_url = HTAnchor_address((HTAnchor *) base);
                    614:        full_url=HTParse(relative, base_url,
                    615:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    616:        postanchor = HTPostFormAbsolute(formdata, full_url, request);
                    617:        HT_FREE(full_url);
                    618:        HT_FREE(base_url);
                    619:     }
                    620:     return postanchor;
                    621: }
                    622: 
                    623: /*     Send a Form request using POST from an anchor
                    624: **     ---------------------------------------------
                    625: **     Request a document referencd by an anchor object appended with the
                    626: **     formdata given. The URL can NOT contain any fragment identifier!
                    627: **     The list of form data must be given as an association list where 
                    628: **     the name is the field name and the value is the value of the field.
                    629: */
                    630: PUBLIC HTParentAnchor * HTPostFormAnchor (HTAssocList *        formdata,
                    631:                                          HTAnchor *    anchor,
                    632:                                          HTRequest *   request)
                    633: {
                    634:     HTParentAnchor * postanchor = NULL;
                    635:     if (formdata && anchor && request) {
                    636:        HTUserProfile * up = HTRequest_userProfile(request);
                    637:        char * tmpfile = HTGetTmpFileName(HTUserProfile_tmp(up));
                    638:        char * tmpurl = HTParse(tmpfile, "file:", PARSE_ALL);
                    639:        char * form_encoded = form_url_encode(NULL, formdata);
                    640:        if (form_encoded) {
                    641: 
                    642:            /*
                    643:            **  Now create a new anchor for the post data and set up
                    644:            **  the rest of the metainformation we know about this anchor. The
                    645:            **  tmp anchor may actually already exist from previous postings.
                    646:            */
                    647:            postanchor = (HTParentAnchor *) HTAnchor_findAddress(tmpurl);
                    648:            HTAnchor_clearHeader(postanchor);
                    649:            HTAnchor_setDocument(postanchor, form_encoded);
                    650:            HTAnchor_setLength(postanchor, strlen(form_encoded));
                    651:            HTAnchor_setFormat(postanchor, WWW_FORM);
                    652: 
                    653:            /*
                    654:            **  Bind the temporary anchor to the anchor that will contain the
                    655:            **  response 
                    656:            */
                    657:            HTLink_removeAll((HTAnchor *) postanchor);
                    658:            HTLink_add((HTAnchor *) postanchor, (HTAnchor *) anchor, 
                    659:                       NULL, METHOD_POST);
                    660: 
                    661:            /* Set up the request object */
                    662:            HTRequest_addGnHd(request, HT_G_DATE);       /* Send date header */
                    663:            HTRequest_setAnchor(request, anchor);
                    664:            HTRequest_setEntityAnchor(request, postanchor);
                    665:            HTRequest_setMethod(request, METHOD_POST);
                    666: 
                    667:            /* Add the post form callback function to provide the form data */
                    668:            HTRequest_setPostCallback(request, HTEntity_callback);
                    669: 
                    670:            /* Now start the load normally */
1.124     frystyk   671:            launch_request(request, NO);
1.123     frystyk   672:        }
                    673:        HT_FREE(tmpfile);
                    674:        HT_FREE(tmpurl);
1.90      frystyk   675:     }
1.123     frystyk   676:     return postanchor;
1.57      howcome   677: }
                    678: 
1.70      frystyk   679: /* --------------------------------------------------------------------------*/
1.124     frystyk   680: /*                             PUT A DOCUMENT                               */
                    681: /* --------------------------------------------------------------------------*/ 
1.125     frystyk   682: PRIVATE BOOL setup_anchors (HTRequest * request,
1.124     frystyk   683:                        HTParentAnchor * source, HTParentAnchor * dest)
                    684: {
                    685:     /*
                    686:     **  Check whether we know if it is possible to PUT to this destination
                    687:     */
                    688:     {
                    689:        HTMethod allowed = HTAnchor_methods(dest);
                    690:        if (!(allowed & METHOD_PUT)) {
                    691:            HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
                    692:            if (prompt) {
                    693:                if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_METHOD,
                    694:                              NULL, NULL, NULL) != YES)
                    695:                    return NO;
                    696:            }
                    697:        }
                    698:     }
                    699: 
                    700:     /*
                    701:     **  Bind the source anchor to the dest anchor that will contain the
                    702:     **  response. If link already exists then ask is we should do it again.
                    703:     **  If so then remove the old link and replace it with a new one.
                    704:     */
                    705:     {
                    706:        HTLink *link = HTLink_find((HTAnchor *) source, (HTAnchor *) dest);
                    707:        if (link && HTLink_method(link) == METHOD_PUT) {
                    708:            HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
                    709:            if (prompt) {
                    710:                if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_REDO,
                    711:                              NULL, NULL, NULL) != YES)
                    712:                    return NO;
                    713:            }
                    714:            HTLink_remove((HTAnchor *) source, (HTAnchor *) dest);
                    715:        }
                    716:        HTLink_add((HTAnchor*) source, (HTAnchor*) dest, NULL, METHOD_PUT);
                    717:     }
                    718:     return YES;
                    719: }
                    720: 
                    721: /*     Send an Anchor using PUT from absolute name
                    722: **     -------------------------------------------
                    723: **     Upload a document referenced by an absolute URL appended.
                    724: **     The URL can NOT contain any fragment identifier!
                    725: **     The list of form data must be given as an association list where 
                    726: **     the name is the field name and the value is the value of the field.
                    727: */
                    728: PUBLIC BOOL HTPutAbsolute (HTParentAnchor *    source,
                    729:                           const char *         destination,
                    730:                           HTRequest *          request)
                    731: {
                    732:     if (source && destination && request) {
                    733:        HTAnchor * dest = HTAnchor_findAddress(destination);
                    734:        return HTPutAnchor(source, dest, request);
                    735:     }
                    736:     return NO;
                    737: }
                    738: 
                    739: /*     Send an Anchor using PUT from relative name
                    740: **     -------------------------------------------
                    741: **     Upload a document referenced by a relative URL appended.
                    742: **     The URL can NOT contain any fragment identifier!
                    743: **     The list of form data must be given as an association list where 
                    744: **     the name is the field name and the value is the value of the field.
                    745: */
                    746: PUBLIC BOOL HTPutRelative (HTParentAnchor *    source,
                    747:                           const char *         relative,
                    748:                           HTParentAnchor *     destination_base,
                    749:                           HTRequest *          request)
                    750: {
                    751:     if (source && relative && destination_base && request) {
                    752:        BOOL status;
                    753:        char * full_url = NULL;
                    754:        char * base_url = HTAnchor_address((HTAnchor *) destination_base);
                    755:        full_url=HTParse(relative, base_url,
                    756:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    757:        status = HTPutAbsolute(source, full_url, request);
                    758:        HT_FREE(full_url);
                    759:        HT_FREE(base_url);
                    760:        return status;
                    761:     }
                    762:     return NO;
                    763: }
                    764: 
                    765: /*     Send an Anchor using PUT from an anchor
                    766: **     ---------------------------------------
                    767: **     Upload a document referenced by an anchor object appended
                    768: **     The URL can NOT contain any fragment identifier!
                    769: **     The list of form data must be given as an association list where 
                    770: **     the name is the field name and the value is the value of the field.
                    771: */
                    772: PUBLIC BOOL HTPutAnchor (HTParentAnchor *      source,
                    773:                         HTAnchor *             destination,
                    774:                         HTRequest *            request)
                    775: {
                    776:     HTParentAnchor * dest = HTAnchor_parent(destination);
                    777:     if (source && dest && request) {
1.125     frystyk   778:        if (setup_anchors(request, source, dest) == YES) {
1.124     frystyk   779: 
                    780:            /* Set up the request object */
                    781:            HTRequest_addGnHd(request, HT_G_DATE);
                    782:            HTRequest_setEntityAnchor(request, source);
                    783:            HTRequest_setMethod(request, METHOD_PUT);
                    784:            HTRequest_setAnchor(request, destination);
                    785: 
                    786:            /* Add the entity callback function to provide the form data */
                    787:            HTRequest_setPostCallback(request, HTEntity_callback);
                    788: 
                    789:            /* Now start the load normally */
                    790:            return launch_request(request, NO);
                    791:        }
                    792:     }
                    793:     return NO;
                    794: }
                    795: 
1.125     frystyk   796: /*     Send an Anchor using POST from absolute name
                    797: **     -------------------------------------------
                    798: **     Upload a document referenced by an absolute URL appended.
                    799: **     The URL can NOT contain any fragment identifier!
                    800: **     The list of form data must be given as an association list where 
                    801: **     the name is the field name and the value is the value of the field.
                    802: */
                    803: PUBLIC BOOL HTPostAbsolute (HTParentAnchor *   source,
                    804:                           const char *         destination,
                    805:                           HTRequest *          request)
                    806: {
                    807:     if (source && destination && request) {
                    808:        HTAnchor * dest = HTAnchor_findAddress(destination);
                    809:        return HTPostAnchor(source, dest, request);
                    810:     }
                    811:     return NO;
                    812: }
                    813: 
                    814: /*     Send an Anchor using POST from relative name
                    815: **     -------------------------------------------
                    816: **     Upload a document referenced by a relative URL appended.
                    817: **     The URL can NOT contain any fragment identifier!
                    818: **     The list of form data must be given as an association list where 
                    819: **     the name is the field name and the value is the value of the field.
                    820: */
                    821: PUBLIC BOOL HTPostRelative (HTParentAnchor *   source,
                    822:                           const char *         relative,
                    823:                           HTParentAnchor *     destination_base,
                    824:                           HTRequest *          request)
                    825: {
                    826:     if (source && relative && destination_base && request) {
                    827:        BOOL status;
                    828:        char * full_url = NULL;
                    829:        char * base_url = HTAnchor_address((HTAnchor *) destination_base);
                    830:        full_url=HTParse(relative, base_url,
                    831:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    832:        status = HTPostAbsolute(source, full_url, request);
                    833:        HT_FREE(full_url);
                    834:        HT_FREE(base_url);
                    835:        return status;
                    836:     }
                    837:     return NO;
                    838: }
                    839: 
                    840: /*     Send an Anchor using POST from an anchor
                    841: **     ---------------------------------------
                    842: **     Upload a document referenced by an anchor object appended
                    843: **     The URL can NOT contain any fragment identifier!
                    844: **     The list of form data must be given as an association list where 
                    845: **     the name is the field name and the value is the value of the field.
                    846: */
                    847: PUBLIC BOOL HTPostAnchor (HTParentAnchor *     source,
                    848:                         HTAnchor *             destination,
                    849:                         HTRequest *            request)
                    850: {
                    851:     HTParentAnchor * dest = HTAnchor_parent(destination);
                    852:     if (source && dest && request) {
                    853:        if (setup_anchors(request, source, dest) == YES) {
                    854: 
                    855:            /* Set up the request object */
                    856:            HTRequest_addGnHd(request, HT_G_DATE);
                    857:            HTRequest_setEntityAnchor(request, source);
                    858:            HTRequest_setMethod(request, METHOD_POST);
                    859:            HTRequest_setAnchor(request, destination);
                    860: 
                    861:            /* Add the entity callback function to provide the form data */
                    862:            HTRequest_setPostCallback(request, HTEntity_callback);
                    863: 
                    864:            /* Now start the load normally */
                    865:            return launch_request(request, NO);
                    866:        }
                    867:     }
                    868:     return NO;
                    869: }
                    870: 
1.124     frystyk   871: /*     Send an Anchor using PUT from absolute name
                    872: **     -------------------------------------------
                    873: **     Upload a document referenced by an absolute URL appended.
                    874: **     The URL can NOT contain any fragment identifier!
                    875: **     The list of form data must be given as an association list where 
                    876: **     the name is the field name and the value is the value of the field.
                    877: */
                    878: PUBLIC BOOL HTPutStructuredAbsolute (HTParentAnchor *  source,
                    879:                                     const char *       destination,
                    880:                                     HTRequest *        request,
                    881:                                     HTPostCallback *   input)
                    882: {
                    883:     if (source && destination && request && input) {
                    884:        HTAnchor * dest = HTAnchor_findAddress(destination);
                    885:        return HTPutStructuredAnchor(source, dest, request, input);
                    886:     }
                    887:     return NO;
                    888: }
                    889: 
                    890: /*     Send an Anchor using PUT from relative name
                    891: **     -------------------------------------------
                    892: **     Upload a document referenced by a relative URL appended.
                    893: **     The URL can NOT contain any fragment identifier!
                    894: **     The list of form data must be given as an association list where 
                    895: **     the name is the field name and the value is the value of the field.
                    896: */
                    897: PUBLIC BOOL HTPutStructuredRelative (HTParentAnchor *  source,
                    898:                                     const char *       relative,
                    899:                                     HTParentAnchor *   destination_base,
                    900:                                     HTRequest *        request,
                    901:                                     HTPostCallback *   input)
                    902: {
                    903:     if (source && relative && destination_base && request && input) {
                    904:        BOOL status;
                    905:        char * full_url = NULL;
                    906:        char * base_url = HTAnchor_address((HTAnchor *) destination_base);
                    907:        full_url=HTParse(relative, base_url,
                    908:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                    909:        status = HTPutStructuredAbsolute(source, full_url, request, input);
                    910:        HT_FREE(full_url);
                    911:        HT_FREE(base_url);
                    912:        return status;
                    913:     }
                    914:     return NO;
                    915: }
                    916: 
                    917: /*     Send an Anchor using PUT from an anchor
                    918: **     ---------------------------------------
                    919: **     Upload a document referenced by an anchor object appended
                    920: **     The URL can NOT contain any fragment identifier!
                    921: **     The list of form data must be given as an association list where 
                    922: **     the name is the field name and the value is the value of the field.
                    923: */
                    924: PUBLIC BOOL HTPutStructuredAnchor (HTParentAnchor *    source,
                    925:                                   HTAnchor *           destination,
                    926:                                   HTRequest *          request,
                    927:                                   HTPostCallback *     input)
                    928: {
                    929:     HTParentAnchor * dest = HTAnchor_parent(destination);
                    930:     if (source && dest && request) {
1.125     frystyk   931:        if (setup_anchors(request, source, dest) == YES) {
1.124     frystyk   932: 
                    933:            /* Set up the request object */
                    934:            HTRequest_addGnHd(request, HT_G_DATE);
                    935:            HTRequest_setEntityAnchor(request, source);
                    936:            HTRequest_setMethod(request, METHOD_PUT);
                    937:            HTRequest_setAnchor(request, destination);
                    938: 
                    939:            /* Add the entity callback function to provide the form data */
                    940:            HTRequest_setPostCallback(request, input);
                    941: 
                    942:            /* Now start the load normally */
                    943:            return launch_request(request, NO);
                    944:        }
                    945:     }
                    946:     return NO;
                    947: }
                    948: 
                    949: /*
                    950: **     After filter for handling PUT of document. We should now have the 
                    951: */
                    952: PRIVATE int HTSaveFilter (HTRequest * request, void * param, int status)
                    953: {
                    954:     HTPutContext * me = (HTPutContext *) param;
                    955:     if (APP_TRACE)
                    956:        HTTrace("Save Filter. Using context %p with state %c\n",
                    957:                me, me->state+0x30);
                    958: 
                    959:     /*
1.125     frystyk   960:     **  Just ignore authentication in the hope that some other filter will
                    961:     **  handle this.
                    962:     */
                    963:     if (status == HT_NO_ACCESS || status == HT_NO_PROXY_ACCESS) {
                    964:        if (APP_TRACE) HTTrace("Save Filter. Waiting for authentication\n");
                    965:        return HT_OK;
                    966:     }
                    967: 
                    968:     /*
1.124     frystyk   969:     **  If either the source or the destination has moved then ask the user
                    970:     **  what to do. If there is no user then stop
                    971:     */
                    972:     if (status == HT_TEMP_REDIRECT || status == HT_PERM_REDIRECT) {
                    973:        HTAlertCallback * prompt = HTAlert_find(HT_A_CONFIRM);
                    974:        HTAnchor * redirection = HTRequest_redirection(request);
                    975:        if (prompt && redirection) {
1.125     frystyk   976:            if (me->state == HT_LOAD_SOURCE) {
1.124     frystyk   977:                if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_SOURCE_MOVED,
                    978:                              NULL, NULL, NULL) == YES) {
                    979:                    me->source = HTAnchor_parent(redirection);
                    980:                    return HT_OK;
                    981:                }
                    982:            } else {
                    983:                if ((*prompt)(request, HT_A_CONFIRM, HT_MSG_DESTINATION_MOVED,
                    984:                              NULL, NULL, NULL) == YES) {
                    985:                    me->destination = redirection;
                    986:                    return HT_OK;
                    987:                }
                    988:            }
                    989:        }
                    990:     }
                    991: 
                    992:     /*
1.125     frystyk   993:     ** If we succeeded getting the source then start the PUT itself. Otherwise
                    994:     ** cleanup the mess
1.124     frystyk   995:     */
1.125     frystyk   996:     if (me->state == HT_LOAD_SOURCE && status == HT_LOADED &&
                    997:        !HTError_hasSeverity(HTRequest_error(request), ERR_INFO)) {
1.124     frystyk   998: 
                    999:        /* Swap the document in the anchor with the new one */
                   1000:        me->placeholder = HTAnchor_document(me->source);
                   1001:        HTAnchor_setDocument(me->source, HTChunk_data(me->document));
                   1002: 
                   1003:        /* Set up the request object */
                   1004:        HTRequest_addGnHd(request, HT_G_DATE);
                   1005:        HTRequest_setEntityAnchor(request, me->source);
                   1006:        HTRequest_setMethod(request, METHOD_PUT);
                   1007:        HTRequest_setAnchor(request, me->destination);
                   1008:        HTRequest_setOutputFormat(request, me->format);
                   1009:        HTRequest_setOutputStream(request, me->target);
                   1010: 
1.125     frystyk  1011:        /* Turn progress notifications back on */
                   1012:        HTRequest_setInternal(request, NO);
                   1013: 
1.124     frystyk  1014:        /* Add the entity callback function to provide the form data */
                   1015:        HTRequest_setPostCallback(request, HTEntity_callback);
                   1016: 
                   1017:        /* Now start the load normally */
                   1018:        me->state = launch_request(request, NO) ?
1.125     frystyk  1019:            HT_SAVE_DEST : HT_LOAD_SOURCE;
1.124     frystyk  1020: 
                   1021:        /*
                   1022:        **  By returning HT_ERROR we make sure that this is the last handler to
                   1023:        **  be called. We do this as we don't want any other filter to delete
                   1024:        **  the request object now when we have just started a new one
                   1025:        **  ourselves
                   1026:        */      
                   1027:        return HT_ERROR;
                   1028: 
                   1029:     } else {
                   1030:        HTRequest_deleteAfter(request, HTSaveFilter);
                   1031:        HTAnchor_setDocument(me->source, me->placeholder);
                   1032:        HTChunk_delete(me->document);
                   1033:        HT_FREE(me);
                   1034:     }
                   1035:     return HT_OK;
                   1036: }
                   1037: 
                   1038: /*     Send an Anchor using PUT from absolute name
                   1039: **     -------------------------------------------
                   1040: **     Upload a document referenced by an absolute URL appended.
                   1041: **     The URL can NOT contain any fragment identifier!
                   1042: **     The list of form data must be given as an association list where 
                   1043: **     the name is the field name and the value is the value of the field.
                   1044: */
                   1045: PUBLIC BOOL HTPutDocumentAbsolute (HTParentAnchor *    source,
                   1046:                                   const char *         destination,
                   1047:                                   HTRequest *          request)
                   1048: {
                   1049:     if (source && destination && request) {
                   1050:        HTAnchor * dest = HTAnchor_findAddress(destination);
                   1051:        return HTPutDocumentAnchor(source, dest, request);
                   1052:     }
                   1053:     return NO;
                   1054: }
                   1055: 
                   1056: /*     Send an Anchor using PUT from relative name
                   1057: **     -------------------------------------------
                   1058: **     Upload a document referenced by a relative URL appended.
                   1059: **     The URL can NOT contain any fragment identifier!
                   1060: **     The list of form data must be given as an association list where 
                   1061: **     the name is the field name and the value is the value of the field.
                   1062: */
                   1063: PUBLIC BOOL HTPutDocumentRelative (HTParentAnchor *    source,
                   1064:                                   const char *         relative,
                   1065:                                   HTParentAnchor *     destination_base,
                   1066:                                   HTRequest *          request)
                   1067: {
                   1068:     if (source && relative && destination_base && request) {
                   1069:        BOOL status;
                   1070:        char * full_url = NULL;
                   1071:        char * base_url = HTAnchor_address((HTAnchor *) destination_base);
                   1072:        full_url=HTParse(relative, base_url,
                   1073:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                   1074:        status = HTPutDocumentAbsolute(source, full_url, request);
                   1075:        HT_FREE(full_url);
                   1076:        HT_FREE(base_url);
                   1077:        return status;
                   1078:     }
                   1079:     return NO;
                   1080: }
                   1081: 
                   1082: /*     Send an Anchor using PUT from an anchor
                   1083: **     ---------------------------------------
                   1084: **     Upload a document referenced by an anchor object appended
                   1085: **     The URL can NOT contain any fragment identifier!
                   1086: **     The source document is first loaded into memory and then the PUT
                   1087: **     to the remote server is done using the memory version
                   1088: */
                   1089: PUBLIC BOOL HTPutDocumentAnchor (HTParentAnchor *      source,
                   1090:                                 HTAnchor *             destination,
                   1091:                                 HTRequest *            request)
                   1092: {
                   1093:     HTParentAnchor * dest = HTAnchor_parent(destination);
                   1094:     if (source && dest && request) {
1.125     frystyk  1095:        if (setup_anchors(request, source, dest) == YES) {
1.124     frystyk  1096:            HTPutContext * context = NULL;
                   1097: 
                   1098:            /*
                   1099:            **  First we register an AFTER filter that can check the result
                   1100:            **  of the source load if success then it can start the PUT
                   1101:            ** operation to the destination.
                   1102:            */
                   1103:            if (!(context=(HTPutContext *) HT_CALLOC(1, sizeof(HTPutContext))))
                   1104:                HT_OUTOFMEM("HTPutDocumentAnchor");
                   1105:            context->source = source;
                   1106:            context->destination = destination;
                   1107:            HTRequest_addAfter(request, HTSaveFilter, context, HT_ALL, NO);
1.125     frystyk  1108: 
                   1109:            /* Turn off progress notifications */
                   1110:            HTRequest_setInternal(request, YES);
1.124     frystyk  1111: 
                   1112:            /*
                   1113:            **  We make sure that we are not using a memory cached element.
                   1114:            **  It's OK to use a file cached element!
                   1115:            */
                   1116:            HTRequest_setReloadMode(request, HT_MEM_REFRESH);
                   1117: 
                   1118:            /*
                   1119:            ** Now we load the source document into a chunk. We specify that
                   1120:            ** we want the document ASIS from the source location. 
                   1121:            */
                   1122:            context->format = HTRequest_outputFormat(request);
                   1123:            context->target = HTRequest_outputStream(request);
                   1124:            HTRequest_setOutputFormat(request, WWW_SOURCE);
                   1125:            context->document = HTLoadAnchorToChunk((HTAnchor*)source,request);
                   1126:            if (context->document == NULL) {
                   1127:                if (APP_TRACE) HTTrace("Put Document No source\n");
                   1128:                HT_FREE(context);
                   1129:                return NO;
                   1130:            }
                   1131:            return YES;
                   1132:        }
                   1133:     }
                   1134:     return NO;
                   1135: }
                   1136: 
                   1137: /* ------------------------------------------------------------------------- */
1.70      frystyk  1138: 
1.90      frystyk  1139: /*     Copy an anchor
1.70      frystyk  1140: **     --------------
1.90      frystyk  1141: **     Fetch the URL (possibly local file URL) and send it using either PUT
                   1142: **     or POST to the remote destination using HTTP. The caller can decide the
                   1143: **     exact method used and which HTTP header fields to transmit by setting
                   1144: **     the user fields in the request structure.
1.92      frystyk  1145: **     If posting to NNTP then we can't dispatch at this level but must pass
                   1146: **     the source anchor to the news module that then takes all the refs
                   1147: **     to NNTP and puts into the "newsgroups" header
1.70      frystyk  1148: */
1.109     frystyk  1149: PUBLIC BOOL HTCopyAnchor (HTAnchor * src_anchor, HTRequest * main_dest)
1.80      frystyk  1150: { 
1.106     frystyk  1151:     HTRequest * src_req;
                   1152:     HTList * cur;
1.109     frystyk  1153:     if (!src_anchor || !main_dest) {
1.115     eric     1154:        if (WWWTRACE) HTTrace("Copy........ BAD ARGUMENT\n");
1.90      frystyk  1155:        return NO;
1.109     frystyk  1156:     }
1.70      frystyk  1157: 
1.112     frystyk  1158:     /* Set the source anchor */
                   1159:     main_dest->source_anchor = HTAnchor_parent(src_anchor);
                   1160: 
1.80      frystyk  1161:     /* Build the POST web if not already there */
1.109     frystyk  1162:     if (!main_dest->source) {
                   1163:        src_req = HTRequest_dupInternal(main_dest);       /* Get a duplicate */
1.80      frystyk  1164:        HTAnchor_clearHeader((HTParentAnchor *) src_anchor);
1.109     frystyk  1165:        src_req->method = METHOD_GET;
1.85      frystyk  1166:        src_req->reload = HT_MEM_REFRESH;
1.104     frystyk  1167:        src_req->output_stream = NULL;
1.80      frystyk  1168:        src_req->output_format = WWW_SOURCE;     /* We want source (for now) */
                   1169: 
                   1170:        /* Set up the main link in the source anchor */
                   1171:        {
1.106     frystyk  1172:            HTLink * main_link = HTAnchor_mainLink((HTAnchor *) src_anchor);
                   1173:            HTAnchor *main_anchor = HTLink_destination(main_link);
                   1174:            HTMethod method = HTLink_method(main_link);
1.85      frystyk  1175:            if (!main_link || method==METHOD_INVALID) {
1.91      frystyk  1176:                if (WWWTRACE)
1.115     eric     1177:                    HTTrace("Copy Anchor. No destination found or unspecified method\n");
1.80      frystyk  1178:                HTRequest_delete(src_req);
1.90      frystyk  1179:                return NO;
1.80      frystyk  1180:            }
1.109     frystyk  1181:            main_dest->GenMask |= HT_G_DATE;             /* Send date header */
                   1182:            main_dest->reload = HT_CACHE_REFRESH;
                   1183:            main_dest->method = method;
                   1184:            main_dest->input_format = WWW_SOURCE;
                   1185:            HTRequest_addDestination(src_req, main_dest);
                   1186:            if (HTLoadAnchor(main_anchor, main_dest) == NO)
                   1187:                return NO;
1.80      frystyk  1188:        }
1.78      frystyk  1189: 
1.80      frystyk  1190:        /* For all other links in the source anchor */
1.106     frystyk  1191:        if ((cur = HTAnchor_subLinks(src_anchor))) {
                   1192:            HTLink * pres;
1.109     frystyk  1193:            while ((pres = (HTLink *) HTList_nextObject(cur))) {
1.106     frystyk  1194:                HTAnchor *dest = HTLink_destination(pres);
                   1195:                HTMethod method = HTLink_method(pres);
1.80      frystyk  1196:                HTRequest *dest_req;
                   1197:                if (!dest || method==METHOD_INVALID) {
1.91      frystyk  1198:                    if (WWWTRACE)
1.115     eric     1199:                        HTTrace("Copy Anchor. Bad anchor setup %p\n",
1.80      frystyk  1200:                                dest);
1.90      frystyk  1201:                    return NO;
1.80      frystyk  1202:                }
1.109     frystyk  1203:                dest_req = HTRequest_dupInternal(main_dest);
1.107     frystyk  1204:                dest_req->GenMask |= HT_G_DATE;          /* Send date header */
1.85      frystyk  1205:                dest_req->reload = HT_CACHE_REFRESH;
1.80      frystyk  1206:                dest_req->method = method;
1.104     frystyk  1207:                dest_req->output_stream = NULL;
                   1208:                dest_req->output_format = WWW_SOURCE;
1.109     frystyk  1209:                HTRequest_addDestination(src_req, dest_req);
1.104     frystyk  1210: 
1.90      frystyk  1211:                if (HTLoadAnchor(dest, dest_req) == NO)
                   1212:                    return NO;
1.80      frystyk  1213:            }
                   1214:        }
                   1215:     } else {                    /* Use the existing Post Web and restart it */
1.109     frystyk  1216:        src_req = main_dest->source;
1.80      frystyk  1217:        if (src_req->mainDestination)
1.124     frystyk  1218:            if (launch_request(main_dest, NO) == NO)
1.90      frystyk  1219:                return NO;
1.80      frystyk  1220:        if (src_req->destinations) {
1.106     frystyk  1221:            HTRequest * pres;
                   1222:            cur = HTAnchor_subLinks(src_anchor);
1.80      frystyk  1223:            while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) {
1.124     frystyk  1224:                if (launch_request(pres, NO) == NO)
1.90      frystyk  1225:                    return NO;
1.80      frystyk  1226:            }
                   1227:        }
1.78      frystyk  1228:     }
                   1229: 
1.80      frystyk  1230:     /* Now open the source */
                   1231:     return HTLoadAnchor(src_anchor, src_req);
1.70      frystyk  1232: }
                   1233: 
1.90      frystyk  1234: /*     Upload an Anchor
1.70      frystyk  1235: **     ----------------
1.111     frystyk  1236: **     This function can be used to send data along with a request to a remote
                   1237: **     server. It can for example be used to POST form data to a remote HTTP
                   1238: **     server - or it can be used to post a newsletter to a NNTP server. In
                   1239: **     either case, you pass a callback function which the request calls when
                   1240: **     the remote destination is ready to accept data. In this callback
                   1241: **     you get the current request object and a stream into where you can 
                   1242: **     write data. It is very important that you return the value returned
                   1243: **     by this stream to the Library so that it knows what to do next. The
                   1244: **     reason is that the outgoing stream might block or an error may
                   1245: **     occur and in that case the Library must know about it. The source
                   1246: **     anchor represents the data object in memory and it points to 
                   1247: **     the destination anchor by using the POSTWeb method. The source anchor
                   1248: **     contains metainformation about the data object in memory and the 
                   1249: **     destination anchor represents the reponse from the remote server.
1.90      frystyk  1250: **     Returns YES if request accepted, else NO
                   1251: */
1.111     frystyk  1252: PUBLIC BOOL HTUploadAnchor (HTAnchor *         source_anchor,
                   1253:                            HTRequest *         request,
                   1254:                            HTPostCallback *    callback)
                   1255: {
                   1256:     HTLink * link = HTAnchor_mainLink((HTAnchor *) source_anchor);
                   1257:     HTAnchor * dest_anchor = HTLink_destination(link);
                   1258:     HTMethod method = HTLink_method(link);
                   1259:     if (!link || method==METHOD_INVALID || !callback) {
                   1260:        if (WWWTRACE)
1.115     eric     1261:            HTTrace("Upload...... No destination found or unspecified method\n");
1.90      frystyk  1262:        return NO;
1.109     frystyk  1263:     }
1.111     frystyk  1264:     request->GenMask |= HT_G_DATE;                      /* Send date header */
                   1265:     request->reload = HT_CACHE_REFRESH;
                   1266:     request->method = method;
                   1267:     request->source_anchor = HTAnchor_parent(source_anchor);
                   1268:     request->PostCallback = callback;
                   1269:     return HTLoadAnchor(dest_anchor, request);
                   1270: }
                   1271: 
                   1272: /*     POST Callback Handler
                   1273: **     ---------------------
                   1274: **     If you do not want to handle the stream interface on your own, you
                   1275: **     can use this function which writes the source anchor hyperdoc to the
                   1276: **     target stream for the anchor upload and also handles the return value
                   1277: **     from the stream. If you don't want to write the source anchor hyperdoc
                   1278: **     then you can register your own callback function that can get the data
                   1279: **     you want.
                   1280: */
                   1281: PUBLIC int HTUpload_callback (HTRequest * request, HTStream * target)
                   1282: {
1.115     eric     1283:     if (WWWTRACE) HTTrace("Uploading... from callback function\n");
1.111     frystyk  1284:     if (!request || !request->source_anchor || !target) return HT_ERROR;
                   1285:     {
                   1286:        int status;
                   1287:        HTParentAnchor * source = request->source_anchor;
                   1288:        char * document = (char *) HTAnchor_document(request->source_anchor);
                   1289:        int len = HTAnchor_length(source);
                   1290:        if (len < 0) {
                   1291:            len = strlen(document);
                   1292:            HTAnchor_setLength(source, len);
                   1293:        }
                   1294:        status = (*target->isa->put_block)(target, document, len);
                   1295:        if (status == HT_OK)
                   1296:            return (*target->isa->flush)(target);
                   1297:        if (status == HT_WOULD_BLOCK) {
1.115     eric     1298:            if (PROT_TRACE)HTTrace("POST Anchor. Target WOULD BLOCK\n");
1.111     frystyk  1299:            return HT_WOULD_BLOCK;
                   1300:        } else if (status == HT_PAUSE) {
1.115     eric     1301:            if (PROT_TRACE) HTTrace("POST Anchor. Target PAUSED\n");
1.111     frystyk  1302:            return HT_PAUSE;
                   1303:        } else if (status > 0) {              /* Stream specific return code */
                   1304:            if (PROT_TRACE)
1.115     eric     1305:                HTTrace("POST Anchor. Target returns %d\n", status);
1.111     frystyk  1306:            return status;
1.120     eric     1307:        } else {                                     /* we have a real error */
1.115     eric     1308:            if (PROT_TRACE) HTTrace("POST Anchor. Target ERROR\n");
1.111     frystyk  1309:            return status;
                   1310:        }
1.70      frystyk  1311:     }
1.123     frystyk  1312: }
                   1313: 
                   1314: /* ------------------------------------------------------------------------- */
                   1315: /*                             HEAD METHOD                                  */
                   1316: /* ------------------------------------------------------------------------- */
                   1317: 
                   1318: /*     Request metainformation about a document from absolute name
                   1319: **     -----------------------------------------------------------
                   1320: **     Request a document referencd by an absolute URL.
                   1321: **     Returns YES if request accepted, else NO
                   1322: */
                   1323: PUBLIC BOOL HTHeadAbsolute (const char * url, HTRequest * request)
                   1324: {
                   1325:     if (url && request) {
                   1326:        HTAnchor * anchor = HTAnchor_findAddress(url);
1.124     frystyk  1327:        return HTHeadAnchor(anchor, request);
1.123     frystyk  1328:     }
                   1329:     return NO;
                   1330: }
                   1331: 
                   1332: /*     Request metainformation about a document from relative name
                   1333: **     -----------------------------------------------------------
                   1334: **     Request a document referenced by a relative URL. The relative URL is 
                   1335: **     made absolute by resolving it relative to the address of the 'base' 
                   1336: **     anchor.
                   1337: **     Returns YES if request accepted, else NO
                   1338: */
                   1339: PUBLIC BOOL HTHeadRelative (const char *       relative,
                   1340:                            HTParentAnchor *    base,
                   1341:                            HTRequest *         request)
                   1342: {
                   1343:     BOOL status = NO;
                   1344:     if (relative && base && request) {
                   1345:        char * rel = NULL;
                   1346:        char * full_url = NULL;
                   1347:        char * base_url = HTAnchor_address((HTAnchor *) base);
                   1348:        StrAllocCopy(rel, relative);
                   1349:        full_url = HTParse(HTStrip(rel), base_url,
                   1350:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.124     frystyk  1351:        status = HTHeadAbsolute(full_url, request);
1.123     frystyk  1352:        HT_FREE(rel);
                   1353:        HT_FREE(full_url);
                   1354:        HT_FREE(base_url);
                   1355:     }
                   1356:     return status;
                   1357: }
                   1358: 
                   1359: /*     Request metainformation about an anchor
                   1360: **     --------------------------------------
                   1361: **     Request the document referenced by the anchor
                   1362: **     Returns YES if request accepted, else NO
                   1363: */
                   1364: PUBLIC BOOL HTHeadAnchor (HTAnchor * anchor, HTRequest * request)
                   1365: {
                   1366:     if (anchor && request) {
                   1367:        HTRequest_setAnchor(request, anchor);
1.124     frystyk  1368:        HTRequest_setOutputFormat(request, WWW_MIME);
1.123     frystyk  1369:        HTRequest_setMethod(request, METHOD_HEAD);
1.124     frystyk  1370:        return launch_request(request, NO);
1.123     frystyk  1371:     }
                   1372:     return NO;
                   1373: }
                   1374: 
                   1375: /* ------------------------------------------------------------------------- */
                   1376: /*                             DELETE METHOD                                */
                   1377: /* ------------------------------------------------------------------------- */
                   1378: 
                   1379: /*     Delete a document on a remote server
                   1380: **     ------------------------------------
                   1381: **     Request a document referencd by an absolute URL.
                   1382: **     Returns YES if request accepted, else NO
                   1383: */
                   1384: PUBLIC BOOL HTDeleteAbsolute (const char * url, HTRequest * request)
                   1385: {
                   1386:     if (url && request) {
                   1387:        HTAnchor * anchor = HTAnchor_findAddress(url);
1.124     frystyk  1388:        return HTDeleteAnchor(anchor, request);
1.123     frystyk  1389:     }
                   1390:     return NO;
                   1391: }
                   1392: 
                   1393: /*     Request metainformation about a document from relative name
                   1394: **     -----------------------------------------------------------
                   1395: **     Request a document referenced by a relative URL. The relative URL is 
                   1396: **     made absolute by resolving it relative to the address of the 'base' 
                   1397: **     anchor.
                   1398: **     Returns YES if request accepted, else NO
                   1399: */
                   1400: PUBLIC BOOL HTDeleteRelative (const char *     relative,
                   1401:                            HTParentAnchor *    base,
                   1402:                            HTRequest *         request)
                   1403: {
                   1404:     BOOL status = NO;
                   1405:     if (relative && base && request) {
                   1406:        char * rel = NULL;
                   1407:        char * full_url = NULL;
                   1408:        char * base_url = HTAnchor_address((HTAnchor *) base);
                   1409:        StrAllocCopy(rel, relative);
                   1410:        full_url = HTParse(HTStrip(rel), base_url,
                   1411:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
1.124     frystyk  1412:        status = HTDeleteAbsolute(full_url, request);
1.123     frystyk  1413:        HT_FREE(rel);
                   1414:        HT_FREE(full_url);
                   1415:        HT_FREE(base_url);
                   1416:     }
                   1417:     return status;
                   1418: }
                   1419: 
                   1420: /*     Request metainformation about an anchor
                   1421: **     --------------------------------------
                   1422: **     Request the document referenced by the anchor
                   1423: **     Returns YES if request accepted, else NO
                   1424: */
                   1425: PUBLIC BOOL HTDeleteAnchor (HTAnchor * anchor, HTRequest * request)
                   1426: {
                   1427:     if (anchor && request) {
                   1428:        HTRequest_setAnchor(request, anchor);
                   1429:        HTRequest_setMethod(request, METHOD_DELETE);
1.124     frystyk  1430:        return launch_request(request, NO);
                   1431:     }
                   1432:     return NO;
                   1433: }
                   1434: 
                   1435: /* ------------------------------------------------------------------------- */
                   1436: /*                             OPTIONS METHOD                               */
                   1437: /* ------------------------------------------------------------------------- */
                   1438: 
                   1439: /*     Options availeble for document from absolute name
                   1440: **     -------------------------------------------------
                   1441: **     Request a document referencd by an absolute URL.
                   1442: **     Returns YES if request accepted, else NO
                   1443: */
                   1444: PUBLIC BOOL HTOptionsAbsolute (const char * url, HTRequest * request)
                   1445: {
                   1446:     if (url && request) {
                   1447:        HTAnchor * anchor = HTAnchor_findAddress(url);
                   1448:        return HTOptionsAnchor(anchor, request);
                   1449:     }
                   1450:     return NO;
                   1451: }
                   1452: 
                   1453: /*     Options available for document from relative name
                   1454: **     -------------------------------------------------
                   1455: **     Request a document referenced by a relative URL. The relative URL is 
                   1456: **     made absolute by resolving it relative to the address of the 'base' 
                   1457: **     anchor.
                   1458: **     Returns YES if request accepted, else NO
                   1459: */
                   1460: PUBLIC BOOL HTOptionsRelative (const char *    relative,
                   1461:                            HTParentAnchor *    base,
                   1462:                            HTRequest *         request)
                   1463: {
                   1464:     BOOL status = NO;
                   1465:     if (relative && base && request) {
                   1466:        char * rel = NULL;
                   1467:        char * full_url = NULL;
                   1468:        char * base_url = HTAnchor_address((HTAnchor *) base);
                   1469:        StrAllocCopy(rel, relative);
                   1470:        full_url = HTParse(HTStrip(rel), base_url,
                   1471:                         PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
                   1472:        status = HTOptionsAbsolute(full_url, request);
                   1473:        HT_FREE(rel);
                   1474:        HT_FREE(full_url);
                   1475:        HT_FREE(base_url);
                   1476:     }
                   1477:     return status;
                   1478: }
                   1479: 
                   1480: /*     Options available for document using Anchor
                   1481: **     -------------------------------------------
                   1482: **     Request the document referenced by the anchor
                   1483: **     Returns YES if request accepted, else NO
                   1484: */
                   1485: PUBLIC BOOL HTOptionsAnchor (HTAnchor * anchor, HTRequest * request)
                   1486: {
                   1487:     if (anchor && request) {
                   1488:        HTRequest_setAnchor(request, anchor);
                   1489:        HTRequest_setMethod(request, METHOD_OPTIONS);
                   1490:        return launch_request(request, NO);
1.123     frystyk  1491:     }
                   1492:     return NO;
1.1       timbl    1493: }

Webmaster