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

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

Webmaster