Ignore:
Timestamp:
Mar 27, 2009 4:54:09 PM (14 years ago)
Author:
gah
Message:

Begin reorganizing Rappture C++ API, starting with the use of
Rappture::Outcome.

Instead of returning an Outcome and expecting every routine to
save it, pass it as a reference. Let's strip it down to what's
needed right now and add back functionality as required.

Right now too many return values are ignored in the rappture library.
We need to check all results and return the error messages via
the Outcome. At the topmost level where is touches the developer
API we can drop the error checking or turn it on via an environment
variable.

Example. Not enough checks are made for memory allocation failure.
If the application is running on an over-committed server memory
allocation errors will be silently passed on. It's okay to be
fault tolerant where possible, but if we fail to check in the internal
library functions, it's too late.

--gah

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpEncode.cc

    r1264 r1366  
    1515#include <cstring>
    1616
    17 #ifdef __cplusplus
    18 extern "C" {
    19 #endif // ifdef __cplusplus
    20 
    21 using namespace Rappture::encoding;
    22 
    2317/**********************************************************************/
    2418// FUNCTION: Rappture::encoding::isbinary()
     
    3226
    3327int
    34 isbinary(const char* buf, int size)
     28Rappture::encoding::isbinary(const char* buf, int size)
    3529{
    3630    if (buf == NULL) {
     
    7064
    7165size_t
    72 isencoded(const char* buf, int size)
     66Rappture::encoding::isencoded(const char* buf, int size)
    7367{
    7468    size_t flags = 0;
     
    117111        }
    118112    }
    119 
    120113    return flags;
    121114}
     
    131124 */
    132125
    133 Rappture::Outcome
    134 encode (Rappture::Buffer& buf, size_t flags)
    135 {
    136 
    137     int compress              = 0;
    138     int base64                = 0;
    139     int addHeader             = 0;
    140     Rappture::Outcome err;
     126bool
     127Rappture::encoding::encode(Rappture::Outcome &err, Rappture::Buffer& buf,
     128        size_t flags)
     129{
    141130    Rappture::Buffer outData;
    142131
    143     if ((flags & RPENC_Z) == RPENC_Z ) {
    144         compress = 1;
    145     }
    146     if ((flags & RPENC_B64) == RPENC_B64 ) {
    147         base64 = 1;
    148     }
    149     if ((flags & RPENC_HDR) == RPENC_HDR ) {
    150         addHeader = 1;
    151     }
    152 
    153     outData.append(buf.bytes(),buf.size());
    154     err = outData.encode(compress,base64);
    155     if (!err) {
     132    bool compress, base64, addHeader;
     133    compress  = (flags & RPENC_Z);
     134    base64    = (flags & RPENC_B64);
     135    addHeader = (flags & RPENC_HDR);
     136
     137    if (outData.append(buf.bytes(), buf.size()) != (int)buf.size()) {
     138        err.addError("can't append %d bytes", buf.size());
     139        return false;
     140    }
     141    if (outData.encode(err, compress, base64)) {
    156142        buf.clear();
    157         if (addHeader == 1) {
    158             if ((compress == 1) && (base64 == 0)) {
    159                 buf.append("@@RP-ENC:z\n",11);
    160             }
    161             else if ((compress == 0) && (base64 == 1)) {
    162                 buf.append("@@RP-ENC:b64\n",13);
    163             }
    164             else if ((compress == 1) && (base64 == 1)) {
    165                 buf.append("@@RP-ENC:zb64\n",14);
    166             }
    167             else {
     143        if (addHeader) {
     144            if ((compress) && (!base64)) {
     145                buf.append("@@RP-ENC:z\n", 11);
     146            } else if ((!compress) && (base64)) {
     147                buf.append("@@RP-ENC:b64\n", 13);
     148            } else if ((compress) && (base64)) {
     149                buf.append("@@RP-ENC:zb64\n", 14);
     150            } else {
    168151                // do nothing
    169152            }
    170         }
    171         else {
     153        } else {
    172154            // do nothing
    173155        }
    174         buf.append(outData.bytes(),outData.size());
    175     }
    176 
    177     return err;
     156        if (buf.append(outData.bytes(),outData.size()) != (int)outData.size()) {
     157            err.addError("can't append %d bytes", outData.size());
     158            return false;
     159        }
     160    }
     161    return true;
    178162}
    179163
     
    190174 */
    191175
    192 Rappture::Outcome
    193 decode (Rappture::Buffer& buf, size_t flags)
    194 {
    195 
    196     int decompress            = 0;
    197     int base64                = 0;
    198     int checkHDR              = 0;
    199     Rappture::Outcome err;
     176bool
     177Rappture::encoding::decode(Rappture::Outcome &err, Rappture::Buffer& buf,
     178                           size_t flags)
     179{
    200180    Rappture::Buffer outData;
    201181
    202     if ((flags & RPENC_Z) == RPENC_Z ) {
    203         decompress = 1;
    204     }
    205     if ((flags & RPENC_B64) == RPENC_B64 ) {
    206         base64 = 1;
    207     }
    208     if ((flags & RPENC_HDR) == RPENC_HDR ) {
    209         checkHDR = 1;
    210     }
    211 
    212     if ((buf.size() > 11) && (strncmp(buf.bytes(),"@@RP-ENC:z\n",11) == 0)) {
    213         outData.append(buf.bytes()+11,buf.size()-11);
    214         if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) {
    215             decompress = 1;
    216             base64 = 0;
    217         }
    218     }
    219     else if ((buf.size() > 13) && (strncmp(buf.bytes(),"@@RP-ENC:b64\n",13) == 0)) {
    220         outData.append(buf.bytes()+13,buf.size()-13);
    221         if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) {
    222             decompress = 0;
    223             base64 = 1;
    224         }
    225     }
    226     else if ((buf.size() > 14) && (strncmp(buf.bytes(),"@@RP-ENC:zb64\n",14) == 0)) {
    227         outData.append(buf.bytes()+14,buf.size()-14);
    228         if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) {
    229             decompress = 1;
    230             base64 = 1;
    231         }
    232     }
    233     else {
    234         // no special recognized tags
    235         outData.append(buf.bytes(),buf.size());
    236     }
    237 
    238     err = outData.decode(decompress,base64);
    239     if (!err) {
    240         buf.move(outData);
    241     }
    242 
    243     return err;
    244 }
    245 
    246 #ifdef __cplusplus
    247 }
    248 #endif // ifdef __cplusplus
     182    bool decompress, base64, checkHDR;
     183    decompress = (flags & RPENC_Z);
     184    base64     = (flags & RPENC_B64);
     185    checkHDR   = (flags & RPENC_HDR);
     186
     187    off_t offset;
     188    if ((buf.size() > 11) && (strncmp(buf.bytes(),"@@RP-ENC:z\n", 11) == 0)) {
     189        offset = 11;
     190        if ((checkHDR) || ((!decompress) && (!base64))) {
     191            decompress = true;
     192            base64 = false;
     193        }
     194    } else if ((buf.size() > 13) &&
     195               (strncmp(buf.bytes(),"@@RP-ENC:b64\n",13) == 0)) {
     196        offset = 13;
     197        if ((checkHDR) || ((!decompress) && (!base64) )) {
     198            decompress = false;
     199            base64 = true;
     200        }
     201    } else if ((buf.size() > 14) &&
     202               (strncmp(buf.bytes(), "@@RP-ENC:zb64\n",14) == 0)) {
     203        offset = 14;
     204        if ((checkHDR) || ((!decompress) && (!base64))) {
     205            decompress = true;
     206            base64 = true;
     207        }
     208    } else {
     209        offset = 0;
     210    }
     211    int nBytes = buf.size() - offset;
     212    if (outData.append(buf.bytes() + offset, nBytes) != nBytes) {
     213        err.addError("can't append %d bytes to buffer", nBytes);
     214        return false;
     215    }
     216    if (!outData.decode(err, decompress, base64)) {
     217        return false;
     218    }
     219    buf.move(outData);
     220    return true;
     221}
     222
Note: See TracChangeset for help on using the changeset viewer.