components/webclient/src/webclient.c
int webclient_response(struct webclient_session *session, unsigned char **response)
{
    unsigned char *buf_ptr;
    unsigned char *response_buf = 0;
    int length, total_read = 0;

    if (!session || !response)
    {
        return WEBCLIENT_ERROR;
    }

    /* initialize response */
    *response = NULL;

    /* not content length field kind */
    if (session->content_length < 0)
    {
        size_t result_sz;

        total_read = 0;
        while (1)
        {
            unsigned char *new_resp = NULL;

            result_sz = total_read + WEBCLIENT_RESPONSE_BUFSZ;
            // new_resp = pvPortRealloc(response_buf, result_sz + 1);
            new_resp = response_buf;
            response_buf = web_malloc(sizeof(response_buf) + result_sz + 1);
            memcpy(response_buf, new_resp, sizeof(response_buf));
            web_free(new_resp);
            new_resp = response_buf;

            if (new_resp == NULL)
            {
                // "no memory for realloc new response buffer!"
                break;
            }

            response_buf = new_resp;
            buf_ptr = (unsigned char *)response_buf + total_read;

            /* read result */
            length = webclient_read(session, buf_ptr, result_sz - total_read);
            if (length <= 0)
                break;

            total_read += length;
        }
    }
    else
    {
        int result_sz;

        result_sz = session->content_length;
        response_buf = web_malloc(result_sz + 1);
        if (!response_buf)
        {
            return -WEBCLIENT_NOMEM;
        }

        buf_ptr = (unsigned char *)response_buf;
        for (total_read = 0; total_read < result_sz;)
        {
            length = webclient_read(session, buf_ptr, result_sz - total_read);
            if (length <= 0)
                break;

            buf_ptr += length;
            total_read += length;
        }
    }

    if ((total_read == 0) && (response_buf != 0))
    {
        web_free(response_buf);
        response_buf = NULL;
    }

    if (response_buf)
    {
        *response = response_buf;
        *(response_buf + total_read) = '\0';
    }

    return total_read;
}
/**
 * get wenclient request response data.
 *
 * @param session wenclient session
 * @param response response buffer address
 *
 * @return response data size
 */
get wenclient request response data.
@param session wenclient session
@param response response buffer address

@return response data size
get wenclient request response data . @param session wenclient session @param response response buffer address @return response data size
int webclient_response(struct webclient_session *session, unsigned char **response)
{
    unsigned char *buf_ptr;
    unsigned char *response_buf = 0;
    int length, total_read = 0;
    if (!session || !response)
    {
        return WEBCLIENT_ERROR;
    }
    *response = NULL;
    if (session->content_length < 0)
    {
        size_t result_sz;
        total_read = 0;
        while (1)
        {
            unsigned char *new_resp = NULL;
            result_sz = total_read + WEBCLIENT_RESPONSE_BUFSZ;
            new_resp = response_buf;
            response_buf = web_malloc(sizeof(response_buf) + result_sz + 1);
            memcpy(response_buf, new_resp, sizeof(response_buf));
            web_free(new_resp);
            new_resp = response_buf;
            if (new_resp == NULL)
            {
                break;
            }
            response_buf = new_resp;
            buf_ptr = (unsigned char *)response_buf + total_read;
            length = webclient_read(session, buf_ptr, result_sz - total_read);
            if (length <= 0)
                break;
            total_read += length;
        }
    }
    else
    {
        int result_sz;
        result_sz = session->content_length;
        response_buf = web_malloc(result_sz + 1);
        if (!response_buf)
        {
            return -WEBCLIENT_NOMEM;
        }
        buf_ptr = (unsigned char *)response_buf;
        for (total_read = 0; total_read < result_sz;)
        {
            length = webclient_read(session, buf_ptr, result_sz - total_read);
            if (length <= 0)
                break;
            buf_ptr += length;
            total_read += length;
        }
    }
    if ((total_read == 0) && (response_buf != 0))
    {
        web_free(response_buf);
        response_buf = NULL;
    }
    if (response_buf)
    {
        *response = response_buf;
        *(response_buf + total_read) = '\0';
    }
    return total_read;
}
int webclient_response ( struct webclient_session * session , unsigned char * * response ) { unsigned char * buf_ptr ; unsigned char * response_buf = 0 ; int length , total_read = 0 ; if ( ! session || ! response ) { return WEBCLIENT_ERROR ; } * response = NULL ; if ( session -> content_length < 0 ) { size_t result_sz ; total_read = 0 ; while ( 1 ) { unsigned char * new_resp = NULL ; result_sz = total_read + WEBCLIENT_RESPONSE_BUFSZ ; new_resp = response_buf ; response_buf = web_malloc ( sizeof ( response_buf ) + result_sz + 1 ) ; memcpy ( response_buf , new_resp , sizeof ( response_buf ) ) ; web_free ( new_resp ) ; new_resp = response_buf ; if ( new_resp == NULL ) { break ; } response_buf = new_resp ; buf_ptr = ( unsigned char * ) response_buf + total_read ; length = webclient_read ( session , buf_ptr , result_sz - total_read ) ; if ( length <= 0 ) break ; total_read += length ; } } else { int result_sz ; result_sz = session -> content_length ; response_buf = web_malloc ( result_sz + 1 ) ; if ( ! response_buf ) { return - WEBCLIENT_NOMEM ; } buf_ptr = ( unsigned char * ) response_buf ; for ( total_read = 0 ; total_read < result_sz ; ) { length = webclient_read ( session , buf_ptr , result_sz - total_read ) ; if ( length <= 0 ) break ; buf_ptr += length ; total_read += length ; } } if ( ( total_read == 0 ) && ( response_buf != 0 ) ) { web_free ( response_buf ) ; response_buf = NULL ; } if ( response_buf ) { * response = response_buf ; * ( response_buf + total_read ) = ' \0 ' ; } return total_read ; }
get wenclient request response data.
get wenclient request response data .
/* initialize response */ /* not content length field kind */ // new_resp = pvPortRealloc(response_buf, result_sz + 1); // "no memory for realloc new response buffer!" /* read result */