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

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.66    ! frystyk     6: **     @(#) $Id: HTMIME.c,v 2.65 1996/06/07 04:40:01 eric 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.18      frystyk    17: **     14 Mar 95  HFN  Now using anchor for storing data. No more `\n',
                     18: **                     static buffers etc.
2.1       timbl      19: */
2.17      frystyk    20: 
                     21: /* Library include files */
2.57      frystyk    22: #include "sysdep.h"
2.60      frystyk    23: #include "WWWUtil.h"
2.61      frystyk    24: #include "WWWCore.h"
                     25: #include "HTReqMan.h"
                     26: #include "HTNetMan.h"
2.36      frystyk    27: #include "HTHeader.h"
2.64      eric       28: #include "HTWWWStr.h"
2.14      frystyk    29: #include "HTMIME.h"                                     /* Implemented here */
2.1       timbl      30: 
2.64      eric       31: #define MIME_HASH_SIZE 101
                     32: 
2.1       timbl      33: /*             MIME Object
                     34: **             -----------
                     35: */
                     36: struct _HTStream {
2.57      frystyk    37:     const HTStreamClass *      isa;
2.18      frystyk    38:     HTRequest *                        request;
2.32      frystyk    39:     HTNet *                    net;
                     40:     HTParentAnchor *           anchor;
2.18      frystyk    41:     HTStream *                 target;
                     42:     HTFormat                   target_format;
2.64      eric       43:     HTChunk *                  token;
                     44:     HTChunk *                  value;
                     45:     int                                hash;
2.59      frystyk    46:     HTEOLState                 EOLstate;
2.18      frystyk    47:     BOOL                       transparent;
2.48      frystyk    48:     BOOL                       head_only;
2.35      frystyk    49:     BOOL                       nntp;
2.62      frystyk    50:     BOOL                       footer;
2.64      eric       51:     BOOL                       haveToken;
2.1       timbl      52: };
                     53: 
2.18      frystyk    54: /* ------------------------------------------------------------------------- */
2.1       timbl      55: 
2.64      eric       56: PRIVATE int pumpData (HTStream * me)
2.18      frystyk    57: {
2.64      eric       58:     HTRequest * request = me->request;
                     59:     HTParentAnchor * anchor = me->anchor;
2.48      frystyk    60:     me->transparent = YES;               /* Pump rest of data right through */
2.27      frystyk    61: 
2.48      frystyk    62:     /* If this request us a source in PostWeb then pause here */
2.66    ! frystyk    63:     if (HTRequest_isSource(request)) return HT_PAUSE;
2.47      frystyk    64: 
2.48      frystyk    65:     /* If HEAD method then we just stop here */
2.66    ! frystyk    66:     if (me->head_only || me->footer ||
        !            67:        request->method == METHOD_HEAD) return HT_LOADED;
2.43      frystyk    68: 
2.60      frystyk    69:     /*
                     70:     ** Handle any Content Type
                     71:     ** News server almost never send content type or content length
                     72:     */
2.61      frystyk    73:     {
                     74:        HTFormat format = HTAnchor_format(anchor);
                     75:        if (format != WWW_UNKNOWN || me->nntp) {
                     76:            if (STREAM_TRACE) HTTrace("Building.... C-T stack from %s to %s\n",
                     77:                                      HTAtom_name(format),
                     78:                                      HTAtom_name(me->target_format));
                     79:            me->target = HTStreamStack(format, me->target_format,
                     80:                                       me->target, request, YES);
                     81:        }
2.18      frystyk    82:     }
2.60      frystyk    83: 
                     84:     /* Handle any Content Encoding */
2.61      frystyk    85:     {
                     86:        HTList * cc = HTAnchor_encoding(anchor);
                     87:        if (cc) {
                     88:            if (STREAM_TRACE) HTTrace("Building.... C-E stack\n");
                     89:            me->target = HTContentDecodingStack(cc, me->target, request, NULL);
                     90:        }
2.60      frystyk    91:     }
                     92: 
                     93:     /* Handle any Transfer encoding */
2.61      frystyk    94:     {
                     95:        HTEncoding transfer = HTAnchor_transfer(anchor);
                     96:        if (!HTFormat_isUnityTransfer(transfer)) {
                     97:            if (STREAM_TRACE) HTTrace("Building.... C-T-E stack\n");
                     98:            me->target = HTTransferCodingStack(transfer, me->target,
                     99:                                               request, NULL, NO);
                    100:        }
                    101:     }
2.27      frystyk   102:     return HT_OK;
2.1       timbl     103: }
                    104: 
2.65      eric      105: /* _dispatchParsers - call request's MIME header parser.
                    106: ** Use global parser if no appropriate one is found for request.
                    107: */
                    108: PRIVATE int _dispatchParsers (HTStream * me)
                    109: {
                    110:     int status;
                    111:     char * token = HTChunk_data(me->token);
                    112:     char * value = HTChunk_data(me->value);
                    113:     BOOL found, local;
                    114:     HTMIMEParseSet * parseSet;
                    115: 
                    116:     /* In case we get an empty header consisting of a CRLF, we fall thru */
2.66    ! frystyk   117:     if (STREAM_TRACE) HTTrace("MIME header. %s: %s\n",
        !           118:                              token ? token : "<null>",
        !           119:                              value ? value : "<null>");
        !           120:     if (!token) return HT_OK;                      /* Ignore noop token */
2.65      eric      121: 
                    122:     if ((parseSet = HTRequest_MIMEParseSet(me->request, &local)) != NULL) {
                    123:         status = HTMIMEParseSet_dispatch(parseSet, me->request, 
                    124:                                         token, value, &found);
                    125:        if (found)
                    126:            return status;
                    127:        if (local)
                    128:            return HT_OK; /* not found, but that's OK */
                    129:     }
                    130: 
                    131:     if ((parseSet = HTHeader_MIMEParseSet()) == NULL)
                    132:         return HT_OK;
                    133:     status = HTMIMEParseSet_dispatch(parseSet, me->request, 
                    134:                                     token, value, &found);
                    135:     if (found)
                    136:         return status;
                    137:     if (STREAM_TRACE) HTTrace("Ignoring MIME header: %s: %s.\n", token, value);
                    138: 
                    139:     return HT_OK;
                    140: }
                    141: 
2.18      frystyk   142: /*
                    143: **     Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
                    144: **     Folding is either of CF LWS, LF LWS, CRLF LWS
                    145: */
2.57      frystyk   146: PRIVATE int HTMIME_put_block (HTStream * me, const char * b, int l)
2.18      frystyk   147: {
2.57      frystyk   148:     const char * start = b;
                    149:     const char * end = start;
2.64      eric      150:     const char * value = me->value->size ? b : NULL;
                    151:     long cl;
                    152:     int status;
                    153:     /*    enum {Line_CHAR, Line_END, Line_FOLD, Line_LINE} line = Line_CHAR; */
                    154: 
                    155:     while (!me->transparent) {
2.18      frystyk   156:        if (me->EOLstate == EOL_FCR) {
2.64      eric      157:            if (*b == CR)                                   /* End of header */
                    158:                me->EOLstate = EOL_END;
                    159:            else if (*b == LF)                               /* CRLF */
2.18      frystyk   160:                me->EOLstate = EOL_FLF;
2.64      eric      161:            else if (WHITE(*b))                            /* Folding: CR SP */
                    162:                me->EOLstate = EOL_FOLD;
                    163:            else                                                 /* New line */
                    164:                me->EOLstate = EOL_LINE;
2.18      frystyk   165:        } else if (me->EOLstate == EOL_FLF) {
                    166:            if (*b == CR)                               /* LF CR or CR LF CR */
                    167:                me->EOLstate = EOL_SCR;
2.64      eric      168:            else if (*b == LF)                              /* End of header */
                    169:                me->EOLstate = EOL_END;
                    170:            else if (WHITE(*b))                /* Folding: LF SP or CR LF SP */
                    171:                me->EOLstate = EOL_FOLD;
                    172:            else                                                /* New line */
                    173:                me->EOLstate = EOL_LINE;
                    174:        } else if (me->EOLstate == EOL_SCR) {
                    175:            if (*b==CR || *b==LF)                           /* End of header */
                    176:                me->EOLstate = EOL_END;
                    177:            else if (WHITE(*b))          /* Folding: LF CR SP or CR LF CR SP */
                    178:                me->EOLstate = EOL_FOLD;
                    179:            else                                                /* New line */
                    180:                me->EOLstate = EOL_LINE;
                    181:        } else if (*b == CR)
                    182:            me->EOLstate = EOL_FCR;
                    183:        else if (*b == LF)
                    184:            me->EOLstate = EOL_FLF;                            /* Line found */
                    185:        else {
                    186:            if (!me->haveToken) {
                    187:                if (*b == ':' || isspace(*b)) {
                    188:                    HTChunk_putb(me->token, start, end-start);
                    189:                    HTChunk_putc(me->token, '\0');
                    190:                    me->haveToken = YES;
                    191:                } else {
                    192:                    unsigned char ch = *(unsigned char *) b;
                    193:                    tolower(ch);
                    194: /*                 if (ch >= 'A' && ch <= 'Z')
                    195:                        ch += ('a' - 'A'); */
                    196:                    me->hash = (me->hash * 3 + ch) % MIME_HASH_SIZE;
                    197:                }
                    198:            } else if (value == NULL && *b != ':' && !isspace(*b))
                    199:                value = b;
                    200:            end++;
                    201:        }
                    202:        switch (me->EOLstate) {
                    203:            case EOL_LINE:
                    204:            case EOL_END: {
                    205:                int status;
                    206:                HTChunk_putb(me->value, value, end-value);
                    207:                HTChunk_putc(me->value, '\0');
                    208:                start=b, end=b;
2.65      eric      209:                status = _dispatchParsers(me);
2.64      eric      210:                if (me->EOLstate == EOL_END) {          /* EOL_END */
                    211:                    if (status == HT_OK)
                    212:                        status = pumpData(me);
                    213:                    HTNet_setBytesRead(me->net, l);
                    214:                } else {                                /* EOL_LINE */
                    215:                    HTChunk_clear(me->token);
                    216:                    HTChunk_clear(me->value);
                    217:                    me->haveToken = NO;
                    218:                    me->hash = 0;
                    219:                    value = NULL;
                    220:                }
2.18      frystyk   221:                me->EOLstate = EOL_BEGIN;
2.27      frystyk   222:                if (status != HT_OK)
                    223:                    return status;
2.64      eric      224:                break;
                    225:                }
                    226:            case EOL_FOLD:
2.18      frystyk   227:                me->EOLstate = EOL_BEGIN;
2.64      eric      228:                if (!me->haveToken) {
                    229:                    HTChunk_putb(me->token, start, end-start);
                    230:                    HTChunk_putc(me->token, '\0');
                    231:                    me->haveToken = YES;
                    232:                } else if (value) {
                    233:                    HTChunk_putb(me->value, value, end-value);
                    234:                    HTChunk_putc(me->value, ' ');
                    235:                }
                    236:                start=b, end=b;
                    237:                break;
                    238:            default: 
                    239:                b++;
                    240:                l--;
                    241:                if (!l) {
                    242:                    if (!me->haveToken)
                    243:                        HTChunk_putb(me->token, start, end-start);
                    244:                    else if (value)
                    245:                        HTChunk_putb(me->value, value, end-value);
                    246:                    return HT_OK;
                    247:                }
                    248:        }
2.18      frystyk   249:     }
2.32      frystyk   250: 
                    251:     /* 
                    252:     ** Put the rest down the stream without touching the data but make sure
                    253:     ** that we get the correct content length of data
                    254:     */
2.66    ! frystyk   255:     if (me->target) {
        !           256:        if ((status = (*me->target->isa->put_block)(me->target, b, l)) != HT_OK)
        !           257:            return status;
        !           258:        /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
        !           259:        cl = HTAnchor_length(me->anchor);
        !           260:        return (cl>=0 && HTNet_bytesRead(me->net)>=cl) ? HT_LOADED : HT_OK;
        !           261:     }
        !           262:     return HT_LOADED;
2.18      frystyk   263: }
                    264: 
                    265: 
                    266: /*     Character handling
                    267: **     ------------------
                    268: */
2.36      frystyk   269: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18      frystyk   270: {
                    271:     return HTMIME_put_block(me, &c, 1);
                    272: }
                    273: 
2.1       timbl     274: 
                    275: /*     String handling
                    276: **     ---------------
                    277: */
2.57      frystyk   278: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1       timbl     279: {
2.18      frystyk   280:     return HTMIME_put_block(me, s, (int) strlen(s));
2.1       timbl     281: }
                    282: 
                    283: 
2.18      frystyk   284: /*     Flush an stream object
                    285: **     ---------------------
2.1       timbl     286: */
2.36      frystyk   287: PRIVATE int HTMIME_flush (HTStream * me)
2.1       timbl     288: {
2.47      frystyk   289:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1       timbl     290: }
                    291: 
2.18      frystyk   292: /*     Free a stream object
                    293: **     --------------------
2.1       timbl     294: */
2.36      frystyk   295: PRIVATE int HTMIME_free (HTStream * me)
2.1       timbl     296: {
2.18      frystyk   297:     int status = HT_OK;
2.64      eric      298:     if (!me->transparent)
2.65      eric      299:         if (_dispatchParsers(me) == HT_OK)
2.64      eric      300:            pumpData(me);
2.25      frystyk   301:     if (me->target) {
                    302:        if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
                    303:            return HT_WOULD_BLOCK;
                    304:     }
2.26      frystyk   305:     if (PROT_TRACE)
2.55      eric      306:        HTTrace("MIME........ FREEING....\n");
2.64      eric      307:     HTChunk_delete(me->token);
                    308:     HTChunk_delete(me->value);
2.52      frystyk   309:     HT_FREE(me);
2.18      frystyk   310:     return status;
2.1       timbl     311: }
                    312: 
                    313: /*     End writing
                    314: */
2.38      frystyk   315: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1       timbl     316: {
2.18      frystyk   317:     int status = HT_ERROR;
2.41      frystyk   318:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26      frystyk   319:     if (PROT_TRACE)
2.55      eric      320:        HTTrace("MIME........ ABORTING...\n");
2.64      eric      321:     HTChunk_delete(me->token);
                    322:     HTChunk_delete(me->value);
2.52      frystyk   323:     HT_FREE(me);
2.18      frystyk   324:     return status;
2.1       timbl     325: }
                    326: 
                    327: 
                    328: 
                    329: /*     Structured Object Class
                    330: **     -----------------------
                    331: */
2.57      frystyk   332: PRIVATE const HTStreamClass HTMIME =
2.1       timbl     333: {              
                    334:        "MIMEParser",
2.18      frystyk   335:        HTMIME_flush,
2.1       timbl     336:        HTMIME_free,
2.6       timbl     337:        HTMIME_abort,
                    338:        HTMIME_put_character,
                    339:        HTMIME_put_string,
2.18      frystyk   340:        HTMIME_put_block
2.1       timbl     341: }; 
                    342: 
                    343: 
2.48      frystyk   344: /*     MIME header parser stream.
2.1       timbl     345: **     -------------------------
2.48      frystyk   346: **     This stream parses a complete MIME header and if a content type header
                    347: **     is found then the stream stack is called. Any left over data is pumped
                    348: **     right through the stream
2.1       timbl     349: */
2.36      frystyk   350: PUBLIC HTStream* HTMIMEConvert (HTRequest *    request,
                    351:                                void *          param,
                    352:                                HTFormat        input_format,
                    353:                                HTFormat        output_format,
                    354:                                HTStream *      output_stream)
2.1       timbl     355: {
2.62      frystyk   356:     HTStream * me;
2.52      frystyk   357:     if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
                    358:         HT_OUTOFMEM("HTMIMEConvert");
2.1       timbl     359:     me->isa = &HTMIME;       
2.18      frystyk   360:     me->request = request;
2.32      frystyk   361:     me->anchor = request->anchor;
                    362:     me->net = request->net;
2.49      frystyk   363:     me->target = output_stream;
2.18      frystyk   364:     me->target_format = output_format;
2.64      eric      365:     me->token = HTChunk_new(256);
                    366:     me->value = HTChunk_new(256);
                    367:     me->hash = 0;
2.18      frystyk   368:     me->EOLstate = EOL_BEGIN;
2.64      eric      369:     me->haveToken = NO;
2.1       timbl     370:     return me;
                    371: }
2.32      frystyk   372: 
2.48      frystyk   373: /*     MIME header ONLY parser stream
                    374: **     ------------------------------
                    375: **     This stream parses a complete MIME header and then returnes HT_PAUSE.
                    376: **     It does not set up any streams and resting data stays in the buffer.
                    377: **     This can be used if you only want to parse the headers before you
                    378: **     decide what to do next. This is for example the case in a server app.
                    379: */
                    380: PUBLIC HTStream * HTMIMEHeader (HTRequest *    request,
                    381:                                void *          param,
                    382:                                HTFormat        input_format,
                    383:                                HTFormat        output_format,
                    384:                                HTStream *      output_stream)
                    385: {
2.62      frystyk   386:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    387:                                  output_format, output_stream);
                    388:     me->head_only = YES;
2.48      frystyk   389:     return me;
                    390: }
2.62      frystyk   391: 
                    392: /*     MIME footer ONLY parser stream
                    393: **     ------------------------------
                    394: **     Parse only a footer, for example after a chunked encoding.
                    395: */
                    396: PUBLIC HTStream * HTMIMEFooter (HTRequest *    request,
                    397:                                void *          param,
                    398:                                HTFormat        input_format,
                    399:                                HTFormat        output_format,
                    400:                                HTStream *      output_stream)
                    401: {
                    402:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    403:                                  output_format, output_stream);
                    404:     me->footer = YES;
                    405:     return me;
                    406: }

Webmaster