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

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

Webmaster