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

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.85    ! frystyk     6: **     @(#) $Id: HTMIME.c,v 2.84 1998/03/20 17:52:59 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.84      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.85    ! frystyk   203: 
        !           204:     /*
        !           205:     ** If we for some reason couldn't find a target stream
        !           206:     */
        !           207:     if (!me->target) me->target = HTBlackHole();
2.27      frystyk   208:     return HT_OK;
2.1       timbl     209: }
                    210: 
2.65      eric      211: /* _dispatchParsers - call request's MIME header parser.
                    212: ** Use global parser if no appropriate one is found for request.
                    213: */
                    214: PRIVATE int _dispatchParsers (HTStream * me)
                    215: {
                    216:     int status;
                    217:     char * token = HTChunk_data(me->token);
                    218:     char * value = HTChunk_data(me->value);
2.71      frystyk   219:     BOOL found = NO;
                    220:     BOOL local = NO;
2.65      eric      221:     HTMIMEParseSet * parseSet;
                    222: 
                    223:     /* In case we get an empty header consisting of a CRLF, we fall thru */
2.66      frystyk   224:     if (STREAM_TRACE) HTTrace("MIME header. %s: %s\n",
                    225:                              token ? token : "<null>",
                    226:                              value ? value : "<null>");
                    227:     if (!token) return HT_OK;                      /* Ignore noop token */
2.65      eric      228: 
2.70      frystyk   229:     /*
2.71      frystyk   230:     ** Remember the original header
                    231:     */
                    232:     HTResponse_addHeader(me->response, token, value);
                    233: 
                    234:     /*
2.70      frystyk   235:     ** Search the local set of MIME parsers
                    236:     */
2.65      eric      237:     if ((parseSet = HTRequest_MIMEParseSet(me->request, &local)) != NULL) {
                    238:         status = HTMIMEParseSet_dispatch(parseSet, me->request, 
2.71      frystyk   239:                                         token, value, &found);
                    240:        if (found) return status;
2.65      eric      241:     }
                    242: 
2.70      frystyk   243:     /*
                    244:     ** Search the global set of MIME parsers
                    245:     */
2.71      frystyk   246:     if (local==NO && (parseSet = HTHeader_MIMEParseSet()) != NULL) {
                    247:        status = HTMIMEParseSet_dispatch(parseSet, me->request, 
                    248:                                         token, value, &found);
                    249:        if (found) return status;
                    250:     }
                    251: 
2.65      eric      252:     return HT_OK;
                    253: }
                    254: 
2.18      frystyk   255: /*
                    256: **     Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
                    257: **     Folding is either of CF LWS, LF LWS, CRLF LWS
                    258: */
2.57      frystyk   259: PRIVATE int HTMIME_put_block (HTStream * me, const char * b, int l)
2.18      frystyk   260: {
2.57      frystyk   261:     const char * start = b;
                    262:     const char * end = start;
2.64      eric      263:     const char * value = me->value->size ? b : NULL;
2.73      frystyk   264:     int length = l;
2.64      eric      265:     int status;
                    266: 
                    267:     while (!me->transparent) {
2.18      frystyk   268:        if (me->EOLstate == EOL_FCR) {
2.64      eric      269:            if (*b == CR)                                   /* End of header */
                    270:                me->EOLstate = EOL_END;
                    271:            else if (*b == LF)                               /* CRLF */
2.18      frystyk   272:                me->EOLstate = EOL_FLF;
2.80      frystyk   273:            else if (isspace((int) *b))                    /* Folding: CR SP */
2.64      eric      274:                me->EOLstate = EOL_FOLD;
                    275:            else                                                 /* New line */
                    276:                me->EOLstate = EOL_LINE;
2.18      frystyk   277:        } else if (me->EOLstate == EOL_FLF) {
                    278:            if (*b == CR)                               /* LF CR or CR LF CR */
                    279:                me->EOLstate = EOL_SCR;
2.64      eric      280:            else if (*b == LF)                              /* End of header */
                    281:                me->EOLstate = EOL_END;
2.80      frystyk   282:            else if (isspace((int) *b))        /* Folding: LF SP or CR LF SP */
2.64      eric      283:                me->EOLstate = EOL_FOLD;
                    284:            else                                                /* New line */
                    285:                me->EOLstate = EOL_LINE;
                    286:        } else if (me->EOLstate == EOL_SCR) {
                    287:            if (*b==CR || *b==LF)                           /* End of header */
                    288:                me->EOLstate = EOL_END;
2.80      frystyk   289:            else if (isspace((int) *b))  /* Folding: LF CR SP or CR LF CR SP */
2.64      eric      290:                me->EOLstate = EOL_FOLD;
                    291:            else                                                /* New line */
                    292:                me->EOLstate = EOL_LINE;
                    293:        } else if (*b == CR)
                    294:            me->EOLstate = EOL_FCR;
                    295:        else if (*b == LF)
                    296:            me->EOLstate = EOL_FLF;                            /* Line found */
                    297:        else {
                    298:            if (!me->haveToken) {
2.80      frystyk   299:                if (*b == ':' || isspace((int) *b)) {
2.64      eric      300:                    HTChunk_putb(me->token, start, end-start);
                    301:                    HTChunk_putc(me->token, '\0');
                    302:                    me->haveToken = YES;
                    303:                } else {
                    304:                    unsigned char ch = *(unsigned char *) b;
2.73      frystyk   305:                    ch = tolower(ch);
2.64      eric      306:                    me->hash = (me->hash * 3 + ch) % MIME_HASH_SIZE;
                    307:                }
2.80      frystyk   308:            } else if (value == NULL && *b != ':' && !isspace((int) *b))
2.64      eric      309:                value = b;
                    310:            end++;
                    311:        }
                    312:        switch (me->EOLstate) {
                    313:            case EOL_LINE:
2.73      frystyk   314:            case EOL_END:
                    315:            {
2.78      frystyk   316:                int ret = HT_ERROR;
2.64      eric      317:                HTChunk_putb(me->value, value, end-value);
                    318:                HTChunk_putc(me->value, '\0');
2.75      frystyk   319:                ret = _dispatchParsers(me);
2.73      frystyk   320:                HTNet_addBytesRead(me->net, b-start);
2.64      eric      321:                start=b, end=b;
                    322:                if (me->EOLstate == EOL_END) {          /* EOL_END */
2.75      frystyk   323:                    if (ret == HT_OK) {
2.67      frystyk   324:                        b++, l--;
2.78      frystyk   325:                        ret = pumpData(me);
2.73      frystyk   326:                        HTNet_addBytesRead(me->net, 1);
2.82      frystyk   327:                        if (me->mode & (HT_MIME_FOOTER | HT_MIME_CONT)) {
2.73      frystyk   328:                            HTHost_setConsumed(HTNet_host(me->net), length - l);
2.75      frystyk   329:                            return ret;
2.78      frystyk   330:                         } else {
                    331:                             HTNet_setHeaderLength(me->net, HTNet_bytesRead(me->net));
                    332:                         }
2.67      frystyk   333:                    }
2.64      eric      334:                } else {                                /* EOL_LINE */
                    335:                    HTChunk_clear(me->token);
                    336:                    HTChunk_clear(me->value);
                    337:                    me->haveToken = NO;
                    338:                    me->hash = 0;
                    339:                    value = NULL;
                    340:                }
2.18      frystyk   341:                me->EOLstate = EOL_BEGIN;
2.78      frystyk   342:                if (ret != HT_OK && ret != HT_LOADED) return ret;
2.64      eric      343:                break;
2.73      frystyk   344:            }
2.64      eric      345:            case EOL_FOLD:
2.18      frystyk   346:                me->EOLstate = EOL_BEGIN;
2.64      eric      347:                if (!me->haveToken) {
                    348:                    HTChunk_putb(me->token, start, end-start);
                    349:                    HTChunk_putc(me->token, '\0');
                    350:                    me->haveToken = YES;
                    351:                } else if (value) {
                    352:                    HTChunk_putb(me->value, value, end-value);
                    353:                    HTChunk_putc(me->value, ' ');
                    354:                }
                    355:                start=b, end=b;
                    356:                break;
                    357:            default: 
2.73      frystyk   358:                b++, l--;
2.64      eric      359:                if (!l) {
                    360:                    if (!me->haveToken)
                    361:                        HTChunk_putb(me->token, start, end-start);
                    362:                    else if (value)
                    363:                        HTChunk_putb(me->value, value, end-value);
2.78      frystyk   364:                    HTHost_setConsumed(HTNet_host(me->net), length - l);
2.64      eric      365:                    return HT_OK;
                    366:                }
                    367:        }
2.18      frystyk   368:     }
2.32      frystyk   369: 
2.78      frystyk   370:     if (length != l) HTHost_setConsumed(HTNet_host(me->net), length - l);
                    371: 
2.32      frystyk   372:     /* 
                    373:     ** Put the rest down the stream without touching the data but make sure
2.73      frystyk   374:     ** that we get the correct content length of data. If we have a CL in
                    375:     ** the headers then this stream is responsible for the accountance.
2.32      frystyk   376:     */
2.82      frystyk   377:     if (me->hasBody) {
2.73      frystyk   378:        HTNet * net = me->net;
2.66      frystyk   379:        /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
2.73      frystyk   380:        long cl = HTResponse_length(me->response);
2.82      frystyk   381:        if (cl >= 0) {
2.73      frystyk   382:            long bodyRead = HTNet_bytesRead(net) - HTNet_headerLength(net);
                    383: 
                    384:            /*
                    385:            **  If we have more than we need then just take what belongs to us.
                    386:            */
                    387:            if (bodyRead + l >= cl) {
                    388:                int consume = cl - bodyRead;
                    389:                if ((status = (*me->target->isa->put_block)(me->target, b, consume)) < 0)
                    390:                    return status;          
                    391:                HTNet_addBytesRead(net, consume);
2.78      frystyk   392:                HTHost_setConsumed(HTNet_host(net), consume);
2.73      frystyk   393:                return HT_LOADED;
                    394:            } else {
                    395:                if ((status = (*me->target->isa->put_block)(me->target, b, l)) < 0)
2.78      frystyk   396:                    return status;
2.73      frystyk   397:                HTNet_addBytesRead(net, l);
2.78      frystyk   398:                HTHost_setConsumed(HTNet_host(net), l);
2.73      frystyk   399:                return status;
                    400:            }
2.78      frystyk   401:        }
2.73      frystyk   402:        return (*me->target->isa->put_block)(me->target, b, l);
2.66      frystyk   403:     }
                    404:     return HT_LOADED;
2.18      frystyk   405: }
                    406: 
                    407: 
                    408: /*     Character handling
                    409: **     ------------------
                    410: */
2.36      frystyk   411: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18      frystyk   412: {
                    413:     return HTMIME_put_block(me, &c, 1);
                    414: }
                    415: 
2.1       timbl     416: 
                    417: /*     String handling
                    418: **     ---------------
                    419: */
2.57      frystyk   420: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1       timbl     421: {
2.18      frystyk   422:     return HTMIME_put_block(me, s, (int) strlen(s));
2.1       timbl     423: }
                    424: 
                    425: 
2.18      frystyk   426: /*     Flush an stream object
                    427: **     ---------------------
2.1       timbl     428: */
2.36      frystyk   429: PRIVATE int HTMIME_flush (HTStream * me)
2.1       timbl     430: {
2.47      frystyk   431:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1       timbl     432: }
                    433: 
2.18      frystyk   434: /*     Free a stream object
                    435: **     --------------------
2.1       timbl     436: */
2.36      frystyk   437: PRIVATE int HTMIME_free (HTStream * me)
2.1       timbl     438: {
2.18      frystyk   439:     int status = HT_OK;
2.64      eric      440:     if (!me->transparent)
2.65      eric      441:         if (_dispatchParsers(me) == HT_OK)
2.64      eric      442:            pumpData(me);
2.25      frystyk   443:     if (me->target) {
                    444:        if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
                    445:            return HT_WOULD_BLOCK;
                    446:     }
2.26      frystyk   447:     if (PROT_TRACE)
2.55      eric      448:        HTTrace("MIME........ FREEING....\n");
2.64      eric      449:     HTChunk_delete(me->token);
                    450:     HTChunk_delete(me->value);
2.52      frystyk   451:     HT_FREE(me);
2.18      frystyk   452:     return status;
2.1       timbl     453: }
                    454: 
                    455: /*     End writing
                    456: */
2.38      frystyk   457: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1       timbl     458: {
2.18      frystyk   459:     int status = HT_ERROR;
2.41      frystyk   460:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26      frystyk   461:     if (PROT_TRACE)
2.55      eric      462:        HTTrace("MIME........ ABORTING...\n");
2.64      eric      463:     HTChunk_delete(me->token);
                    464:     HTChunk_delete(me->value);
2.52      frystyk   465:     HT_FREE(me);
2.18      frystyk   466:     return status;
2.1       timbl     467: }
                    468: 
                    469: 
                    470: 
                    471: /*     Structured Object Class
                    472: **     -----------------------
                    473: */
2.57      frystyk   474: PRIVATE const HTStreamClass HTMIME =
2.1       timbl     475: {              
                    476:        "MIMEParser",
2.18      frystyk   477:        HTMIME_flush,
2.1       timbl     478:        HTMIME_free,
2.6       timbl     479:        HTMIME_abort,
                    480:        HTMIME_put_character,
                    481:        HTMIME_put_string,
2.18      frystyk   482:        HTMIME_put_block
2.1       timbl     483: }; 
                    484: 
                    485: 
2.48      frystyk   486: /*     MIME header parser stream.
2.1       timbl     487: **     -------------------------
2.48      frystyk   488: **     This stream parses a complete MIME header and if a content type header
                    489: **     is found then the stream stack is called. Any left over data is pumped
                    490: **     right through the stream
2.1       timbl     491: */
2.36      frystyk   492: PUBLIC HTStream* HTMIMEConvert (HTRequest *    request,
                    493:                                void *          param,
                    494:                                HTFormat        input_format,
                    495:                                HTFormat        output_format,
                    496:                                HTStream *      output_stream)
2.1       timbl     497: {
2.62      frystyk   498:     HTStream * me;
2.52      frystyk   499:     if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
                    500:         HT_OUTOFMEM("HTMIMEConvert");
2.1       timbl     501:     me->isa = &HTMIME;       
2.18      frystyk   502:     me->request = request;
2.71      frystyk   503:     me->response = HTRequest_response(request);
2.70      frystyk   504:     me->net = HTRequest_net(request);
2.49      frystyk   505:     me->target = output_stream;
2.18      frystyk   506:     me->target_format = output_format;
2.64      eric      507:     me->token = HTChunk_new(256);
                    508:     me->value = HTChunk_new(256);
                    509:     me->hash = 0;
2.18      frystyk   510:     me->EOLstate = EOL_BEGIN;
2.64      eric      511:     me->haveToken = NO;
2.1       timbl     512:     return me;
                    513: }
2.32      frystyk   514: 
2.48      frystyk   515: /*     MIME header ONLY parser stream
                    516: **     ------------------------------
                    517: **     This stream parses a complete MIME header and then returnes HT_PAUSE.
                    518: **     It does not set up any streams and resting data stays in the buffer.
                    519: **     This can be used if you only want to parse the headers before you
                    520: **     decide what to do next. This is for example the case in a server app.
                    521: */
                    522: PUBLIC HTStream * HTMIMEHeader (HTRequest *    request,
                    523:                                void *          param,
                    524:                                HTFormat        input_format,
                    525:                                HTFormat        output_format,
                    526:                                HTStream *      output_stream)
                    527: {
2.62      frystyk   528:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    529:                                  output_format, output_stream);
2.70      frystyk   530:     me->mode |= HT_MIME_HEADER;
2.48      frystyk   531:     return me;
                    532: }
2.77      frystyk   533: 
                    534: PUBLIC HTStream * HTMIMEContinue (HTRequest *  request,
                    535:                                  void *        param,
                    536:                                  HTFormat      input_format,
                    537:                                  HTFormat      output_format,
                    538:                                  HTStream *    output_stream)
                    539: {
                    540:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    541:                                  output_format, output_stream);
                    542:     me->mode |= HT_MIME_CONT;
                    543:     return me;
                    544: }
2.62      frystyk   545: 
                    546: /*     MIME footer ONLY parser stream
                    547: **     ------------------------------
                    548: **     Parse only a footer, for example after a chunked encoding.
                    549: */
                    550: PUBLIC HTStream * HTMIMEFooter (HTRequest *    request,
                    551:                                void *          param,
                    552:                                HTFormat        input_format,
                    553:                                HTFormat        output_format,
                    554:                                HTStream *      output_stream)
                    555: {
                    556:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    557:                                  output_format, output_stream);
2.70      frystyk   558:     me->mode |= HT_MIME_FOOTER;
2.62      frystyk   559:     return me;
                    560: }
2.71      frystyk   561: 
2.72      frystyk   562: /*
                    563: **     A small BEFORE filter that just finds a cache entry unconditionally
                    564: **     and loads the entry. All freshness and any other constraints are 
                    565: **     ignored.
                    566: */
                    567: PRIVATE int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
                    568: {
                    569:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    570:     HTCache * cache = HTCache_find(anchor);
                    571:     if (STREAM_TRACE) HTTrace("Cache Load.. loading partial cache entry\n");
                    572:     if (cache) {
                    573:        char * name = HTCache_name(cache);
                    574:        HTAnchor_setPhysical(anchor, name);
                    575:        HTCache_addHit(cache);
                    576:        HT_FREE(name);
                    577:     }
                    578:     return HT_OK;
                    579: }
                    580: 
                    581: /*
                    582: **     A small AFTER filter that flushes the PIPE buffer so that we can
                    583: **     get the rest of the data
                    584: */
                    585: PRIVATE int HTCacheFlushFilter (HTRequest * request, HTResponse * response,
                    586:                                void * param, int mode)
                    587: {
                    588:     HTStream * pipe = (HTStream *) param;    
                    589:     if (pipe) {
                    590:        if (STREAM_TRACE) HTTrace("Cache Flush. Flushing and freeing PIPE buffer\n");
                    591:        (*pipe->isa->flush)(pipe);
                    592:        (*pipe->isa->_free)(pipe);
                    593:     }
                    594: 
                    595:     /*
                    596:     **  We also delete the request obejct and stop more filters from being called.
                    597:     **  As this is our own request, it's OK to do that
                    598:     */
                    599:     HTRequest_delete(request);
                    600:     return HT_ERROR;
                    601: }
                    602: 
2.71      frystyk   603: /*     Partial Response MIME parser stream
                    604: **     -----------------------------------
                    605: **     In case we sent a Range conditional GET we may get back a partial
                    606: **     response. This response must be appended to the already existing
                    607: **     cache entry before presented to the user.
                    608: **     We do this by continuing to load the new object into a temporary 
                    609: **     buffer and at the same time start the cache load of the already
                    610: **     existing object. When we have loaded the cache we merge the two
                    611: **     buffers.
                    612: */
                    613: PUBLIC HTStream * HTMIMEPartial (HTRequest *   request,
                    614:                                 void *         param,
                    615:                                 HTFormat       input_format,
                    616:                                 HTFormat       output_format,
                    617:                                 HTStream *     output_stream)
                    618: {
                    619:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.72      frystyk   620:     HTFormat format = HTAnchor_format(anchor);
                    621:     HTStream * pipe = NULL;
                    622: 
2.71      frystyk   623:     /*
                    624:     **  The merge stream is a place holder for where we can put data when it
                    625:     **  arrives. We have two feeds: one from the cache and one from the net.
                    626:     **  We call the stream stack already now to get the right output stream.
                    627:     **  We can do this as we already know the content type from when we got the
                    628:     **  first part of the object.
                    629:     */
2.72      frystyk   630:     HTStream * merge = HTMerge(HTStreamStack(format,
                    631:                                             output_format, output_stream,
                    632:                                             request, YES), 2);
2.71      frystyk   633: 
                    634:     /*
2.72      frystyk   635:     **  Now we create the MIME parser stream in partial data mode. We also
                    636:     **  set the target to our merge stream.
2.71      frystyk   637:     */
                    638:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    639:                                  output_format, output_stream);
                    640:     me->mode |= HT_MIME_PARTIAL;
2.72      frystyk   641:     me->target = merge;
                    642: 
                    643:     /*
                    644:     **  Create the cache append stream, and a Tee stream
                    645:     */
                    646:     {
                    647:        HTStream * append = HTStreamStack(WWW_CACHE_APPEND, output_format,
                    648:                                          output_stream, request, NO);
                    649:        if (append) me->target = HTTee(me->target, append, NULL);
                    650:     }
                    651: 
                    652:     /*
                    653:     **  Create the pipe buffer stream to buffer the data that we read
                    654:     **  from the network
                    655:     */
2.74      frystyk   656:     if ((pipe = HTPipeBuffer(me->target, 0))) me->target = pipe;
2.71      frystyk   657: 
                    658:     /*
                    659:     **  Now start the second load from the cache. First we read this data from
                    660:     **  the cache and then we flush the data that we have read from the net.
                    661:     */
                    662:     {
2.72      frystyk   663:        HTRequest * cache_request = HTRequest_new();
2.71      frystyk   664: 
2.72      frystyk   665:        /*
                    666:        **  Set the output format to source and the output stream to the
                    667:        **  merge stream. As we have already set up the stream pipe, we just 
                    668:        **  load it as source.
                    669:        */
                    670:        HTRequest_setOutputFormat(cache_request, WWW_SOURCE);
                    671:        HTRequest_setOutputStream(cache_request, merge);
                    672: 
                    673:        /*
                    674:        **  Bind the anchor to the new request and also register a local
                    675:        **  AFTER filter to flush the pipe buffer so that we can get
                    676:        **  rest of the data through. 
                    677:        */
                    678:        HTRequest_setAnchor(cache_request, (HTAnchor *) anchor);
                    679:        HTRequest_addBefore(cache_request, HTCacheLoadFilter, NULL, NULL,
                    680:                            HT_FILTER_FIRST, YES);
                    681:        HTRequest_addAfter(cache_request, HTCacheFlushFilter, NULL, pipe,
                    682:                           HT_ALL, HT_FILTER_FIRST, YES);
2.71      frystyk   683: 
2.72      frystyk   684:        if (STREAM_TRACE) HTTrace("Partial..... Starting cache load\n");
                    685:        HTLoad(cache_request, NO);
2.71      frystyk   686:     }
                    687:     return me;
                    688: }
                    689: 

Webmaster