Annotation of libwww/Library/src/HTMIME.c, revision 2.83

2.15      frystyk     1: /*                                                                    HTMIME.c
                      2: **     MIME MESSAGE PARSE
                      3: **
2.22      frystyk     4: **     (c) COPYRIGHT MIT 1995.
2.15      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
2.83    ! frystyk     6: **     @(#) $Id: HTMIME.c,v 2.82 1998/02/27 18:35:45 frystyk Exp $
2.1       timbl       7: **
                      8: **     This is RFC 1341-specific code.
                      9: **     The input stream pushed into this parser is assumed to be
                     10: **     stripped on CRs, ie lines end with LF, not CR LF.
                     11: **     (It is easy to change this except for the body part where
                     12: **     conversion can be slow.)
                     13: **
                     14: ** History:
                     15: **        Feb 92       Written Tim Berners-Lee, CERN
2.13      duns       16: **      8 Jul 94  FM   Insulate free() from _free structure element.
2.71      frystyk    17: **     14 Mar 95  HFN  Now using response for storing data. No more `\n',
2.18      frystyk    18: **                     static buffers etc.
2.1       timbl      19: */
2.17      frystyk    20: 
                     21: /* Library include files */
2.57      frystyk    22: #include "sysdep.h"
2.60      frystyk    23: #include "WWWUtil.h"
2.61      frystyk    24: #include "WWWCore.h"
2.70      frystyk    25: #include "WWWCache.h"
                     26: #include "WWWStream.h"
2.61      frystyk    27: #include "HTReqMan.h"
                     28: #include "HTNetMan.h"
2.36      frystyk    29: #include "HTHeader.h"
2.64      eric       30: #include "HTWWWStr.h"
2.14      frystyk    31: #include "HTMIME.h"                                     /* Implemented here */
2.1       timbl      32: 
2.64      eric       33: #define MIME_HASH_SIZE 101
                     34: 
2.70      frystyk    35: typedef enum _HTMIMEMode {
                     36:     HT_MIME_HEADER     = 0x1,
2.71      frystyk    37:     HT_MIME_FOOTER     = 0x2,
2.77      frystyk    38:     HT_MIME_PARTIAL    = 0x4,
                     39:     HT_MIME_CONT       = 0x8
2.70      frystyk    40: } HTMIMEMode;
                     41: 
2.1       timbl      42: struct _HTStream {
2.57      frystyk    43:     const HTStreamClass *      isa;
2.18      frystyk    44:     HTRequest *                        request;
2.71      frystyk    45:     HTResponse *               response;
2.32      frystyk    46:     HTNet *                    net;
2.18      frystyk    47:     HTStream *                 target;
                     48:     HTFormat                   target_format;
2.64      eric       49:     HTChunk *                  token;
                     50:     HTChunk *                  value;
                     51:     int                                hash;
2.59      frystyk    52:     HTEOLState                 EOLstate;
2.70      frystyk    53:     HTMIMEMode                 mode;
2.18      frystyk    54:     BOOL                       transparent;
2.64      eric       55:     BOOL                       haveToken;
2.78      frystyk    56:     BOOL                       hasBody;
2.1       timbl      57: };
                     58: 
2.18      frystyk    59: /* ------------------------------------------------------------------------- */
2.1       timbl      60: 
2.64      eric       61: PRIVATE int pumpData (HTStream * me)
2.18      frystyk    62: {
2.64      eric       63:     HTRequest * request = me->request;
2.71      frystyk    64:     HTResponse * response = me->response;
                     65:     HTFormat format = HTResponse_format(response);
2.83    ! frystyk    66:     HTList * te = HTResponse_transfer(response);
        !            67:     HTList * ce = HTResponse_encoding(response);
2.71      frystyk    68:     long length = HTResponse_length(response);
2.83    ! frystyk    69:     HTStream * BlackHole = HTBlackHole();
        !            70:     BOOL savestream = NO;
2.48      frystyk    71:     me->transparent = YES;               /* Pump rest of data right through */
2.27      frystyk    72: 
2.71      frystyk    73:     /*  If this request is a source in PostWeb then pause here */
2.66      frystyk    74:     if (HTRequest_isSource(request)) return HT_PAUSE;
2.47      frystyk    75: 
2.71      frystyk    76:     /*
2.77      frystyk    77:     **  Cache the metainformation in the anchor object by copying
2.71      frystyk    78:     **  it from the response object. This we do regardless if
                     79:     **  we have a persistent cache or not as the memory cache will
                     80:     **  use it as well. If we are updating a cache entry using
2.77      frystyk    81:     **  byte ranges then we already have the metainformation and
2.71      frystyk    82:     **  hence we can ignore the new one as it'd better be the same.
                     83:     */
2.77      frystyk    84:     if (!(me->mode & HT_MIME_PARTIAL) && HTResponse_isCachable(me->response))
2.71      frystyk    85:        HTAnchor_update(HTRequest_anchor(request), me->response);
                     86: 
                     87:     /*
                     88:     **  If we asked only to read the header or footer or we used a HEAD
                     89:     **  method then we stop here as we don't expect any body part.
                     90:     */
2.70      frystyk    91:     if (me->mode & (HT_MIME_HEADER | HT_MIME_FOOTER) ||
2.71      frystyk    92:        HTRequest_method(request) == METHOD_HEAD) {
2.78      frystyk    93:         return HT_LOADED;
2.70      frystyk    94:     }
2.43      frystyk    95: 
2.60      frystyk    96:     /*
2.77      frystyk    97:     **  If we are paring a 1xx response then return HT_CONTINUE
                     98:     */
                     99:     if (me->mode & HT_MIME_CONT)
                    100:        return HT_CONTINUE;
                    101: 
                    102:     /*
2.71      frystyk   103:     **  If there is no content-length, no transfer encoding and no
                    104:     **  content type then we assume that there is no body part in
                    105:     **  the message and we can return HT_LOADED
2.68      frystyk   106:     */
2.79      frystyk   107:     {
                    108:        HTHost * host = HTNet_host(me->net);
2.83    ! frystyk   109:        if (length<=0 && te==NULL &&
2.79      frystyk   110:            HTHost_isPersistent(host) && !HTHost_closeNotification(host)) {
2.81      frystyk   111:            if (format != WWW_UNKNOWN) {
                    112:                if (STREAM_TRACE) HTTrace("MIME Parser. BAD - there seems to be a body but no length. This must be an HTTP/1.0 server pretending that it is HTTP/1.1\n");
                    113:                HTHost_setCloseNotification(host, YES);
                    114:            } else {
                    115:                if (STREAM_TRACE) HTTrace("MIME Parser. No body in this messsage\n");
                    116:                return HT_LOADED;
                    117:            }
2.79      frystyk   118:        }
2.68      frystyk   119:     }
                    120: 
                    121:     /*
2.78      frystyk   122:     **  Deal with the body
                    123:     */
                    124:     me->hasBody = YES;
                    125: 
                    126:     /*
2.71      frystyk   127:     **  Handle any Content Type
2.60      frystyk   128:     */
2.71      frystyk   129:     if (!(me->mode & HT_MIME_PARTIAL) &&
2.83    ! frystyk   130:        (format != WWW_UNKNOWN || length > 0 || te)) {
        !           131:        HTStream * target;
2.71      frystyk   132:        if (STREAM_TRACE) HTTrace("Building.... C-T stack from %s to %s\n",
                    133:                                  HTAtom_name(format),
                    134:                                  HTAtom_name(me->target_format));
2.83    ! frystyk   135:        if ((target = HTStreamStack(format, me->target_format,
        !           136:                                    me->target, request, YES))==BlackHole) {
        !           137:            if (!savestream) {
        !           138:                if (me->target) (*me->target->isa->abort)(me->target, NULL);
        !           139:                me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
        !           140:                savestream = YES;
        !           141:            }
        !           142:        } else
        !           143:            me->target = target;
2.18      frystyk   144:     }
2.60      frystyk   145: 
2.71      frystyk   146:     /*
                    147:     **  Can we cache the data object? If so then create a T stream and hook it 
                    148:     **  into the stream pipe. We do it before the transfer decoding so that we
                    149:     **  don't have to deal with that when we retrieve the object from cache.
                    150:     **  If we are appending to a cache entry then use a different stream than
                    151:     **  if creating a new entry.
                    152:     */
2.83    ! frystyk   153:     if (!savestream && HTCacheMode_enabled()) {
2.71      frystyk   154:        if (me->mode & HT_MIME_PARTIAL) {
                    155:            HTStream * append = HTStreamStack(WWW_CACHE_APPEND,
                    156:                                              me->target_format,
                    157:                                              me->target, request, NO);
                    158: #if 0
                    159:        if (cache) me->target = HTTee(me->target, cache, NULL);
                    160:        me->target = HTPipeBuffer_new(me->target, request, 0);
                    161: #else
                    162:        me->target = append;
                    163: #endif
                    164:        } else if (HTResponse_isCachable(me->response)) {
                    165:            HTStream * cache = HTStreamStack(WWW_CACHE, me->target_format,
                    166:                                             me->target, request, NO);
                    167:            if (cache) me->target = HTTee(me->target, cache, NULL);
                    168:        }
2.70      frystyk   169:     }
2.76      frystyk   170: 
                    171:     /*
2.83    ! frystyk   172:     **  Handle any Content Encodings
2.76      frystyk   173:     */
2.83    ! frystyk   174:     if (STREAM_TRACE) HTTrace("Building.... Content-Decoding stack\n");
        !           175:     if (ce) {
        !           176:        HTStream * target = HTContentDecodingStack(ce, me->target, request, NULL);
        !           177:        if (target == BlackHole) {
        !           178:            if (!savestream) {
        !           179:                if (me->target) (*me->target->isa->abort)(me->target, NULL);
        !           180:                me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
        !           181:                savestream = YES;
        !           182:            }
        !           183:        } else
        !           184:            me->target = target;
2.76      frystyk   185:     }
2.70      frystyk   186: 
2.71      frystyk   187:     /*
2.83    ! frystyk   188:     **  Handle any Transfer Encodings
2.71      frystyk   189:     */
2.83    ! frystyk   190:     if (STREAM_TRACE) HTTrace("Building.... Transfer-Decoding stack\n");
        !           191:     if (te) {
        !           192:        HTStream * target = HTTransferDecodingStack(te, me->target, request, NULL);
        !           193:        if (target == BlackHole) {
        !           194:            if (!savestream) {
        !           195:                if (me->target) (*me->target->isa->abort)(me->target, NULL);
        !           196:                me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
        !           197:                savestream = YES;
        !           198:            }
        !           199:        } else
        !           200:            me->target = target;
2.61      frystyk   201:     }
2.71      frystyk   202: 
2.27      frystyk   203:     return HT_OK;
2.1       timbl     204: }
                    205: 
2.65      eric      206: /* _dispatchParsers - call request's MIME header parser.
                    207: ** Use global parser if no appropriate one is found for request.
                    208: */
                    209: PRIVATE int _dispatchParsers (HTStream * me)
                    210: {
                    211:     int status;
                    212:     char * token = HTChunk_data(me->token);
                    213:     char * value = HTChunk_data(me->value);
2.71      frystyk   214:     BOOL found = NO;
                    215:     BOOL local = NO;
2.65      eric      216:     HTMIMEParseSet * parseSet;
                    217: 
                    218:     /* In case we get an empty header consisting of a CRLF, we fall thru */
2.66      frystyk   219:     if (STREAM_TRACE) HTTrace("MIME header. %s: %s\n",
                    220:                              token ? token : "<null>",
                    221:                              value ? value : "<null>");
                    222:     if (!token) return HT_OK;                      /* Ignore noop token */
2.65      eric      223: 
2.70      frystyk   224:     /*
2.71      frystyk   225:     ** Remember the original header
                    226:     */
                    227:     HTResponse_addHeader(me->response, token, value);
                    228: 
                    229:     /*
2.70      frystyk   230:     ** Search the local set of MIME parsers
                    231:     */
2.65      eric      232:     if ((parseSet = HTRequest_MIMEParseSet(me->request, &local)) != NULL) {
                    233:         status = HTMIMEParseSet_dispatch(parseSet, me->request, 
2.71      frystyk   234:                                         token, value, &found);
                    235:        if (found) return status;
2.65      eric      236:     }
                    237: 
2.70      frystyk   238:     /*
                    239:     ** Search the global set of MIME parsers
                    240:     */
2.71      frystyk   241:     if (local==NO && (parseSet = HTHeader_MIMEParseSet()) != NULL) {
                    242:        status = HTMIMEParseSet_dispatch(parseSet, me->request, 
                    243:                                         token, value, &found);
                    244:        if (found) return status;
                    245:     }
                    246: 
2.65      eric      247:     return HT_OK;
                    248: }
                    249: 
2.18      frystyk   250: /*
                    251: **     Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
                    252: **     Folding is either of CF LWS, LF LWS, CRLF LWS
                    253: */
2.57      frystyk   254: PRIVATE int HTMIME_put_block (HTStream * me, const char * b, int l)
2.18      frystyk   255: {
2.57      frystyk   256:     const char * start = b;
                    257:     const char * end = start;
2.64      eric      258:     const char * value = me->value->size ? b : NULL;
2.73      frystyk   259:     int length = l;
2.64      eric      260:     int status;
                    261: 
                    262:     while (!me->transparent) {
2.18      frystyk   263:        if (me->EOLstate == EOL_FCR) {
2.64      eric      264:            if (*b == CR)                                   /* End of header */
                    265:                me->EOLstate = EOL_END;
                    266:            else if (*b == LF)                               /* CRLF */
2.18      frystyk   267:                me->EOLstate = EOL_FLF;
2.80      frystyk   268:            else if (isspace((int) *b))                    /* Folding: CR SP */
2.64      eric      269:                me->EOLstate = EOL_FOLD;
                    270:            else                                                 /* New line */
                    271:                me->EOLstate = EOL_LINE;
2.18      frystyk   272:        } else if (me->EOLstate == EOL_FLF) {
                    273:            if (*b == CR)                               /* LF CR or CR LF CR */
                    274:                me->EOLstate = EOL_SCR;
2.64      eric      275:            else if (*b == LF)                              /* End of header */
                    276:                me->EOLstate = EOL_END;
2.80      frystyk   277:            else if (isspace((int) *b))        /* Folding: LF SP or CR LF SP */
2.64      eric      278:                me->EOLstate = EOL_FOLD;
                    279:            else                                                /* New line */
                    280:                me->EOLstate = EOL_LINE;
                    281:        } else if (me->EOLstate == EOL_SCR) {
                    282:            if (*b==CR || *b==LF)                           /* End of header */
                    283:                me->EOLstate = EOL_END;
2.80      frystyk   284:            else if (isspace((int) *b))  /* Folding: LF CR SP or CR LF CR SP */
2.64      eric      285:                me->EOLstate = EOL_FOLD;
                    286:            else                                                /* New line */
                    287:                me->EOLstate = EOL_LINE;
                    288:        } else if (*b == CR)
                    289:            me->EOLstate = EOL_FCR;
                    290:        else if (*b == LF)
                    291:            me->EOLstate = EOL_FLF;                            /* Line found */
                    292:        else {
                    293:            if (!me->haveToken) {
2.80      frystyk   294:                if (*b == ':' || isspace((int) *b)) {
2.64      eric      295:                    HTChunk_putb(me->token, start, end-start);
                    296:                    HTChunk_putc(me->token, '\0');
                    297:                    me->haveToken = YES;
                    298:                } else {
                    299:                    unsigned char ch = *(unsigned char *) b;
2.73      frystyk   300:                    ch = tolower(ch);
2.64      eric      301:                    me->hash = (me->hash * 3 + ch) % MIME_HASH_SIZE;
                    302:                }
2.80      frystyk   303:            } else if (value == NULL && *b != ':' && !isspace((int) *b))
2.64      eric      304:                value = b;
                    305:            end++;
                    306:        }
                    307:        switch (me->EOLstate) {
                    308:            case EOL_LINE:
2.73      frystyk   309:            case EOL_END:
                    310:            {
2.78      frystyk   311:                int ret = HT_ERROR;
2.64      eric      312:                HTChunk_putb(me->value, value, end-value);
                    313:                HTChunk_putc(me->value, '\0');
2.75      frystyk   314:                ret = _dispatchParsers(me);
2.73      frystyk   315:                HTNet_addBytesRead(me->net, b-start);
2.64      eric      316:                start=b, end=b;
                    317:                if (me->EOLstate == EOL_END) {          /* EOL_END */
2.75      frystyk   318:                    if (ret == HT_OK) {
2.67      frystyk   319:                        b++, l--;
2.78      frystyk   320:                        ret = pumpData(me);
2.73      frystyk   321:                        HTNet_addBytesRead(me->net, 1);
2.82      frystyk   322:                        if (me->mode & (HT_MIME_FOOTER | HT_MIME_CONT)) {
2.73      frystyk   323:                            HTHost_setConsumed(HTNet_host(me->net), length - l);
2.75      frystyk   324:                            return ret;
2.78      frystyk   325:                         } else {
                    326:                             HTNet_setHeaderLength(me->net, HTNet_bytesRead(me->net));
                    327:                         }
2.67      frystyk   328:                    }
2.64      eric      329:                } else {                                /* EOL_LINE */
                    330:                    HTChunk_clear(me->token);
                    331:                    HTChunk_clear(me->value);
                    332:                    me->haveToken = NO;
                    333:                    me->hash = 0;
                    334:                    value = NULL;
                    335:                }
2.18      frystyk   336:                me->EOLstate = EOL_BEGIN;
2.78      frystyk   337:                if (ret != HT_OK && ret != HT_LOADED) return ret;
2.64      eric      338:                break;
2.73      frystyk   339:            }
2.64      eric      340:            case EOL_FOLD:
2.18      frystyk   341:                me->EOLstate = EOL_BEGIN;
2.64      eric      342:                if (!me->haveToken) {
                    343:                    HTChunk_putb(me->token, start, end-start);
                    344:                    HTChunk_putc(me->token, '\0');
                    345:                    me->haveToken = YES;
                    346:                } else if (value) {
                    347:                    HTChunk_putb(me->value, value, end-value);
                    348:                    HTChunk_putc(me->value, ' ');
                    349:                }
                    350:                start=b, end=b;
                    351:                break;
                    352:            default: 
2.73      frystyk   353:                b++, l--;
2.64      eric      354:                if (!l) {
                    355:                    if (!me->haveToken)
                    356:                        HTChunk_putb(me->token, start, end-start);
                    357:                    else if (value)
                    358:                        HTChunk_putb(me->value, value, end-value);
2.78      frystyk   359:                    HTHost_setConsumed(HTNet_host(me->net), length - l);
2.64      eric      360:                    return HT_OK;
                    361:                }
                    362:        }
2.18      frystyk   363:     }
2.32      frystyk   364: 
2.78      frystyk   365:     if (length != l) HTHost_setConsumed(HTNet_host(me->net), length - l);
                    366: 
2.32      frystyk   367:     /* 
                    368:     ** Put the rest down the stream without touching the data but make sure
2.73      frystyk   369:     ** that we get the correct content length of data. If we have a CL in
                    370:     ** the headers then this stream is responsible for the accountance.
2.32      frystyk   371:     */
2.82      frystyk   372:     if (me->hasBody) {
2.73      frystyk   373:        HTNet * net = me->net;
2.66      frystyk   374:        /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
2.73      frystyk   375:        long cl = HTResponse_length(me->response);
2.82      frystyk   376:        if (cl >= 0) {
2.73      frystyk   377:            long bodyRead = HTNet_bytesRead(net) - HTNet_headerLength(net);
                    378: 
                    379:            /*
                    380:            **  If we have more than we need then just take what belongs to us.
                    381:            */
                    382:            if (bodyRead + l >= cl) {
                    383:                int consume = cl - bodyRead;
                    384:                if ((status = (*me->target->isa->put_block)(me->target, b, consume)) < 0)
                    385:                    return status;          
                    386:                HTNet_addBytesRead(net, consume);
2.78      frystyk   387:                HTHost_setConsumed(HTNet_host(net), consume);
2.73      frystyk   388:                return HT_LOADED;
                    389:            } else {
                    390:                if ((status = (*me->target->isa->put_block)(me->target, b, l)) < 0)
2.78      frystyk   391:                    return status;
2.73      frystyk   392:                HTNet_addBytesRead(net, l);
2.78      frystyk   393:                HTHost_setConsumed(HTNet_host(net), l);
2.73      frystyk   394:                return status;
                    395:            }
2.78      frystyk   396:        }
2.73      frystyk   397:        return (*me->target->isa->put_block)(me->target, b, l);
2.66      frystyk   398:     }
                    399:     return HT_LOADED;
2.18      frystyk   400: }
                    401: 
                    402: 
                    403: /*     Character handling
                    404: **     ------------------
                    405: */
2.36      frystyk   406: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18      frystyk   407: {
                    408:     return HTMIME_put_block(me, &c, 1);
                    409: }
                    410: 
2.1       timbl     411: 
                    412: /*     String handling
                    413: **     ---------------
                    414: */
2.57      frystyk   415: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1       timbl     416: {
2.18      frystyk   417:     return HTMIME_put_block(me, s, (int) strlen(s));
2.1       timbl     418: }
                    419: 
                    420: 
2.18      frystyk   421: /*     Flush an stream object
                    422: **     ---------------------
2.1       timbl     423: */
2.36      frystyk   424: PRIVATE int HTMIME_flush (HTStream * me)
2.1       timbl     425: {
2.47      frystyk   426:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1       timbl     427: }
                    428: 
2.18      frystyk   429: /*     Free a stream object
                    430: **     --------------------
2.1       timbl     431: */
2.36      frystyk   432: PRIVATE int HTMIME_free (HTStream * me)
2.1       timbl     433: {
2.18      frystyk   434:     int status = HT_OK;
2.64      eric      435:     if (!me->transparent)
2.65      eric      436:         if (_dispatchParsers(me) == HT_OK)
2.64      eric      437:            pumpData(me);
2.25      frystyk   438:     if (me->target) {
                    439:        if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
                    440:            return HT_WOULD_BLOCK;
                    441:     }
2.26      frystyk   442:     if (PROT_TRACE)
2.55      eric      443:        HTTrace("MIME........ FREEING....\n");
2.64      eric      444:     HTChunk_delete(me->token);
                    445:     HTChunk_delete(me->value);
2.52      frystyk   446:     HT_FREE(me);
2.18      frystyk   447:     return status;
2.1       timbl     448: }
                    449: 
                    450: /*     End writing
                    451: */
2.38      frystyk   452: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1       timbl     453: {
2.18      frystyk   454:     int status = HT_ERROR;
2.41      frystyk   455:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26      frystyk   456:     if (PROT_TRACE)
2.55      eric      457:        HTTrace("MIME........ ABORTING...\n");
2.64      eric      458:     HTChunk_delete(me->token);
                    459:     HTChunk_delete(me->value);
2.52      frystyk   460:     HT_FREE(me);
2.18      frystyk   461:     return status;
2.1       timbl     462: }
                    463: 
                    464: 
                    465: 
                    466: /*     Structured Object Class
                    467: **     -----------------------
                    468: */
2.57      frystyk   469: PRIVATE const HTStreamClass HTMIME =
2.1       timbl     470: {              
                    471:        "MIMEParser",
2.18      frystyk   472:        HTMIME_flush,
2.1       timbl     473:        HTMIME_free,
2.6       timbl     474:        HTMIME_abort,
                    475:        HTMIME_put_character,
                    476:        HTMIME_put_string,
2.18      frystyk   477:        HTMIME_put_block
2.1       timbl     478: }; 
                    479: 
                    480: 
2.48      frystyk   481: /*     MIME header parser stream.
2.1       timbl     482: **     -------------------------
2.48      frystyk   483: **     This stream parses a complete MIME header and if a content type header
                    484: **     is found then the stream stack is called. Any left over data is pumped
                    485: **     right through the stream
2.1       timbl     486: */
2.36      frystyk   487: PUBLIC HTStream* HTMIMEConvert (HTRequest *    request,
                    488:                                void *          param,
                    489:                                HTFormat        input_format,
                    490:                                HTFormat        output_format,
                    491:                                HTStream *      output_stream)
2.1       timbl     492: {
2.62      frystyk   493:     HTStream * me;
2.52      frystyk   494:     if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
                    495:         HT_OUTOFMEM("HTMIMEConvert");
2.1       timbl     496:     me->isa = &HTMIME;       
2.18      frystyk   497:     me->request = request;
2.71      frystyk   498:     me->response = HTRequest_response(request);
2.70      frystyk   499:     me->net = HTRequest_net(request);
2.49      frystyk   500:     me->target = output_stream;
2.18      frystyk   501:     me->target_format = output_format;
2.64      eric      502:     me->token = HTChunk_new(256);
                    503:     me->value = HTChunk_new(256);
                    504:     me->hash = 0;
2.18      frystyk   505:     me->EOLstate = EOL_BEGIN;
2.64      eric      506:     me->haveToken = NO;
2.1       timbl     507:     return me;
                    508: }
2.32      frystyk   509: 
2.48      frystyk   510: /*     MIME header ONLY parser stream
                    511: **     ------------------------------
                    512: **     This stream parses a complete MIME header and then returnes HT_PAUSE.
                    513: **     It does not set up any streams and resting data stays in the buffer.
                    514: **     This can be used if you only want to parse the headers before you
                    515: **     decide what to do next. This is for example the case in a server app.
                    516: */
                    517: PUBLIC HTStream * HTMIMEHeader (HTRequest *    request,
                    518:                                void *          param,
                    519:                                HTFormat        input_format,
                    520:                                HTFormat        output_format,
                    521:                                HTStream *      output_stream)
                    522: {
2.62      frystyk   523:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    524:                                  output_format, output_stream);
2.70      frystyk   525:     me->mode |= HT_MIME_HEADER;
2.48      frystyk   526:     return me;
                    527: }
2.77      frystyk   528: 
                    529: PUBLIC HTStream * HTMIMEContinue (HTRequest *  request,
                    530:                                  void *        param,
                    531:                                  HTFormat      input_format,
                    532:                                  HTFormat      output_format,
                    533:                                  HTStream *    output_stream)
                    534: {
                    535:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    536:                                  output_format, output_stream);
                    537:     me->mode |= HT_MIME_CONT;
                    538:     return me;
                    539: }
2.62      frystyk   540: 
                    541: /*     MIME footer ONLY parser stream
                    542: **     ------------------------------
                    543: **     Parse only a footer, for example after a chunked encoding.
                    544: */
                    545: PUBLIC HTStream * HTMIMEFooter (HTRequest *    request,
                    546:                                void *          param,
                    547:                                HTFormat        input_format,
                    548:                                HTFormat        output_format,
                    549:                                HTStream *      output_stream)
                    550: {
                    551:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    552:                                  output_format, output_stream);
2.70      frystyk   553:     me->mode |= HT_MIME_FOOTER;
2.62      frystyk   554:     return me;
                    555: }
2.71      frystyk   556: 
2.72      frystyk   557: /*
                    558: **     A small BEFORE filter that just finds a cache entry unconditionally
                    559: **     and loads the entry. All freshness and any other constraints are 
                    560: **     ignored.
                    561: */
                    562: PRIVATE int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
                    563: {
                    564:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    565:     HTCache * cache = HTCache_find(anchor);
                    566:     if (STREAM_TRACE) HTTrace("Cache Load.. loading partial cache entry\n");
                    567:     if (cache) {
                    568:        char * name = HTCache_name(cache);
                    569:        HTAnchor_setPhysical(anchor, name);
                    570:        HTCache_addHit(cache);
                    571:        HT_FREE(name);
                    572:     }
                    573:     return HT_OK;
                    574: }
                    575: 
                    576: /*
                    577: **     A small AFTER filter that flushes the PIPE buffer so that we can
                    578: **     get the rest of the data
                    579: */
                    580: PRIVATE int HTCacheFlushFilter (HTRequest * request, HTResponse * response,
                    581:                                void * param, int mode)
                    582: {
                    583:     HTStream * pipe = (HTStream *) param;    
                    584:     if (pipe) {
                    585:        if (STREAM_TRACE) HTTrace("Cache Flush. Flushing and freeing PIPE buffer\n");
                    586:        (*pipe->isa->flush)(pipe);
                    587:        (*pipe->isa->_free)(pipe);
                    588:     }
                    589: 
                    590:     /*
                    591:     **  We also delete the request obejct and stop more filters from being called.
                    592:     **  As this is our own request, it's OK to do that
                    593:     */
                    594:     HTRequest_delete(request);
                    595:     return HT_ERROR;
                    596: }
                    597: 
2.71      frystyk   598: /*     Partial Response MIME parser stream
                    599: **     -----------------------------------
                    600: **     In case we sent a Range conditional GET we may get back a partial
                    601: **     response. This response must be appended to the already existing
                    602: **     cache entry before presented to the user.
                    603: **     We do this by continuing to load the new object into a temporary 
                    604: **     buffer and at the same time start the cache load of the already
                    605: **     existing object. When we have loaded the cache we merge the two
                    606: **     buffers.
                    607: */
                    608: PUBLIC HTStream * HTMIMEPartial (HTRequest *   request,
                    609:                                 void *         param,
                    610:                                 HTFormat       input_format,
                    611:                                 HTFormat       output_format,
                    612:                                 HTStream *     output_stream)
                    613: {
                    614:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.72      frystyk   615:     HTFormat format = HTAnchor_format(anchor);
                    616:     HTStream * pipe = NULL;
                    617: 
2.71      frystyk   618:     /*
                    619:     **  The merge stream is a place holder for where we can put data when it
                    620:     **  arrives. We have two feeds: one from the cache and one from the net.
                    621:     **  We call the stream stack already now to get the right output stream.
                    622:     **  We can do this as we already know the content type from when we got the
                    623:     **  first part of the object.
                    624:     */
2.72      frystyk   625:     HTStream * merge = HTMerge(HTStreamStack(format,
                    626:                                             output_format, output_stream,
                    627:                                             request, YES), 2);
2.71      frystyk   628: 
                    629:     /*
2.72      frystyk   630:     **  Now we create the MIME parser stream in partial data mode. We also
                    631:     **  set the target to our merge stream.
2.71      frystyk   632:     */
                    633:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    634:                                  output_format, output_stream);
                    635:     me->mode |= HT_MIME_PARTIAL;
2.72      frystyk   636:     me->target = merge;
                    637: 
                    638:     /*
                    639:     **  Create the cache append stream, and a Tee stream
                    640:     */
                    641:     {
                    642:        HTStream * append = HTStreamStack(WWW_CACHE_APPEND, output_format,
                    643:                                          output_stream, request, NO);
                    644:        if (append) me->target = HTTee(me->target, append, NULL);
                    645:     }
                    646: 
                    647:     /*
                    648:     **  Create the pipe buffer stream to buffer the data that we read
                    649:     **  from the network
                    650:     */
2.74      frystyk   651:     if ((pipe = HTPipeBuffer(me->target, 0))) me->target = pipe;
2.71      frystyk   652: 
                    653:     /*
                    654:     **  Now start the second load from the cache. First we read this data from
                    655:     **  the cache and then we flush the data that we have read from the net.
                    656:     */
                    657:     {
2.72      frystyk   658:        HTRequest * cache_request = HTRequest_new();
2.71      frystyk   659: 
2.72      frystyk   660:        /*
                    661:        **  Set the output format to source and the output stream to the
                    662:        **  merge stream. As we have already set up the stream pipe, we just 
                    663:        **  load it as source.
                    664:        */
                    665:        HTRequest_setOutputFormat(cache_request, WWW_SOURCE);
                    666:        HTRequest_setOutputStream(cache_request, merge);
                    667: 
                    668:        /*
                    669:        **  Bind the anchor to the new request and also register a local
                    670:        **  AFTER filter to flush the pipe buffer so that we can get
                    671:        **  rest of the data through. 
                    672:        */
                    673:        HTRequest_setAnchor(cache_request, (HTAnchor *) anchor);
                    674:        HTRequest_addBefore(cache_request, HTCacheLoadFilter, NULL, NULL,
                    675:                            HT_FILTER_FIRST, YES);
                    676:        HTRequest_addAfter(cache_request, HTCacheFlushFilter, NULL, pipe,
                    677:                           HT_ALL, HT_FILTER_FIRST, YES);
2.71      frystyk   678: 
2.72      frystyk   679:        if (STREAM_TRACE) HTTrace("Partial..... Starting cache load\n");
                    680:        HTLoad(cache_request, NO);
2.71      frystyk   681:     }
                    682:     return me;
                    683: }
                    684: 

Webmaster