Annotation of libwww/Library/src/HTTPReq.c, revision 2.64

2.1       frystyk     1: /*                                                                   HTTPReq.c
2.23      frystyk     2: **     HTTP REQUEST GENERATION
2.1       frystyk     3: **
2.32      frystyk     4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.64    ! duerst      6: **     @(#) $Id: HTTPReq.c,v 2.63 2005/01/04 15:18:11 duerst Exp $
2.32      frystyk     7: **
2.1       frystyk     8: **     This module implements the output stream for HTTP used for sending
                      9: **     requests with or without a entity body.
                     10: **
                     11: ** History:
                     12: **     Jan 95 HFN      Written from scratch
                     13: */
                     14: 
                     15: /* Library Includes */
2.52      frystyk    16: #include "wwwsys.h"
2.28      frystyk    17: #include "WWWUtil.h"
2.32      frystyk    18: #include "WWWCore.h"
                     19: 
2.23      frystyk    20: #include "HTTPGen.h"
2.21      frystyk    21: #include "HTTPUtil.h"
2.63      duerst     22: #include "HTDNS.h"
2.1       frystyk    23: #include "HTTPReq.h"                                          /* Implements */
                     24: 
2.24      frystyk    25: #define PUTC(c)                (*me->target->isa->put_character)(me->target, c)
                     26: #define PUTS(s)                (*me->target->isa->put_string)(me->target, s)
2.1       frystyk    27: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
                     28: 
                     29: struct _HTStream {
2.29      frystyk    30:     const HTStreamClass *      isa;
2.1       frystyk    31:     HTStream *                 target;
                     32:     HTRequest *                        request;
2.18      frystyk    33:     SOCKET                     sockfd;
2.11      frystyk    34:     int                                version;
2.38      frystyk    35:     int                        state;    
                     36:     char *                     url;
2.1       frystyk    37:     BOOL                       transparent;
                     38: };
                     39: 
                     40: /* ------------------------------------------------------------------------- */
                     41: /*                         HTTP Output Request Stream                       */
                     42: /* ------------------------------------------------------------------------- */
                     43: 
2.11      frystyk    44: /*     HTTP09Request
                     45: **     -------------
                     46: **     Makes a HTTP/0.9 request
2.1       frystyk    47: */
2.38      frystyk    48: PRIVATE int HTTP09Request (HTStream * me, HTRequest * request)
2.11      frystyk    49: {
2.34      frystyk    50:     HTParentAnchor * anchor = HTRequest_anchor(request);
                     51:     char * addr = HTAnchor_physical(anchor);
2.38      frystyk    52:     if (!me->url) me->url = HTParse(addr, "", PARSE_PATH|PARSE_PUNCTUATION);
                     53:     if (me->state == 0) {
2.47      frystyk    54:        int status = PUTS("GET ");
                     55:        if (status != HT_OK) return status;
2.38      frystyk    56:        me->state++;
                     57:     }
                     58:     if (me->state == 1) {
                     59:        int status = PUTS(me->url);
                     60:        if (status != HT_OK) return status;
                     61:        me->state++;
                     62:     }
2.24      frystyk    63:     PUTC(CR);
                     64:     PUTC(LF);
2.61      frystyk    65:     HTTRACE(PROT_TRACE, "HTTP........ Generating HTTP/0.9 Request\n");
2.38      frystyk    66:     return HT_OK;
2.11      frystyk    67: }
                     68: 
                     69: /*     HTTPMakeRequest
                     70: **     ---------------
                     71: **     Makes a HTTP/1.0-1.1 request header.
                     72: */
2.38      frystyk    73: PRIVATE int HTTPMakeRequest (HTStream * me, HTRequest * request)
2.1       frystyk    74: {
2.39      frystyk    75:     HTMethod method = HTRequest_method(request);
                     76:     HTRqHd request_mask = HTRequest_rqHd(request);
                     77:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.43      frystyk    78:     char * etag = HTAnchor_etag(anchor);
2.24      frystyk    79:     char crlf[3];
                     80:     char qstr[10];
                     81:     *crlf = CR; *(crlf+1) = LF; *(crlf+2) = '\0';
2.1       frystyk    82: 
2.41      frystyk    83:     /* Generate the HTTP/1.x RequestLine */
2.38      frystyk    84:     if (me->state == 0) {
2.34      frystyk    85:        if (method != METHOD_INVALID) {
                     86:            PUTS(HTMethod_name(method));
                     87:            PUTC(' ');
                     88:        } else
                     89:            PUTS("GET ");
2.38      frystyk    90:        me->state++;
2.34      frystyk    91:     }
2.1       frystyk    92: 
2.39      frystyk    93:     /*
2.45      frystyk    94:     **  Generate the Request URI. If we are using full request URI then it's
                     95:     **  easy. Otherwise we must filter out the path part of the URI.
                     96:     **  In case it's a OPTIONS request then if there is no pathinfo then use
                     97:     **  a * instead. If we use a method different from GET or HEAD then use
                     98:     **  the content-location if available.
2.39      frystyk    99:     */
2.45      frystyk   100:     if (me->state == 1) {
                    101:        char * abs_location = NULL;
2.38      frystyk   102:        char * addr = HTAnchor_physical(anchor);
2.62      kahan     103:        char * location;
                    104: 
                    105:         /* JK: If the application specified a content-location (which is
                    106:            stored in the request in default put-name!), we use it instead
                    107:            of the URL that's being saved to. This is like having a user
                    108:            defined Content-Location */
                    109:         location = HTRequest_defaultPutName (request);
                    110:         if (location)
                    111:          {
                    112:            if (HTURL_isAbsolute (location))
                    113:              {
                    114:                char * relative;
                    115:                relative = HTRelative (location, location);
                    116:                abs_location = HTParse (relative + 2, addr, PARSE_ALL);
                    117:                HT_FREE (relative);
                    118:              }
                    119:            else
                    120:              abs_location = HTParse (location, addr, PARSE_ALL);
                    121:            addr = abs_location;
                    122:          }
2.45      frystyk   123: 
2.60      frystyk   124: #if 0
                    125:        /*
                    126:        **  We don't use the content-location any more as it is superseeded
                    127:        **  by etags and the combination of the two might do more harm than
                    128:        **  good (The etag is not guaranteed to be unique over multiple URIs)
                    129:        */
                    130: 
2.45      frystyk   131:        /*
                    132:        **  If we are using a method different from HEAD and GET then use
                    133:        **  the Content-Location if available, else the Request-URI.
                    134:        */
                    135:        if (!HTMethod_isSafe(method)) {
                    136:            char * location = HTAnchor_location(anchor);
                    137:            if (location) {
                    138:                if (HTURL_isAbsolute(location))
                    139:                    addr = location;
                    140:                else {
                    141:                    /*
                    142:                    **  We have a content-location but it is relative and
                    143:                    **  must expand it either to the content-base or to
                    144:                    **  the Request-URI itself.
                    145:                    */
                    146:                    char * base = HTAnchor_base(anchor);
                    147:                    abs_location = HTParse(location, base, PARSE_ALL);
                    148:                    addr = abs_location;
                    149:                }
                    150:            }
                    151:        }
2.60      frystyk   152: #endif
2.45      frystyk   153: 
                    154:        /*
                    155:        **  If we are using a proxy or newer versions of HTTP then we can
                    156:        **  send the full URL. Otherwise we only send the path.
                    157:        */
                    158:        if (HTRequest_fullURI(request))
                    159:            StrAllocCopy(me->url, addr);
                    160:        else {
                    161:            me->url = HTParse(addr, "", PARSE_PATH | PARSE_PUNCTUATION);
2.39      frystyk   162:            if (method == METHOD_OPTIONS) {
                    163:                /*
                    164:                ** We don't preserve the final slash or lack of same through
                    165:                ** out the code. This is mainly for optimization reasons
2.44      frystyk   166:                ** but it gives a problem OPTIONS. We can either send a "*"
                    167:                ** or a "/" but not both. For now we send a "*".
2.39      frystyk   168:                */
2.45      frystyk   169:                if (!strcmp(me->url, "/")) *me->url = '*';
2.39      frystyk   170:            }
2.1       frystyk   171:        }
2.45      frystyk   172:        HT_FREE(abs_location);
                    173:        me->state++;
                    174:     }
                    175: 
                    176:     /*
                    177:     **  Now send the URL that we have put together
                    178:     */
                    179:     if (me->state == 2) {
                    180:        int status = HT_OK;
                    181:        if ((status = PUTS(me->url)) != HT_OK) return status;
2.38      frystyk   182:        me->state++;
2.48      frystyk   183: 
                    184: #if 0
                    185:        fprintf(stderr, "Requesting '%s'\n", me->url);
                    186: #endif
                    187: 
2.1       frystyk   188:     }
2.25      frystyk   189:     PUTC(' ');
2.41      frystyk   190: 
                    191:     /*
                    192:     **  Send out the version number. If we know it is a HTTP/1.0 server we
                    193:     **  are talking to then use HTTP/1.0, else use HTTP/1.1 as default version
                    194:     **  number
                    195:     */
                    196:     if (me->version == HTTP_10)
                    197:        PUTS(HTTP_VERSION_10);
                    198:     else
                    199:        PUTS(HTTP_VERSION);
2.24      frystyk   200:     PUTBLOCK(crlf, 2);
2.1       frystyk   201: 
2.4       frystyk   202:     /* Request Headers */
2.34      frystyk   203:     if (request_mask & HT_C_ACCEPT_TYPE) {
2.58      frystyk   204:        HTFormat format = HTRequest_outputFormat(request);
                    205:        
2.37      frystyk   206:        /*
                    207:        ** If caller has specified a specific output format then use this.
                    208:        ** Otherwise use all the registered converters to generate the 
                    209:        ** accept header
                    210:        */
2.58      frystyk   211:        if (format == WWW_PRESENT) {
2.37      frystyk   212:            int list;
                    213:            HTList *cur;
                    214:            BOOL first=YES;
                    215:            for (list=0; list<2; list++) {
                    216:                if ((!list && ((cur = HTFormat_conversion()) != NULL)) ||
                    217:                    (list && ((cur = HTRequest_conversion(request))!=NULL))) {
                    218:                    HTPresentation * pres;
                    219:                    while ((pres=(HTPresentation *) HTList_nextObject(cur))) {
                    220:                        if (pres->rep_out==WWW_PRESENT && pres->quality<=1.0) {
                    221:                            if (first) {
                    222:                                PUTS("Accept: ");
                    223:                                first=NO;
                    224:                            } else
                    225:                                PUTC(',');
                    226:                            PUTS(HTAtom_name(pres->rep));
2.46      frystyk   227:                            if (pres->quality < 1.0 && pres->quality >= 0.0) {
2.37      frystyk   228:                                sprintf(qstr, ";q=%1.1f", pres->quality);
                    229:                                PUTS(qstr);
                    230:                            }
2.1       frystyk   231:                        }
                    232:                    }
                    233:                }
                    234:            }
2.37      frystyk   235:            if (!first) PUTBLOCK(crlf, 2);
                    236:        } else {
2.58      frystyk   237: 
                    238:            /*
                    239:            **  If we have an explicit output format then only send
                    240:            **  this one if not this is an internal libwww format
                    241:            **  of type www/<star>
                    242:            */
                    243:            if (!HTMIMEMatch(WWW_INTERNAL, format)) {
                    244:                PUTS("Accept: ");
                    245:                PUTS(HTAtom_name(format));
                    246:                PUTBLOCK(crlf, 2);
                    247:            }
2.37      frystyk   248:        }       
2.1       frystyk   249:     }
2.34      frystyk   250:     if (request_mask & HT_C_ACCEPT_CHAR) {
2.4       frystyk   251:        int list;
                    252:        HTList *cur;
2.24      frystyk   253:        BOOL first=YES;
2.4       frystyk   254:        for (list=0; list<2; list++) {
2.14      frystyk   255:            if ((!list && ((cur = HTFormat_charset()) != NULL)) ||
                    256:                (list && ((cur = HTRequest_charset(request)) != NULL))) {
2.4       frystyk   257:                HTAcceptNode *pres;
                    258:                while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
2.5       frystyk   259:                    if (first) {
2.24      frystyk   260:                        PUTS("Accept-Charset: ");
2.5       frystyk   261:                        first=NO;
2.24      frystyk   262:                    } else
                    263:                        PUTC(',');
                    264:                    PUTS(HTAtom_name(pres->atom));
2.46      frystyk   265:                    if (pres->quality < 1.0 && pres->quality >= 0.0) {
                    266:                        sprintf(qstr, ";q=%1.1f", pres->quality);
                    267:                        PUTS(qstr);
                    268:                    }
2.4       frystyk   269:                }
                    270:            }
                    271:        }
2.31      frystyk   272:        if (!first) PUTBLOCK(crlf, 2);
2.4       frystyk   273:     }
2.34      frystyk   274:     if (request_mask & HT_C_ACCEPT_ENC) {
2.4       frystyk   275:        int list;
                    276:        HTList *cur;
2.24      frystyk   277:        BOOL first=YES;
2.4       frystyk   278:        for (list=0; list<2; list++) {
2.33      frystyk   279:            if ((!list && ((cur = HTFormat_contentCoding()) != NULL)) ||
2.14      frystyk   280:                (list && ((cur = HTRequest_encoding(request)) != NULL))) {
2.33      frystyk   281:                HTCoding * pres;
                    282:                while ((pres = (HTCoding *) HTList_nextObject(cur))) {
2.46      frystyk   283:                    double quality = HTCoding_quality(pres);
2.5       frystyk   284:                    if (first) {
2.24      frystyk   285:                        PUTS("Accept-Encoding: ");
2.32      frystyk   286:                        first = NO;
2.24      frystyk   287:                    } else
                    288:                        PUTC(',');
2.33      frystyk   289:                    PUTS(HTCoding_name(pres));
2.46      frystyk   290:                    if (quality < 1.0 && quality >= 0.0) {
                    291:                        sprintf(qstr, ";q=%1.1f", quality);
                    292:                        PUTS(qstr);
                    293:                    }
                    294:                }
                    295:            }
                    296:        }
                    297:        if (!first) PUTBLOCK(crlf, 2);
                    298:     }
                    299:     if (request_mask & HT_C_ACCEPT_TE) {
                    300:        int list;
                    301:        HTList *cur;
                    302:        BOOL first=YES;
                    303:        for (list=0; list<2; list++) {
                    304:            if ((!list && ((cur = HTFormat_transferCoding()) != NULL)) ||
                    305:                (list && ((cur = HTRequest_transfer(request)) != NULL))) {
                    306:                HTCoding * pres;
                    307:                while ((pres = (HTCoding *) HTList_nextObject(cur))) {
                    308:                    double quality = HTCoding_quality(pres);
2.55      frystyk   309:                    const char * coding = HTCoding_name(pres);
2.46      frystyk   310:                    if (first) {
                    311:                        PUTS("TE: ");
                    312:                        first = NO;
                    313:                    } else
                    314:                        PUTC(',');
2.55      frystyk   315: 
                    316:                    /* Special check for "chunked" which is translated to "trailers" */
                    317:                    if (!strcasecomp(coding, "chunked"))
                    318:                        PUTS("trailers");
                    319:                    else
                    320:                        PUTS(coding);
2.46      frystyk   321:                    if (quality < 1.0 && quality >= 0.0) {
                    322:                        sprintf(qstr, ";q=%1.1f", quality);
                    323:                        PUTS(qstr);
                    324:                    }
2.4       frystyk   325:                }
                    326:            }
                    327:        }
2.31      frystyk   328:        if (!first) PUTBLOCK(crlf, 2);
2.4       frystyk   329:     }
2.34      frystyk   330:     if (request_mask & HT_C_ACCEPT_LAN) {
2.4       frystyk   331:        int list;
                    332:        HTList *cur;
2.24      frystyk   333:        BOOL first=YES;
2.4       frystyk   334:        for (list=0; list<2; list++) {
2.14      frystyk   335:            if ((!list && ((cur = HTFormat_language()) != NULL)) ||
                    336:                (list && ((cur = HTRequest_language(request)) != NULL))) {
2.4       frystyk   337:                HTAcceptNode *pres;
                    338:                while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
2.5       frystyk   339:                    if (first) {
2.24      frystyk   340:                        PUTS("Accept-Language: ");
2.5       frystyk   341:                        first=NO;
2.24      frystyk   342:                    } else
                    343:                        PUTC(',');
                    344:                    PUTS(HTAtom_name(pres->atom));
2.46      frystyk   345:                    if (pres->quality < 1.0 && pres->quality >= 0.0) {
2.24      frystyk   346:                        sprintf(qstr, ";q=%1.1f", pres->quality);
                    347:                        PUTS(qstr);
2.5       frystyk   348:                    }
2.4       frystyk   349:                }
                    350:            }
                    351:        }
2.31      frystyk   352:        if (!first) PUTBLOCK(crlf, 2);
2.4       frystyk   353:     }
2.36      frystyk   354:     if (request_mask & HT_C_AUTH) {
2.34      frystyk   355:        HTAssocList * cur = HTRequest_credentials(request);
                    356:        if (cur) {                                  /* Access authentication */
                    357:            HTAssoc * pres;
                    358:            while ((pres = (HTAssoc *) HTAssocList_nextObject(cur))) {
                    359:                PUTS(HTAssoc_name(pres));
                    360:                PUTS(": ");
                    361:                PUTS(HTAssoc_value(pres));
                    362:                PUTBLOCK(crlf, 2);
                    363:            }
2.28      frystyk   364:        }
2.1       frystyk   365:     }
2.46      frystyk   366:     if (request_mask & HT_C_EXPECT) {
                    367:        HTAssocList * cur = HTRequest_expect(request);
                    368:        if (cur) {
                    369:            BOOL first=YES;
                    370:            HTAssoc * pres;
                    371:            while ((pres = (HTAssoc *) HTAssocList_nextObject(cur))) {
                    372:                char * value = HTAssoc_value(pres);
                    373:                if (first) {
                    374:                    PUTS("Expect: ");
                    375:                    first = NO;
                    376:                } else
                    377:                    PUTC(',');
                    378: 
                    379:                /* Output the name */
                    380:                PUTS(HTAssoc_name(pres));
                    381: 
                    382:                /* Only output the value if not empty string */
                    383:                if (*value) {
                    384:                    PUTS("=");
                    385:                    PUTS(value);
                    386:                }
                    387:            }
                    388:            PUTBLOCK(crlf, 2);
                    389:        }
                    390:     }
2.34      frystyk   391:     if (request_mask & HT_C_FROM) {
                    392:        HTUserProfile * up = HTRequest_userProfile(request);
                    393:        const char * mailaddress = HTUserProfile_email(up);
2.1       frystyk   394:        if (mailaddress) {
2.24      frystyk   395:            PUTS("From: ");
                    396:            PUTS(mailaddress);
                    397:            PUTBLOCK(crlf, 2);
2.1       frystyk   398:        }
                    399:     }
2.34      frystyk   400:     if (request_mask & HT_C_HOST) {
2.13      frystyk   401:        char *orig = HTAnchor_address((HTAnchor *) anchor);
2.12      frystyk   402:        char *host = HTParse(orig, "", PARSE_HOST);
2.64    ! duerst    403:        char hostace[256];
2.34      frystyk   404: #if 0
                    405:        /* Keep the port number for HTTP/1.1 compliance */
2.12      frystyk   406:        char *ptr = strchr(host, ':');               /* Chop off port number */
                    407:        if (ptr) *ptr = '\0';
2.34      frystyk   408: #endif
2.63      duerst    409:         PUTS("Host: ");
2.64    ! duerst    410: #ifdef LIBWWW_USEIDN
        !           411:     /****** still have to check UTF8toACE with port number */
        !           412:     if (!HTACEfromUTF8 (host, hostace, 255)) {
2.63      duerst    413:            PUTS(hostace);
                    414:        }
                    415:        else {
                    416:            PUTS(host); /* this may be dangerous, but helps server side debugging */
2.64    ! duerst    417:         HTTRACE(PROT_TRACE, "HTTP........ Error: Cannot convert to ACE: `%s\'\n", host);
2.63      duerst    418:        }
2.64    ! duerst    419: #else
        !           420:        PUTS(host);
        !           421: #endif
2.24      frystyk   422:        PUTBLOCK(crlf, 2);
2.26      frystyk   423:        HT_FREE(orig);
                    424:        HT_FREE(host);
2.39      frystyk   425:     }
2.42      frystyk   426: 
                    427:     /*
                    428:     **  In the "If-*" series of headers, the ones related to etags have higher
                    429:     **  priority than the date relates ones. That is, if we have a etag then
2.45      frystyk   430:     **  use that, otherwise use the date. First we check for range, match, and
                    431:     **  unmodified-since.
2.42      frystyk   432:     */
2.45      frystyk   433:     if (request_mask & HT_C_IF_RANGE && etag) {
                    434:        PUTS("If-Range: \"");
                    435:        PUTS(etag);
                    436:        PUTC('"');
                    437:        PUTBLOCK(crlf, 2);
2.61      frystyk   438:        HTTRACE(PROT_TRACE, "HTTP........ If-Range using etag `%s\'\n" _ etag);
2.57      frystyk   439:     } else if (request_mask & HT_C_IF_MATCH_ANY) {
2.56      frystyk   440:        PUTS("If-Match: *");
                    441:        PUTBLOCK(crlf, 2);
2.61      frystyk   442:        HTTRACE(PROT_TRACE, "HTTP........ If-Match using `*\'\n");
2.45      frystyk   443:     } else if (request_mask & HT_C_IF_MATCH && etag) {
2.43      frystyk   444:        PUTS("If-Match: \"");
                    445:        PUTS(etag);
                    446:        PUTC('"');
                    447:        PUTBLOCK(crlf, 2);
2.61      frystyk   448:        HTTRACE(PROT_TRACE, "HTTP........ If-Match using etag `%s\'\n" _ etag);
2.43      frystyk   449:     } else if (request_mask & HT_C_IF_UNMOD_SINCE) {
2.42      frystyk   450:        time_t lm = HTAnchor_lastModified(anchor);
                    451:        if (lm > 0) {
2.43      frystyk   452:            PUTS("If-Unmodified-Since: ");
2.42      frystyk   453:            PUTS(HTDateTimeStr(&lm, NO));
                    454:            PUTBLOCK(crlf, 2);
2.61      frystyk   455:            HTTRACE(PROT_TRACE, "HTTP........ If-Unmodified-Since `%s\'\n" _ HTDateTimeStr(&lm, NO));
2.42      frystyk   456:        }
                    457:     }
2.45      frystyk   458: 
                    459:     /*
                    460:     **  If-None-Match and If-Modified-Since are equivalent except that the
                    461:     **  first uses etags and the second uses dates. Etags have precedence over
                    462:     **  dates.
                    463:     */
2.57      frystyk   464:     if (request_mask & HT_C_IF_NONE_MATCH_ANY) {
2.56      frystyk   465:        PUTS("If-None-Match: *");
                    466:        PUTBLOCK(crlf, 2);
2.61      frystyk   467:        HTTRACE(PROT_TRACE, "HTTP........ If-None-Match using `*\'\n");
2.56      frystyk   468:     } else if (request_mask & HT_C_IF_NONE_MATCH && etag) {
2.43      frystyk   469:        PUTS("If-None-Match: \"");
                    470:        PUTS(etag);
                    471:        PUTC('"');
                    472:        PUTBLOCK(crlf, 2);
2.61      frystyk   473:        HTTRACE(PROT_TRACE, "HTTP........ If-None-Match `%s\'\n" _ etag);
2.46      frystyk   474:     }
                    475:     if (request_mask & HT_C_IMS) {
2.42      frystyk   476:        time_t lm = HTAnchor_lastModified(anchor);
                    477:        if (lm > 0) {
2.43      frystyk   478:            PUTS("If-Modified-Since: ");
2.42      frystyk   479:            PUTS(HTDateTimeStr(&lm, NO));
                    480:            PUTBLOCK(crlf, 2);
2.61      frystyk   481:            HTTRACE(PROT_TRACE, "HTTP........ If-Modified-Since `%s\'\n" _ HTDateTimeStr(&lm, NO));
2.42      frystyk   482:        }
                    483:     }
                    484: 
2.45      frystyk   485:     /*
                    486:     **  Max forwards is mainly for TRACE where we want to be able to stop the
                    487:     **  TRACE at a specific location un the message path.
                    488:     */
2.39      frystyk   489:     if (request_mask & HT_C_MAX_FORWARDS) {
                    490:        int hops = HTRequest_maxForwards(request);
                    491:        if (hops >= 0) {
                    492:            sprintf(qstr, "%d", hops);
                    493:            PUTS("Max-Forwards: ");
                    494:            PUTS(qstr);
                    495:            PUTBLOCK(crlf, 2);
                    496:        }
2.42      frystyk   497:     }
2.45      frystyk   498: 
                    499:     /*
                    500:     **  Range requests. For now, we only take the first entry registered for
                    501:     **  this request. This means that you can only send a single "unit" and
                    502:     **  then a set of range within this unit. This is in accordance with 
                    503:     **  HTTP/1.1. Multiple units will go on multiple lines.
                    504:     */
2.42      frystyk   505:     if (request_mask & HT_C_RANGE) {
2.45      frystyk   506:        HTAssocList * cur = HTRequest_range(request);
                    507:        if (cur) {                                         /* Range requests */
                    508:            HTAssoc * pres;
                    509:            while ((pres = (HTAssoc *) HTAssocList_nextObject(cur))) {
                    510:                PUTS("Range: ");
                    511:                PUTS(HTAssoc_name(pres));                            /* Unit */
                    512:                PUTS("=");
                    513:                PUTS(HTAssoc_value(pres));        /* Ranges within this unit */
                    514:                PUTBLOCK(crlf, 2);
                    515:            }
                    516:        }
2.1       frystyk   517:     }
2.34      frystyk   518:     if (request_mask & HT_C_REFERER) {
                    519:        HTParentAnchor * parent_anchor = HTRequest_parent(request);
                    520:        if (parent_anchor) {
                    521:            char * act = HTAnchor_address((HTAnchor *) anchor);
                    522:            char * parent = HTAnchor_address((HTAnchor *) parent_anchor);
2.50      frystyk   523: #if 1
                    524:            char * relative = HTRelative(parent, act);
                    525: #else
2.34      frystyk   526:            char * relative = HTParse(parent, act,
                    527:                                      PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
2.50      frystyk   528: #endif
2.34      frystyk   529:            if (relative && *relative) {
                    530:                PUTS("Referer: ");
2.50      frystyk   531:                PUTS(relative);
2.34      frystyk   532:                PUTBLOCK(crlf, 2);
                    533:            }
                    534:            HT_FREE(act);
                    535:            HT_FREE(parent);
                    536:            HT_FREE(relative);
2.1       frystyk   537:        }
                    538:     }
2.34      frystyk   539:     if (request_mask & HT_C_USER_AGENT) {
2.24      frystyk   540:        PUTS("User-Agent: ");
                    541:        PUTS(HTLib_appName());
                    542:        PUTC('/');
                    543:        PUTS(HTLib_appVersion());
                    544:        PUTC(' ');
                    545:        PUTS(HTLib_name());
                    546:        PUTC('/');
                    547:        PUTS(HTLib_version());
                    548:        PUTBLOCK(crlf, 2);
2.1       frystyk   549:     }
2.61      frystyk   550:     HTTRACE(PROT_TRACE, "HTTP........ Generating HTTP/1.x Request Headers\n");
2.38      frystyk   551:     return HT_OK;
2.1       frystyk   552: }
                    553: 
2.29      frystyk   554: PRIVATE int HTTPRequest_put_block (HTStream * me, const char * b, int l)
2.1       frystyk   555: {
2.7       frystyk   556:     if (!me->target) {
2.1       frystyk   557:        return HT_WOULD_BLOCK;
2.7       frystyk   558:     } else if (me->transparent)
2.11      frystyk   559:        return b ? PUTBLOCK(b, l) : HT_OK;
2.1       frystyk   560:     else {
2.38      frystyk   561:        int status = HT_OK;
                    562:        if (me->version == HTTP_09) {
                    563:            status = HTTP09Request(me, me->request);
                    564:            if (status != HT_OK) return status;
                    565:        } else {
                    566:            status = HTTPMakeRequest(me, me->request);
                    567:            if (status != HT_OK) return status;
2.1       frystyk   568:            me->transparent = YES;
2.11      frystyk   569:            return b ? PUTBLOCK(b, l) : HT_OK;
2.1       frystyk   570:        }
                    571:        return status;
                    572:     }
                    573: }
                    574: 
2.16      frystyk   575: PRIVATE int HTTPRequest_put_character (HTStream * me, char c)
2.1       frystyk   576: {
2.11      frystyk   577:     return HTTPRequest_put_block(me, &c, 1);
2.1       frystyk   578: }
                    579: 
2.29      frystyk   580: PRIVATE int HTTPRequest_put_string (HTStream * me, const char * s)
2.1       frystyk   581: {
2.11      frystyk   582:     return HTTPRequest_put_block(me, s, strlen(s));
2.1       frystyk   583: }
                    584: 
                    585: /*
                    586: **     Flushes data but doesn't free stream object
                    587: */
2.16      frystyk   588: PRIVATE int HTTPRequest_flush (HTStream * me)
2.1       frystyk   589: {
2.13      frystyk   590:     int status = HTTPRequest_put_block(me, NULL, 0);
                    591:     return status==HT_OK ? (*me->target->isa->flush)(me->target) : status;
2.1       frystyk   592: }
                    593: 
                    594: /*
                    595: **     Flushes data and frees stream object
                    596: */
2.16      frystyk   597: PRIVATE int HTTPRequest_free (HTStream * me)
2.1       frystyk   598: {
2.11      frystyk   599:     int status = HTTPRequest_flush(me);
                    600:     if (status != HT_WOULD_BLOCK) {
                    601:        if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
                    602:            return HT_WOULD_BLOCK;
2.38      frystyk   603:        HT_FREE(me->url);
2.26      frystyk   604:        HT_FREE(me);
2.1       frystyk   605:     }
2.7       frystyk   606:     return status;
2.1       frystyk   607: }
                    608: 
2.16      frystyk   609: PRIVATE int HTTPRequest_abort (HTStream * me, HTList * e)
2.1       frystyk   610: {
2.61      frystyk   611:     HTTRACE(PROT_TRACE, "HTTPRequest. ABORTING...\n");
2.51      kahan     612:     /* JK: Added protection against NULL pointers */
2.49      frystyk   613:     if (me) {
2.51      kahan     614:        if (me->target && me->target->isa)
                    615:          (*me->target->isa->abort)(me->target, e);
                    616:        if (me->url)
                    617:          HT_FREE(me->url);
2.49      frystyk   618:        HT_FREE(me);
                    619:     }
2.1       frystyk   620:     return HT_ERROR;
                    621: }
                    622: 
                    623: /*     HTTPRequest Stream
                    624: **     -----------------
                    625: */
2.29      frystyk   626: PRIVATE const HTStreamClass HTTPRequestClass =
2.1       frystyk   627: {              
                    628:     "HTTPRequest",
                    629:     HTTPRequest_flush,
                    630:     HTTPRequest_free,
                    631:     HTTPRequest_abort,
                    632:     HTTPRequest_put_character,
                    633:     HTTPRequest_put_string,
                    634:     HTTPRequest_put_block
                    635: };
                    636: 
2.23      frystyk   637: PUBLIC HTStream * HTTPRequest_new (HTRequest * request, HTStream * target,
2.45      frystyk   638:                                   BOOL endHeader, int version)
2.1       frystyk   639: {
2.26      frystyk   640:     HTStream * me;
                    641:     if ((me = (HTStream  *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
                    642:         HT_OUTOFMEM("HTTPRequest_new");
2.1       frystyk   643:     me->isa = &HTTPRequestClass;
                    644:     me->target = target;
                    645:     me->request = request;
2.45      frystyk   646:     me->version = version;
2.1       frystyk   647:     me->transparent = NO;
2.46      frystyk   648: 
                    649:     /*
                    650:     ** If sending a body in the request then we want a 100 code!
                    651:     */
                    652:     if (HTMethod_hasEntity(HTRequest_method(request)))
                    653:        HTRequest_addExpect(request, "100-continue", "");
                    654: 
2.23      frystyk   655: 
                    656:     /* Return general HTTP header stream */
2.45      frystyk   657:     return HTTPGen_new(request, me, endHeader, version);
2.1       frystyk   658: }

Webmaster