Annotation of libwww/Library/src/HTSChunk.c, revision 2.6

2.1       frystyk     1: /*
                      2: **     STREAM TO CHUNK CONVERTER
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995.
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.6     ! frystyk     6: **     @(#) $Id: HTSChunk.c,v 2.5 1998/02/27 18:35:52 frystyk Exp $
2.1       frystyk     7: **
                      8: **     This stream converts a stream to a chunk object.
                      9: */
                     10: 
                     11: /* Library include files */
2.6     ! frystyk    12: #include "wwwsys.h"
2.1       frystyk    13: #include "WWWUtil.h"
                     14: #include "WWWCore.h"
                     15: #include "HTSChunk.h"                                   /* Implemented here */
                     16: 
                     17: #define HT_MAXSIZE     0x10000               /* Max size of allocated chunk */
2.4       frystyk    18: #define HT_MAXGROWSIZE 0x4000           /* Increment buffer by no more than */
2.1       frystyk    19: 
                     20: struct _HTStream {
                     21:     HTStreamClass *    isa;
                     22:     HTRequest *                request;
                     23:     HTChunk *          chunk;
                     24: 
                     25:     int                        cur_size;
                     26:     int                        max_size;
                     27:     BOOL               give_up;
2.3       frystyk    28:     BOOL               ignore;
2.5       frystyk    29:     BOOL               ensure;
2.1       frystyk    30: };
                     31: 
                     32: /* ------------------------------------------------------------------------- */
                     33: 
                     34: PRIVATE int HTSC_flush (HTStream * me)
                     35: {
                     36:     return HT_OK;                      /* Nothing to flush */
                     37: }
                     38: 
                     39: /*
                     40: **     We don't free the chunk as this is for the caller to do
                     41: */
                     42: PRIVATE int HTSC_free (HTStream * me)
                     43: {
                     44:     if (STREAM_TRACE) HTTrace("Chunkstream. FREEING...\n");
                     45:     HT_FREE(me);
                     46:     return HT_OK;
                     47: }
                     48: 
                     49: /*
                     50: **     We don't free the chunk as this is for the caller to do
                     51: */
                     52: PRIVATE int HTSC_abort (HTStream * me, HTList * errorlist)
                     53: {
                     54:     if (STREAM_TRACE) HTTrace("Chunkstream. ABORTING...\n");
                     55:     HT_FREE(me);
                     56:     return HT_ERROR;
                     57: }
                     58: 
                     59: PRIVATE int HTSC_putBlock (HTStream * me, const char * b, int l)
                     60: {
                     61:     me->cur_size += l;
2.2       frystyk    62: 
                     63:     /*
2.4       frystyk    64:     ** If we get a buffer overflow and we are going to PUT or POST the document
                     65:     ** then ask the user whether it is OK to proceed buffering. Otherwise we
                     66:     ** must give up the request. In all other cases we stop if the buffer fills
                     67:     ** up.
2.2       frystyk    68:     */
2.5       frystyk    69:     if (!me->ignore && me->max_size > 0 && me->cur_size > me->max_size) {
2.4       frystyk    70:        HTMethod method = HTRequest_method(me->request);
                     71:        if (HTMethod_hasEntity(method)) {
                     72:            HTAlertCallback *cbf = HTAlert_find(HT_A_CONFIRM);
                     73:            if ((cbf && (*cbf)(me->request, HT_A_CONFIRM, HT_MSG_BIG_PUT,
                     74:                               NULL, NULL, NULL)))
                     75:                me->ignore = YES;
                     76:            else
                     77:                me->give_up = YES;
                     78:        } else {
2.2       frystyk    79:            me->give_up = YES;
2.4       frystyk    80:        }
2.5       frystyk    81:     } else if (!me->ensure) {
                     82:        HTParentAnchor * anchor = HTRequest_anchor(me->request);
                     83:        int cl = HTAnchor_length(anchor);
                     84:        if (cl > 0) HTChunk_ensure(me->chunk, cl);
                     85:        me->ensure = YES;
2.2       frystyk    86:     }
2.1       frystyk    87:     if (!me->give_up) {
                     88:        HTChunk_putb(me->chunk, b, l);
                     89:        return HT_OK;
2.2       frystyk    90:     }    
2.1       frystyk    91:     return HT_ERROR;
                     92: }
                     93: 
                     94: PRIVATE int HTSC_putCharacter (HTStream * me, char c)
                     95: {
                     96:     return HTSC_putBlock(me, &c, 1);
                     97: }
                     98: 
                     99: PRIVATE int HTSC_putString (HTStream * me, const char * s)
                    100: {
                    101:     return HTSC_putBlock(me, s, (int) strlen(s));
                    102: }
                    103: 
                    104: HTStreamClass HTStreamToChunkClass = {
                    105:     "StreamToChunk",
                    106:     HTSC_flush,
                    107:     HTSC_free,
                    108:     HTSC_abort,
                    109:     HTSC_putCharacter,
                    110:     HTSC_putString,
                    111:     HTSC_putBlock
                    112: };
                    113: 
                    114: PUBLIC HTStream * HTStreamToChunk (HTRequest *         request,
                    115:                                   HTChunk **   chunk,
                    116:                                   int          max_size)
                    117: {
                    118:     if (request) {
                    119:        HTStream * me;
                    120:        *chunk = NULL;
                    121:        if ((me = (HTStream  *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
                    122:            HT_OUTOFMEM("HTStreamToChunk");
                    123:        me->isa = &HTStreamToChunkClass;
                    124:        me->request = request;
2.5       frystyk   125:        me->max_size = (!max_size) ? max_size : HT_MAXSIZE;
                    126:        me->chunk = *chunk = HTChunk_new(me->max_size > 0 ?
                    127:                                         HTMIN(me->max_size, HT_MAXGROWSIZE) :
                    128:                                         HT_MAXGROWSIZE);
2.1       frystyk   129:        if (STREAM_TRACE)
2.4       frystyk   130:            HTTrace("ChunkStream. Chunk %p created with max size %d\n",
                    131:                    me->chunk, me->max_size);
2.1       frystyk   132:        return me;
                    133:     }
                    134:     return HTErrorStream();
                    135: }
                    136: 

Webmaster