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

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.100.2.1! kahan       6: **     @(#) $Id: HTMIME.c,v 2.101 2000/12/18 17:00:56 kahan 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"
                     25: #include "HTReqMan.h"
                     26: #include "HTNetMan.h"
2.36      frystyk    27: #include "HTHeader.h"
2.64      eric       28: #include "HTWWWStr.h"
2.93      frystyk    29: 
                     30: #ifndef NO_CACHE
                     31: #include "HTTee.h"
                     32: #include "HTConLen.h"
                     33: #include "HTMerge.h"
                     34: #include "WWWCache.h"
                     35: #endif
                     36: 
2.14      frystyk    37: #include "HTMIME.h"                                     /* Implemented here */
2.1       timbl      38: 
2.70      frystyk    39: typedef enum _HTMIMEMode {
                     40:     HT_MIME_HEADER     = 0x1,
2.71      frystyk    41:     HT_MIME_FOOTER     = 0x2,
2.77      frystyk    42:     HT_MIME_PARTIAL    = 0x4,
2.95      frystyk    43:     HT_MIME_CONT       = 0x8,
                     44:     HT_MIME_UPGRADE    = 0x10
2.70      frystyk    45: } HTMIMEMode;
                     46: 
2.1       timbl      47: struct _HTStream {
2.57      frystyk    48:     const HTStreamClass *      isa;
2.18      frystyk    49:     HTRequest *                        request;
2.71      frystyk    50:     HTResponse *               response;
2.32      frystyk    51:     HTNet *                    net;
2.18      frystyk    52:     HTStream *                 target;
2.93      frystyk    53:     HTConverter *              save_stream;
2.18      frystyk    54:     HTFormat                   target_format;
2.64      eric       55:     HTChunk *                  token;
                     56:     HTChunk *                  value;
                     57:     int                                hash;
2.59      frystyk    58:     HTEOLState                 EOLstate;
2.70      frystyk    59:     HTMIMEMode                 mode;
2.18      frystyk    60:     BOOL                       transparent;
2.64      eric       61:     BOOL                       haveToken;
2.78      frystyk    62:     BOOL                       hasBody;
2.1       timbl      63: };
                     64: 
2.93      frystyk    65: PRIVATE HTConverter * LocalSaveStream = NULL; /* Where to save unknown stuff */
                     66: 
2.18      frystyk    67: /* ------------------------------------------------------------------------- */
2.1       timbl      68: 
2.64      eric       69: PRIVATE int pumpData (HTStream * me)
2.18      frystyk    70: {
2.64      eric       71:     HTRequest * request = me->request;
2.71      frystyk    72:     HTResponse * response = me->response;
                     73:     HTFormat format = HTResponse_format(response);
2.83      frystyk    74:     HTList * te = HTResponse_transfer(response);
                     75:     HTList * ce = HTResponse_encoding(response);
2.71      frystyk    76:     long length = HTResponse_length(response);
2.83      frystyk    77:     HTStream * BlackHole = HTBlackHole();
                     78:     BOOL savestream = NO;
2.48      frystyk    79:     me->transparent = YES;               /* Pump rest of data right through */
2.47      frystyk    80: 
2.71      frystyk    81:     /*
2.77      frystyk    82:     **  Cache the metainformation in the anchor object by copying
2.71      frystyk    83:     **  it from the response object. This we do regardless if
                     84:     **  we have a persistent cache or not as the memory cache will
                     85:     **  use it as well. If we are updating a cache entry using
2.77      frystyk    86:     **  byte ranges then we already have the metainformation and
2.71      frystyk    87:     **  hence we can ignore the new one as it'd better be the same.
                     88:     */
2.90      frystyk    89:     if (!(me->mode & HT_MIME_PARTIAL) &&
                     90:        HTResponse_isCachable(me->response) != HT_NO_CACHE)
2.71      frystyk    91:        HTAnchor_update(HTRequest_anchor(request), me->response);
                     92: 
                     93:     /*
                     94:     **  If we asked only to read the header or footer or we used a HEAD
                     95:     **  method then we stop here as we don't expect any body part.
                     96:     */
2.70      frystyk    97:     if (me->mode & (HT_MIME_HEADER | HT_MIME_FOOTER) ||
2.71      frystyk    98:        HTRequest_method(request) == METHOD_HEAD) {
2.88      frystyk    99:         HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    100:         if (cbf) (*cbf)(request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
2.78      frystyk   101:         return HT_LOADED;
2.70      frystyk   102:     }
2.43      frystyk   103: 
2.60      frystyk   104:     /*
2.77      frystyk   105:     **  If we are paring a 1xx response then return HT_CONTINUE
                    106:     */
                    107:     if (me->mode & HT_MIME_CONT)
                    108:        return HT_CONTINUE;
                    109: 
                    110:     /*
2.95      frystyk   111:     **  If we get a 101 Protocol Switch then we are done here
                    112:     **  but not done with the response (which we don't know
                    113:     **  how to go about parsing
                    114:     */
                    115:     if (me->mode & HT_MIME_UPGRADE) {
                    116:        me->hasBody = YES;
                    117:        return HT_OK;
                    118:     }
                    119: 
                    120:     /*
2.71      frystyk   121:     **  If there is no content-length, no transfer encoding and no
                    122:     **  content type then we assume that there is no body part in
                    123:     **  the message and we can return HT_LOADED
2.68      frystyk   124:     */
2.79      frystyk   125:     {
                    126:        HTHost * host = HTNet_host(me->net);
2.84      frystyk   127:        if (length<0 && te==NULL &&
2.79      frystyk   128:            HTHost_isPersistent(host) && !HTHost_closeNotification(host)) {
2.81      frystyk   129:            if (format != WWW_UNKNOWN) {
2.94      frystyk   130:                HTTRACE(STREAM_TRACE, "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");
2.81      frystyk   131:                HTHost_setCloseNotification(host, YES);
                    132:            } else {
2.88      frystyk   133:                 HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    134:                 if (cbf) (*cbf)(request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
2.97      frystyk   135:                HTTRACE(STREAM_TRACE, "MIME Parser. No body in this message\n");
2.81      frystyk   136:                return HT_LOADED;
                    137:            }
2.79      frystyk   138:        }
2.68      frystyk   139:     }
                    140: 
                    141:     /*
2.78      frystyk   142:     **  Deal with the body
                    143:     */
                    144:     me->hasBody = YES;
                    145: 
                    146:     /*
2.71      frystyk   147:     **  Handle any Content Type
2.60      frystyk   148:     */
2.71      frystyk   149:     if (!(me->mode & HT_MIME_PARTIAL) &&
2.83      frystyk   150:        (format != WWW_UNKNOWN || length > 0 || te)) {
                    151:        HTStream * target;
2.94      frystyk   152:        HTTRACE(STREAM_TRACE, "Building.... C-T stack from %s to %s\n" _ 
                    153:                                  HTAtom_name(format) _ 
2.71      frystyk   154:                                  HTAtom_name(me->target_format));
2.83      frystyk   155:        if ((target = HTStreamStack(format, me->target_format,
                    156:                                    me->target, request, YES))==BlackHole) {
                    157:            if (!savestream) {
2.90      frystyk   158:                 if (me->target) (*me->target->isa->abort)(me->target, NULL);
2.93      frystyk   159:                 me->target = me->save_stream(request, NULL,
                    160:                                             format, me->target_format, me->target);
2.90      frystyk   161:                savestream = YES;
                    162:            }
                    163:        } else
                    164:            me->target = target;
                    165:     }
                    166: 
                    167:     /*
                    168:     **  Handle any Content Encodings
                    169:     */
2.94      frystyk   170:     HTTRACE(STREAM_TRACE, "Building.... Content-Decoding stack\n");
2.90      frystyk   171:     if (ce) {
                    172:        HTStream * target = HTContentDecodingStack(ce, me->target, request, NULL);
                    173:        if (target == BlackHole) {
                    174:            if (!savestream) {
2.83      frystyk   175:                if (me->target) (*me->target->isa->abort)(me->target, NULL);
2.93      frystyk   176:                 me->target = me->save_stream(request, NULL,
                    177:                                             format, me->target_format, me->target);
2.83      frystyk   178:                savestream = YES;
                    179:            }
                    180:        } else
                    181:            me->target = target;
2.18      frystyk   182:     }
2.60      frystyk   183: 
2.71      frystyk   184:     /*
                    185:     **  Can we cache the data object? If so then create a T stream and hook it 
                    186:     **  into the stream pipe. We do it before the transfer decoding so that we
                    187:     **  don't have to deal with that when we retrieve the object from cache.
                    188:     **  If we are appending to a cache entry then use a different stream than
                    189:     **  if creating a new entry.
                    190:     */
2.93      frystyk   191: #ifndef NO_CACHE
2.90      frystyk   192:     if (HTCacheMode_enabled()) {
2.71      frystyk   193:        if (me->mode & HT_MIME_PARTIAL) {
                    194:            HTStream * append = HTStreamStack(WWW_CACHE_APPEND,
                    195:                                              me->target_format,
                    196:                                              me->target, request, NO);
2.98      kahan     197:            if (append) me->target = HTTee(me->target, append, NULL);
                    198: #if 0
                    199:            /* @@ JK: change */
                    200:            if (append) me->target = append;
                    201: #endif
2.90      frystyk   202:        } else if (HTResponse_isCachable(me->response) == HT_CACHE_ALL) {
2.71      frystyk   203:            HTStream * cache = HTStreamStack(WWW_CACHE, me->target_format,
                    204:                                             me->target, request, NO);
                    205:            if (cache) me->target = HTTee(me->target, cache, NULL);
                    206:        }
2.70      frystyk   207:     }
2.93      frystyk   208: #endif
2.90      frystyk   209:     
2.71      frystyk   210:     /*
2.83      frystyk   211:     **  Handle any Transfer Encodings
2.71      frystyk   212:     */
2.94      frystyk   213:     HTTRACE(STREAM_TRACE, "Building.... Transfer-Decoding stack\n");
2.83      frystyk   214:     if (te) {
                    215:        HTStream * target = HTTransferDecodingStack(te, me->target, request, NULL);
                    216:        if (target == BlackHole) {
                    217:            if (!savestream) {
                    218:                if (me->target) (*me->target->isa->abort)(me->target, NULL);
2.93      frystyk   219:                 me->target = me->save_stream(request, NULL,
                    220:                                             format, me->target_format, me->target);
2.83      frystyk   221:                savestream = YES;
                    222:            }
                    223:        } else
                    224:            me->target = target;
2.61      frystyk   225:     }
2.71      frystyk   226: 
2.85      frystyk   227: 
                    228:     /*
                    229:     ** If we for some reason couldn't find a target stream
                    230:     */
                    231:     if (!me->target) me->target = HTBlackHole();
2.27      frystyk   232:     return HT_OK;
2.1       timbl     233: }
                    234: 
2.100     kahan     235: /* _dispatchParsers
                    236:  * call request's MIME header parser. Use global parser if no 
                    237:  * appropriate one is found for request.
2.65      eric      238: */
2.100     kahan     239: PRIVATE int _dispatchParsers (HTRequest * req, char * token, char * value)
2.65      eric      240: {
                    241:     int status;
2.71      frystyk   242:     BOOL found = NO;
                    243:     BOOL local = NO;
2.65      eric      244:     HTMIMEParseSet * parseSet;
                    245: 
                    246:     /* In case we get an empty header consisting of a CRLF, we fall thru */
2.94      frystyk   247:     HTTRACE(STREAM_TRACE, "MIME header. %s: %s\n" _ 
                    248:                              token ? token : "<null>" _ 
2.66      frystyk   249:                              value ? value : "<null>");
                    250:     if (!token) return HT_OK;                      /* Ignore noop token */
2.65      eric      251: 
2.70      frystyk   252:     /*
                    253:     ** Search the local set of MIME parsers
                    254:     */
2.100     kahan     255:     if ((parseSet = HTRequest_MIMEParseSet(req, &local)) != NULL) {
                    256:         status = HTMIMEParseSet_dispatch(parseSet, req, 
2.71      frystyk   257:                                         token, value, &found);
                    258:        if (found) return status;
2.65      eric      259:     }
                    260: 
2.70      frystyk   261:     /*
                    262:     ** Search the global set of MIME parsers
                    263:     */
2.71      frystyk   264:     if (local==NO && (parseSet = HTHeader_MIMEParseSet()) != NULL) {
2.100     kahan     265:        status = HTMIMEParseSet_dispatch(parseSet, req, 
2.71      frystyk   266:                                         token, value, &found);
                    267:        if (found) return status;
                    268:     }
                    269: 
2.65      eric      270:     return HT_OK;
                    271: }
                    272: 
2.100     kahan     273: /* _stream2dispatchParsers - extracts the arguments from a
                    274:  * MIME stream before calling the generic _dispatchParser
                    275:  * function.
                    276:  */
                    277: PRIVATE int _stream2dispatchParsers (HTStream * me)
                    278: {
                    279:     char * token = HTChunk_data(me->token);
                    280:     char * value = HTChunk_data(me->value);
                    281: 
                    282:     /* In case we get an empty header consisting of a CRLF, we fall thru */
                    283:     HTTRACE(STREAM_TRACE, "MIME header. %s: %s\n" _ 
                    284:                              token ? token : "<null>" _ 
                    285:                              value ? value : "<null>");
                    286:     if (!token) return HT_OK;                      /* Ignore noop token */
                    287: 
                    288:     /*
                    289:     ** Remember the original header
                    290:     */
                    291:     HTResponse_addHeader(me->response, token, value);
                    292: 
                    293:     /* call the parsers to set the headers */
                    294:     return (_dispatchParsers (me->request, token, value));
                    295: }
                    296: 
2.18      frystyk   297: /*
                    298: **     Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
                    299: **     Folding is either of CF LWS, LF LWS, CRLF LWS
                    300: */
2.57      frystyk   301: PRIVATE int HTMIME_put_block (HTStream * me, const char * b, int l)
2.18      frystyk   302: {
2.57      frystyk   303:     const char * start = b;
                    304:     const char * end = start;
2.96      frystyk   305:     const char * value = HTChunk_size(me->value) > 0 ? b : NULL;
2.73      frystyk   306:     int length = l;
2.64      eric      307:     int status;
                    308: 
                    309:     while (!me->transparent) {
2.18      frystyk   310:        if (me->EOLstate == EOL_FCR) {
2.64      eric      311:            if (*b == CR)                                   /* End of header */
                    312:                me->EOLstate = EOL_END;
                    313:            else if (*b == LF)                               /* CRLF */
2.18      frystyk   314:                me->EOLstate = EOL_FLF;
2.80      frystyk   315:            else if (isspace((int) *b))                    /* Folding: CR SP */
2.64      eric      316:                me->EOLstate = EOL_FOLD;
                    317:            else                                                 /* New line */
                    318:                me->EOLstate = EOL_LINE;
2.18      frystyk   319:        } else if (me->EOLstate == EOL_FLF) {
                    320:            if (*b == CR)                               /* LF CR or CR LF CR */
                    321:                me->EOLstate = EOL_SCR;
2.64      eric      322:            else if (*b == LF)                              /* End of header */
                    323:                me->EOLstate = EOL_END;
2.80      frystyk   324:            else if (isspace((int) *b))        /* Folding: LF SP or CR LF SP */
2.64      eric      325:                me->EOLstate = EOL_FOLD;
                    326:            else                                                /* New line */
                    327:                me->EOLstate = EOL_LINE;
                    328:        } else if (me->EOLstate == EOL_SCR) {
                    329:            if (*b==CR || *b==LF)                           /* End of header */
                    330:                me->EOLstate = EOL_END;
2.80      frystyk   331:            else if (isspace((int) *b))  /* Folding: LF CR SP or CR LF CR SP */
2.64      eric      332:                me->EOLstate = EOL_FOLD;
                    333:            else                                                /* New line */
                    334:                me->EOLstate = EOL_LINE;
                    335:        } else if (*b == CR)
                    336:            me->EOLstate = EOL_FCR;
                    337:        else if (*b == LF)
                    338:            me->EOLstate = EOL_FLF;                            /* Line found */
                    339:        else {
                    340:            if (!me->haveToken) {
2.80      frystyk   341:                if (*b == ':' || isspace((int) *b)) {
2.64      eric      342:                    HTChunk_putb(me->token, start, end-start);
                    343:                    HTChunk_putc(me->token, '\0');
                    344:                    me->haveToken = YES;
                    345:                } else {
                    346:                    unsigned char ch = *(unsigned char *) b;
2.92      frystyk   347:                    ch = TOLOWER(ch);
2.64      eric      348:                    me->hash = (me->hash * 3 + ch) % MIME_HASH_SIZE;
                    349:                }
2.80      frystyk   350:            } else if (value == NULL && *b != ':' && !isspace((int) *b))
2.64      eric      351:                value = b;
                    352:            end++;
                    353:        }
                    354:        switch (me->EOLstate) {
                    355:            case EOL_LINE:
2.73      frystyk   356:            case EOL_END:
                    357:            {
2.78      frystyk   358:                int ret = HT_ERROR;
2.64      eric      359:                HTChunk_putb(me->value, value, end-value);
                    360:                HTChunk_putc(me->value, '\0');
2.100     kahan     361:                ret =  _stream2dispatchParsers(me);
2.73      frystyk   362:                HTNet_addBytesRead(me->net, b-start);
2.64      eric      363:                start=b, end=b;
                    364:                if (me->EOLstate == EOL_END) {          /* EOL_END */
2.75      frystyk   365:                    if (ret == HT_OK) {
2.67      frystyk   366:                        b++, l--;
2.78      frystyk   367:                        ret = pumpData(me);
2.73      frystyk   368:                        HTNet_addBytesRead(me->net, 1);
2.82      frystyk   369:                        if (me->mode & (HT_MIME_FOOTER | HT_MIME_CONT)) {
2.73      frystyk   370:                            HTHost_setConsumed(HTNet_host(me->net), length - l);
2.75      frystyk   371:                            return ret;
2.78      frystyk   372:                         } else {
2.89      frystyk   373:                             HTNet_setHeaderBytesRead(me->net, HTNet_bytesRead(me->net));
2.78      frystyk   374:                         }
2.67      frystyk   375:                    }
2.64      eric      376:                } else {                                /* EOL_LINE */
2.99      kahan     377:                    HTChunk_truncate(me->token,0);
                    378:                    HTChunk_truncate(me->value,0);
2.64      eric      379:                    me->haveToken = NO;
                    380:                    me->hash = 0;
                    381:                    value = NULL;
                    382:                }
2.18      frystyk   383:                me->EOLstate = EOL_BEGIN;
2.78      frystyk   384:                if (ret != HT_OK && ret != HT_LOADED) return ret;
2.64      eric      385:                break;
2.73      frystyk   386:            }
2.64      eric      387:            case EOL_FOLD:
2.18      frystyk   388:                me->EOLstate = EOL_BEGIN;
2.64      eric      389:                if (!me->haveToken) {
                    390:                    HTChunk_putb(me->token, start, end-start);
                    391:                    HTChunk_putc(me->token, '\0');
                    392:                    me->haveToken = YES;
                    393:                } else if (value) {
                    394:                    HTChunk_putb(me->value, value, end-value);
                    395:                    HTChunk_putc(me->value, ' ');
                    396:                }
                    397:                start=b, end=b;
                    398:                break;
                    399:            default: 
2.73      frystyk   400:                b++, l--;
2.64      eric      401:                if (!l) {
2.87      frystyk   402:                    BOOL stop = NO;
                    403:                    if (!me->haveToken) {
                    404:                        /* If empty header then prepare to stop */
                    405:                        if (end-start)
                    406:                            HTChunk_putb(me->token, start, end-start);
                    407:                        else
                    408:                            stop = YES;
                    409:                    } else if (value)
2.64      eric      410:                        HTChunk_putb(me->value, value, end-value);
2.78      frystyk   411:                    HTHost_setConsumed(HTNet_host(me->net), length - l);
2.87      frystyk   412:                    return stop ? pumpData(me) : HT_OK;
2.64      eric      413:                }
                    414:        }
2.18      frystyk   415:     }
2.32      frystyk   416: 
2.78      frystyk   417:     if (length != l) HTHost_setConsumed(HTNet_host(me->net), length - l);
                    418: 
2.32      frystyk   419:     /* 
                    420:     ** Put the rest down the stream without touching the data but make sure
2.73      frystyk   421:     ** that we get the correct content length of data. If we have a CL in
                    422:     ** the headers then this stream is responsible for the accountance.
2.32      frystyk   423:     */
2.82      frystyk   424:     if (me->hasBody) {
2.73      frystyk   425:        HTNet * net = me->net;
2.66      frystyk   426:        /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
2.73      frystyk   427:        long cl = HTResponse_length(me->response);
2.82      frystyk   428:        if (cl >= 0) {
2.89      frystyk   429:            long bodyRead = HTNet_bytesRead(net) - HTNet_headerBytesRead(net);
2.73      frystyk   430: 
                    431:            /*
                    432:            **  If we have more than we need then just take what belongs to us.
                    433:            */
                    434:            if (bodyRead + l >= cl) {
                    435:                int consume = cl - bodyRead;
                    436:                if ((status = (*me->target->isa->put_block)(me->target, b, consume)) < 0)
                    437:                    return status;          
2.88      frystyk   438:                 else {
                    439:                     HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    440:                     HTNet_addBytesRead(net, consume);
                    441:                     HTHost_setConsumed(HTNet_host(net), consume);
                    442:                     if (cbf) (*cbf)(me->request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
                    443:                     return HT_LOADED;
                    444:                 }
                    445:             } else {
2.73      frystyk   446:                if ((status = (*me->target->isa->put_block)(me->target, b, l)) < 0)
2.78      frystyk   447:                    return status;
2.73      frystyk   448:                HTNet_addBytesRead(net, l);
2.78      frystyk   449:                HTHost_setConsumed(HTNet_host(net), l);
2.73      frystyk   450:                return status;
                    451:            }
2.78      frystyk   452:        }
2.73      frystyk   453:        return (*me->target->isa->put_block)(me->target, b, l);
2.88      frystyk   454:     } else {
                    455:         HTAlertCallback * cbf = HTAlert_find(HT_PROG_DONE);
                    456:         if (cbf) (*cbf)(me->request, HT_PROG_DONE, HT_MSG_NULL, NULL, NULL, NULL);
2.66      frystyk   457:     }
                    458:     return HT_LOADED;
2.18      frystyk   459: }
                    460: 
                    461: 
                    462: /*     Character handling
                    463: **     ------------------
                    464: */
2.36      frystyk   465: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18      frystyk   466: {
                    467:     return HTMIME_put_block(me, &c, 1);
                    468: }
                    469: 
2.1       timbl     470: 
                    471: /*     String handling
                    472: **     ---------------
                    473: */
2.57      frystyk   474: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1       timbl     475: {
2.18      frystyk   476:     return HTMIME_put_block(me, s, (int) strlen(s));
2.1       timbl     477: }
                    478: 
                    479: 
2.18      frystyk   480: /*     Flush an stream object
                    481: **     ---------------------
2.1       timbl     482: */
2.36      frystyk   483: PRIVATE int HTMIME_flush (HTStream * me)
2.1       timbl     484: {
2.47      frystyk   485:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1       timbl     486: }
                    487: 
2.18      frystyk   488: /*     Free a stream object
                    489: **     --------------------
2.1       timbl     490: */
2.36      frystyk   491: PRIVATE int HTMIME_free (HTStream * me)
2.1       timbl     492: {
2.18      frystyk   493:     int status = HT_OK;
2.64      eric      494:     if (!me->transparent)
2.100     kahan     495:         if (_stream2dispatchParsers(me) == HT_OK)
2.64      eric      496:            pumpData(me);
2.25      frystyk   497:     if (me->target) {
                    498:        if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
                    499:            return HT_WOULD_BLOCK;
                    500:     }
2.94      frystyk   501:     HTTRACE(PROT_TRACE, "MIME........ FREEING....\n");
2.64      eric      502:     HTChunk_delete(me->token);
                    503:     HTChunk_delete(me->value);
2.52      frystyk   504:     HT_FREE(me);
2.18      frystyk   505:     return status;
2.1       timbl     506: }
                    507: 
                    508: /*     End writing
                    509: */
2.38      frystyk   510: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1       timbl     511: {
2.18      frystyk   512:     int status = HT_ERROR;
2.41      frystyk   513:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.94      frystyk   514:     HTTRACE(PROT_TRACE, "MIME........ ABORTING...\n");
2.64      eric      515:     HTChunk_delete(me->token);
                    516:     HTChunk_delete(me->value);
2.52      frystyk   517:     HT_FREE(me);
2.18      frystyk   518:     return status;
2.1       timbl     519: }
                    520: 
                    521: 
                    522: 
                    523: /*     Structured Object Class
                    524: **     -----------------------
                    525: */
2.57      frystyk   526: PRIVATE const HTStreamClass HTMIME =
2.1       timbl     527: {              
                    528:        "MIMEParser",
2.18      frystyk   529:        HTMIME_flush,
2.1       timbl     530:        HTMIME_free,
2.6       timbl     531:        HTMIME_abort,
                    532:        HTMIME_put_character,
                    533:        HTMIME_put_string,
2.18      frystyk   534:        HTMIME_put_block
2.1       timbl     535: }; 
                    536: 
                    537: 
2.48      frystyk   538: /*     MIME header parser stream.
2.1       timbl     539: **     -------------------------
2.48      frystyk   540: **     This stream parses a complete MIME header and if a content type header
                    541: **     is found then the stream stack is called. Any left over data is pumped
                    542: **     right through the stream
2.1       timbl     543: */
2.36      frystyk   544: PUBLIC HTStream* HTMIMEConvert (HTRequest *    request,
                    545:                                void *          param,
                    546:                                HTFormat        input_format,
                    547:                                HTFormat        output_format,
                    548:                                HTStream *      output_stream)
2.1       timbl     549: {
2.62      frystyk   550:     HTStream * me;
2.52      frystyk   551:     if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
                    552:         HT_OUTOFMEM("HTMIMEConvert");
2.1       timbl     553:     me->isa = &HTMIME;       
2.18      frystyk   554:     me->request = request;
2.71      frystyk   555:     me->response = HTRequest_response(request);
2.70      frystyk   556:     me->net = HTRequest_net(request);
2.49      frystyk   557:     me->target = output_stream;
2.18      frystyk   558:     me->target_format = output_format;
2.93      frystyk   559:     me->save_stream = LocalSaveStream ? LocalSaveStream : HTBlackHoleConverter;
2.64      eric      560:     me->token = HTChunk_new(256);
                    561:     me->value = HTChunk_new(256);
                    562:     me->hash = 0;
2.18      frystyk   563:     me->EOLstate = EOL_BEGIN;
2.64      eric      564:     me->haveToken = NO;
2.1       timbl     565:     return me;
                    566: }
2.32      frystyk   567: 
2.48      frystyk   568: /*     MIME header ONLY parser stream
                    569: **     ------------------------------
                    570: **     This stream parses a complete MIME header and then returnes HT_PAUSE.
                    571: **     It does not set up any streams and resting data stays in the buffer.
                    572: **     This can be used if you only want to parse the headers before you
                    573: **     decide what to do next. This is for example the case in a server app.
                    574: */
                    575: PUBLIC HTStream * HTMIMEHeader (HTRequest *    request,
                    576:                                void *          param,
                    577:                                HTFormat        input_format,
                    578:                                HTFormat        output_format,
                    579:                                HTStream *      output_stream)
                    580: {
2.62      frystyk   581:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    582:                                  output_format, output_stream);
2.70      frystyk   583:     me->mode |= HT_MIME_HEADER;
2.48      frystyk   584:     return me;
                    585: }
2.77      frystyk   586: 
                    587: PUBLIC HTStream * HTMIMEContinue (HTRequest *  request,
                    588:                                  void *        param,
                    589:                                  HTFormat      input_format,
                    590:                                  HTFormat      output_format,
                    591:                                  HTStream *    output_stream)
                    592: {
                    593:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    594:                                  output_format, output_stream);
                    595:     me->mode |= HT_MIME_CONT;
2.95      frystyk   596:     return me;
                    597: }
                    598: 
                    599: PUBLIC HTStream * HTMIMEUpgrade  (HTRequest *  request,
                    600:                                  void *        param,
                    601:                                  HTFormat      input_format,
                    602:                                  HTFormat      output_format,
                    603:                                  HTStream *    output_stream)
                    604: {
                    605:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    606:                                  output_format, output_stream);
                    607:     me->mode |= HT_MIME_UPGRADE;
2.77      frystyk   608:     return me;
                    609: }
2.62      frystyk   610: 
                    611: /*     MIME footer ONLY parser stream
                    612: **     ------------------------------
                    613: **     Parse only a footer, for example after a chunked encoding.
                    614: */
                    615: PUBLIC HTStream * HTMIMEFooter (HTRequest *    request,
                    616:                                void *          param,
                    617:                                HTFormat        input_format,
                    618:                                HTFormat        output_format,
                    619:                                HTStream *      output_stream)
                    620: {
                    621:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    622:                                  output_format, output_stream);
2.70      frystyk   623:     me->mode |= HT_MIME_FOOTER;
2.62      frystyk   624:     return me;
                    625: }
2.71      frystyk   626: 
2.93      frystyk   627: #ifndef NO_CACHE
2.72      frystyk   628: /*
                    629: **     A small BEFORE filter that just finds a cache entry unconditionally
                    630: **     and loads the entry. All freshness and any other constraints are 
                    631: **     ignored.
                    632: */
                    633: PRIVATE int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
                    634: {
                    635:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.98      kahan     636:     char * default_name;
                    637:     HTCache * cache;
                    638: 
                    639:     default_name = HTRequest_defaultPutName (request);
                    640:     cache = HTCache_find(anchor, default_name);
                    641: 
2.94      frystyk   642:     HTTRACE(STREAM_TRACE, "Cache Load.. loading partial cache entry\n");
2.72      frystyk   643:     if (cache) {
                    644:        char * name = HTCache_name(cache);
                    645:        HTAnchor_setPhysical(anchor, name);
                    646:        HTCache_addHit(cache);
                    647:        HT_FREE(name);
                    648:     }
                    649:     return HT_OK;
                    650: }
                    651: 
                    652: /*
                    653: **     A small AFTER filter that flushes the PIPE buffer so that we can
                    654: **     get the rest of the data
                    655: */
                    656: PRIVATE int HTCacheFlushFilter (HTRequest * request, HTResponse * response,
                    657:                                void * param, int mode)
                    658: {
                    659:     HTStream * pipe = (HTStream *) param;    
                    660:     if (pipe) {
2.94      frystyk   661:        HTTRACE(STREAM_TRACE, "Cache Flush. Flushing and freeing PIPE buffer\n");
2.72      frystyk   662:        (*pipe->isa->flush)(pipe);
2.98      kahan     663: #if 0
                    664:        /* @@ JK: flush converts the pipe to an open one, we shouldn't
                    665:           free it as we'll loose our references */
2.72      frystyk   666:        (*pipe->isa->_free)(pipe);
2.98      kahan     667: #endif
2.72      frystyk   668:     }
                    669: 
                    670:     /*
                    671:     **  We also delete the request obejct and stop more filters from being called.
                    672:     **  As this is our own request, it's OK to do that
                    673:     */
                    674:     HTRequest_delete(request);
                    675:     return HT_ERROR;
                    676: }
2.93      frystyk   677: #endif
2.72      frystyk   678: 
2.71      frystyk   679: /*     Partial Response MIME parser stream
                    680: **     -----------------------------------
                    681: **     In case we sent a Range conditional GET we may get back a partial
                    682: **     response. This response must be appended to the already existing
                    683: **     cache entry before presented to the user.
                    684: **     We do this by continuing to load the new object into a temporary 
                    685: **     buffer and at the same time start the cache load of the already
                    686: **     existing object. When we have loaded the cache we merge the two
                    687: **     buffers.
                    688: */
                    689: PUBLIC HTStream * HTMIMEPartial (HTRequest *   request,
                    690:                                 void *         param,
                    691:                                 HTFormat       input_format,
                    692:                                 HTFormat       output_format,
                    693:                                 HTStream *     output_stream)
                    694: {
2.93      frystyk   695: #ifndef NO_CACHE
2.71      frystyk   696:     HTParentAnchor * anchor = HTRequest_anchor(request);
2.72      frystyk   697:     HTFormat format = HTAnchor_format(anchor);
                    698:     HTStream * pipe = NULL;
                    699: 
2.71      frystyk   700:     /*
                    701:     **  The merge stream is a place holder for where we can put data when it
                    702:     **  arrives. We have two feeds: one from the cache and one from the net.
                    703:     **  We call the stream stack already now to get the right output stream.
                    704:     **  We can do this as we already know the content type from when we got the
                    705:     **  first part of the object.
                    706:     */
2.72      frystyk   707:     HTStream * merge = HTMerge(HTStreamStack(format,
                    708:                                             output_format, output_stream,
                    709:                                             request, YES), 2);
2.71      frystyk   710: 
                    711:     /*
2.72      frystyk   712:     **  Now we create the MIME parser stream in partial data mode. We also
                    713:     **  set the target to our merge stream.
2.71      frystyk   714:     */
                    715:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    716:                                  output_format, output_stream);
                    717:     me->mode |= HT_MIME_PARTIAL;
2.72      frystyk   718:     me->target = merge;
                    719: 
2.98      kahan     720: #if 0
                    721:     /* JK: this doesn't work because this work is repeated before */
2.72      frystyk   722:     /*
                    723:     **  Create the cache append stream, and a Tee stream
                    724:     */
                    725:     {
                    726:        HTStream * append = HTStreamStack(WWW_CACHE_APPEND, output_format,
                    727:                                          output_stream, request, NO);
                    728:        if (append) me->target = HTTee(me->target, append, NULL);
                    729:     }
2.98      kahan     730: #endif
2.72      frystyk   731: 
                    732:     /*
                    733:     **  Create the pipe buffer stream to buffer the data that we read
                    734:     **  from the network
                    735:     */
2.74      frystyk   736:     if ((pipe = HTPipeBuffer(me->target, 0))) me->target = pipe;
2.71      frystyk   737: 
                    738:     /*
                    739:     **  Now start the second load from the cache. First we read this data from
                    740:     **  the cache and then we flush the data that we have read from the net.
                    741:     */
                    742:     {
2.72      frystyk   743:        HTRequest * cache_request = HTRequest_new();
2.71      frystyk   744: 
2.72      frystyk   745:        /*
                    746:        **  Set the output format to source and the output stream to the
                    747:        **  merge stream. As we have already set up the stream pipe, we just 
                    748:        **  load it as source.
                    749:        */
                    750:        HTRequest_setOutputFormat(cache_request, WWW_SOURCE);
                    751:        HTRequest_setOutputStream(cache_request, merge);
                    752: 
                    753:        /*
                    754:        **  Bind the anchor to the new request and also register a local
                    755:        **  AFTER filter to flush the pipe buffer so that we can get
                    756:        **  rest of the data through. 
                    757:        */
                    758:        HTRequest_setAnchor(cache_request, (HTAnchor *) anchor);
                    759:        HTRequest_addBefore(cache_request, HTCacheLoadFilter, NULL, NULL,
                    760:                            HT_FILTER_FIRST, YES);
                    761:        HTRequest_addAfter(cache_request, HTCacheFlushFilter, NULL, pipe,
                    762:                           HT_ALL, HT_FILTER_FIRST, YES);
2.71      frystyk   763: 
2.94      frystyk   764:        HTTRACE(STREAM_TRACE, "Partial..... Starting cache load\n");
2.72      frystyk   765:        HTLoad(cache_request, NO);
2.71      frystyk   766:     }
                    767:     return me;
2.93      frystyk   768: #else
                    769:     return NULL;
                    770: #endif
2.71      frystyk   771: }
                    772: 
2.93      frystyk   773: PUBLIC void HTMIME_setSaveStream (HTConverter * save_stream)
                    774: {
                    775:     LocalSaveStream = save_stream;
                    776: }
                    777: 
                    778: PUBLIC HTConverter * HTMIME_saveStream (void)
                    779: {
                    780:     return LocalSaveStream;
                    781: }
2.100     kahan     782: 
2.100.2.1! kahan     783: #ifndef NO_CACHE
2.100     kahan     784: /* HTMIME_anchor2response
                    785:  * Copies the anchor HTTP headers into a response object by means
                    786:  * of the generic _dispatchParsers function. Written so that we can
                    787:  * copy the HTTP headers stored in the cache to the response object.
                    788:  */
2.100.2.1! kahan     789: PRIVATE void HTMIME_anchor2response (HTRequest * req)
2.100     kahan     790: {
                    791:   char * token;
                    792:   char * value;
                    793:   HTAssocList * header;
                    794:   HTAssoc * pres;
                    795:   HTResponse * res;
                    796:   HTParentAnchor * anchor;
                    797: 
                    798:   if (!req)
                    799:     return;
                    800:   
                    801:   anchor = HTRequest_anchor (req);
                    802:   header = HTAnchor_header (anchor);
2.100.2.1! kahan     803:   if (!anchor || !header)
        !           804:     return;
        !           805: 
2.100     kahan     806:   while ((pres = (HTAssoc *) HTAssocList_nextObject (header)))
                    807:     {
                    808:       token = HTAssoc_name (pres);
                    809:       value = HTAssoc_value (pres);
                    810:       _dispatchParsers (req, token, value);
                    811:     }
                    812:   
                    813:   /*
                    814:   **  Notify the response object not to delete the lists that we
                    815:   **  have inherited from the anchor object
                    816:   */
                    817:   res = HTRequest_response (req);
                    818:   HTResponse_isCached (res, YES);  
                    819: }
                    820: 
2.100.2.1! kahan     821: /*
        !           822: **     A small AFTER filter that is a frontend to the 
        !           823: **      HTMIME_anchor2headers function.
        !           824: */
        !           825: 
        !           826: PUBLIC HTStream * HTCacheCopyHeaders   (HTRequest *    request,
        !           827:                                        void *          param,
        !           828:                                        HTFormat        input_format,
        !           829:                                        HTFormat        output_format,
        !           830:                                        HTStream *      output_stream)
        !           831: {
        !           832:     HTTRACE(STREAM_TRACE, "Cache Copy Headers.. Copying headers into the response object\n");
        !           833:     HTMIME_anchor2response (request);
        !           834:     return HT_OK;
        !           835: }
        !           836: #endif /* NO_CACHE */

Webmaster