Annotation of libwww/Library/src/HTMIME.c, revision 2.65
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.65 ! eric 6: ** @(#) $Id: HTMIME.c,v 2.64 1996/06/03 19:25:20 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 */
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.65 ! eric 104: /* _dispatchParsers - call request's MIME header parser.
! 105: ** Use global parser if no appropriate one is found for request.
! 106: */
! 107: PRIVATE int _dispatchParsers (HTStream * me)
! 108: {
! 109: int status;
! 110: char * token = HTChunk_data(me->token);
! 111: char * value = HTChunk_data(me->value);
! 112: BOOL found, local;
! 113: HTMIMEParseSet * parseSet;
! 114:
! 115: /* In case we get an empty header consisting of a CRLF, we fall thru */
! 116: if (STREAM_TRACE) HTTrace("checking MIME header %s: %s\n", token, value);
! 117:
! 118: if ((parseSet = HTRequest_MIMEParseSet(me->request, &local)) != NULL) {
! 119: status = HTMIMEParseSet_dispatch(parseSet, me->request,
! 120: token, value, &found);
! 121: if (found)
! 122: return status;
! 123: if (local)
! 124: return HT_OK; /* not found, but that's OK */
! 125: }
! 126:
! 127: if ((parseSet = HTHeader_MIMEParseSet()) == NULL)
! 128: return HT_OK;
! 129: status = HTMIMEParseSet_dispatch(parseSet, me->request,
! 130: token, value, &found);
! 131: if (found)
! 132: return status;
! 133: if (STREAM_TRACE) HTTrace("Ignoring MIME header: %s: %s.\n", token, value);
! 134:
! 135: return HT_OK;
! 136: }
! 137:
2.18 frystyk 138: /*
139: ** Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
140: ** Folding is either of CF LWS, LF LWS, CRLF LWS
141: */
2.57 frystyk 142: PRIVATE int HTMIME_put_block (HTStream * me, const char * b, int l)
2.18 frystyk 143: {
2.57 frystyk 144: const char * start = b;
145: const char * end = start;
2.64 eric 146: const char * value = me->value->size ? b : NULL;
147: long cl;
148: int status;
149: /* enum {Line_CHAR, Line_END, Line_FOLD, Line_LINE} line = Line_CHAR; */
150:
151: while (!me->transparent) {
2.18 frystyk 152: if (me->EOLstate == EOL_FCR) {
2.64 eric 153: if (*b == CR) /* End of header */
154: me->EOLstate = EOL_END;
155: else if (*b == LF) /* CRLF */
2.18 frystyk 156: me->EOLstate = EOL_FLF;
2.64 eric 157: else if (WHITE(*b)) /* Folding: CR SP */
158: me->EOLstate = EOL_FOLD;
159: else /* New line */
160: me->EOLstate = EOL_LINE;
2.18 frystyk 161: } else if (me->EOLstate == EOL_FLF) {
162: if (*b == CR) /* LF CR or CR LF CR */
163: me->EOLstate = EOL_SCR;
2.64 eric 164: else if (*b == LF) /* End of header */
165: me->EOLstate = EOL_END;
166: else if (WHITE(*b)) /* Folding: LF SP or CR LF SP */
167: me->EOLstate = EOL_FOLD;
168: else /* New line */
169: me->EOLstate = EOL_LINE;
170: } else if (me->EOLstate == EOL_SCR) {
171: if (*b==CR || *b==LF) /* End of header */
172: me->EOLstate = EOL_END;
173: else if (WHITE(*b)) /* Folding: LF CR SP or CR LF CR SP */
174: me->EOLstate = EOL_FOLD;
175: else /* New line */
176: me->EOLstate = EOL_LINE;
177: } else if (*b == CR)
178: me->EOLstate = EOL_FCR;
179: else if (*b == LF)
180: me->EOLstate = EOL_FLF; /* Line found */
181: else {
182: if (!me->haveToken) {
183: if (*b == ':' || isspace(*b)) {
184: HTChunk_putb(me->token, start, end-start);
185: HTChunk_putc(me->token, '\0');
186: me->haveToken = YES;
187: } else {
188: unsigned char ch = *(unsigned char *) b;
189: tolower(ch);
190: /* if (ch >= 'A' && ch <= 'Z')
191: ch += ('a' - 'A'); */
192: me->hash = (me->hash * 3 + ch) % MIME_HASH_SIZE;
193: }
194: } else if (value == NULL && *b != ':' && !isspace(*b))
195: value = b;
196: end++;
197: }
198: switch (me->EOLstate) {
199: case EOL_LINE:
200: case EOL_END: {
201: int status;
202: HTChunk_putb(me->value, value, end-value);
203: HTChunk_putc(me->value, '\0');
204: start=b, end=b;
2.65 ! eric 205: status = _dispatchParsers(me);
2.64 eric 206: if (me->EOLstate == EOL_END) { /* EOL_END */
207: if (status == HT_OK)
208: status = pumpData(me);
209: HTNet_setBytesRead(me->net, l);
210: } else { /* EOL_LINE */
211: HTChunk_clear(me->token);
212: HTChunk_clear(me->value);
213: me->haveToken = NO;
214: me->hash = 0;
215: value = NULL;
216: }
2.18 frystyk 217: me->EOLstate = EOL_BEGIN;
2.27 frystyk 218: if (status != HT_OK)
219: return status;
2.64 eric 220: break;
221: }
222: case EOL_FOLD:
2.18 frystyk 223: me->EOLstate = EOL_BEGIN;
2.64 eric 224: if (!me->haveToken) {
225: HTChunk_putb(me->token, start, end-start);
226: HTChunk_putc(me->token, '\0');
227: me->haveToken = YES;
228: } else if (value) {
229: HTChunk_putb(me->value, value, end-value);
230: HTChunk_putc(me->value, ' ');
231: }
232: start=b, end=b;
233: break;
234: default:
235: b++;
236: l--;
237: if (!l) {
238: if (!me->haveToken)
239: HTChunk_putb(me->token, start, end-start);
240: else if (value)
241: HTChunk_putb(me->value, value, end-value);
242: return HT_OK;
243: }
244: }
2.18 frystyk 245: }
2.32 frystyk 246:
247: /*
248: ** Put the rest down the stream without touching the data but make sure
249: ** that we get the correct content length of data
250: */
2.64 eric 251: if ((status = (*me->target->isa->put_block)(me->target, b, l)) != HT_OK)
252: return status;
253: /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
254: cl = HTAnchor_length(me->anchor);
255: return (cl>=0 && HTNet_bytesRead(me->net)>=cl) ? HT_LOADED : HT_OK;
2.18 frystyk 256: }
257:
258:
259: /* Character handling
260: ** ------------------
261: */
2.36 frystyk 262: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18 frystyk 263: {
264: return HTMIME_put_block(me, &c, 1);
265: }
266:
2.1 timbl 267:
268: /* String handling
269: ** ---------------
270: */
2.57 frystyk 271: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1 timbl 272: {
2.18 frystyk 273: return HTMIME_put_block(me, s, (int) strlen(s));
2.1 timbl 274: }
275:
276:
2.18 frystyk 277: /* Flush an stream object
278: ** ---------------------
2.1 timbl 279: */
2.36 frystyk 280: PRIVATE int HTMIME_flush (HTStream * me)
2.1 timbl 281: {
2.47 frystyk 282: return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1 timbl 283: }
284:
2.18 frystyk 285: /* Free a stream object
286: ** --------------------
2.1 timbl 287: */
2.36 frystyk 288: PRIVATE int HTMIME_free (HTStream * me)
2.1 timbl 289: {
2.18 frystyk 290: int status = HT_OK;
2.64 eric 291: if (!me->transparent)
2.65 ! eric 292: if (_dispatchParsers(me) == HT_OK)
2.64 eric 293: pumpData(me);
2.25 frystyk 294: if (me->target) {
295: if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
296: return HT_WOULD_BLOCK;
297: }
2.26 frystyk 298: if (PROT_TRACE)
2.55 eric 299: HTTrace("MIME........ FREEING....\n");
2.64 eric 300: HTChunk_delete(me->token);
301: HTChunk_delete(me->value);
2.52 frystyk 302: HT_FREE(me);
2.18 frystyk 303: return status;
2.1 timbl 304: }
305:
306: /* End writing
307: */
2.38 frystyk 308: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1 timbl 309: {
2.18 frystyk 310: int status = HT_ERROR;
2.41 frystyk 311: if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26 frystyk 312: if (PROT_TRACE)
2.55 eric 313: HTTrace("MIME........ ABORTING...\n");
2.64 eric 314: HTChunk_delete(me->token);
315: HTChunk_delete(me->value);
2.52 frystyk 316: HT_FREE(me);
2.18 frystyk 317: return status;
2.1 timbl 318: }
319:
320:
321:
322: /* Structured Object Class
323: ** -----------------------
324: */
2.57 frystyk 325: PRIVATE const HTStreamClass HTMIME =
2.1 timbl 326: {
327: "MIMEParser",
2.18 frystyk 328: HTMIME_flush,
2.1 timbl 329: HTMIME_free,
2.6 timbl 330: HTMIME_abort,
331: HTMIME_put_character,
332: HTMIME_put_string,
2.18 frystyk 333: HTMIME_put_block
2.1 timbl 334: };
335:
336:
2.48 frystyk 337: /* MIME header parser stream.
2.1 timbl 338: ** -------------------------
2.48 frystyk 339: ** This stream parses a complete MIME header and if a content type header
340: ** is found then the stream stack is called. Any left over data is pumped
341: ** right through the stream
2.1 timbl 342: */
2.36 frystyk 343: PUBLIC HTStream* HTMIMEConvert (HTRequest * request,
344: void * param,
345: HTFormat input_format,
346: HTFormat output_format,
347: HTStream * output_stream)
2.1 timbl 348: {
2.62 frystyk 349: HTStream * me;
2.52 frystyk 350: if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
351: HT_OUTOFMEM("HTMIMEConvert");
2.1 timbl 352: me->isa = &HTMIME;
2.18 frystyk 353: me->request = request;
2.32 frystyk 354: me->anchor = request->anchor;
355: me->net = request->net;
2.49 frystyk 356: me->target = output_stream;
2.18 frystyk 357: me->target_format = output_format;
2.64 eric 358: me->token = HTChunk_new(256);
359: me->value = HTChunk_new(256);
360: me->hash = 0;
2.18 frystyk 361: me->EOLstate = EOL_BEGIN;
2.64 eric 362: me->haveToken = NO;
2.1 timbl 363: return me;
364: }
2.32 frystyk 365:
2.48 frystyk 366: /* MIME header ONLY parser stream
367: ** ------------------------------
368: ** This stream parses a complete MIME header and then returnes HT_PAUSE.
369: ** It does not set up any streams and resting data stays in the buffer.
370: ** This can be used if you only want to parse the headers before you
371: ** decide what to do next. This is for example the case in a server app.
372: */
373: PUBLIC HTStream * HTMIMEHeader (HTRequest * request,
374: void * param,
375: HTFormat input_format,
376: HTFormat output_format,
377: HTStream * output_stream)
378: {
2.62 frystyk 379: HTStream * me = HTMIMEConvert(request, param, input_format,
380: output_format, output_stream);
381: me->head_only = YES;
2.48 frystyk 382: return me;
383: }
2.62 frystyk 384:
385: /* MIME footer ONLY parser stream
386: ** ------------------------------
387: ** Parse only a footer, for example after a chunked encoding.
388: */
389: PUBLIC HTStream * HTMIMEFooter (HTRequest * request,
390: void * param,
391: HTFormat input_format,
392: HTFormat output_format,
393: HTStream * output_stream)
394: {
395: HTStream * me = HTMIMEConvert(request, param, input_format,
396: output_format, output_stream);
397: me->footer = YES;
398: return me;
399: }
Webmaster