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

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.90    ! frystyk     6: **     @(#) $Id: HTMIME.c,v 2.89 1998/07/22 19:18:57 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.86      frystyk    22: #include "wwwsys.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.90    ! frystyk    84:     if (!(me->mode & HT_MIME_PARTIAL) &&
        !            85:        HTResponse_isCachable(me->response) != HT_NO_CACHE)
2.71      frystyk    86:        HTAnchor_update(HTRequest_anchor(request), me->response);
                     87: 
                     88:     /*
                     89:     **  If we asked only to read the header or footer or we used a HEAD
                     90:     **  method then we stop here as we don't expect any body part.
                     91:     */
2.70      frystyk    92:     if (me->mode & (HT_MIME_HEADER | HT_MIME_FOOTER) ||
2.71      frystyk    93:        HTRequest_method(request) == METHOD_HEAD) {
2.88      frystyk    94:         HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                     95:         if (cbf) (*cbf)(request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
2.78      frystyk    96:         return HT_LOADED;
2.70      frystyk    97:     }
2.43      frystyk    98: 
2.60      frystyk    99:     /*
2.77      frystyk   100:     **  If we are paring a 1xx response then return HT_CONTINUE
                    101:     */
                    102:     if (me->mode & HT_MIME_CONT)
                    103:        return HT_CONTINUE;
                    104: 
                    105:     /*
2.71      frystyk   106:     **  If there is no content-length, no transfer encoding and no
                    107:     **  content type then we assume that there is no body part in
                    108:     **  the message and we can return HT_LOADED
2.68      frystyk   109:     */
2.79      frystyk   110:     {
                    111:        HTHost * host = HTNet_host(me->net);
2.84      frystyk   112:        if (length<0 && te==NULL &&
2.79      frystyk   113:            HTHost_isPersistent(host) && !HTHost_closeNotification(host)) {
2.81      frystyk   114:            if (format != WWW_UNKNOWN) {
                    115:                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");
                    116:                HTHost_setCloseNotification(host, YES);
                    117:            } else {
2.88      frystyk   118:                 HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    119:                 if (cbf) (*cbf)(request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
2.81      frystyk   120:                if (STREAM_TRACE) HTTrace("MIME Parser. No body in this messsage\n");
                    121:                return HT_LOADED;
                    122:            }
2.79      frystyk   123:        }
2.68      frystyk   124:     }
                    125: 
                    126:     /*
2.78      frystyk   127:     **  Deal with the body
                    128:     */
                    129:     me->hasBody = YES;
                    130: 
                    131:     /*
2.71      frystyk   132:     **  Handle any Content Type
2.60      frystyk   133:     */
2.71      frystyk   134:     if (!(me->mode & HT_MIME_PARTIAL) &&
2.83      frystyk   135:        (format != WWW_UNKNOWN || length > 0 || te)) {
                    136:        HTStream * target;
2.71      frystyk   137:        if (STREAM_TRACE) HTTrace("Building.... C-T stack from %s to %s\n",
                    138:                                  HTAtom_name(format),
                    139:                                  HTAtom_name(me->target_format));
2.83      frystyk   140:        if ((target = HTStreamStack(format, me->target_format,
                    141:                                    me->target, request, YES))==BlackHole) {
                    142:            if (!savestream) {
2.90    ! frystyk   143:                 if (me->target) (*me->target->isa->abort)(me->target, NULL);
        !           144:                 me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
        !           145:                savestream = YES;
        !           146:            }
        !           147:        } else
        !           148:            me->target = target;
        !           149:     }
        !           150: 
        !           151:     /*
        !           152:     **  Handle any Content Encodings
        !           153:     */
        !           154:     if (STREAM_TRACE) HTTrace("Building.... Content-Decoding stack\n");
        !           155:     if (ce) {
        !           156:        HTStream * target = HTContentDecodingStack(ce, me->target, request, NULL);
        !           157:        if (target == BlackHole) {
        !           158:            if (!savestream) {
2.83      frystyk   159:                if (me->target) (*me->target->isa->abort)(me->target, NULL);
                    160:                me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
                    161:                savestream = YES;
                    162:            }
                    163:        } else
                    164:            me->target = target;
2.18      frystyk   165:     }
2.60      frystyk   166: 
2.71      frystyk   167:     /*
                    168:     **  Can we cache the data object? If so then create a T stream and hook it 
                    169:     **  into the stream pipe. We do it before the transfer decoding so that we
                    170:     **  don't have to deal with that when we retrieve the object from cache.
                    171:     **  If we are appending to a cache entry then use a different stream than
                    172:     **  if creating a new entry.
                    173:     */
2.90    ! frystyk   174:     if (HTCacheMode_enabled()) {
2.71      frystyk   175:        if (me->mode & HT_MIME_PARTIAL) {
                    176:            HTStream * append = HTStreamStack(WWW_CACHE_APPEND,
                    177:                                              me->target_format,
                    178:                                              me->target, request, NO);
2.90    ! frystyk   179:             me->target = append;
        !           180:        } else if (HTResponse_isCachable(me->response) == HT_CACHE_ALL) {
2.71      frystyk   181:            HTStream * cache = HTStreamStack(WWW_CACHE, me->target_format,
                    182:                                             me->target, request, NO);
                    183:            if (cache) me->target = HTTee(me->target, cache, NULL);
                    184:        }
2.70      frystyk   185:     }
2.90    ! 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 {
2.89      frystyk   331:                             HTNet_setHeaderBytesRead(me->net, HTNet_bytesRead(me->net));
2.78      frystyk   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) {
2.87      frystyk   360:                    BOOL stop = NO;
                    361:                    if (!me->haveToken) {
                    362:                        /* If empty header then prepare to stop */
                    363:                        if (end-start)
                    364:                            HTChunk_putb(me->token, start, end-start);
                    365:                        else
                    366:                            stop = YES;
                    367:                    } else if (value)
2.64      eric      368:                        HTChunk_putb(me->value, value, end-value);
2.78      frystyk   369:                    HTHost_setConsumed(HTNet_host(me->net), length - l);
2.87      frystyk   370:                    return stop ? pumpData(me) : HT_OK;
2.64      eric      371:                }
                    372:        }
2.18      frystyk   373:     }
2.32      frystyk   374: 
2.78      frystyk   375:     if (length != l) HTHost_setConsumed(HTNet_host(me->net), length - l);
                    376: 
2.32      frystyk   377:     /* 
                    378:     ** Put the rest down the stream without touching the data but make sure
2.73      frystyk   379:     ** that we get the correct content length of data. If we have a CL in
                    380:     ** the headers then this stream is responsible for the accountance.
2.32      frystyk   381:     */
2.82      frystyk   382:     if (me->hasBody) {
2.73      frystyk   383:        HTNet * net = me->net;
2.66      frystyk   384:        /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
2.73      frystyk   385:        long cl = HTResponse_length(me->response);
2.82      frystyk   386:        if (cl >= 0) {
2.89      frystyk   387:            long bodyRead = HTNet_bytesRead(net) - HTNet_headerBytesRead(net);
2.73      frystyk   388: 
                    389:            /*
                    390:            **  If we have more than we need then just take what belongs to us.
                    391:            */
                    392:            if (bodyRead + l >= cl) {
                    393:                int consume = cl - bodyRead;
                    394:                if ((status = (*me->target->isa->put_block)(me->target, b, consume)) < 0)
                    395:                    return status;          
2.88      frystyk   396:                 else {
                    397:                     HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    398:                     HTNet_addBytesRead(net, consume);
                    399:                     HTHost_setConsumed(HTNet_host(net), consume);
                    400:                     if (cbf) (*cbf)(me->request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
                    401:                     return HT_LOADED;
                    402:                 }
                    403:             } else {
2.73      frystyk   404:                if ((status = (*me->target->isa->put_block)(me->target, b, l)) < 0)
2.78      frystyk   405:                    return status;
2.73      frystyk   406:                HTNet_addBytesRead(net, l);
2.78      frystyk   407:                HTHost_setConsumed(HTNet_host(net), l);
2.73      frystyk   408:                return status;
                    409:            }
2.78      frystyk   410:        }
2.73      frystyk   411:        return (*me->target->isa->put_block)(me->target, b, l);
2.88      frystyk   412:     } else {
                    413:         HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    414:         if (cbf) (*cbf)(me->request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
2.66      frystyk   415:     }
                    416:     return HT_LOADED;
2.18      frystyk   417: }
                    418: 
                    419: 
                    420: /*     Character handling
                    421: **     ------------------
                    422: */
2.36      frystyk   423: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18      frystyk   424: {
                    425:     return HTMIME_put_block(me, &c, 1);
                    426: }
                    427: 
2.1       timbl     428: 
                    429: /*     String handling
                    430: **     ---------------
                    431: */
2.57      frystyk   432: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1       timbl     433: {
2.18      frystyk   434:     return HTMIME_put_block(me, s, (int) strlen(s));
2.1       timbl     435: }
                    436: 
                    437: 
2.18      frystyk   438: /*     Flush an stream object
                    439: **     ---------------------
2.1       timbl     440: */
2.36      frystyk   441: PRIVATE int HTMIME_flush (HTStream * me)
2.1       timbl     442: {
2.47      frystyk   443:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1       timbl     444: }
                    445: 
2.18      frystyk   446: /*     Free a stream object
                    447: **     --------------------
2.1       timbl     448: */
2.36      frystyk   449: PRIVATE int HTMIME_free (HTStream * me)
2.1       timbl     450: {
2.18      frystyk   451:     int status = HT_OK;
2.64      eric      452:     if (!me->transparent)
2.65      eric      453:         if (_dispatchParsers(me) == HT_OK)
2.64      eric      454:            pumpData(me);
2.25      frystyk   455:     if (me->target) {
                    456:        if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
                    457:            return HT_WOULD_BLOCK;
                    458:     }
2.26      frystyk   459:     if (PROT_TRACE)
2.55      eric      460:        HTTrace("MIME........ FREEING....\n");
2.64      eric      461:     HTChunk_delete(me->token);
                    462:     HTChunk_delete(me->value);
2.52      frystyk   463:     HT_FREE(me);
2.18      frystyk   464:     return status;
2.1       timbl     465: }
                    466: 
                    467: /*     End writing
                    468: */
2.38      frystyk   469: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1       timbl     470: {
2.18      frystyk   471:     int status = HT_ERROR;
2.41      frystyk   472:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26      frystyk   473:     if (PROT_TRACE)
2.55      eric      474:        HTTrace("MIME........ ABORTING...\n");
2.64      eric      475:     HTChunk_delete(me->token);
                    476:     HTChunk_delete(me->value);
2.52      frystyk   477:     HT_FREE(me);
2.18      frystyk   478:     return status;
2.1       timbl     479: }
                    480: 
                    481: 
                    482: 
                    483: /*     Structured Object Class
                    484: **     -----------------------
                    485: */
2.57      frystyk   486: PRIVATE const HTStreamClass HTMIME =
2.1       timbl     487: {              
                    488:        "MIMEParser",
2.18      frystyk   489:        HTMIME_flush,
2.1       timbl     490:        HTMIME_free,
2.6       timbl     491:        HTMIME_abort,
                    492:        HTMIME_put_character,
                    493:        HTMIME_put_string,
2.18      frystyk   494:        HTMIME_put_block
2.1       timbl     495: }; 
                    496: 
                    497: 
2.48      frystyk   498: /*     MIME header parser stream.
2.1       timbl     499: **     -------------------------
2.48      frystyk   500: **     This stream parses a complete MIME header and if a content type header
                    501: **     is found then the stream stack is called. Any left over data is pumped
                    502: **     right through the stream
2.1       timbl     503: */
2.36      frystyk   504: PUBLIC HTStream* HTMIMEConvert (HTRequest *    request,
                    505:                                void *          param,
                    506:                                HTFormat        input_format,
                    507:                                HTFormat        output_format,
                    508:                                HTStream *      output_stream)
2.1       timbl     509: {
2.62      frystyk   510:     HTStream * me;
2.52      frystyk   511:     if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
                    512:         HT_OUTOFMEM("HTMIMEConvert");
2.1       timbl     513:     me->isa = &HTMIME;       
2.18      frystyk   514:     me->request = request;
2.71      frystyk   515:     me->response = HTRequest_response(request);
2.70      frystyk   516:     me->net = HTRequest_net(request);
2.49      frystyk   517:     me->target = output_stream;
2.18      frystyk   518:     me->target_format = output_format;
2.64      eric      519:     me->token = HTChunk_new(256);
                    520:     me->value = HTChunk_new(256);
                    521:     me->hash = 0;
2.18      frystyk   522:     me->EOLstate = EOL_BEGIN;
2.64      eric      523:     me->haveToken = NO;
2.1       timbl     524:     return me;
                    525: }
2.32      frystyk   526: 
2.48      frystyk   527: /*     MIME header ONLY parser stream
                    528: **     ------------------------------
                    529: **     This stream parses a complete MIME header and then returnes HT_PAUSE.
                    530: **     It does not set up any streams and resting data stays in the buffer.
                    531: **     This can be used if you only want to parse the headers before you
                    532: **     decide what to do next. This is for example the case in a server app.
                    533: */
                    534: PUBLIC HTStream * HTMIMEHeader (HTRequest *    request,
                    535:                                void *          param,
                    536:                                HTFormat        input_format,
                    537:                                HTFormat        output_format,
                    538:                                HTStream *      output_stream)
                    539: {
2.62      frystyk   540:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    541:                                  output_format, output_stream);
2.70      frystyk   542:     me->mode |= HT_MIME_HEADER;
2.48      frystyk   543:     return me;
                    544: }
2.77      frystyk   545: 
                    546: PUBLIC HTStream * HTMIMEContinue (HTRequest *  request,
                    547:                                  void *        param,
                    548:                                  HTFormat      input_format,
                    549:                                  HTFormat      output_format,
                    550:                                  HTStream *    output_stream)
                    551: {
                    552:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    553:                                  output_format, output_stream);
                    554:     me->mode |= HT_MIME_CONT;
                    555:     return me;
                    556: }
2.62      frystyk   557: 
                    558: /*     MIME footer ONLY parser stream
                    559: **     ------------------------------
                    560: **     Parse only a footer, for example after a chunked encoding.
                    561: */
                    562: PUBLIC HTStream * HTMIMEFooter (HTRequest *    request,
                    563:                                void *          param,
                    564:                                HTFormat        input_format,
                    565:                                HTFormat        output_format,
                    566:                                HTStream *      output_stream)
                    567: {
                    568:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    569:                                  output_format, output_stream);
2.70      frystyk   570:     me->mode |= HT_MIME_FOOTER;
2.62      frystyk   571:     return me;
                    572: }
2.71      frystyk   573: 
2.72      frystyk   574: /*
                    575: **     A small BEFORE filter that just finds a cache entry unconditionally
                    576: **     and loads the entry. All freshness and any other constraints are 
                    577: **     ignored.
                    578: */
                    579: PRIVATE int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
                    580: {
                    581:     HTParentAnchor * anchor = HTRequest_anchor(request);
                    582:     HTCache * cache = HTCache_find(anchor);
                    583:     if (STREAM_TRACE) HTTrace("Cache Load.. loading partial cache entry\n");
                    584:     if (cache) {
                    585:        char * name = HTCache_name(cache);
                    586:        HTAnchor_setPhysical(anchor, name);
                    587:        HTCache_addHit(cache);
                    588:        HT_FREE(name);
                    589:     }
                    590:     return HT_OK;
                    591: }
                    592: 
                    593: /*
                    594: **     A small AFTER filter that flushes the PIPE buffer so that we can
                    595: **     get the rest of the data
                    596: */
                    597: PRIVATE int HTCacheFlushFilter (HTRequest * request, HTResponse * response,
                    598:                                void * param, int mode)
                    599: {
                    600:     HTStream * pipe = (HTStream *) param;    
                    601:     if (pipe) {
                    602:        if (STREAM_TRACE) HTTrace("Cache Flush. Flushing and freeing PIPE buffer\n");
                    603:        (*pipe->isa->flush)(pipe);
                    604:        (*pipe->isa->_free)(pipe);
                    605:     }
                    606: 
                    607:     /*
                    608:     **  We also delete the request obejct and stop more filters from being called.
                    609:     **  As this is our own request, it's OK to do that
                    610:     */
                    611:     HTRequest_delete(request);
                    612:     return HT_ERROR;
                    613: }
                    614: 
2.71      frystyk   615: /*     Partial Response MIME parser stream
                    616: **     -----------------------------------
                    617: **     In case we sent a Range conditional GET we may get back a partial
                    618: **     response. This response must be appended to the already existing
                    619: **     cache entry before presented to the user.
                    620: **     We do this by continuing to load the new object into a temporary 
                    621: **     buffer and at the same time start the cache load of the already
                    622: **     existing object. When we have loaded the cache we merge the two
                    623: **     buffers.
                    624: */
                    625: PUBLIC HTStream * HTMIMEPartial (HTRequest *   request,
                    626:                                 void *         param,
                    627:                                 HTFormat       input_format,
                    628:                                 HTFormat       output_format,
                    629:                                 HTStream *     output_stream)
                    630: {
                    631:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.72      frystyk   632:     HTFormat format = HTAnchor_format(anchor);
                    633:     HTStream * pipe = NULL;
                    634: 
2.71      frystyk   635:     /*
                    636:     **  The merge stream is a place holder for where we can put data when it
                    637:     **  arrives. We have two feeds: one from the cache and one from the net.
                    638:     **  We call the stream stack already now to get the right output stream.
                    639:     **  We can do this as we already know the content type from when we got the
                    640:     **  first part of the object.
                    641:     */
2.72      frystyk   642:     HTStream * merge = HTMerge(HTStreamStack(format,
                    643:                                             output_format, output_stream,
                    644:                                             request, YES), 2);
2.71      frystyk   645: 
                    646:     /*
2.72      frystyk   647:     **  Now we create the MIME parser stream in partial data mode. We also
                    648:     **  set the target to our merge stream.
2.71      frystyk   649:     */
                    650:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    651:                                  output_format, output_stream);
                    652:     me->mode |= HT_MIME_PARTIAL;
2.72      frystyk   653:     me->target = merge;
                    654: 
                    655:     /*
                    656:     **  Create the cache append stream, and a Tee stream
                    657:     */
                    658:     {
                    659:        HTStream * append = HTStreamStack(WWW_CACHE_APPEND, output_format,
                    660:                                          output_stream, request, NO);
                    661:        if (append) me->target = HTTee(me->target, append, NULL);
                    662:     }
                    663: 
                    664:     /*
                    665:     **  Create the pipe buffer stream to buffer the data that we read
                    666:     **  from the network
                    667:     */
2.74      frystyk   668:     if ((pipe = HTPipeBuffer(me->target, 0))) me->target = pipe;
2.71      frystyk   669: 
                    670:     /*
                    671:     **  Now start the second load from the cache. First we read this data from
                    672:     **  the cache and then we flush the data that we have read from the net.
                    673:     */
                    674:     {
2.72      frystyk   675:        HTRequest * cache_request = HTRequest_new();
2.71      frystyk   676: 
2.72      frystyk   677:        /*
                    678:        **  Set the output format to source and the output stream to the
                    679:        **  merge stream. As we have already set up the stream pipe, we just 
                    680:        **  load it as source.
                    681:        */
                    682:        HTRequest_setOutputFormat(cache_request, WWW_SOURCE);
                    683:        HTRequest_setOutputStream(cache_request, merge);
                    684: 
                    685:        /*
                    686:        **  Bind the anchor to the new request and also register a local
                    687:        **  AFTER filter to flush the pipe buffer so that we can get
                    688:        **  rest of the data through. 
                    689:        */
                    690:        HTRequest_setAnchor(cache_request, (HTAnchor *) anchor);
                    691:        HTRequest_addBefore(cache_request, HTCacheLoadFilter, NULL, NULL,
                    692:                            HT_FILTER_FIRST, YES);
                    693:        HTRequest_addAfter(cache_request, HTCacheFlushFilter, NULL, pipe,
                    694:                           HT_ALL, HT_FILTER_FIRST, YES);
2.71      frystyk   695: 
2.72      frystyk   696:        if (STREAM_TRACE) HTTrace("Partial..... Starting cache load\n");
                    697:        HTLoad(cache_request, NO);
2.71      frystyk   698:     }
                    699:     return me;
                    700: }
                    701: 

Webmaster