components/webclient/src/webclient.c
int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t length)
{
    int bytes_read = 0;
    int total_read = 0;
    int left;

    if (!session)
    {
        return WEBCLIENT_ERROR;
    }

    if (session->socket < 0)
    {
        return -WEBCLIENT_DISCONNECT;
    }

    if (length == 0)
    {
        return 0;
    }

    /* which is transfered as chunk mode */
    if (session->chunk_sz)
    {
        if ((int)length > (session->chunk_sz - session->chunk_offset))
        {
            length = session->chunk_sz - session->chunk_offset;
        }

        bytes_read = webclient_recv(session, buffer, length, 0);
        if (bytes_read <= 0)
        {
            if (errno == EWOULDBLOCK || errno == EAGAIN)
            {
                /* recv timeout */
                return -WEBCLIENT_TIMEOUT;
            }
            else
            {
                closesocket(session->socket);
                session->socket = -1;
                return 0;
            }
        }

        session->chunk_offset += bytes_read;
        if (session->chunk_offset >= session->chunk_sz)
        {
            webclient_next_chunk(session);
        }

        return bytes_read;
    }

    if (session->content_length > 0)
    {
        if (length > session->content_remainder)
        {
            length = session->content_remainder;
        }

        if (length == 0)
        {
            return 0;
        }
    }

    /*
     * Read until: there is an error, we've read "size" bytes or the remote
     * side has closed the connection.
     */
    left = length;
    do
    {
        bytes_read = webclient_recv(session, buffer + total_read, left, 0);
        if (bytes_read <= 0)
        {
#ifdef WEBCLIENT_USING_TLS
            if (session->tls_session && bytes_read == MBEDTLS_ERR_SSL_WANT_READ)
                continue;
#endif
            // "receive data error(%d).", bytes_read
            if (total_read)
            {
                break;
            }
            else
            {
                if (errno == EWOULDBLOCK || errno == EAGAIN)
                {
                    /* recv timeout */
                    // "receive data timeout."
                    return -WEBCLIENT_TIMEOUT;
                }
                else
                {
                    closesocket(session->socket);
                    session->socket = -1;
                    return 0;
                }
            }
        }

        left -= bytes_read;
        total_read += bytes_read;
    } while (left);

    if (session->content_length > 0)
    {
        session->content_remainder -= total_read;
    }

    return total_read;
}
/**
 *  read data from http server.
 *
 * @param session http session
 * @param buffer read buffer
 * @param length the maximum of read buffer size
 *
 * @return <0: read data error
 *         =0: http server disconnect
 *         >0: successfully read data length
 */
read data from http server.
@param session http session
@param buffer read buffer
@param length the maximum of read buffer size

@return <0: read data error
=0: http server disconnect
>0: successfully read data length
read data from http server . @param session http session @param buffer read buffer @param length the maximum of read buffer size @return <0 : read data error = 0 : http server disconnect > 0 : successfully read data length
int webclient_read(struct webclient_session *session, unsigned char *buffer, size_t length)
{
    int bytes_read = 0;
    int total_read = 0;
    int left;
    if (!session)
    {
        return WEBCLIENT_ERROR;
    }
    if (session->socket < 0)
    {
        return -WEBCLIENT_DISCONNECT;
    }
    if (length == 0)
    {
        return 0;
    }
    if (session->chunk_sz)
    {
        if ((int)length > (session->chunk_sz - session->chunk_offset))
        {
            length = session->chunk_sz - session->chunk_offset;
        }
        bytes_read = webclient_recv(session, buffer, length, 0);
        if (bytes_read <= 0)
        {
            if (errno == EWOULDBLOCK || errno == EAGAIN)
            {
                return -WEBCLIENT_TIMEOUT;
            }
            else
            {
                closesocket(session->socket);
                session->socket = -1;
                return 0;
            }
        }
        session->chunk_offset += bytes_read;
        if (session->chunk_offset >= session->chunk_sz)
        {
            webclient_next_chunk(session);
        }
        return bytes_read;
    }
    if (session->content_length > 0)
    {
        if (length > session->content_remainder)
        {
            length = session->content_remainder;
        }
        if (length == 0)
        {
            return 0;
        }
    }
    left = length;
    do
    {
        bytes_read = webclient_recv(session, buffer + total_read, left, 0);
        if (bytes_read <= 0)
        {
#ifdef WEBCLIENT_USING_TLS
            if (session->tls_session && bytes_read == MBEDTLS_ERR_SSL_WANT_READ)
                continue;
#endif
            if (total_read)
            {
                break;
            }
            else
            {
                if (errno == EWOULDBLOCK || errno == EAGAIN)
                {
                    return -WEBCLIENT_TIMEOUT;
                }
                else
                {
                    closesocket(session->socket);
                    session->socket = -1;
                    return 0;
                }
            }
        }
        left -= bytes_read;
        total_read += bytes_read;
    } while (left);
    if (session->content_length > 0)
    {
        session->content_remainder -= total_read;
    }
    return total_read;
}
int webclient_read ( struct webclient_session * session , unsigned char * buffer , size_t length ) { int bytes_read = 0 ; int total_read = 0 ; int left ; if ( ! session ) { return WEBCLIENT_ERROR ; } if ( session -> socket < 0 ) { return - WEBCLIENT_DISCONNECT ; } if ( length == 0 ) { return 0 ; } if ( session -> chunk_sz ) { if ( ( int ) length > ( session -> chunk_sz - session -> chunk_offset ) ) { length = session -> chunk_sz - session -> chunk_offset ; } bytes_read = webclient_recv ( session , buffer , length , 0 ) ; if ( bytes_read <= 0 ) { if ( errno == EWOULDBLOCK || errno == EAGAIN ) { return - WEBCLIENT_TIMEOUT ; } else { closesocket ( session -> socket ) ; session -> socket = -1 ; return 0 ; } } session -> chunk_offset += bytes_read ; if ( session -> chunk_offset >= session -> chunk_sz ) { webclient_next_chunk ( session ) ; } return bytes_read ; } if ( session -> content_length > 0 ) { if ( length > session -> content_remainder ) { length = session -> content_remainder ; } if ( length == 0 ) { return 0 ; } } left = length ; do { bytes_read = webclient_recv ( session , buffer + total_read , left , 0 ) ; if ( bytes_read <= 0 ) { #ifdef WEBCLIENT_USING_TLS if ( session -> tls_session && bytes_read == MBEDTLS_ERR_SSL_WANT_READ ) continue ; #endif if ( total_read ) { break ; } else { if ( errno == EWOULDBLOCK || errno == EAGAIN ) { return - WEBCLIENT_TIMEOUT ; } else { closesocket ( session -> socket ) ; session -> socket = -1 ; return 0 ; } } } left -= bytes_read ; total_read += bytes_read ; } while ( left ) ; if ( session -> content_length > 0 ) { session -> content_remainder -= total_read ; } return total_read ; }
read data from http server.
read data from http server .
/* which is transfered as chunk mode */ /* recv timeout */ /*
     * Read until: there is an error, we've read "size" bytes or the remote
     * side has closed the connection.
     */ // "receive data error(%d).", bytes_read /* recv timeout */ // "receive data timeout."