Add error handling to the stream functions
authorWilfried Goesgens <dothebart@citadel.org>
Tue, 20 Jan 2015 22:27:46 +0000 (23:27 +0100)
committerWilfried Goesgens <dothebart@citadel.org>
Tue, 20 Jan 2015 22:27:46 +0000 (23:27 +0100)
libcitadel/lib/libcitadel.h
libcitadel/lib/stringbuf.c
libcitadel/tests/stringbuf_stream.c
webcit/tcp_sockets.c

index cba677a..00e8410 100644 (file)
@@ -346,9 +346,9 @@ typedef enum __eStreamType {
        eEmtyCodec
 } eStreamType;
 
-void *StrBufNewStreamContext(eStreamType type);
-void StrBufDestroyStreamContext(eStreamType type, void **Stream);
-int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *Stream, int LastChunk);
+void *StrBufNewStreamContext(eStreamType type, const char **Err);
+int StrBufDestroyStreamContext(eStreamType type, void **Stream, const char **Err);
+int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *Stream, int LastChunk, const char **Err);
 
 int StrBufDecodeBase64(StrBuf *Buf);
 int StrBufDecodeBase64To(const StrBuf *BufIn, StrBuf *BufOut);
index 97260c4..813def3 100644 (file)
@@ -2859,9 +2859,10 @@ typedef struct __z_enc_stream {
        z_stream zstream;
 } z_enc_stream;
 
-void *StrBufNewStreamContext(eStreamType type)
+void *StrBufNewStreamContext(eStreamType type, const char **Err)
 {
        base64_decodestate *state;;
+       *Err = NULL;
 
        switch (type)
        {
@@ -2885,7 +2886,8 @@ void *StrBufNewStreamContext(eStreamType type)
                err = inflateInit(&stream->zstream);
 
                if (err != Z_OK) {
-                       StrBufDestroyStreamContext(type, (void**)&stream);
+                       StrBufDestroyStreamContext(type, (void**)&stream, Err);
+                       *Err = zError(err);
                        return NULL;
                }
                return stream;
@@ -2916,7 +2918,8 @@ void *StrBufNewStreamContext(eStreamType type)
                                   DEF_MEM_LEVEL,
                                   Z_DEFAULT_STRATEGY);
                if (err != Z_OK) {
-                       StrBufDestroyStreamContext(type, (void**) &stream);
+                       StrBufDestroyStreamContext(type, (void**) &stream, Err);
+                       *Err = zError(err);
                        return NULL;
                }
                return stream;
@@ -2929,10 +2932,15 @@ void *StrBufNewStreamContext(eStreamType type)
        return NULL;
 }
 
-void StrBufDestroyStreamContext(eStreamType type, void **vStream)
+int StrBufDestroyStreamContext(eStreamType type, void **vStream, const char **Err)
 {
+       int err;
+       int rc = 0;
+       *Err = NULL;
+       
        if ((vStream == NULL) || (*vStream==NULL)) {
-               return;
+               *Err = strerror(EINVAL);
+               return EINVAL;
        }
        switch (type)
        {
@@ -2951,20 +2959,25 @@ void StrBufDestroyStreamContext(eStreamType type, void **vStream)
        case eZLibEncode:
        {
                z_enc_stream *stream = (z_enc_stream *)*vStream;
+               err = deflateEnd(&stream->zstream);
+               if (err != Z_OK) {
+                       *Err = zError(err);
+                       rc = -1;
+               }
                free(stream->OutBuf.buf);
                free(stream);
-// todo more?
                *vStream = NULL;
                break;
        }
        case eEmtyCodec: 
                break; /// TODO
        }
+       return rc;
 }
 
-int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *vStream, int LastChunk)
+int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, const char* pIn, long pInLen, void *vStream, int LastChunk, const char **Err)
 {
-
+       int rc = 0;
        switch (type)
        {
        case eBase64Encode:
@@ -3079,7 +3092,10 @@ int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, cons
                                        (In->Buf->BufUsed - stream->zstream.avail_in);
                        }
                }
-               return (LastChunk && (err != Z_FINISH));
+               rc = (LastChunk && (err != Z_FINISH));
+               if (!rc && (err != Z_OK)) {
+                       *Err = zError(err);
+               }
                
        }
        break;
@@ -3113,7 +3129,7 @@ int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, cons
                        err = Z_DATA_ERROR;     /* and fall through */
 
                case Z_DATA_ERROR:
-                       fprintf(stderr, "sanoteuh\n");
+                       *Err = zError(err);
                case Z_MEM_ERROR:
                        (void)inflateEnd(&stream->zstream);
                        return err;
@@ -3153,7 +3169,7 @@ int StrBufStreamTranscode(eStreamType type, IOBuffer *Target, IOBuffer *In, cons
        }
                break; /// TODO
        }
-       return 0;
+       return rc;
 }
 
 /**
index 1c4a52e..1c0fca4 100644 (file)
@@ -70,6 +70,7 @@ static void StreamEncode(void)
        IOBuffer ReadBuffer;
        IOBuffer WriteBuffer;
        int err;
+       const char *Err = NULL;
        int ret = 0;
        int done = 0;
        void *vStream;
@@ -96,7 +97,7 @@ static void StreamEncode(void)
                ST = eBase64Decode;
        else
                ST = eEmtyCodec;
-       vStream = StrBufNewStreamContext(ST);
+       vStream = StrBufNewStreamContext(ST, &Err);
 
        while (!done && (fdin >= 0) && (fdout >= 0) && (!feof(stdin)))
        {
@@ -111,7 +112,7 @@ static void StreamEncode(void)
                do
                {
                        do {
-                               ret = StrBufStreamTranscode(ST, &WriteBuffer, &ReadBuffer, NULL, -1, vStream, done);
+                               ret = StrBufStreamTranscode(ST, &WriteBuffer, &ReadBuffer, NULL, -1, vStream, done, &Err);
                                
                                while (IOBufferStrLength(&WriteBuffer) > 0)
                                {
@@ -124,7 +125,7 @@ static void StreamEncode(void)
        }
 
        
-       StrBufDestroyStreamContext(ST, &vStream);
+       StrBufDestroyStreamContext(ST, &vStream, &Err);
        
        FreeStrBuf(&ReadBuffer.Buf);
        FreeStrBuf(&WriteBuffer.Buf);
index 33889f6..70e3cff 100644 (file)
@@ -556,6 +556,7 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
        int client_con_state = 0;
        int chunked = 0;
        int is_gzip = 0;
+       const char *Err = NULL;
        StrBuf *BufHeader = NULL;
        StrBuf *Buf;
        StrBuf *pBuf = NULL;
@@ -613,7 +614,12 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
        if (chunked && !DisableGzip && WCC->Hdr->HR.gzip_ok)
        {
                is_gzip = 1;
-               SC = StrBufNewStreamContext (eZLibEncode);
+               SC = StrBufNewStreamContext (eZLibEncode, &Err);
+               if (SC == NULL) {
+                       syslog(LOG_ERR, "Error while initializing stream context: %s", Err);
+                       FreeStrBuf(&Buf);
+                       return;
+               }
 
                memset(&ReadBuffer, 0, sizeof(IOBuffer));
                ReadBuffer.Buf = WCC->WBuf;
@@ -635,7 +641,9 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                        FreeStrBuf(&Buf);
                        FreeStrBuf(&WriteBuffer.Buf);
                        FreeStrBuf(&BufHeader);
-                       StrBufDestroyStreamContext(eZLibEncode, SC);
+                       if (StrBufDestroyStreamContext(eZLibEncode, SC, &Err) && Err) {
+                               syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
+                       }
                        return;
                }
        }
@@ -650,7 +658,10 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                        FreeStrBuf(&Buf);
                        FreeStrBuf(&WriteBuffer.Buf);
                        FreeStrBuf(&BufHeader);
-                       StrBufDestroyStreamContext(eZLibEncode, SC);
+                       StrBufDestroyStreamContext(eZLibEncode, SC, &Err);
+                       if (StrBufDestroyStreamContext(eZLibEncode, SC, &Err) && Err) {
+                               syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
+                       }
                        return;
                }
 
@@ -684,7 +695,7 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                                int rc;
 
                                do {
-                                       rc = StrBufStreamTranscode(eZLibEncode, &WriteBuffer, &ReadBuffer, NULL, -1, SC, done);
+                                       rc = StrBufStreamTranscode(eZLibEncode, &WriteBuffer, &ReadBuffer, NULL, -1, SC, done, &Err);
 
                                        if (StrLength (pBuf) > 0) {
                                                StrBufPrintf(BufHeader, "%s%x\r\n", 
@@ -718,7 +729,9 @@ void serv_read_binary_to_http(StrBuf *MimeType, size_t total_len, int is_static,
                }
        }
 
-       StrBufDestroyStreamContext(eZLibEncode, &SC);
+       if (SC && StrBufDestroyStreamContext(eZLibEncode, SC, &Err) && Err) {
+               syslog(LOG_ERR, "Error while destroying stream context: %s", Err);
+       }
        FreeStrBuf(&WriteBuffer.Buf);
        if ((chunked) && (client_con_state == 0))
        {