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

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.67    ! frystyk     6: **     @(#) $Id: HTMIME.c,v 2.66 1996/07/02 22:54:42 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.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 */
2.67    ! frystyk   211:                    if (status == HT_OK) {
        !           212:                        b++, l--;
2.64      eric      213:                        status = pumpData(me);
2.67    ! frystyk   214:                    }
2.64      eric      215:                    HTNet_setBytesRead(me->net, l);
                    216:                } else {                                /* EOL_LINE */
                    217:                    HTChunk_clear(me->token);
                    218:                    HTChunk_clear(me->value);
                    219:                    me->haveToken = NO;
                    220:                    me->hash = 0;
                    221:                    value = NULL;
                    222:                }
2.18      frystyk   223:                me->EOLstate = EOL_BEGIN;
2.27      frystyk   224:                if (status != HT_OK)
                    225:                    return status;
2.64      eric      226:                break;
                    227:                }
                    228:            case EOL_FOLD:
2.18      frystyk   229:                me->EOLstate = EOL_BEGIN;
2.64      eric      230:                if (!me->haveToken) {
                    231:                    HTChunk_putb(me->token, start, end-start);
                    232:                    HTChunk_putc(me->token, '\0');
                    233:                    me->haveToken = YES;
                    234:                } else if (value) {
                    235:                    HTChunk_putb(me->value, value, end-value);
                    236:                    HTChunk_putc(me->value, ' ');
                    237:                }
                    238:                start=b, end=b;
                    239:                break;
                    240:            default: 
                    241:                b++;
                    242:                l--;
                    243:                if (!l) {
                    244:                    if (!me->haveToken)
                    245:                        HTChunk_putb(me->token, start, end-start);
                    246:                    else if (value)
                    247:                        HTChunk_putb(me->value, value, end-value);
                    248:                    return HT_OK;
                    249:                }
                    250:        }
2.18      frystyk   251:     }
2.32      frystyk   252: 
                    253:     /* 
                    254:     ** Put the rest down the stream without touching the data but make sure
                    255:     ** that we get the correct content length of data
                    256:     */
2.66      frystyk   257:     if (me->target) {
                    258:        if ((status = (*me->target->isa->put_block)(me->target, b, l)) != HT_OK)
                    259:            return status;
                    260:        /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
                    261:        cl = HTAnchor_length(me->anchor);
                    262:        return (cl>=0 && HTNet_bytesRead(me->net)>=cl) ? HT_LOADED : HT_OK;
                    263:     }
                    264:     return HT_LOADED;
2.18      frystyk   265: }
                    266: 
                    267: 
                    268: /*     Character handling
                    269: **     ------------------
                    270: */
2.36      frystyk   271: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18      frystyk   272: {
                    273:     return HTMIME_put_block(me, &c, 1);
                    274: }
                    275: 
2.1       timbl     276: 
                    277: /*     String handling
                    278: **     ---------------
                    279: */
2.57      frystyk   280: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1       timbl     281: {
2.18      frystyk   282:     return HTMIME_put_block(me, s, (int) strlen(s));
2.1       timbl     283: }
                    284: 
                    285: 
2.18      frystyk   286: /*     Flush an stream object
                    287: **     ---------------------
2.1       timbl     288: */
2.36      frystyk   289: PRIVATE int HTMIME_flush (HTStream * me)
2.1       timbl     290: {
2.47      frystyk   291:     return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1       timbl     292: }
                    293: 
2.18      frystyk   294: /*     Free a stream object
                    295: **     --------------------
2.1       timbl     296: */
2.36      frystyk   297: PRIVATE int HTMIME_free (HTStream * me)
2.1       timbl     298: {
2.18      frystyk   299:     int status = HT_OK;
2.64      eric      300:     if (!me->transparent)
2.65      eric      301:         if (_dispatchParsers(me) == HT_OK)
2.64      eric      302:            pumpData(me);
2.25      frystyk   303:     if (me->target) {
                    304:        if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
                    305:            return HT_WOULD_BLOCK;
                    306:     }
2.26      frystyk   307:     if (PROT_TRACE)
2.55      eric      308:        HTTrace("MIME........ FREEING....\n");
2.64      eric      309:     HTChunk_delete(me->token);
                    310:     HTChunk_delete(me->value);
2.52      frystyk   311:     HT_FREE(me);
2.18      frystyk   312:     return status;
2.1       timbl     313: }
                    314: 
                    315: /*     End writing
                    316: */
2.38      frystyk   317: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1       timbl     318: {
2.18      frystyk   319:     int status = HT_ERROR;
2.41      frystyk   320:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26      frystyk   321:     if (PROT_TRACE)
2.55      eric      322:        HTTrace("MIME........ ABORTING...\n");
2.64      eric      323:     HTChunk_delete(me->token);
                    324:     HTChunk_delete(me->value);
2.52      frystyk   325:     HT_FREE(me);
2.18      frystyk   326:     return status;
2.1       timbl     327: }
                    328: 
                    329: 
                    330: 
                    331: /*     Structured Object Class
                    332: **     -----------------------
                    333: */
2.57      frystyk   334: PRIVATE const HTStreamClass HTMIME =
2.1       timbl     335: {              
                    336:        "MIMEParser",
2.18      frystyk   337:        HTMIME_flush,
2.1       timbl     338:        HTMIME_free,
2.6       timbl     339:        HTMIME_abort,
                    340:        HTMIME_put_character,
                    341:        HTMIME_put_string,
2.18      frystyk   342:        HTMIME_put_block
2.1       timbl     343: }; 
                    344: 
                    345: 
2.48      frystyk   346: /*     MIME header parser stream.
2.1       timbl     347: **     -------------------------
2.48      frystyk   348: **     This stream parses a complete MIME header and if a content type header
                    349: **     is found then the stream stack is called. Any left over data is pumped
                    350: **     right through the stream
2.1       timbl     351: */
2.36      frystyk   352: PUBLIC HTStream* HTMIMEConvert (HTRequest *    request,
                    353:                                void *          param,
                    354:                                HTFormat        input_format,
                    355:                                HTFormat        output_format,
                    356:                                HTStream *      output_stream)
2.1       timbl     357: {
2.62      frystyk   358:     HTStream * me;
2.52      frystyk   359:     if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
                    360:         HT_OUTOFMEM("HTMIMEConvert");
2.1       timbl     361:     me->isa = &HTMIME;       
2.18      frystyk   362:     me->request = request;
2.32      frystyk   363:     me->anchor = request->anchor;
                    364:     me->net = request->net;
2.49      frystyk   365:     me->target = output_stream;
2.18      frystyk   366:     me->target_format = output_format;
2.64      eric      367:     me->token = HTChunk_new(256);
                    368:     me->value = HTChunk_new(256);
                    369:     me->hash = 0;
2.18      frystyk   370:     me->EOLstate = EOL_BEGIN;
2.64      eric      371:     me->haveToken = NO;
2.1       timbl     372:     return me;
                    373: }
2.32      frystyk   374: 
2.48      frystyk   375: /*     MIME header ONLY parser stream
                    376: **     ------------------------------
                    377: **     This stream parses a complete MIME header and then returnes HT_PAUSE.
                    378: **     It does not set up any streams and resting data stays in the buffer.
                    379: **     This can be used if you only want to parse the headers before you
                    380: **     decide what to do next. This is for example the case in a server app.
                    381: */
                    382: PUBLIC HTStream * HTMIMEHeader (HTRequest *    request,
                    383:                                void *          param,
                    384:                                HTFormat        input_format,
                    385:                                HTFormat        output_format,
                    386:                                HTStream *      output_stream)
                    387: {
2.62      frystyk   388:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    389:                                  output_format, output_stream);
                    390:     me->head_only = YES;
2.48      frystyk   391:     return me;
                    392: }
2.62      frystyk   393: 
                    394: /*     MIME footer ONLY parser stream
                    395: **     ------------------------------
                    396: **     Parse only a footer, for example after a chunked encoding.
                    397: */
                    398: PUBLIC HTStream * HTMIMEFooter (HTRequest *    request,
                    399:                                void *          param,
                    400:                                HTFormat        input_format,
                    401:                                HTFormat        output_format,
                    402:                                HTStream *      output_stream)
                    403: {
                    404:     HTStream * me = HTMIMEConvert(request, param, input_format,
                    405:                                  output_format, output_stream);
                    406:     me->footer = YES;
2.67    ! frystyk   407:     me->EOLstate = EOL_FLF;
2.62      frystyk   408:     return me;
                    409: }

Webmaster