解析URL, 从中解出来host, path

/* 解析URL, 从中解出来host, path */
static int32_t _download_parse_url(const char *url, char *host, uint32_t max_host_len, char *path,
                                   uint32_t max_path_len)
{
          
   
    char *host_ptr = (char *) strstr(url, "://");
    uint32_t host_len = 0;
    uint32_t path_len;
    char *path_ptr;
    char *fragment_ptr;

    if (host_ptr == NULL) {
          
   
        return STATE_OTA_PARSE_URL_HOST_IS_NULL;
    }
    host_ptr += 3;

    path_ptr = strchr(host_ptr, /);
    if (NULL == path_ptr) {
          
   
        return STATE_OTA_PARSE_URL_PATH_IS_NULL;
    }

    if (host_len == 0) {
          
   
        host_len = path_ptr - host_ptr;
    }

    if (host_len >= max_host_len) {
          
   
        return STATE_OTA_HOST_STRING_OVERFLOW;
    }

    memcpy(host, host_ptr, host_len);
    host[host_len] = ;
    fragment_ptr = strchr(host_ptr, #);
    if (fragment_ptr != NULL) {
          
   
        path_len = fragment_ptr - path_ptr;
    } else {
          
   
        path_len = strlen(path_ptr);
    }

    if (path_len >= max_path_len) {
          
   
        return STATE_OTA_PATH_STRING_OVERFLOW;
    }

    memcpy(path, path_ptr, path_len);
    path[path_len] = ;
    return STATE_SUCCESS;
}
经验分享 程序员 微信小程序 职场和发展