Best ways of parsing a URL using C?

后端 未结 10 1841
死守一世寂寞
死守一世寂寞 2020-11-27 15:36

I have a URL like this:

http://192.168.0.1:8080/servlet/rece

I want to parse the URL to get the values:

IP: 192.168.0.1
Por         


        
10条回答
  •  日久生厌
    2020-11-27 15:41

    I wrote this

    #include 
    #include 
    #include 
    #include 
    typedef struct
    {
        const char* protocol = 0;
        const char* site = 0;
        const char* port = 0;
        const char* path = 0;
    } URL_INFO;
    URL_INFO* split_url(URL_INFO* info, const char* url)
    {
        if (!info || !url)
            return NULL;
        info->protocol = strtok(strcpy((char*)malloc(strlen(url)+1), url), "://");
        info->site = strstr(url, "://");
        if (info->site)
        {
            info->site += 3;
            char* site_port_path = strcpy((char*)calloc(1, strlen(info->site) + 1), info->site);
            info->site = strtok(site_port_path, ":");
            info->site = strtok(site_port_path, "/");
        }
        else
        {
            char* site_port_path = strcpy((char*)calloc(1, strlen(url) + 1), url);
            info->site = strtok(site_port_path, ":");
            info->site = strtok(site_port_path, "/");
        }
        char* URL = strcpy((char*)malloc(strlen(url) + 1), url);
        info->port = strstr(URL + 6, ":");
        char* port_path = 0;
        char* port_path_copy = 0;
        if (info->port && isdigit(*(port_path = (char*)info->port + 1)))
        {
            port_path_copy = strcpy((char*)malloc(strlen(port_path) + 1), port_path);
            char * r = strtok(port_path, "/");
            if (r)
                info->port = r;
            else
                info->port = port_path;
        }
        else
            info->port = "80";
        if (port_path_copy)
            info->path = port_path_copy + strlen(info->port ? info->port : "");
        else 
        {
            char* path = strstr(URL + 8, "/");
            info->path = path ? path : "/";
        }
        int r = strcmp(info->protocol, info->site) == 0;
        if (r && info->port == "80")
            info->protocol = "http";
        else if (r)
            info->protocol = "tcp";
        return info;
    }
    

    Test

    int main()
    {
        URL_INFO info;
        split_url(&info, "ftp://192.168.0.1:8080/servlet/rece");
        printf("Protocol: %s\nSite: %s\nPort: %s\nPath: %s\n", info.protocol, info.site, info.port, info.path);
        return 0;
    }
    

    Out

    Protocol: ftp
    Site: 192.168.0.1
    Port: 8080
    Path: /servlet/rece
    

提交回复
热议问题