Annotation of libwww/Library/src/HTMIMERq.c, revision 2.37

2.1       frystyk     1: /*                                                                  HTMIMERq.c
2.10      frystyk     2: **     MIME ENTITY HEADERS GENERATION
2.1       frystyk     3: **
2.15      frystyk     4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.37    ! frystyk     6: **     @(#) $Id: HTMIMERq.c,v 2.36 1998/05/04 19:36:53 frystyk Exp $
2.15      frystyk     7: **
2.1       frystyk     8: **     This module implements the output stream for MIME used for sending
2.10      frystyk     9: **     requests with a entity body to HTTP, NEWS, etc. or for generating
                     10: **     responses
2.1       frystyk    11: **
                     12: ** History:
                     13: **     Jan 95 HFN      Written
                     14: */
                     15: 
                     16: /* Library Includes */
2.36      frystyk    17: #include "wwwsys.h"
2.21      frystyk    18: #include "WWWUtil.h"
                     19: #include "WWWCore.h"
2.7       frystyk    20: #include "HTAncMan.h"
2.1       frystyk    21: #include "HTNetMan.h"
2.21      frystyk    22: #include "HTReqMan.h"
2.2       frystyk    23: #include "HTHeader.h"
2.21      frystyk    24: #include "HTMIMERq.h"                                         /* Implements */
2.1       frystyk    25: 
2.17      frystyk    26: #define PUTC(c)                (*me->target->isa->put_character)(me->target, c)
                     27: #define PUTS(s)                (*me->target->isa->put_string)(me->target, s)
2.1       frystyk    28: #define PUTBLOCK(b, l) (*me->target->isa->put_block)(me->target, b, l)
                     29: 
                     30: struct _HTStream {
2.14      frystyk    31:     const HTStreamClass *      isa;
2.1       frystyk    32:     HTStream *                 target;
                     33:     HTRequest *                        request;
2.10      frystyk    34:     BOOL                       endHeader;
2.1       frystyk    35:     BOOL                       transparent;
                     36: };
                     37: 
2.27      frystyk    38: #define HT_MAX_WAIT            8      /* Max number of secs to wait for PUT */
2.25      frystyk    39: 
2.32      frystyk    40: PRIVATE int MIMERequest_put_block (HTStream * me, const char * b, int l);
                     41: 
2.1       frystyk    42: /* ------------------------------------------------------------------------- */
                     43: /*                         MIME Output Request Stream                       */
                     44: /* ------------------------------------------------------------------------- */
                     45: 
                     46: /*     MIMEMakeRequest
                     47: **     ---------------
2.10      frystyk    48: **     Generates the BODY parts of a MIME message.
2.1       frystyk    49: */
2.2       frystyk    50: PRIVATE int MIMEMakeRequest (HTStream * me, HTRequest * request)
2.1       frystyk    51: {
2.17      frystyk    52:     char crlf[3];
2.10      frystyk    53:     char linebuf[256];                 /* @@@ */
2.21      frystyk    54:     HTParentAnchor * entity = HTRequest_entityAnchor(request);
                     55:     HTEnHd EntityMask = HTRequest_enHd(request);
2.34      frystyk    56:     BOOL transfer_coding = NO;         /* We should get this from the Host object */
2.17      frystyk    57:     *crlf = CR; *(crlf+1) = LF; *(crlf+2) = '\0';
2.1       frystyk    58: 
2.21      frystyk    59:     if (EntityMask & HT_E_ALLOW) {
2.34      frystyk    60:        BOOL first = YES;
                     61:        int cnt;
                     62:        HTMethod methodset = HTAnchor_allow(entity);
                     63:        for (cnt=0; cnt<sizeof(HTMethod)<<3 ; cnt++) {
                     64:            if (methodset & (1<<cnt)) {
                     65:                if (first) {
                     66:                    PUTS("Allow: ");
                     67:                    first = NO;
                     68:                } else
                     69:                    PUTC(',');
                     70:                PUTS(HTMethod_name(1<<cnt));
                     71:            }
                     72:        }
                     73:        if (!first) PUTBLOCK(crlf, 2);
2.10      frystyk    74:     }
2.35      frystyk    75:     if (EntityMask & HT_E_CONTENT_ENCODING && entity->content_encoding) {
2.17      frystyk    76:        BOOL first = YES;
                     77:        HTList * cur = entity->content_encoding;
                     78:        HTEncoding pres;
2.35      frystyk    79:        while ((pres = (HTEncoding) HTList_nextObject(cur)) &&
                     80:               !HTFormat_isUnityContent(pres)) {
2.17      frystyk    81:            if (first) {
                     82:                PUTS("Content-Encoding: ");
                     83:                first = NO;
                     84:            } else
                     85:                PUTC(',');
                     86:            PUTS(HTAtom_name(pres));
                     87:        }
                     88:        if (!first) PUTBLOCK(crlf, 2);
2.1       frystyk    89:     }
2.34      frystyk    90:     if (EntityMask & HT_E_CTE && entity->cte) {
                     91:        HTEncoding cte = HTAnchor_contentTransferEncoding(entity);
                     92:        if (!HTFormat_isUnityTransfer(cte)) {
                     93:            sprintf(linebuf, "Content-Transfer-Encoding: %s%c%c",
                     94:                    HTAtom_name(cte), CR, LF);
                     95:            PUTBLOCK(linebuf, (int) strlen(linebuf));
                     96:        }
                     97:     }
2.21      frystyk    98:     if (EntityMask & HT_E_CONTENT_LANGUAGE && entity->content_language){
2.17      frystyk    99:        BOOL first = YES;
                    100:        HTList * cur = entity->content_language;
                    101:        HTLanguage pres;
                    102:        while ((pres = (HTLanguage) HTList_nextObject(cur))) {
                    103:            if (first) {
                    104:                PUTS("Content-Language: ");
                    105:                first = NO;
                    106:            } else
                    107:                PUTC(',');
                    108:            PUTS(HTAtom_name(pres));
                    109:        }
                    110:        if (!first) PUTBLOCK(crlf, 2);
2.1       frystyk   111:     }
2.34      frystyk   112: 
                    113:     /* Only send out Content-Length if we don't have a transfer coding */
                    114:     if (!HTRequest_transfer(request)) {
                    115:        if (EntityMask & HT_E_CONTENT_LENGTH) {
                    116:            if (entity->content_length >= 0) {
                    117:                sprintf(linebuf, "Content-Length: %ld%c%c",
                    118:                        entity->content_length, CR, LF);
                    119:                PUTBLOCK(linebuf, (int) strlen(linebuf));       
                    120:            } else {
                    121:                transfer_coding = YES;
                    122:            }
2.20      frystyk   123:        }
2.1       frystyk   124:     }
2.33      frystyk   125:     if (EntityMask & HT_E_CONTENT_TYPE && entity->content_type) {
                    126:        HTFormat format = entity->content_type != WWW_UNKNOWN ?
                    127:            entity->content_type : WWW_BINARY;
2.19      frystyk   128:        HTAssocList * parameters = HTAnchor_formatParam(entity);
                    129: 
                    130:        /* Output the content type */
                    131:        PUTS("Content-Type: ");
2.33      frystyk   132:        PUTS(HTAtom_name(format));
2.19      frystyk   133: 
                    134:        /* Add all parameters */
                    135:        if (parameters) {
                    136:            HTAssoc * pres;
                    137:            while ((pres = (HTAssoc *) HTAssocList_nextObject(parameters))) {
                    138:                PUTS(";");
                    139:                PUTS(HTAssoc_name(pres));
                    140:                PUTS("=");
                    141:                PUTS(HTAssoc_value(pres));
                    142:            }
                    143:        }
                    144:        PUTBLOCK(crlf, 2);
2.10      frystyk   145:     }
2.21      frystyk   146:     if (EntityMask & HT_E_DERIVED_FROM && entity->derived_from) {
2.10      frystyk   147:        sprintf(linebuf, "Derived-From: %s%c%c", entity->derived_from,
                    148:                CR, LF);
2.2       frystyk   149:        PUTBLOCK(linebuf, (int) strlen(linebuf));
2.1       frystyk   150:     }
2.21      frystyk   151:     if (EntityMask & HT_E_EXPIRES) {
2.10      frystyk   152:        if (entity->expires != -1) {
                    153:            sprintf(linebuf, "Expires: %s%c%c",
                    154:                    HTDateTimeStr(&entity->expires, NO), CR,LF);
2.2       frystyk   155:            PUTBLOCK(linebuf, (int) strlen(linebuf));
2.1       frystyk   156:        }
2.10      frystyk   157:     }
2.21      frystyk   158:     if (EntityMask & HT_E_LAST_MODIFIED) {
2.10      frystyk   159:        if (entity->last_modified != -1) {
                    160:            sprintf(linebuf, "Last-Modified: %s%c%c",
                    161:                    HTDateTimeStr(&entity->last_modified, NO), CR,LF);
2.2       frystyk   162:            PUTBLOCK(linebuf, (int) strlen(linebuf));
2.1       frystyk   163:        }
2.10      frystyk   164:     }
2.37    ! frystyk   165:     if (EntityMask & HT_E_LINK) {
        !           166:        HTLink * link = HTAnchor_mainLink((HTAnchor *) entity);
        !           167:        HTList * sublinks = HTAnchor_subLinks((HTAnchor *) entity);
        !           168:        HTLinkType linktype = NULL;
        !           169: 
        !           170:        /* First look in the main link */
        !           171:        if (link && (linktype = HTLink_type(link))) {               
        !           172:            char * src = HTAnchor_address((HTAnchor *) entity);
        !           173:            HTParentAnchor * dest = HTAnchor_parent(HTLink_destination(link));
        !           174:            char * dst = HTAnchor_address((HTAnchor *) dest);
        !           175:            char * rel_dst = HTRelative(src, dst);
        !           176:            if (rel_dst) {
        !           177:                PUTS("Link: <");
        !           178:                PUTS(rel_dst);
        !           179:                PUTS(">");
        !           180:                sprintf(linebuf, ";rel=\"%s\"", HTAtom_name(linktype));
        !           181:                PUTBLOCK(linebuf, (int) strlen(linebuf));
        !           182:                HT_FREE(rel_dst);
        !           183:                HT_FREE(dst);
        !           184:            }
2.1       frystyk   185: 
2.37    ! frystyk   186:            /* ... and then in any sublinks */
        !           187:            if (sublinks) {
        !           188:                HTLink * pres;
        !           189:                while ((pres = (HTLink *) HTList_nextObject(sublinks))) {
        !           190:                    if ((linktype = HTLink_type(pres))) {
        !           191:                        dest = HTAnchor_parent(HTLink_destination(pres));
        !           192:                        dst = HTAnchor_address((HTAnchor *) dest);
        !           193:                        rel_dst = HTRelative(src, dst);
        !           194:                        if (rel_dst) {
        !           195:                            PUTS(", <");
        !           196:                            PUTS(rel_dst);
        !           197:                            PUTS(">");
        !           198:                            sprintf(linebuf, ";rel=\"%s\"", HTAtom_name(linktype));
        !           199:                            PUTBLOCK(linebuf, (int) strlen(linebuf));
        !           200:                            HT_FREE(rel_dst);
        !           201:                            HT_FREE(dst);
        !           202:                        }
        !           203:                    }
        !           204:                }
        !           205:            }
        !           206:            PUTBLOCK(crlf, 2);
        !           207:            HT_FREE(src);
        !           208:        }
2.10      frystyk   209:     }
2.37    ! frystyk   210:     if (EntityMask & HT_E_TITLE && entity->title) {
        !           211:        sprintf(linebuf, "Title: %s%c%c", entity->title, CR, LF);
        !           212:        PUTBLOCK(linebuf, (int) strlen(linebuf));
2.10      frystyk   213:     }
2.21      frystyk   214:     if (EntityMask & HT_E_URI) {               /* @@@@@@@@@@ */
2.1       frystyk   215: 
                    216:     }
2.21      frystyk   217:     if (EntityMask & HT_E_VERSION && entity->version) {
2.37    ! frystyk   218:        sprintf(linebuf, "Content-Version: %s%c%c", entity->version, CR, LF);
2.10      frystyk   219:        PUTBLOCK(linebuf, (int) strlen(linebuf));
                    220:     }
                    221:     if (me->endHeader) {
                    222:        sprintf(linebuf, "%c%c", CR, LF);          /* Blank line means "end" */
                    223:        PUTBLOCK(linebuf, (int) strlen(linebuf));
2.2       frystyk   224:     }
2.20      frystyk   225: 
                    226:     /*
2.34      frystyk   227:     **  Handle any Transfer encoding. This is really a transport issue but
                    228:     **  as it often pops up when we are sending an entity then we put it
                    229:     **  here for now. A better place whould be in the HTTPGen stream.
                    230:     **  the real problem is that the server doesn't have any mechanism of
                    231:     **  telling the client what transports it can handle. The best we can
                    232:     **  hope for is that the server understands "chunked" although we are
                    233:     **  certainly capable of handling nested encodings :(
                    234:     */
                    235:     if (transfer_coding) {
                    236:        HTStream * target = HTTransferCodingStack(WWW_CODING_CHUNKED,
                    237:                                                  me->target, request, NULL, NO);
                    238:        if (STREAM_TRACE) HTTrace("Building.... Transfer-Encoding stack\n");
                    239:        if (target == HTBlackHole()) {
                    240:            if (me->target) (*me->target->isa->abort)(me->target, NULL);
                    241:            me->target = HTErrorStream();
                    242:        } else
                    243:            me->target = target;
                    244:     }
                    245: 
                    246: #if 0
                    247:     /*
                    248:     **  We expect the anchor object already to have the right encoding and
                    249:     **  we therefore should not set up extra streams for doing this.
2.20      frystyk   250:     */
                    251: 
2.34      frystyk   252:     /* Handle any Content Transfer encoding */
2.20      frystyk   253:     {
2.34      frystyk   254:        HTEncoding cte = HTAnchor_contentTransferEncoding(entity);
                    255:        if (!HTFormat_isUnityTransfer(cte)) {
2.20      frystyk   256:            if (STREAM_TRACE) HTTrace("Building.... C-T-E stack\n");
2.34      frystyk   257:            me->target = HTContentTransferCodingStack(cte, me->target,
                    258:                                                      request, NULL, YES);
2.20      frystyk   259:        }
                    260:     }
                    261: 
2.34      frystyk   262:     /* Handle any Content Encodings */
2.20      frystyk   263:     {
                    264:        HTList * cc = HTAnchor_encoding(entity);
                    265:        if (cc) {
                    266:            if (STREAM_TRACE) HTTrace("Building.... C-E stack\n");
2.33      frystyk   267:            me->target = HTContentEncodingStack(cc, me->target, request, NULL);
2.20      frystyk   268:        }
                    269:     }
2.34      frystyk   270: #endif
2.20      frystyk   271: 
2.13      eric      272:     if (PROT_TRACE) HTTrace("MIME........ Generating Entity Headers\n");
2.2       frystyk   273:     return HT_OK;
2.1       frystyk   274: }
                    275: 
2.32      frystyk   276: /*
                    277: **     Flushes header but doesn't free stream object
                    278: */
                    279: PRIVATE int MIMERequest_flush (HTStream * me)
                    280: {
                    281:     int status = MIMERequest_put_block(me, NULL, 0);
                    282:     return status==HT_OK ? (*me->target->isa->flush)(me->target) : status;
                    283: }
                    284: 
2.14      frystyk   285: PRIVATE int MIMERequest_put_block (HTStream * me, const char * b, int l)
2.1       frystyk   286: {
2.23      frystyk   287:     HTNet * net = HTRequest_net(me->request);
2.32      frystyk   288:     if (!me->transparent) {
2.1       frystyk   289:        MIMEMakeRequest(me, me->request);
2.9       frystyk   290:        me->transparent = YES;  
2.22      frystyk   291: 
                    292:        /*
2.32      frystyk   293:        **  First we only send the header
2.22      frystyk   294:        */
                    295:        if (HTMethod_hasEntity(HTRequest_method(me->request))) {
                    296:            HTHost * host = HTNet_host(net);
                    297:            char * class = HTHost_class(host);
2.23      frystyk   298:            if (class && !strcmp(class, "http")) {
2.32      frystyk   299:                MIMERequest_flush(me);
                    300:                return HT_PAUSE;
2.22      frystyk   301:            }
                    302:        }
2.1       frystyk   303:     }
2.32      frystyk   304:     
2.23      frystyk   305:     /* Check if we have written it all */
                    306:     if (b) {
                    307:        HTParentAnchor * entity = HTRequest_entityAnchor(me->request);
                    308:        long cl = HTAnchor_length(entity);
                    309:        return (cl>=0 && HTNet_bytesWritten(net) >= cl) ?
                    310:            HT_LOADED : PUTBLOCK(b, l);
                    311:     }
                    312:     return HT_OK;
2.1       frystyk   313: }
                    314: 
                    315: PRIVATE int MIMERequest_put_character (HTStream * me, char c)
                    316: {
                    317:     return MIMERequest_put_block(me, &c, 1);
                    318: }
                    319: 
2.14      frystyk   320: PRIVATE int MIMERequest_put_string (HTStream * me, const char * s)
2.1       frystyk   321: {
                    322:     return MIMERequest_put_block(me, s, strlen(s));
                    323: }
                    324: 
                    325: /*
                    326: **     Flushes data and frees stream object
                    327: */
                    328: PRIVATE int MIMERequest_free (HTStream * me)
                    329: {
                    330:     int status = MIMERequest_flush(me);
                    331:     if (status != HT_WOULD_BLOCK) {
                    332:        if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
                    333:            return HT_WOULD_BLOCK;
2.12      frystyk   334:        HT_FREE(me);
2.1       frystyk   335:     }
                    336:     return status;
                    337: }
                    338: 
2.4       frystyk   339: PRIVATE int MIMERequest_abort (HTStream * me, HTList * e)
2.1       frystyk   340: {
                    341:     if (me->target) (*me->target->isa->abort)(me->target, e);
2.12      frystyk   342:     HT_FREE(me);
2.13      eric      343:     if (PROT_TRACE) HTTrace("MIMERequest. ABORTING...\n");
2.1       frystyk   344:     return HT_ERROR;
                    345: }
                    346: 
                    347: /*     MIMERequest Stream
                    348: **     -----------------
                    349: */
2.14      frystyk   350: PRIVATE const HTStreamClass MIMERequestClass =
2.1       frystyk   351: {              
                    352:     "MIMERequest",
                    353:     MIMERequest_flush,
                    354:     MIMERequest_free,
                    355:     MIMERequest_abort,
                    356:     MIMERequest_put_character,
                    357:     MIMERequest_put_string,
                    358:     MIMERequest_put_block
                    359: };
                    360: 
2.10      frystyk   361: PUBLIC HTStream * HTMIMERequest_new (HTRequest * request, HTStream * target,
                    362:                                     BOOL endHeader)
2.1       frystyk   363: {
2.12      frystyk   364:     HTStream * me;
                    365:     if ((me = (HTStream  *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
                    366:         HT_OUTOFMEM("HTMIMERequest_new");
2.1       frystyk   367:     me->isa = &MIMERequestClass;
                    368:     me->target = target;
                    369:     me->request = request;
2.10      frystyk   370:     me->endHeader = endHeader;
2.1       frystyk   371:     me->transparent = NO;
                    372:     return me;
                    373: }

Webmaster