Question

I am using VC++ and trying to parse the ip (ipv4/ipv6) address from the url received. Is there any way I can achieve this. I know one way is to iterate through each character in the url and to look for [ and ] in the url for ipv6. This doesn't seem to be a good way so wondering if there is any function which help me extracting ip address from the URL?

For example I've url as given below,

http://[fe80::222:bdff:fef5:56a4]:80/index.html?version=1.0&id=1

Was it helpful?

Solution

Since you tagged the question with , I could suggest you to use InternetCrackUrl() from wininet library for parsing.

OTHER TIPS

A bit of code from a url parser. Regular expressions are overkill.

    // Look for IPv6
    if( s[domain_start] == '[' ) {
        pos = s.find(']', domain_start);
        if (pos != string::npos && pos < domain_end && s[pos+1] == ':')
            ++pos;
        else
            pos = string::npos;
    } else {
        pos = s.find(':', domain_start);
    }

But hey if you like regular expressions here is one from a Perl URL cracker:

my ($protocol, $domain, $port, $path, $query, $fragment) = $url =~
    m!
        ^([^:/]*://)?
        ((?:\[[^]]*]) | (?:[^:/\\\#?]*))
        (:\d+)?
        (/[^?#]*)?
        (\?[^#]*)?
        (\#.*)?
    !x;

It should be straight-forward to convert that to a C++ regex library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top