components/webclient/src/webclient.c
static int webclient_resolve_address(struct webclient_session *session, struct addrinfo **res, const char *url, char **request)
{
    int rc = WEBCLIENT_OK;
    char *ptr;
    char port_str[6] = "80"; /* default port of 80(http) */

    const char *host_addr = 0;
    int url_len, host_addr_len = 0;

    if (!res || !request)
    {
        return WEBCLIENT_ERROR;
    }

    url_len = strlen(url);

    /* strip protocol(http or https) */
    if (strncmp(url, "http://", 7) == 0)
    {
        host_addr = url + 7;
    }
    else if (strncmp(url, "https://", 8) == 0)
    {
        strncpy(port_str, "443", 4);
        host_addr = url + 8;
    }
    else
    {
        rc = -WEBCLIENT_ERROR;
        goto __exit;
    }

    /* ipv6 address */
    if (host_addr[0] == '[')
    {
        host_addr += 1;
        ptr = strstr(host_addr, "]");
        if (!ptr)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        host_addr_len = ptr - host_addr;

        ptr = strstr(host_addr + host_addr_len, "/");
        if (!ptr)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        else if (ptr != (host_addr + host_addr_len + 1))
        {
            int port_len = ptr - host_addr - host_addr_len - 2;

            strncpy(port_str, host_addr + host_addr_len + 2, port_len);
            port_str[port_len] = '\0';
        }

        *request = (char *)ptr;
    }
    else
    { /* ipv4 or domain. */
        char *port_ptr;

        ptr = strstr(host_addr, "/");
        if (!ptr)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        host_addr_len = ptr - host_addr;
        *request = (char *)ptr;

        /* resolve port */
        port_ptr = strstr(host_addr, ":");
        if (port_ptr && port_ptr < ptr)
        {
            int port_len = ptr - port_ptr - 1;

            strncpy(port_str, port_ptr + 1, port_len);
            port_str[port_len] = '\0';

            host_addr_len = port_ptr - host_addr;
        }
    }

    if ((host_addr_len < 1) || (host_addr_len > url_len))
    {
        rc = -WEBCLIENT_ERROR;
        goto __exit;
    }

    /* get host address ok. */
    {
        char *host_addr_new = web_malloc(host_addr_len + 1);

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

        memcpy(host_addr_new, host_addr, host_addr_len);
        host_addr_new[host_addr_len] = '\0';
        session->host = host_addr_new;

#ifdef WEBCLIENT_USING_TLS
        if (session->tls_session)
            session->tls_session->host = strdup(host_addr_new);
#endif
    }

    /* resolve the host name. */
    {
        struct addrinfo hint;
        int ret;

        memset(&hint, 0, sizeof(hint));

#ifdef WEBCLIENT_USING_TLS
        if (session->tls_session)
        {
            session->tls_session->port = strdup(port_str);
            ret = getaddrinfo(session->tls_session->host, port_str, &hint, res);
            if (ret != 0)
            {
                // "getaddrinfo err: %d '%s'", ret, session->host
                rc = -WEBCLIENT_ERROR;
            }

            goto __exit;
        }
#endif

        ret = getaddrinfo(session->host, port_str, &hint, res);
        if (ret != 0)
        {
            // "getaddrinfo err: %d, host = '%s', port_str = %s.", ret, session->host, port_str
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
    }
__exit:
    if (rc != WEBCLIENT_OK)
    {
        if (session->host)
        {
            web_free(session->host);
            session->host = NULL;
        }

        if (*res)
        {
            freeaddrinfo(*res);
            *res = NULL;
        }
    }

    return rc;
}
/**
 * resolve server address
 *
 * @param session http session
 * @param res the server address information
 * @param url the input server URI address
 * @param request the pointer to point the request url, for example, /index.html
 *
 * @return 0 on resolve server address OK, others failed
 *
 * URL example:
 * http://www.rt-thread.org/
 * http://192.168.1.1:80/index.htm
 * http://[fe80::1]/index.html
 * http://[fe80::1]:80/index.html
 */
resolve server address
@param session http session
@param res the server address information
@param url the input server URI address
@param request the pointer to point the request url, for example, /index.html

@return 0 on resolve server address OK, others failed


resolve server address @param session http session @param res the server address information @param url the input server URI address @param request the pointer to point the request url for example / index . html @return 0 on resolve server address OK others failed
static int webclient_resolve_address(struct webclient_session *session, struct addrinfo **res, const char *url, char **request)
{
    int rc = WEBCLIENT_OK;
    char *ptr;
    char port_str[6] = "80"; 
    const char *host_addr = 0;
    int url_len, host_addr_len = 0;
    if (!res || !request)
    {
        return WEBCLIENT_ERROR;
    }
    url_len = strlen(url);
    if (strncmp(url, "http://", 7) == 0)
    {
        host_addr = url + 7;
    }
    else if (strncmp(url, "https://", 8) == 0)
    {
        strncpy(port_str, "443", 4);
        host_addr = url + 8;
    }
    else
    {
        rc = -WEBCLIENT_ERROR;
        goto __exit;
    }
    if (host_addr[0] == '[')
    {
        host_addr += 1;
        ptr = strstr(host_addr, "]");
        if (!ptr)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        host_addr_len = ptr - host_addr;
        ptr = strstr(host_addr + host_addr_len, "/");
        if (!ptr)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        else if (ptr != (host_addr + host_addr_len + 1))
        {
            int port_len = ptr - host_addr - host_addr_len - 2;
            strncpy(port_str, host_addr + host_addr_len + 2, port_len);
            port_str[port_len] = '\0';
        }
        *request = (char *)ptr;
    }
    else
    { 
        char *port_ptr;
        ptr = strstr(host_addr, "/");
        if (!ptr)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        host_addr_len = ptr - host_addr;
        *request = (char *)ptr;
        port_ptr = strstr(host_addr, ":");
        if (port_ptr && port_ptr < ptr)
        {
            int port_len = ptr - port_ptr - 1;
            strncpy(port_str, port_ptr + 1, port_len);
            port_str[port_len] = '\0';
            host_addr_len = port_ptr - host_addr;
        }
    }
    if ((host_addr_len < 1) || (host_addr_len > url_len))
    {
        rc = -WEBCLIENT_ERROR;
        goto __exit;
    }
    {
        char *host_addr_new = web_malloc(host_addr_len + 1);
        if (!host_addr_new)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
        memcpy(host_addr_new, host_addr, host_addr_len);
        host_addr_new[host_addr_len] = '\0';
        session->host = host_addr_new;
#ifdef WEBCLIENT_USING_TLS
        if (session->tls_session)
            session->tls_session->host = strdup(host_addr_new);
#endif
    }
    {
        struct addrinfo hint;
        int ret;
        memset(&hint, 0, sizeof(hint));
#ifdef WEBCLIENT_USING_TLS
        if (session->tls_session)
        {
            session->tls_session->port = strdup(port_str);
            ret = getaddrinfo(session->tls_session->host, port_str, &hint, res);
            if (ret != 0)
            {
                rc = -WEBCLIENT_ERROR;
            }
            goto __exit;
        }
#endif
        ret = getaddrinfo(session->host, port_str, &hint, res);
        if (ret != 0)
        {
            rc = -WEBCLIENT_ERROR;
            goto __exit;
        }
    }
__exit:
    if (rc != WEBCLIENT_OK)
    {
        if (session->host)
        {
            web_free(session->host);
            session->host = NULL;
        }
        if (*res)
        {
            freeaddrinfo(*res);
            *res = NULL;
        }
    }
    return rc;
}
static int webclient_resolve_address ( struct webclient_session * session , struct addrinfo * * res , const char * url , char * * request ) { int rc = WEBCLIENT_OK ; char * ptr ; char port_str [ 6 ] = " " ; const char * host_addr = 0 ; int url_len , host_addr_len = 0 ; if ( ! res || ! request ) { return WEBCLIENT_ERROR ; } url_len = strlen ( url ) ; if ( strncmp ( url , " " , 7 ) == 0 ) { host_addr = url + 7 ; } else if ( strncmp ( url , " " , 8 ) == 0 ) { strncpy ( port_str , " " , 4 ) ; host_addr = url + 8 ; } else { rc = - WEBCLIENT_ERROR ; goto __exit ; } if ( host_addr [ 0 ] == ' ' ) { host_addr += 1 ; ptr = strstr ( host_addr , " " ) ; if ( ! ptr ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } host_addr_len = ptr - host_addr ; ptr = strstr ( host_addr + host_addr_len , " " ) ; if ( ! ptr ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } else if ( ptr != ( host_addr + host_addr_len + 1 ) ) { int port_len = ptr - host_addr - host_addr_len - 2 ; strncpy ( port_str , host_addr + host_addr_len + 2 , port_len ) ; port_str [ port_len ] = ' \0 ' ; } * request = ( char * ) ptr ; } else { char * port_ptr ; ptr = strstr ( host_addr , " " ) ; if ( ! ptr ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } host_addr_len = ptr - host_addr ; * request = ( char * ) ptr ; port_ptr = strstr ( host_addr , " " ) ; if ( port_ptr && port_ptr < ptr ) { int port_len = ptr - port_ptr - 1 ; strncpy ( port_str , port_ptr + 1 , port_len ) ; port_str [ port_len ] = ' \0 ' ; host_addr_len = port_ptr - host_addr ; } } if ( ( host_addr_len < 1 ) || ( host_addr_len > url_len ) ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } { char * host_addr_new = web_malloc ( host_addr_len + 1 ) ; if ( ! host_addr_new ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } memcpy ( host_addr_new , host_addr , host_addr_len ) ; host_addr_new [ host_addr_len ] = ' \0 ' ; session -> host = host_addr_new ; #ifdef WEBCLIENT_USING_TLS if ( session -> tls_session ) session -> tls_session -> host = strdup ( host_addr_new ) ; #endif } { struct addrinfo hint ; int ret ; memset ( & hint , 0 , sizeof ( hint ) ) ; #ifdef WEBCLIENT_USING_TLS if ( session -> tls_session ) { session -> tls_session -> port = strdup ( port_str ) ; ret = getaddrinfo ( session -> tls_session -> host , port_str , & hint , res ) ; if ( ret != 0 ) { rc = - WEBCLIENT_ERROR ; } goto __exit ; } #endif ret = getaddrinfo ( session -> host , port_str , & hint , res ) ; if ( ret != 0 ) { rc = - WEBCLIENT_ERROR ; goto __exit ; } } __exit : if ( rc != WEBCLIENT_OK ) { if ( session -> host ) { web_free ( session -> host ) ; session -> host = NULL ; } if ( * res ) { freeaddrinfo ( * res ) ; * res = NULL ; } } return rc ; }
resolve server address
 @param session http session
 @param res the server address information
 @param url the input server URI address
 @param request the pointer to point the request url, for example, /index.html
resolve server address @param session http session @param res the server address information @param url the input server URI address @param request the pointer to point the request url for example / index . html
/* default port of 80(http) */ /* strip protocol(http or https) */ /* ipv6 address */ /* ipv4 or domain. */ /* resolve port */ /* get host address ok. */ /* resolve the host name. */ // "getaddrinfo err: %d '%s'", ret, session->host // "getaddrinfo err: %d, host = '%s', port_str = %s.", ret, session->host, port_str