Best ways of parsing a URL using C?

后端 未结 10 1805
死守一世寂寞
死守一世寂寞 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 16:07

    This C gist could be useful. It implements a pure C solution with sscanf.

    https://github.com/luismartingil/per.scripts/tree/master/c_parse_http_url

    It uses

    // Parsing the tmp_source char*
    if (sscanf(tmp_source, "http://%99[^:]:%i/%199[^\n]", ip, &port, page) == 3) { succ_parsing = 1;}
    else if (sscanf(tmp_source, "http://%99[^/]/%199[^\n]", ip, page) == 2) { succ_parsing = 1;}
    else if (sscanf(tmp_source, "http://%99[^:]:%i[^\n]", ip, &port) == 2) { succ_parsing = 1;}
    else if (sscanf(tmp_source, "http://%99[^\n]", ip) == 1) { succ_parsing = 1;}
    (...)
    

提交回复
热议问题