Annotation of libwww/Library/src/HTMIME.c, revision 2.49
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.1 timbl 6: **
7: ** This is RFC 1341-specific code.
8: ** The input stream pushed into this parser is assumed to be
9: ** stripped on CRs, ie lines end with LF, not CR LF.
10: ** (It is easy to change this except for the body part where
11: ** conversion can be slow.)
12: **
13: ** History:
14: ** Feb 92 Written Tim Berners-Lee, CERN
2.13 duns 15: ** 8 Jul 94 FM Insulate free() from _free structure element.
2.18 frystyk 16: ** 14 Mar 95 HFN Now using anchor for storing data. No more `\n',
17: ** static buffers etc.
2.1 timbl 18: */
2.17 frystyk 19:
20: /* Library include files */
21: #include "tcp.h"
22: #include "HTUtils.h"
23: #include "HTString.h"
2.9 luotonen 24: #include "HTFormat.h"
2.27 frystyk 25: #include "HTCache.h"
26: #include "HTAlert.h"
2.42 frystyk 27: #include "HTAncMan.h"
2.18 frystyk 28: #include "HTChunk.h"
2.26 frystyk 29: #include "HTMethod.h"
2.36 frystyk 30: #include "HTHeader.h"
2.24 frystyk 31: #include "HTSocket.h"
2.39 frystyk 32: #include "HTWWWStr.h"
2.17 frystyk 33: #include "HTFWrite.h"
2.32 frystyk 34: #include "HTNetMan.h"
2.31 frystyk 35: #include "HTReqMan.h"
2.14 frystyk 36: #include "HTMIME.h" /* Implemented here */
2.1 timbl 37:
38: /* MIME Object
39: ** -----------
40: */
41: typedef enum _MIME_state {
2.23 frystyk 42: BEGINNING_OF_LINE=0,
2.18 frystyk 43: CHECK, /* check against check_pointer */
44: UNKNOWN, /* Unknown header */
45: JUNK_LINE, /* Ignore rest of header */
46:
2.32 frystyk 47: CON, /* Intermediate states */
48: CONTENT,
2.45 frystyk 49: FIRSTLETTER_A,
2.18 frystyk 50: FIRSTLETTER_D,
51: FIRSTLETTER_L,
52: CONTENTLETTER_L,
53: CONTENTLETTER_T,
54:
2.45 frystyk 55: ACCEPT_TYPE, /* Headers supported */
56: ACCEPT_CHARSET,
57: ACCEPT_ENCODING,
58: ACCEPT_LANGUAGE,
59: ALLOW,
2.18 frystyk 60: AUTHENTICATE,
2.32 frystyk 61: CONNECTION,
2.18 frystyk 62: CONTENT_ENCODING,
63: CONTENT_LANGUAGE,
64: CONTENT_LENGTH,
2.14 frystyk 65: CONTENT_TRANSFER_ENCODING,
66: CONTENT_TYPE,
2.23 frystyk 67: MIME_DATE,
2.18 frystyk 68: DERIVED_FROM,
69: EXPIRES,
70: LAST_MODIFIED,
71: LINK,
2.14 frystyk 72: LOCATION,
2.18 frystyk 73: PUBLIC_METHODS,
74: RETRY_AFTER,
75: TITLE,
76: URI_HEADER,
77: VERSION
2.1 timbl 78: } MIME_state;
79:
80: struct _HTStream {
2.18 frystyk 81: CONST HTStreamClass * isa;
82: HTRequest * request;
2.32 frystyk 83: HTNet * net;
84: HTParentAnchor * anchor;
2.18 frystyk 85: HTStream * target;
86: HTFormat target_format;
87: HTChunk * buffer;
88: HTSocketEOL EOLstate;
89: BOOL transparent;
2.48 frystyk 90: BOOL head_only;
2.35 frystyk 91: BOOL nntp;
2.1 timbl 92: };
93:
2.18 frystyk 94: /* ------------------------------------------------------------------------- */
2.1 timbl 95:
2.18 frystyk 96: /*
2.1 timbl 97: ** This is a FSM parser which is tolerant as it can be of all
98: ** syntax errors. It ignores field names it does not understand,
99: ** and resynchronises on line beginnings.
100: */
2.36 frystyk 101: PRIVATE int parseheader (HTStream * me, HTRequest * request,
102: HTParentAnchor * anchor)
2.18 frystyk 103: {
104: MIME_state state = BEGINNING_OF_LINE;
105: MIME_state ok_state; /* got this state if match */
106: char *ptr = me->buffer->data-1; /* We dont change the data in length */
107: char *stop = ptr+me->buffer->size; /* When to stop */
108: char *header = ptr; /* For diagnostics */
109: CONST char * check_pointer; /* checking input */
110: char *value;
2.27 frystyk 111:
112: /* In case we get an empty header consisting of a CRLF, we fall thru */
2.18 frystyk 113: while (ptr < stop) {
114: switch (state) {
115: case BEGINNING_OF_LINE:
116: header = ++ptr;
117: switch (TOLOWER(*ptr)) {
118: case 'a':
2.45 frystyk 119: state = FIRSTLETTER_A;
2.18 frystyk 120: break;
121:
122: case 'c':
2.32 frystyk 123: check_pointer = "on";
124: ok_state = CON;
2.18 frystyk 125: state = CHECK;
126: break;
127:
128: case 'd':
129: state = FIRSTLETTER_D;
130: break;
131:
132: case 'e':
133: check_pointer = "xpires";
134: ok_state = EXPIRES;
135: state = CHECK;
136: break;
137:
2.32 frystyk 138: case 'k':
2.33 frystyk 139: check_pointer = "eep-alive";
140: ok_state = JUNK_LINE; /* We don't use this but recognize it */
141: state = CHECK;
2.32 frystyk 142: break;
143:
2.18 frystyk 144: case 'l':
145: state = FIRSTLETTER_L;
146: break;
147:
148: case 'm':
149: check_pointer = "ime-version";
150: ok_state = JUNK_LINE; /* We don't use this but recognize it */
151: state = CHECK;
152: break;
153:
2.35 frystyk 154: case 'n':
155: check_pointer = "ewsgroups";
156: me->nntp = YES; /* Due to news brain damage */
157: ok_state = JUNK_LINE; /* We don't use this but recognize it */
158: state = CHECK;
159: break;
160:
2.18 frystyk 161: case 'r':
162: check_pointer = "etry-after";
163: ok_state = RETRY_AFTER;
164: state = CHECK;
165: break;
166:
167: case 's':
168: check_pointer = "erver";
169: ok_state = JUNK_LINE; /* We don't use this but recognize it */
170: state = CHECK;
171: break;
2.1 timbl 172:
2.18 frystyk 173: case 't':
174: check_pointer = "itle";
175: ok_state = TITLE;
176: state = CHECK;
177: break;
178:
179: case 'u':
180: check_pointer = "ri";
181: ok_state = URI_HEADER;
182: state = CHECK;
183: break;
184:
185: case 'v':
186: check_pointer = "ersion";
187: ok_state = VERSION;
188: state = CHECK;
189: break;
190:
191: case 'w':
192: check_pointer = "ww-authenticate";
193: ok_state = AUTHENTICATE;
194: state = CHECK;
195: break;
2.1 timbl 196:
2.18 frystyk 197: default:
198: state = UNKNOWN;
199: break;
200: }
201: ptr++;
2.1 timbl 202: break;
203:
2.45 frystyk 204: case FIRSTLETTER_A:
205: if (!strncasecomp(ptr, "llow", 4)) {
206: state = ALLOW;
207: ptr += 4;
208: } else if (!strncasecomp(ptr, "ccept-language", 14)) {
209: state = ACCEPT_LANGUAGE;
210: ptr += 14;
211: } else if (!strncasecomp(ptr, "ccept-charset", 13)) {
212: state = ACCEPT_CHARSET;
213: ptr += 13;
214: } else if (!strncasecomp(ptr, "ccept", 5)) {
215: state = ACCEPT_TYPE;
216: ptr += 5;
217: } else
218: state = UNKNOWN;
219: ptr++;
220: break;
221:
2.18 frystyk 222: case FIRSTLETTER_D:
223: switch (TOLOWER(*ptr)) {
224: case 'a':
225: check_pointer = "te";
2.23 frystyk 226: ok_state = MIME_DATE;
2.18 frystyk 227: state = CHECK;
228: break;
229:
230: case 'e':
231: check_pointer = "rived-from";
232: ok_state = DERIVED_FROM;
233: state = CHECK;
234: break;
235:
236: default:
237: state = UNKNOWN;
238: break;
239: }
240: ptr++;
241: break;
242:
243: case FIRSTLETTER_L:
244: switch (TOLOWER(*ptr)) {
245: case 'a':
246: check_pointer = "st-modified";
247: ok_state = LAST_MODIFIED;
248: state = CHECK;
249: break;
250:
251: case 'i':
252: check_pointer = "nk";
253: ok_state = LINK;
254: state = CHECK;
255: break;
256:
257: case 'o':
258: check_pointer = "cation";
259: ok_state = LOCATION;
260: state = CHECK;
261: break;
262:
263: default:
264: state = UNKNOWN;
265: break;
266: }
267: ptr++;
268: break;
269:
2.32 frystyk 270: case CON:
271: switch (TOLOWER(*ptr)) {
272: case 'n':
273: check_pointer = "ection";
274: ok_state = CONNECTION;
275: state = CHECK;
276: break;
277:
278: case 't':
279: check_pointer = "ent-";
280: ok_state = CONTENT;
281: state = CHECK;
282: break;
283:
284: default:
285: state = UNKNOWN;
286: break;
287: }
288: ptr++;
289: break;
290:
2.18 frystyk 291: case CONTENT:
292: switch (TOLOWER(*ptr)) {
293: case 'e':
294: check_pointer = "ncoding";
295: ok_state = CONTENT_ENCODING;
296: state = CHECK;
297: break;
298:
299: case 'l':
300: state = CONTENTLETTER_L;
301: break;
302:
303: case 't':
304: state = CONTENTLETTER_T;
305: break;
306:
307: default:
308: state = UNKNOWN;
309: break;
310: }
311: ptr++;
2.1 timbl 312: break;
2.14 frystyk 313:
2.18 frystyk 314: case CONTENTLETTER_L:
315: switch (TOLOWER(*ptr)) {
316: case 'a':
317: check_pointer = "nguage";
318: ok_state = CONTENT_LANGUAGE;
319: state = CHECK;
320: break;
321:
322: case 'e':
323: check_pointer = "ngth";
324: ok_state = CONTENT_LENGTH;
325: state = CHECK;
326: break;
327:
328: default:
329: state = UNKNOWN;
330: break;
331: }
332: ptr++;
2.14 frystyk 333: break;
334:
2.18 frystyk 335: case CONTENTLETTER_T:
336: switch (TOLOWER(*ptr)) {
337: case 'r':
338: check_pointer = "ansfer-encoding";
339: ok_state = CONTENT_TRANSFER_ENCODING;
340: state = CHECK;
341: break;
342:
343: case 'y':
344: check_pointer = "pe";
345: ok_state = CONTENT_TYPE;
346: state = CHECK;
347: break;
348:
349: default:
350: state = UNKNOWN;
351: break;
352: }
353: ptr++;
2.14 frystyk 354: break;
355:
2.18 frystyk 356: case CHECK: /* Check against string */
2.45 frystyk 357: while (TOLOWER(*ptr) == *check_pointer) ptr++, check_pointer++;
358: if (!*check_pointer) {
2.18 frystyk 359: state = ok_state;
360: while (*ptr && (WHITE(*ptr) || *ptr==':')) /* Spool to value */
361: ptr++;
362: } else
363: state = UNKNOWN;
2.14 frystyk 364: break;
365:
2.45 frystyk 366: case ACCEPT_TYPE: /* @@@ */
367: state = JUNK_LINE;
368: break;
369:
370: case ACCEPT_CHARSET: /* @@@ */
371: state = JUNK_LINE;
372: break;
373:
374: case ACCEPT_ENCODING: /* @@@ */
375: state = JUNK_LINE;
376: break;
377:
378: case ACCEPT_LANGUAGE: /* @@@ */
379: state = JUNK_LINE;
380: break;
381:
382: case ALLOW:
2.20 frystyk 383: while ((value = HTNextField(&ptr)) != NULL) {
384: HTMethod new_method;
2.26 frystyk 385: /* We treat them as case-insensitive! */
2.20 frystyk 386: if ((new_method = HTMethod_enum(value)) != METHOD_INVALID)
387: anchor->methods += new_method;
2.1 timbl 388: }
2.18 frystyk 389: if (STREAM_TRACE)
2.37 frystyk 390: TTYPrint(TDEST, "MIMEParser.. Methods allowed: %d\n",
2.18 frystyk 391: anchor->methods);
392: state = JUNK_LINE;
2.1 timbl 393: break;
2.18 frystyk 394:
395: case AUTHENTICATE:
396: if ((value = HTNextField(&ptr)) != NULL) {
397: StrAllocCopy(request->WWWAAScheme, value);
2.20 frystyk 398:
399: /* The parsing is done in HTSSUtils.c for the moment */
400: if (*ptr) StrAllocCopy(request->WWWAARealm, ptr);
2.1 timbl 401: }
2.18 frystyk 402: state = JUNK_LINE;
403: break;
404:
2.32 frystyk 405: case CONNECTION:
406: if ((value = HTNextField(&ptr)) != NULL) {
407: if (!strcasecomp(value, "keep-alive")) {
408: if (STREAM_TRACE)
2.43 frystyk 409: TTYPrint(TDEST,"MIMEParser.. Persistent Connection\n");
2.32 frystyk 410: HTDNS_setSocket(me->net->dns, me->net->sockfd);
411: }
412: }
413: state = JUNK_LINE;
414: break;
415:
2.18 frystyk 416: case CONTENT_ENCODING:
417: if ((value = HTNextField(&ptr)) != NULL) {
418: char *lc = value;
2.20 frystyk 419: while ((*lc = TOLOWER(*lc))) lc++;
2.18 frystyk 420: anchor->content_encoding = HTAtom_for(value);
421: }
422: state = JUNK_LINE;
423: break;
424:
2.21 frystyk 425: case CONTENT_LANGUAGE: /* @@@ SHOULD BE A LIST @@@ */
426: if ((value = HTNextField(&ptr)) != NULL) {
427: char *lc = value;
428: while ((*lc = TOLOWER(*lc))) lc++;
429: anchor->content_language = HTAtom_for(value);
430: }
431: state = JUNK_LINE;
2.18 frystyk 432: break;
433:
434: case CONTENT_LENGTH:
435: if ((value = HTNextField(&ptr)) != NULL)
436: anchor->content_length = atol(value);
437: state = JUNK_LINE;
438: break;
439:
440: case CONTENT_TRANSFER_ENCODING:
441: if ((value = HTNextField(&ptr)) != NULL) {
442: char *lc = value;
2.20 frystyk 443: while ((*lc = TOLOWER(*lc))) lc++;
2.18 frystyk 444: anchor->cte = HTAtom_for(value);
445: }
446: state = JUNK_LINE;
447: break;
448:
449: case CONTENT_TYPE:
450: if ((value = HTNextField(&ptr)) != NULL) {
451: char *lc = value;
452: while ((*lc = TOLOWER(*lc))) lc++;
453: anchor->content_type = HTAtom_for(value);
2.38 frystyk 454: while ((value = HTNextField(&ptr)) != NULL) {
2.20 frystyk 455: if (!strcasecomp(value, "charset")) {
456: if ((value = HTNextField(&ptr)) != NULL) {
457: lc = value;
458: while ((*lc = TOLOWER(*lc))) lc++;
459: anchor->charset = HTAtom_for(value);
460: }
2.38 frystyk 461: } else if (!strcasecomp(value, "level")) {
2.20 frystyk 462: if ((value = HTNextField(&ptr)) != NULL) {
463: lc = value;
464: while ((*lc = TOLOWER(*lc))) lc++;
465: anchor->level = HTAtom_for(value);
466: }
2.38 frystyk 467: } else if (!strcasecomp(value, "boundary")) {
468: if ((value = HTNextField(&ptr)) != NULL) {
469: StrAllocCopy(request->boundary, value);
470: }
2.20 frystyk 471: }
472: }
2.1 timbl 473: }
2.20 frystyk 474: state = JUNK_LINE;
2.18 frystyk 475: break;
476:
2.23 frystyk 477: case MIME_DATE:
2.18 frystyk 478: anchor->date = HTParseTime(ptr);
479: state = JUNK_LINE;
480: break;
481:
482: case DERIVED_FROM:
483: if ((value = HTNextField(&ptr)) != NULL)
484: StrAllocCopy(anchor->derived_from, value);
485: state = JUNK_LINE;
486: break;
487:
488: case EXPIRES:
489: anchor->expires = HTParseTime(ptr);
490: state = JUNK_LINE;
491: break;
492:
493: case LAST_MODIFIED:
494: anchor->last_modified = HTParseTime(ptr);
495: state = JUNK_LINE;
496: break;
497:
498: case LINK:
2.20 frystyk 499: state = UNKNOWN; /* @@@@@@@@@@@ */
2.18 frystyk 500: break;
501:
502: case LOCATION:
2.46 frystyk 503: request->redirectionAnchor = HTAnchor_findAddress(HTStrip(ptr));
2.18 frystyk 504: state = JUNK_LINE;
505: break;
506:
507: case PUBLIC_METHODS:
2.20 frystyk 508: state = UNKNOWN; /* @@@@@@@@@@@ */
2.18 frystyk 509: break;
510:
511: case RETRY_AFTER:
2.19 frystyk 512: request->retry_after = HTParseTime(ptr);
513: state = JUNK_LINE;
2.18 frystyk 514: break;
515:
516: case TITLE: /* Can't reuse buffer as HTML version might differ */
517: if ((value = HTNextField(&ptr)) != NULL)
518: StrAllocCopy(anchor->title, value);
519: state = JUNK_LINE;
520: break;
521:
522: case URI_HEADER:
523: state = LOCATION; /* @@@ Need extended parsing */
524: break;
525:
526: case VERSION:
527: if ((value = HTNextField(&ptr)) != NULL)
528: StrAllocCopy(anchor->version, value);
529: state = JUNK_LINE;
530: break;
531:
532: case UNKNOWN:
2.40 frystyk 533: {
2.36 frystyk 534: HTList * list;
535: HTParserCallback *cbf;
536: int status;
537: BOOL override;
538: if (STREAM_TRACE)
2.38 frystyk 539: TTYPrint(TDEST,"MIMEParser.. Unknown `%s\'\n", header);
2.36 frystyk 540: if ((list = HTRequest_parser(request, &override)) &&
2.40 frystyk 541: (cbf = HTParser_find(list, header)) &&
542: (status = (*cbf)(request, header) != HT_OK)) {
2.36 frystyk 543: return status;
544: } else if (!override &&
545: (list = HTHeader_parser()) &&
2.40 frystyk 546: (cbf = HTParser_find(list, header)) &&
547: (status = (*cbf)(request, header) != HT_OK)) {
2.36 frystyk 548: return status;
549: }
550: }
2.18 frystyk 551:
552: case JUNK_LINE:
553: while (*ptr) ptr++;
554: state = BEGINNING_OF_LINE;
555: break;
2.1 timbl 556: }
2.18 frystyk 557: }
2.48 frystyk 558: me->transparent = YES; /* Pump rest of data right through */
2.27 frystyk 559:
2.48 frystyk 560: /* If this request us a source in PostWeb then pause here */
561: if (me->head_only || HTRequest_isSource(request)) return HT_PAUSE;
2.47 frystyk 562:
2.48 frystyk 563: /* If HEAD method then we just stop here */
2.47 frystyk 564: if (request->method == METHOD_HEAD) return HT_LOADED;
2.43 frystyk 565:
2.35 frystyk 566: /* News server almost never send content type or content length */
567: if (anchor->content_type != WWW_UNKNOWN || me->nntp) {
2.48 frystyk 568: if (STREAM_TRACE) TTYPrint(TDEST, "MIMEParser.. Convert %s to %s\n",
569: HTAtom_name(anchor->content_type),
570: HTAtom_name(me->target_format));
571: me->target = HTStreamStack(anchor->content_type, me->target_format,
572: me->target, request, YES);
2.18 frystyk 573: }
2.49 ! frystyk 574: if (!me->target) me->target = HTErrorStream();
2.27 frystyk 575: return HT_OK;
2.1 timbl 576: }
577:
578:
2.18 frystyk 579: /*
580: ** Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
581: ** Folding is either of CF LWS, LF LWS, CRLF LWS
582: */
2.36 frystyk 583: PRIVATE int HTMIME_put_block (HTStream * me, CONST char * b, int l)
2.18 frystyk 584: {
585: while (!me->transparent && l-- > 0) {
586: if (me->EOLstate == EOL_FCR) {
2.27 frystyk 587: if (*b == CR) { /* End of header */
2.32 frystyk 588: int status = parseheader(me, me->request, me->anchor);
2.46 frystyk 589: HTNet_setBytesRead(me->net, l);
2.27 frystyk 590: if (status != HT_OK)
591: return status;
592: } else if (*b == LF) /* CRLF */
2.18 frystyk 593: me->EOLstate = EOL_FLF;
594: else if (WHITE(*b)) { /* Folding: CR SP */
595: me->EOLstate = EOL_BEGIN;
2.44 frystyk 596: HTChunk_putc(me->buffer, ' ');
2.18 frystyk 597: } else { /* New line */
598: me->EOLstate = EOL_BEGIN;
2.44 frystyk 599: HTChunk_putc(me->buffer, '\0');
2.48 frystyk 600: HTChunk_putb(me->buffer, b, 1);
2.18 frystyk 601: }
602: } else if (me->EOLstate == EOL_FLF) {
603: if (*b == CR) /* LF CR or CR LF CR */
604: me->EOLstate = EOL_SCR;
2.27 frystyk 605: else if (*b == LF) { /* End of header */
2.32 frystyk 606: int status = parseheader(me, me->request, me->anchor);
2.46 frystyk 607: HTNet_setBytesRead(me->net, l);
2.27 frystyk 608: if (status != HT_OK)
609: return status;
610: } else if (WHITE(*b)) { /* Folding: LF SP or CR LF SP */
2.18 frystyk 611: me->EOLstate = EOL_BEGIN;
2.44 frystyk 612: HTChunk_putc(me->buffer, ' ');
2.18 frystyk 613: } else { /* New line */
614: me->EOLstate = EOL_BEGIN;
2.44 frystyk 615: HTChunk_putc(me->buffer, '\0');
2.48 frystyk 616: HTChunk_putb(me->buffer, b, 1);
2.18 frystyk 617: }
618: } else if (me->EOLstate == EOL_SCR) {
2.27 frystyk 619: if (*b==CR || *b==LF) { /* End of header */
2.32 frystyk 620: int status = parseheader(me, me->request, me->anchor);
2.46 frystyk 621: HTNet_setBytesRead(me->net, l);
2.27 frystyk 622: if (status != HT_OK)
623: return status;
624: } else if (WHITE(*b)) { /* Folding: LF CR SP or CR LF CR SP */
2.18 frystyk 625: me->EOLstate = EOL_BEGIN;
2.44 frystyk 626: HTChunk_putc(me->buffer, ' ');
2.18 frystyk 627: } else { /* New line */
628: me->EOLstate = EOL_BEGIN;
2.44 frystyk 629: HTChunk_putc(me->buffer, '\0');
2.48 frystyk 630: HTChunk_putb(me->buffer, b, 1);
2.18 frystyk 631: }
632: } else if (*b == CR) {
633: me->EOLstate = EOL_FCR;
634: } else if (*b == LF) {
635: me->EOLstate = EOL_FLF; /* Line found */
636: } else
2.48 frystyk 637: HTChunk_putb(me->buffer, b, 1);
2.18 frystyk 638: b++;
639: }
2.32 frystyk 640:
641: /*
642: ** Put the rest down the stream without touching the data but make sure
643: ** that we get the correct content length of data
644: */
2.48 frystyk 645: if (me->transparent) {
2.47 frystyk 646: int status = (*me->target->isa->put_block)(me->target, b, l);
2.48 frystyk 647: if (status == HT_OK) {
2.47 frystyk 648: /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
2.48 frystyk 649: long cl = HTAnchor_length(me->anchor);
650: return (cl>=0 && HTNet_bytesRead(me->net)>=cl) ? HT_LOADED : HT_OK;
651: } else
2.47 frystyk 652: return status;
2.48 frystyk 653: }
654: return HT_OK;
2.18 frystyk 655: }
656:
657:
658: /* Character handling
659: ** ------------------
660: */
2.36 frystyk 661: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18 frystyk 662: {
663: return HTMIME_put_block(me, &c, 1);
664: }
665:
2.1 timbl 666:
667: /* String handling
668: ** ---------------
669: */
2.36 frystyk 670: PRIVATE int HTMIME_put_string (HTStream * me, CONST char * s)
2.1 timbl 671: {
2.18 frystyk 672: return HTMIME_put_block(me, s, (int) strlen(s));
2.1 timbl 673: }
674:
675:
2.18 frystyk 676: /* Flush an stream object
677: ** ---------------------
2.1 timbl 678: */
2.36 frystyk 679: PRIVATE int HTMIME_flush (HTStream * me)
2.1 timbl 680: {
2.47 frystyk 681: return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1 timbl 682: }
683:
2.18 frystyk 684: /* Free a stream object
685: ** --------------------
2.1 timbl 686: */
2.36 frystyk 687: PRIVATE int HTMIME_free (HTStream * me)
2.1 timbl 688: {
2.18 frystyk 689: int status = HT_OK;
2.25 frystyk 690: if (me->target) {
691: if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
692: return HT_WOULD_BLOCK;
693: }
2.26 frystyk 694: if (PROT_TRACE)
2.37 frystyk 695: TTYPrint(TDEST, "MIME........ FREEING....\n");
2.44 frystyk 696: HTChunk_delete(me->buffer);
2.1 timbl 697: free(me);
2.18 frystyk 698: return status;
2.1 timbl 699: }
700:
701: /* End writing
702: */
2.38 frystyk 703: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1 timbl 704: {
2.18 frystyk 705: int status = HT_ERROR;
2.41 frystyk 706: if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26 frystyk 707: if (PROT_TRACE)
2.37 frystyk 708: TTYPrint(TDEST, "MIME........ ABORTING...\n");
2.44 frystyk 709: HTChunk_delete(me->buffer);
2.6 timbl 710: free(me);
2.18 frystyk 711: return status;
2.1 timbl 712: }
713:
714:
715:
716: /* Structured Object Class
717: ** -----------------------
718: */
2.6 timbl 719: PRIVATE CONST HTStreamClass HTMIME =
2.1 timbl 720: {
721: "MIMEParser",
2.18 frystyk 722: HTMIME_flush,
2.1 timbl 723: HTMIME_free,
2.6 timbl 724: HTMIME_abort,
725: HTMIME_put_character,
726: HTMIME_put_string,
2.18 frystyk 727: HTMIME_put_block
2.1 timbl 728: };
729:
730:
2.48 frystyk 731: /* MIME header parser stream.
2.1 timbl 732: ** -------------------------
2.48 frystyk 733: ** This stream parses a complete MIME header and if a content type header
734: ** is found then the stream stack is called. Any left over data is pumped
735: ** right through the stream
2.1 timbl 736: */
2.36 frystyk 737: PUBLIC HTStream* HTMIMEConvert (HTRequest * request,
738: void * param,
739: HTFormat input_format,
740: HTFormat output_format,
741: HTStream * output_stream)
2.1 timbl 742: {
743: HTStream* me;
2.18 frystyk 744: if ((me=(HTStream *) calloc(1, sizeof(* me))) == NULL)
745: outofmem(__FILE__, "HTMIMEConvert");
2.1 timbl 746: me->isa = &HTMIME;
2.18 frystyk 747: me->request = request;
2.32 frystyk 748: me->anchor = request->anchor;
749: me->net = request->net;
2.49 ! frystyk 750: me->target = output_stream;
2.18 frystyk 751: me->target_format = output_format;
2.44 frystyk 752: me->buffer = HTChunk_new(512);
2.18 frystyk 753: me->EOLstate = EOL_BEGIN;
2.1 timbl 754: return me;
755: }
2.32 frystyk 756:
2.48 frystyk 757: /* MIME header ONLY parser stream
758: ** ------------------------------
759: ** This stream parses a complete MIME header and then returnes HT_PAUSE.
760: ** It does not set up any streams and resting data stays in the buffer.
761: ** This can be used if you only want to parse the headers before you
762: ** decide what to do next. This is for example the case in a server app.
763: */
764: PUBLIC HTStream * HTMIMEHeader (HTRequest * request,
765: void * param,
766: HTFormat input_format,
767: HTFormat output_format,
768: HTStream * output_stream)
769: {
770: HTStream * me;
771: if ((me = (HTStream *) calloc(1, sizeof(HTStream))) == NULL)
772: outofmem(__FILE__, "HTMIMEConvert");
773: me->isa = &HTMIME;
774: me->request = request;
775: me->anchor = request->anchor;
776: me->net = request->net;
2.49 ! frystyk 777: me->target = output_stream;
2.48 frystyk 778: me->target_format = output_format;
779: me->buffer = HTChunk_new(512);
780: me->EOLstate = EOL_BEGIN;
781: me->head_only = YES; /* We want to pause after header */
782: return me;
783: }
Webmaster