components/webclient/src/webclient.c
int webclient_connect(struct webclient_session *session, const char *URI)
{
    int rc = WEBCLIENT_OK;
    int socket_handle = -1;
    struct timeval timeout;
    struct addrinfo *res = NULL;
    char *req_url;

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

    /* initialize the socket of session */
    session->socket = -1;

    timeout.tv_sec = WEBCLIENT_DEFAULT_TIMEO;
    timeout.tv_usec = 0;

    if (strncmp(URI, "https://", 8) == 0)
    {
#ifdef WEBCLIENT_USING_TLS
        if (webclient_open_tls(session, URI) < 0)
        {
            // "connect failed, https client open URI(%s) failed!", URI
            return -WEBCLIENT_ERROR;
        }
#else
        // "not support https connect, please enable webclient https configure!"
        rc = -WEBCLIENT_ERROR;
        goto __exit;
#endif
    }

    /* Check valid IP address and URL */
    rc = webclient_resolve_address(session, &res, URI, &req_url);
    if (rc != WEBCLIENT_OK)
    {
        // "connect failed, resolve address error(%d).", rc
        goto __exit;
    }

    if (!res)
    {
        rc = -WEBCLIENT_ERROR;
        goto __exit;
    }

    /* copy host address */
    if (*req_url)
    {
        session->req_url = strdup(req_url);
    }

#ifdef WEBCLIENT_USING_TLS
    if (session->tls_session)
    {
        int tls_ret = 0;

        if ((tls_ret = mbedtls_client_context(session->tls_session)) < 0)
        {
            // "connect failed, https client context return: -0x%x", -tls_ret
            return -WEBCLIENT_ERROR;
        }

        if ((tls_ret = mbedtls_client_connect(session->tls_session)) < 0)
        {
            // "connect failed, https client connect return: -0x%x", -tls_ret
            rc = -WEBCLIENT_CONNECT_FAILED;
            goto __exit;
        }

        socket_handle = session->tls_session->server_fd.fd;

        /* set recv timeout option */
        setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
        setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));

        session->socket = socket_handle;

        rc = WEBCLIENT_OK;
        goto __exit;
    }
#endif

    {
        socket_handle = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
        if (socket_handle < 0)
        {
            // "connect failed, create socket(%d) error.", socket_handle
            rc = -WEBCLIENT_NOSOCKET;
            goto __exit;
        }

        /* set receive and send timeout option */
        setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
        setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));

        if (connect(socket_handle, res->ai_addr, res->ai_addrlen) != 0)
        {
            /* connect failed */
            // "connect failed, connect socket(%d) error.", socket_handle
            rc = -WEBCLIENT_CONNECT_FAILED;
            goto __exit;
        }

        session->socket = socket_handle;
    }

__exit:
    if (res)
    {
        freeaddrinfo(res);
    }
    if (rc != WEBCLIENT_OK)
    {
        if (socket_handle >= 0)
        {
            closesocket(socket_handle);
        }
    }

    return rc;
}
/**
 * connect to http server.
 *
 * @param session webclient session
 * @param URI the input server URI address
 *
 * @return <0: connect failed or other error
 *         =0: connect success
 */
connect to http server.
@param session webclient session
@param URI the input server URI address

@return <0: connect failed or other error
=0: connect success
connect to http server . @param session webclient session @param URI the input server URI address @return <0 : connect failed or other error = 0 : connect success
int webclient_connect(struct webclient_session *session, const char *URI)
{
    int rc = WEBCLIENT_OK;
    int socket_handle = -1;
    struct timeval timeout;
    struct addrinfo *res = NULL;
    char *req_url;
    if (!URI || !session)
    {
        return WEBCLIENT_ERROR;
    }
    session->socket = -1;
    timeout.tv_sec = WEBCLIENT_DEFAULT_TIMEO;
    timeout.tv_usec = 0;
    if (strncmp(URI, "https://", 8) == 0)
    {
#ifdef WEBCLIENT_USING_TLS
        if (webclient_open_tls(session, URI) < 0)
        {
            return -WEBCLIENT_ERROR;
        }
#else
        rc = -WEBCLIENT_ERROR;
        goto __exit;
#endif
    }
    rc = webclient_resolve_address(session, &res, URI, &req_url);
    if (rc != WEBCLIENT_OK)
    {
        goto __exit;
    }
    if (!res)
    {
        rc = -WEBCLIENT_ERROR;
        goto __exit;
    }
    if (*req_url)
    {
        session->req_url = strdup(req_url);
    }
#ifdef WEBCLIENT_USING_TLS
    if (session->tls_session)
    {
        int tls_ret = 0;
        if ((tls_ret = mbedtls_client_context(session->tls_session)) < 0)
        {
            return -WEBCLIENT_ERROR;
        }
        if ((tls_ret = mbedtls_client_connect(session->tls_session)) < 0)
        {
            rc = -WEBCLIENT_CONNECT_FAILED;
            goto __exit;
        }
        socket_handle = session->tls_session->server_fd.fd;
        setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
        setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));
        session->socket = socket_handle;
        rc = WEBCLIENT_OK;
        goto __exit;
    }
#endif
    {
        socket_handle = socket(res->ai_family, SOCK_STREAM, IPPROTO_TCP);
        if (socket_handle < 0)
        {
            rc = -WEBCLIENT_NOSOCKET;
            goto __exit;
        }
        setsockopt(socket_handle, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));
        setsockopt(socket_handle, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, sizeof(timeout));
        if (connect(socket_handle, res->ai_addr, res->ai_addrlen) != 0)
        {
            rc = -WEBCLIENT_CONNECT_FAILED;
            goto __exit;
        }
        session->socket = socket_handle;
    }
__exit:
    if (res)
    {
        freeaddrinfo(res);
    }
    if (rc != WEBCLIENT_OK)
    {
        if (socket_handle >= 0)
        {
            closesocket(socket_handle);
        }
    }
    return rc;
}
int webclient_connect ( struct webclient_session * session , const char * URI ) { int rc = WEBCLIENT_OK ; int socket_handle = -1 ; struct timeval timeout ; struct addrinfo * res = NULL ; char * req_url ; if ( ! URI || ! session ) { return WEBCLIENT_ERROR ; } session -> socket = -1 ; timeout . tv_sec = WEBCLIENT_DEFAULT_TIMEO ; timeout . tv_usec = 0 ; if ( strncmp ( URI , " " , 8 ) == 0 ) { #ifdef WEBCLIENT_USING_TLS if ( webclient_open_tls ( session , URI ) < 0 ) { return - WEBCLIENT_ERROR ; } #else rc = - WEBCLIENT_ERROR ; goto __exit ; #endif } rc = webclient_resolve_address ( session , & res , URI , & req_url ) ; if ( rc != WEBCLIENT_OK ) { goto __exit ; } if ( ! res ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } if ( * req_url ) { session -> req_url = strdup ( req_url ) ; } #ifdef WEBCLIENT_USING_TLS if ( session -> tls_session ) { int tls_ret = 0 ; if ( ( tls_ret = mbedtls_client_context ( session -> tls_session ) ) < 0 ) { return - WEBCLIENT_ERROR ; } if ( ( tls_ret = mbedtls_client_connect ( session -> tls_session ) ) < 0 ) { rc = - WEBCLIENT_CONNECT_FAILED ; goto __exit ; } socket_handle = session -> tls_session -> server_fd . fd ; setsockopt ( socket_handle , SOL_SOCKET , SO_RCVTIMEO , ( void * ) & timeout , sizeof ( timeout ) ) ; setsockopt ( socket_handle , SOL_SOCKET , SO_SNDTIMEO , ( void * ) & timeout , sizeof ( timeout ) ) ; session -> socket = socket_handle ; rc = WEBCLIENT_OK ; goto __exit ; } #endif { socket_handle = socket ( res -> ai_family , SOCK_STREAM , IPPROTO_TCP ) ; if ( socket_handle < 0 ) { rc = - WEBCLIENT_NOSOCKET ; goto __exit ; } setsockopt ( socket_handle , SOL_SOCKET , SO_RCVTIMEO , ( void * ) & timeout , sizeof ( timeout ) ) ; setsockopt ( socket_handle , SOL_SOCKET , SO_SNDTIMEO , ( void * ) & timeout , sizeof ( timeout ) ) ; if ( connect ( socket_handle , res -> ai_addr , res -> ai_addrlen ) != 0 ) { rc = - WEBCLIENT_CONNECT_FAILED ; goto __exit ; } session -> socket = socket_handle ; } __exit : if ( res ) { freeaddrinfo ( res ) ; } if ( rc != WEBCLIENT_OK ) { if ( socket_handle >= 0 ) { closesocket ( socket_handle ) ; } } return rc ; }
connect to http server.
connect to http server .
/* initialize the socket of session */ // "connect failed, https client open URI(%s) failed!", URI // "not support https connect, please enable webclient https configure!" /* Check valid IP address and URL */ // "connect failed, resolve address error(%d).", rc /* copy host address */ // "connect failed, https client context return: -0x%x", -tls_ret // "connect failed, https client connect return: -0x%x", -tls_ret /* set recv timeout option */ // "connect failed, create socket(%d) error.", socket_handle /* set receive and send timeout option */ /* connect failed */ // "connect failed, connect socket(%d) error.", socket_handle