Beispiel #1
0
int flushloopbuf(lsi_param_t *rec, int iState, ls_loopbuf_t *pBuf)
{
    int written = 0;
    int sz = 0;
    while (!ls_loopbuf_empty(pBuf))
    {
        sz = g_api->stream_write_next(rec, ls_loopbuf_begin(pBuf),
                                      ls_loopbuf_blksize(pBuf));
        if (sz < 0)
            return LS_FAIL;
        else if (sz > 0)
        {
            ls_loopbuf_popfront(pBuf, sz);
            written += sz;
        }
        else
            break;
    }

    if (ls_loopbuf_empty(pBuf) && iState == Z_END)
    {
        rec->flag_in |= LSI_CBFI_EOF;
        g_api->stream_write_next(rec, NULL, 0);
    }

    return written;
}
Beispiel #2
0
//expand the recieved data to base64 encode
int l4recv1(lsi_cb_param_t *rec)
{
#define PLAIN_BLOCK_SIZE 600
#define ENCODE_BLOCK_SIZE (PLAIN_BLOCK_SIZE * 4 / 3 + 1)

    MyData *myData = NULL;
    char *pBegin;
    char tmpBuf[ENCODE_BLOCK_SIZE];
    int len, sz;

    myData = (MyData *)g_api->get_module_data(rec->_session, &MNAME,
             LSI_MODULE_DATA_L4);
    if (!myData)
        return LS_FAIL;

    while ((len = g_api->stream_read_next(rec, tmpBuf, ENCODE_BLOCK_SIZE)) > 0)
    {
        g_api->log(NULL, LSI_LOG_DEBUG,
                   "#### updatetcpin1 test l4recv, inLn = %d\n", len);
        ls_loopbuf_append(&myData->inBuf, tmpBuf, len);
    }

    while (!ls_loopbuf_empty(&myData->inBuf))
    {
        ls_loopbuf_straight(&myData->inBuf);
        pBegin = ls_loopbuf_begin(&myData->inBuf);
        sz = ls_loopbuf_size(&myData->inBuf);
        if (sz > PLAIN_BLOCK_SIZE)
            sz = PLAIN_BLOCK_SIZE;
        len = ls_base64_encode((const char *)pBegin, sz, tmpBuf);
        if (len > 0)
        {
            ls_loopbuf_append(&myData->outBuf, tmpBuf, len);
            ls_loopbuf_popfront(&myData->inBuf, sz);
        }
        else
            break;
    }

    ////////////////////////////////////////////////////////////////////////////////
    if (ls_loopbuf_size(&myData->outBuf) < rec->_param_len)
        rec->_param_len = ls_loopbuf_size(&myData->outBuf);

    if (rec->_param_len > 0)
        ls_loopbuf_moveto(&myData->outBuf, (char *)rec->_param, rec->_param_len);

    return rec->_param_len;
}
Beispiel #3
0
int httprespwrite(lsi_cb_param_t *rec)
{
    MyData *myData = NULL;
    ls_xpool_t *pool = g_api->get_session_pool(rec->_session);
    const char *in = rec->_param;
    int inLen = rec->_param_len;
    int written, total = 0;
//    int j;
//    char s[4] = {0};
    myData = (MyData *)g_api->get_module_data(rec->_session, &MNAME,
             LSI_MODULE_DATA_HTTP);

//     for ( j=0; j<inLen; ++j )
//     {
//         sprintf(s, "%c ", (unsigned char)in[j]);
//         ls_loopbuf_xappend( &myData->m_outWBuf, s, 2, pool );
//         total += 2;
//     }

    //If have content, append a string for testing
    if (inLen > 0)
    {
        ls_loopbuf_xappend(&myData->outWBuf, TEST_STRING, sizeof(TEST_STRING) - 1,
                           pool);
        ls_loopbuf_xappend(&myData->outWBuf, in, inLen, pool);
        total = inLen + sizeof(TEST_STRING) - 1;
    }

    ls_loopbuf_xstraight(&myData->outWBuf, pool);

    written = g_api->stream_write_next(rec, ls_loopbuf_begin(&myData->outWBuf),
                                       ls_loopbuf_size(&myData->outWBuf));
    ls_loopbuf_popfront(&myData->outWBuf, written);

    g_api->log(NULL, LSI_LOG_DEBUG,
               "#### mymodulehttp test, next caller written %d, return %d, left %d\n",
               written, total, ls_loopbuf_size(&myData->outWBuf));


    if (!ls_loopbuf_empty(&myData->outWBuf))
    {
        int hasData = 1;
        rec->_flag_out = &hasData;
    }
    return inLen; //Because all data used, ruturn thr orignal length
}
Beispiel #4
0
static int doCompression(lsi_param_t *rec, zbufinfo_t *pBufInfo,
                         int iDoFlush, const char *pModuleStr,
                         const char *pSendingStr, const char *pCompressStr)
{
    int ret, len, sz, consumed = 0, written = 0;
    ls_loopbuf_t *pBuf = &pBufInfo->loopbuf;
    z_stream *pStream = &pBufInfo->zstream;

    if (!ls_loopbuf_empty(pBuf))
    {
        written = flushloopbuf(rec, pBufInfo->zstate, pBuf);
        if (written == -1)
            return LS_FAIL;
        if (!ls_loopbuf_empty(pBuf))
        {
            if (rec->flag_out != NULL)
                *rec->flag_out |= LSI_CBFO_BUFFERED;
            return 0;
        }
    }

    if (pBufInfo->zstate == Z_END)
        return rec->len1;

    do
    {
        ls_loopbuf_xguarantee(pBuf, 1024,
                              g_api->get_session_pool(rec->session));
        len = ls_loopbuf_contiguous(pBuf);
        pStream->avail_out = len;
        pStream->next_out = (unsigned char *)ls_loopbuf_end(pBuf);

        if (pBufInfo->compresslevel == 0)
            ret = inflate(pStream, iDoFlush);
        else
            ret = deflate(pStream, iDoFlush);

        if (ret >= Z_OK)
        {
            consumed = rec->len1 - pStream->avail_in;
            ls_loopbuf_used(pBuf, len - pStream->avail_out);

            if (pBufInfo->zstate == Z_EOF && ret == Z_STREAM_END)
            {
                pBufInfo->zstate = Z_END;
                g_api->log(rec->session, LSI_LOG_DEBUG,
                           "[%s%s] compressbuf end of stream set.\n",
                           pModuleStr, pCompressStr);
            }

            sz = flushloopbuf(rec, pBufInfo->zstate, pBuf);
            if (sz > 0)
                written += sz;
            else if (sz < 0)
            {
                g_api->log(rec->session, LSI_LOG_ERROR,
                           "[%s%s] compressbuf in %d, return %d (written %d, "
                           "flag in %d)\n", pModuleStr, pCompressStr,
                           rec->len1, sz, written, rec->flag_in);
                return LSI_ERROR;
            }
            if (!ls_loopbuf_empty(pBuf))
                break;
        }
        else
        {
            g_api->log(rec->session, LSI_LOG_ERROR,
                       "[%s%s] compressbuf in %d, inflate/deflate return %d\n",
                       pModuleStr, pCompressStr, rec->len1, ret);
            if (ret != Z_BUF_ERROR)
                return LSI_ERROR;
        }
    }
    while (pBufInfo->zstate != Z_END && pStream->avail_out == 0);

    if (!(ls_loopbuf_empty(pBuf)) || (pBufInfo->zstate != Z_END
                                      && pStream->avail_out == 0))
    {
        if (rec->flag_out != NULL)
            *rec->flag_out |= LSI_CBFO_BUFFERED;
    }

    g_api->log(rec->session, LSI_LOG_DEBUG,
               "[%s%s] compressbuf [%s] in %d, consumed: %d, written %d, "
               "flag in %d, buffer has %d.\n", pModuleStr, pCompressStr,
               pSendingStr, rec->len1, consumed,
               written, rec->flag_in, ls_loopbuf_size(pBuf));
    return consumed;
}