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

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

Webmaster