Annotation of libwww/Library/src/HTMIME.c, revision 2.87
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.87 ! frystyk 6: ** @(#) $Id: HTMIME.c,v 2.86 1998/05/04 19:36:52 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.71 frystyk 17: ** 14 Mar 95 HFN Now using response for storing data. No more `\n',
2.18 frystyk 18: ** static buffers etc.
2.1 timbl 19: */
2.17 frystyk 20:
21: /* Library include files */
2.86 frystyk 22: #include "wwwsys.h"
2.60 frystyk 23: #include "WWWUtil.h"
2.61 frystyk 24: #include "WWWCore.h"
2.70 frystyk 25: #include "WWWCache.h"
26: #include "WWWStream.h"
2.61 frystyk 27: #include "HTReqMan.h"
28: #include "HTNetMan.h"
2.36 frystyk 29: #include "HTHeader.h"
2.64 eric 30: #include "HTWWWStr.h"
2.14 frystyk 31: #include "HTMIME.h" /* Implemented here */
2.1 timbl 32:
2.64 eric 33: #define MIME_HASH_SIZE 101
34:
2.70 frystyk 35: typedef enum _HTMIMEMode {
36: HT_MIME_HEADER = 0x1,
2.71 frystyk 37: HT_MIME_FOOTER = 0x2,
2.77 frystyk 38: HT_MIME_PARTIAL = 0x4,
39: HT_MIME_CONT = 0x8
2.70 frystyk 40: } HTMIMEMode;
41:
2.1 timbl 42: struct _HTStream {
2.57 frystyk 43: const HTStreamClass * isa;
2.18 frystyk 44: HTRequest * request;
2.71 frystyk 45: HTResponse * response;
2.32 frystyk 46: HTNet * net;
2.18 frystyk 47: HTStream * target;
48: HTFormat target_format;
2.64 eric 49: HTChunk * token;
50: HTChunk * value;
51: int hash;
2.59 frystyk 52: HTEOLState EOLstate;
2.70 frystyk 53: HTMIMEMode mode;
2.18 frystyk 54: BOOL transparent;
2.64 eric 55: BOOL haveToken;
2.78 frystyk 56: BOOL hasBody;
2.1 timbl 57: };
58:
2.18 frystyk 59: /* ------------------------------------------------------------------------- */
2.1 timbl 60:
2.64 eric 61: PRIVATE int pumpData (HTStream * me)
2.18 frystyk 62: {
2.64 eric 63: HTRequest * request = me->request;
2.71 frystyk 64: HTResponse * response = me->response;
65: HTFormat format = HTResponse_format(response);
2.83 frystyk 66: HTList * te = HTResponse_transfer(response);
67: HTList * ce = HTResponse_encoding(response);
2.71 frystyk 68: long length = HTResponse_length(response);
2.83 frystyk 69: HTStream * BlackHole = HTBlackHole();
70: BOOL savestream = NO;
2.48 frystyk 71: me->transparent = YES; /* Pump rest of data right through */
2.27 frystyk 72:
2.71 frystyk 73: /* If this request is a source in PostWeb then pause here */
2.66 frystyk 74: if (HTRequest_isSource(request)) return HT_PAUSE;
2.47 frystyk 75:
2.71 frystyk 76: /*
2.77 frystyk 77: ** Cache the metainformation in the anchor object by copying
2.71 frystyk 78: ** it from the response object. This we do regardless if
79: ** we have a persistent cache or not as the memory cache will
80: ** use it as well. If we are updating a cache entry using
2.77 frystyk 81: ** byte ranges then we already have the metainformation and
2.71 frystyk 82: ** hence we can ignore the new one as it'd better be the same.
83: */
2.77 frystyk 84: if (!(me->mode & HT_MIME_PARTIAL) && HTResponse_isCachable(me->response))
2.71 frystyk 85: HTAnchor_update(HTRequest_anchor(request), me->response);
86:
87: /*
88: ** If we asked only to read the header or footer or we used a HEAD
89: ** method then we stop here as we don't expect any body part.
90: */
2.70 frystyk 91: if (me->mode & (HT_MIME_HEADER | HT_MIME_FOOTER) ||
2.71 frystyk 92: HTRequest_method(request) == METHOD_HEAD) {
2.78 frystyk 93: return HT_LOADED;
2.70 frystyk 94: }
2.43 frystyk 95:
2.60 frystyk 96: /*
2.77 frystyk 97: ** If we are paring a 1xx response then return HT_CONTINUE
98: */
99: if (me->mode & HT_MIME_CONT)
100: return HT_CONTINUE;
101:
102: /*
2.71 frystyk 103: ** If there is no content-length, no transfer encoding and no
104: ** content type then we assume that there is no body part in
105: ** the message and we can return HT_LOADED
2.68 frystyk 106: */
2.79 frystyk 107: {
108: HTHost * host = HTNet_host(me->net);
2.84 frystyk 109: if (length<0 && te==NULL &&
2.79 frystyk 110: HTHost_isPersistent(host) && !HTHost_closeNotification(host)) {
2.81 frystyk 111: if (format != WWW_UNKNOWN) {
112: if (STREAM_TRACE) HTTrace("MIME Parser. BAD - there seems to be a body but no length. This must be an HTTP/1.0 server pretending that it is HTTP/1.1\n");
113: HTHost_setCloseNotification(host, YES);
114: } else {
115: if (STREAM_TRACE) HTTrace("MIME Parser. No body in this messsage\n");
116: return HT_LOADED;
117: }
2.79 frystyk 118: }
2.68 frystyk 119: }
120:
121: /*
2.78 frystyk 122: ** Deal with the body
123: */
124: me->hasBody = YES;
125:
126: /*
2.71 frystyk 127: ** Handle any Content Type
2.60 frystyk 128: */
2.71 frystyk 129: if (!(me->mode & HT_MIME_PARTIAL) &&
2.83 frystyk 130: (format != WWW_UNKNOWN || length > 0 || te)) {
131: HTStream * target;
2.71 frystyk 132: if (STREAM_TRACE) HTTrace("Building.... C-T stack from %s to %s\n",
133: HTAtom_name(format),
134: HTAtom_name(me->target_format));
2.83 frystyk 135: if ((target = HTStreamStack(format, me->target_format,
136: me->target, request, YES))==BlackHole) {
137: if (!savestream) {
138: if (me->target) (*me->target->isa->abort)(me->target, NULL);
139: me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
140: savestream = YES;
141: }
142: } else
143: me->target = target;
2.18 frystyk 144: }
2.60 frystyk 145:
2.71 frystyk 146: /*
147: ** Can we cache the data object? If so then create a T stream and hook it
148: ** into the stream pipe. We do it before the transfer decoding so that we
149: ** don't have to deal with that when we retrieve the object from cache.
150: ** If we are appending to a cache entry then use a different stream than
151: ** if creating a new entry.
152: */
2.83 frystyk 153: if (!savestream && HTCacheMode_enabled()) {
2.71 frystyk 154: if (me->mode & HT_MIME_PARTIAL) {
155: HTStream * append = HTStreamStack(WWW_CACHE_APPEND,
156: me->target_format,
157: me->target, request, NO);
158: #if 0
159: if (cache) me->target = HTTee(me->target, cache, NULL);
160: me->target = HTPipeBuffer_new(me->target, request, 0);
161: #else
162: me->target = append;
163: #endif
164: } else if (HTResponse_isCachable(me->response)) {
165: HTStream * cache = HTStreamStack(WWW_CACHE, me->target_format,
166: me->target, request, NO);
167: if (cache) me->target = HTTee(me->target, cache, NULL);
168: }
2.70 frystyk 169: }
2.76 frystyk 170:
171: /*
2.83 frystyk 172: ** Handle any Content Encodings
2.76 frystyk 173: */
2.83 frystyk 174: if (STREAM_TRACE) HTTrace("Building.... Content-Decoding stack\n");
175: if (ce) {
176: HTStream * target = HTContentDecodingStack(ce, me->target, request, NULL);
177: if (target == BlackHole) {
178: if (!savestream) {
179: if (me->target) (*me->target->isa->abort)(me->target, NULL);
180: me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
181: savestream = YES;
182: }
183: } else
184: me->target = target;
2.76 frystyk 185: }
2.70 frystyk 186:
2.71 frystyk 187: /*
2.83 frystyk 188: ** Handle any Transfer Encodings
2.71 frystyk 189: */
2.83 frystyk 190: if (STREAM_TRACE) HTTrace("Building.... Transfer-Decoding stack\n");
191: if (te) {
192: HTStream * target = HTTransferDecodingStack(te, me->target, request, NULL);
193: if (target == BlackHole) {
194: if (!savestream) {
195: if (me->target) (*me->target->isa->abort)(me->target, NULL);
196: me->target = HTSaveLocally(request, NULL, NULL, NULL, NULL);
197: savestream = YES;
198: }
199: } else
200: me->target = target;
2.61 frystyk 201: }
2.71 frystyk 202:
2.85 frystyk 203:
204: /*
205: ** If we for some reason couldn't find a target stream
206: */
207: if (!me->target) me->target = HTBlackHole();
2.27 frystyk 208: return HT_OK;
2.1 timbl 209: }
210:
2.65 eric 211: /* _dispatchParsers - call request's MIME header parser.
212: ** Use global parser if no appropriate one is found for request.
213: */
214: PRIVATE int _dispatchParsers (HTStream * me)
215: {
216: int status;
217: char * token = HTChunk_data(me->token);
218: char * value = HTChunk_data(me->value);
2.71 frystyk 219: BOOL found = NO;
220: BOOL local = NO;
2.65 eric 221: HTMIMEParseSet * parseSet;
222:
223: /* In case we get an empty header consisting of a CRLF, we fall thru */
2.66 frystyk 224: if (STREAM_TRACE) HTTrace("MIME header. %s: %s\n",
225: token ? token : "<null>",
226: value ? value : "<null>");
227: if (!token) return HT_OK; /* Ignore noop token */
2.65 eric 228:
2.70 frystyk 229: /*
2.71 frystyk 230: ** Remember the original header
231: */
232: HTResponse_addHeader(me->response, token, value);
233:
234: /*
2.70 frystyk 235: ** Search the local set of MIME parsers
236: */
2.65 eric 237: if ((parseSet = HTRequest_MIMEParseSet(me->request, &local)) != NULL) {
238: status = HTMIMEParseSet_dispatch(parseSet, me->request,
2.71 frystyk 239: token, value, &found);
240: if (found) return status;
2.65 eric 241: }
242:
2.70 frystyk 243: /*
244: ** Search the global set of MIME parsers
245: */
2.71 frystyk 246: if (local==NO && (parseSet = HTHeader_MIMEParseSet()) != NULL) {
247: status = HTMIMEParseSet_dispatch(parseSet, me->request,
248: token, value, &found);
249: if (found) return status;
250: }
251:
2.65 eric 252: return HT_OK;
253: }
254:
2.18 frystyk 255: /*
256: ** Header is terminated by CRCR, LFLF, CRLFLF, CRLFCRLF
257: ** Folding is either of CF LWS, LF LWS, CRLF LWS
258: */
2.57 frystyk 259: PRIVATE int HTMIME_put_block (HTStream * me, const char * b, int l)
2.18 frystyk 260: {
2.57 frystyk 261: const char * start = b;
262: const char * end = start;
2.64 eric 263: const char * value = me->value->size ? b : NULL;
2.73 frystyk 264: int length = l;
2.64 eric 265: int status;
266:
267: while (!me->transparent) {
2.18 frystyk 268: if (me->EOLstate == EOL_FCR) {
2.64 eric 269: if (*b == CR) /* End of header */
270: me->EOLstate = EOL_END;
271: else if (*b == LF) /* CRLF */
2.18 frystyk 272: me->EOLstate = EOL_FLF;
2.80 frystyk 273: else if (isspace((int) *b)) /* Folding: CR SP */
2.64 eric 274: me->EOLstate = EOL_FOLD;
275: else /* New line */
276: me->EOLstate = EOL_LINE;
2.18 frystyk 277: } else if (me->EOLstate == EOL_FLF) {
278: if (*b == CR) /* LF CR or CR LF CR */
279: me->EOLstate = EOL_SCR;
2.64 eric 280: else if (*b == LF) /* End of header */
281: me->EOLstate = EOL_END;
2.80 frystyk 282: else if (isspace((int) *b)) /* Folding: LF SP or CR LF SP */
2.64 eric 283: me->EOLstate = EOL_FOLD;
284: else /* New line */
285: me->EOLstate = EOL_LINE;
286: } else if (me->EOLstate == EOL_SCR) {
287: if (*b==CR || *b==LF) /* End of header */
288: me->EOLstate = EOL_END;
2.80 frystyk 289: else if (isspace((int) *b)) /* Folding: LF CR SP or CR LF CR SP */
2.64 eric 290: me->EOLstate = EOL_FOLD;
291: else /* New line */
292: me->EOLstate = EOL_LINE;
293: } else if (*b == CR)
294: me->EOLstate = EOL_FCR;
295: else if (*b == LF)
296: me->EOLstate = EOL_FLF; /* Line found */
297: else {
298: if (!me->haveToken) {
2.80 frystyk 299: if (*b == ':' || isspace((int) *b)) {
2.64 eric 300: HTChunk_putb(me->token, start, end-start);
301: HTChunk_putc(me->token, '\0');
302: me->haveToken = YES;
303: } else {
304: unsigned char ch = *(unsigned char *) b;
2.73 frystyk 305: ch = tolower(ch);
2.64 eric 306: me->hash = (me->hash * 3 + ch) % MIME_HASH_SIZE;
307: }
2.80 frystyk 308: } else if (value == NULL && *b != ':' && !isspace((int) *b))
2.64 eric 309: value = b;
310: end++;
311: }
312: switch (me->EOLstate) {
313: case EOL_LINE:
2.73 frystyk 314: case EOL_END:
315: {
2.78 frystyk 316: int ret = HT_ERROR;
2.64 eric 317: HTChunk_putb(me->value, value, end-value);
318: HTChunk_putc(me->value, '\0');
2.75 frystyk 319: ret = _dispatchParsers(me);
2.73 frystyk 320: HTNet_addBytesRead(me->net, b-start);
2.64 eric 321: start=b, end=b;
322: if (me->EOLstate == EOL_END) { /* EOL_END */
2.75 frystyk 323: if (ret == HT_OK) {
2.67 frystyk 324: b++, l--;
2.78 frystyk 325: ret = pumpData(me);
2.73 frystyk 326: HTNet_addBytesRead(me->net, 1);
2.82 frystyk 327: if (me->mode & (HT_MIME_FOOTER | HT_MIME_CONT)) {
2.73 frystyk 328: HTHost_setConsumed(HTNet_host(me->net), length - l);
2.75 frystyk 329: return ret;
2.78 frystyk 330: } else {
331: HTNet_setHeaderLength(me->net, HTNet_bytesRead(me->net));
332: }
2.67 frystyk 333: }
2.64 eric 334: } else { /* EOL_LINE */
335: HTChunk_clear(me->token);
336: HTChunk_clear(me->value);
337: me->haveToken = NO;
338: me->hash = 0;
339: value = NULL;
340: }
2.18 frystyk 341: me->EOLstate = EOL_BEGIN;
2.78 frystyk 342: if (ret != HT_OK && ret != HT_LOADED) return ret;
2.64 eric 343: break;
2.73 frystyk 344: }
2.64 eric 345: case EOL_FOLD:
2.18 frystyk 346: me->EOLstate = EOL_BEGIN;
2.64 eric 347: if (!me->haveToken) {
348: HTChunk_putb(me->token, start, end-start);
349: HTChunk_putc(me->token, '\0');
350: me->haveToken = YES;
351: } else if (value) {
352: HTChunk_putb(me->value, value, end-value);
353: HTChunk_putc(me->value, ' ');
354: }
355: start=b, end=b;
356: break;
357: default:
2.73 frystyk 358: b++, l--;
2.64 eric 359: if (!l) {
2.87 ! frystyk 360: BOOL stop = NO;
! 361: if (!me->haveToken) {
! 362: /* If empty header then prepare to stop */
! 363: if (end-start)
! 364: HTChunk_putb(me->token, start, end-start);
! 365: else
! 366: stop = YES;
! 367: } else if (value)
2.64 eric 368: HTChunk_putb(me->value, value, end-value);
2.78 frystyk 369: HTHost_setConsumed(HTNet_host(me->net), length - l);
2.87 ! frystyk 370: return stop ? pumpData(me) : HT_OK;
2.64 eric 371: }
372: }
2.18 frystyk 373: }
2.32 frystyk 374:
2.78 frystyk 375: if (length != l) HTHost_setConsumed(HTNet_host(me->net), length - l);
376:
2.32 frystyk 377: /*
378: ** Put the rest down the stream without touching the data but make sure
2.73 frystyk 379: ** that we get the correct content length of data. If we have a CL in
380: ** the headers then this stream is responsible for the accountance.
2.32 frystyk 381: */
2.82 frystyk 382: if (me->hasBody) {
2.73 frystyk 383: HTNet * net = me->net;
2.66 frystyk 384: /* Check if CL at all - thanks to jwei@hal.com (John Wei) */
2.73 frystyk 385: long cl = HTResponse_length(me->response);
2.82 frystyk 386: if (cl >= 0) {
2.73 frystyk 387: long bodyRead = HTNet_bytesRead(net) - HTNet_headerLength(net);
388:
389: /*
390: ** If we have more than we need then just take what belongs to us.
391: */
392: if (bodyRead + l >= cl) {
393: int consume = cl - bodyRead;
394: if ((status = (*me->target->isa->put_block)(me->target, b, consume)) < 0)
395: return status;
396: HTNet_addBytesRead(net, consume);
2.78 frystyk 397: HTHost_setConsumed(HTNet_host(net), consume);
2.73 frystyk 398: return HT_LOADED;
399: } else {
400: if ((status = (*me->target->isa->put_block)(me->target, b, l)) < 0)
2.78 frystyk 401: return status;
2.73 frystyk 402: HTNet_addBytesRead(net, l);
2.78 frystyk 403: HTHost_setConsumed(HTNet_host(net), l);
2.73 frystyk 404: return status;
405: }
2.78 frystyk 406: }
2.73 frystyk 407: return (*me->target->isa->put_block)(me->target, b, l);
2.66 frystyk 408: }
409: return HT_LOADED;
2.18 frystyk 410: }
411:
412:
413: /* Character handling
414: ** ------------------
415: */
2.36 frystyk 416: PRIVATE int HTMIME_put_character (HTStream * me, char c)
2.18 frystyk 417: {
418: return HTMIME_put_block(me, &c, 1);
419: }
420:
2.1 timbl 421:
422: /* String handling
423: ** ---------------
424: */
2.57 frystyk 425: PRIVATE int HTMIME_put_string (HTStream * me, const char * s)
2.1 timbl 426: {
2.18 frystyk 427: return HTMIME_put_block(me, s, (int) strlen(s));
2.1 timbl 428: }
429:
430:
2.18 frystyk 431: /* Flush an stream object
432: ** ---------------------
2.1 timbl 433: */
2.36 frystyk 434: PRIVATE int HTMIME_flush (HTStream * me)
2.1 timbl 435: {
2.47 frystyk 436: return me->target ? (*me->target->isa->flush)(me->target) : HT_OK;
2.1 timbl 437: }
438:
2.18 frystyk 439: /* Free a stream object
440: ** --------------------
2.1 timbl 441: */
2.36 frystyk 442: PRIVATE int HTMIME_free (HTStream * me)
2.1 timbl 443: {
2.18 frystyk 444: int status = HT_OK;
2.64 eric 445: if (!me->transparent)
2.65 eric 446: if (_dispatchParsers(me) == HT_OK)
2.64 eric 447: pumpData(me);
2.25 frystyk 448: if (me->target) {
449: if ((status = (*me->target->isa->_free)(me->target))==HT_WOULD_BLOCK)
450: return HT_WOULD_BLOCK;
451: }
2.26 frystyk 452: if (PROT_TRACE)
2.55 eric 453: HTTrace("MIME........ FREEING....\n");
2.64 eric 454: HTChunk_delete(me->token);
455: HTChunk_delete(me->value);
2.52 frystyk 456: HT_FREE(me);
2.18 frystyk 457: return status;
2.1 timbl 458: }
459:
460: /* End writing
461: */
2.38 frystyk 462: PRIVATE int HTMIME_abort (HTStream * me, HTList * e)
2.1 timbl 463: {
2.18 frystyk 464: int status = HT_ERROR;
2.41 frystyk 465: if (me->target) status = (*me->target->isa->abort)(me->target, e);
2.26 frystyk 466: if (PROT_TRACE)
2.55 eric 467: HTTrace("MIME........ ABORTING...\n");
2.64 eric 468: HTChunk_delete(me->token);
469: HTChunk_delete(me->value);
2.52 frystyk 470: HT_FREE(me);
2.18 frystyk 471: return status;
2.1 timbl 472: }
473:
474:
475:
476: /* Structured Object Class
477: ** -----------------------
478: */
2.57 frystyk 479: PRIVATE const HTStreamClass HTMIME =
2.1 timbl 480: {
481: "MIMEParser",
2.18 frystyk 482: HTMIME_flush,
2.1 timbl 483: HTMIME_free,
2.6 timbl 484: HTMIME_abort,
485: HTMIME_put_character,
486: HTMIME_put_string,
2.18 frystyk 487: HTMIME_put_block
2.1 timbl 488: };
489:
490:
2.48 frystyk 491: /* MIME header parser stream.
2.1 timbl 492: ** -------------------------
2.48 frystyk 493: ** This stream parses a complete MIME header and if a content type header
494: ** is found then the stream stack is called. Any left over data is pumped
495: ** right through the stream
2.1 timbl 496: */
2.36 frystyk 497: PUBLIC HTStream* HTMIMEConvert (HTRequest * request,
498: void * param,
499: HTFormat input_format,
500: HTFormat output_format,
501: HTStream * output_stream)
2.1 timbl 502: {
2.62 frystyk 503: HTStream * me;
2.52 frystyk 504: if ((me = (HTStream *) HT_CALLOC(1, sizeof(* me))) == NULL)
505: HT_OUTOFMEM("HTMIMEConvert");
2.1 timbl 506: me->isa = &HTMIME;
2.18 frystyk 507: me->request = request;
2.71 frystyk 508: me->response = HTRequest_response(request);
2.70 frystyk 509: me->net = HTRequest_net(request);
2.49 frystyk 510: me->target = output_stream;
2.18 frystyk 511: me->target_format = output_format;
2.64 eric 512: me->token = HTChunk_new(256);
513: me->value = HTChunk_new(256);
514: me->hash = 0;
2.18 frystyk 515: me->EOLstate = EOL_BEGIN;
2.64 eric 516: me->haveToken = NO;
2.1 timbl 517: return me;
518: }
2.32 frystyk 519:
2.48 frystyk 520: /* MIME header ONLY parser stream
521: ** ------------------------------
522: ** This stream parses a complete MIME header and then returnes HT_PAUSE.
523: ** It does not set up any streams and resting data stays in the buffer.
524: ** This can be used if you only want to parse the headers before you
525: ** decide what to do next. This is for example the case in a server app.
526: */
527: PUBLIC HTStream * HTMIMEHeader (HTRequest * request,
528: void * param,
529: HTFormat input_format,
530: HTFormat output_format,
531: HTStream * output_stream)
532: {
2.62 frystyk 533: HTStream * me = HTMIMEConvert(request, param, input_format,
534: output_format, output_stream);
2.70 frystyk 535: me->mode |= HT_MIME_HEADER;
2.48 frystyk 536: return me;
537: }
2.77 frystyk 538:
539: PUBLIC HTStream * HTMIMEContinue (HTRequest * request,
540: void * param,
541: HTFormat input_format,
542: HTFormat output_format,
543: HTStream * output_stream)
544: {
545: HTStream * me = HTMIMEConvert(request, param, input_format,
546: output_format, output_stream);
547: me->mode |= HT_MIME_CONT;
548: return me;
549: }
2.62 frystyk 550:
551: /* MIME footer ONLY parser stream
552: ** ------------------------------
553: ** Parse only a footer, for example after a chunked encoding.
554: */
555: PUBLIC HTStream * HTMIMEFooter (HTRequest * request,
556: void * param,
557: HTFormat input_format,
558: HTFormat output_format,
559: HTStream * output_stream)
560: {
561: HTStream * me = HTMIMEConvert(request, param, input_format,
562: output_format, output_stream);
2.70 frystyk 563: me->mode |= HT_MIME_FOOTER;
2.62 frystyk 564: return me;
565: }
2.71 frystyk 566:
2.72 frystyk 567: /*
568: ** A small BEFORE filter that just finds a cache entry unconditionally
569: ** and loads the entry. All freshness and any other constraints are
570: ** ignored.
571: */
572: PRIVATE int HTCacheLoadFilter (HTRequest * request, void * param, int mode)
573: {
574: HTParentAnchor * anchor = HTRequest_anchor(request);
575: HTCache * cache = HTCache_find(anchor);
576: if (STREAM_TRACE) HTTrace("Cache Load.. loading partial cache entry\n");
577: if (cache) {
578: char * name = HTCache_name(cache);
579: HTAnchor_setPhysical(anchor, name);
580: HTCache_addHit(cache);
581: HT_FREE(name);
582: }
583: return HT_OK;
584: }
585:
586: /*
587: ** A small AFTER filter that flushes the PIPE buffer so that we can
588: ** get the rest of the data
589: */
590: PRIVATE int HTCacheFlushFilter (HTRequest * request, HTResponse * response,
591: void * param, int mode)
592: {
593: HTStream * pipe = (HTStream *) param;
594: if (pipe) {
595: if (STREAM_TRACE) HTTrace("Cache Flush. Flushing and freeing PIPE buffer\n");
596: (*pipe->isa->flush)(pipe);
597: (*pipe->isa->_free)(pipe);
598: }
599:
600: /*
601: ** We also delete the request obejct and stop more filters from being called.
602: ** As this is our own request, it's OK to do that
603: */
604: HTRequest_delete(request);
605: return HT_ERROR;
606: }
607:
2.71 frystyk 608: /* Partial Response MIME parser stream
609: ** -----------------------------------
610: ** In case we sent a Range conditional GET we may get back a partial
611: ** response. This response must be appended to the already existing
612: ** cache entry before presented to the user.
613: ** We do this by continuing to load the new object into a temporary
614: ** buffer and at the same time start the cache load of the already
615: ** existing object. When we have loaded the cache we merge the two
616: ** buffers.
617: */
618: PUBLIC HTStream * HTMIMEPartial (HTRequest * request,
619: void * param,
620: HTFormat input_format,
621: HTFormat output_format,
622: HTStream * output_stream)
623: {
624: HTParentAnchor * anchor = HTRequest_anchor(request);
2.72 frystyk 625: HTFormat format = HTAnchor_format(anchor);
626: HTStream * pipe = NULL;
627:
2.71 frystyk 628: /*
629: ** The merge stream is a place holder for where we can put data when it
630: ** arrives. We have two feeds: one from the cache and one from the net.
631: ** We call the stream stack already now to get the right output stream.
632: ** We can do this as we already know the content type from when we got the
633: ** first part of the object.
634: */
2.72 frystyk 635: HTStream * merge = HTMerge(HTStreamStack(format,
636: output_format, output_stream,
637: request, YES), 2);
2.71 frystyk 638:
639: /*
2.72 frystyk 640: ** Now we create the MIME parser stream in partial data mode. We also
641: ** set the target to our merge stream.
2.71 frystyk 642: */
643: HTStream * me = HTMIMEConvert(request, param, input_format,
644: output_format, output_stream);
645: me->mode |= HT_MIME_PARTIAL;
2.72 frystyk 646: me->target = merge;
647:
648: /*
649: ** Create the cache append stream, and a Tee stream
650: */
651: {
652: HTStream * append = HTStreamStack(WWW_CACHE_APPEND, output_format,
653: output_stream, request, NO);
654: if (append) me->target = HTTee(me->target, append, NULL);
655: }
656:
657: /*
658: ** Create the pipe buffer stream to buffer the data that we read
659: ** from the network
660: */
2.74 frystyk 661: if ((pipe = HTPipeBuffer(me->target, 0))) me->target = pipe;
2.71 frystyk 662:
663: /*
664: ** Now start the second load from the cache. First we read this data from
665: ** the cache and then we flush the data that we have read from the net.
666: */
667: {
2.72 frystyk 668: HTRequest * cache_request = HTRequest_new();
2.71 frystyk 669:
2.72 frystyk 670: /*
671: ** Set the output format to source and the output stream to the
672: ** merge stream. As we have already set up the stream pipe, we just
673: ** load it as source.
674: */
675: HTRequest_setOutputFormat(cache_request, WWW_SOURCE);
676: HTRequest_setOutputStream(cache_request, merge);
677:
678: /*
679: ** Bind the anchor to the new request and also register a local
680: ** AFTER filter to flush the pipe buffer so that we can get
681: ** rest of the data through.
682: */
683: HTRequest_setAnchor(cache_request, (HTAnchor *) anchor);
684: HTRequest_addBefore(cache_request, HTCacheLoadFilter, NULL, NULL,
685: HT_FILTER_FIRST, YES);
686: HTRequest_addAfter(cache_request, HTCacheFlushFilter, NULL, pipe,
687: HT_ALL, HT_FILTER_FIRST, YES);
2.71 frystyk 688:
2.72 frystyk 689: if (STREAM_TRACE) HTTrace("Partial..... Starting cache load\n");
690: HTLoad(cache_request, NO);
2.71 frystyk 691: }
692: return me;
693: }
694:
Webmaster