Is there an API in Windows that can crack a url into parts?
Background
The format of a URL is:
stackoverflow://iboyd:password01@mail.stackoverflow.com:12386/questions/SubmitQuestion.aspx?useLiveData=1&internal=0#nose
\___________/ \___/ \________/ \____________________/ \___/ \___________________________/\_______________________/ \__/
| | | | | | | |
scheme username password hostname port path query fragment
Is there a function in (native) Win32 api that can crack a URL into parts:
- Scheme:
stackoverflow
- Username:
iboyd
- Password:
password01
- Host name:
mail.stackoverflow.com
- Port:
12386
- Path:
questions/SubmitQuestion.aspx
- Query:
?useLiveData=1&internal=0
- Fragment:
nose
Some functions don't work
There are some functions in WinApi, but they fail to do the job because they don't understand schemes except the ones that WinHttp
can use:
- UrlGetPart
- WinHttpCrackUrl (forbids any scheme other than http or https)
both fail to understand urls such as:
ws://stackoverflow.com
(web-socket)wss://stackoverflow.com
(web-socket secure)sftp://fincen.gov/submit
(SSL file transfer)magnet:?xt=urn:btih:c4244b6d0901f71add9a1f9e88013a2fa51a9900
stratum+udp://blockchain.info
WinHttpCrackUrl actively prevents being used to crack URLs:
If the Internet protocol of the URL passed in for pwszUrl is not HTTP or HTTPS, then WinHttpCrackUrl returns FALSE and GetLastError indicates ERROR_WINHTTP_UNRECOGNIZED_SCHEME.
Is there another native API in Windows that can get parts of a url?
Bonus Chatter
Here's how you do it in CLR (e.g. C#): (fiddle)
using System;
public class Program
{
public static void Main()
{
var uri = new Uri("stackoverflow://iboyd:password01@mail.stackoverflow.com:12386/questions/SubmitQuestion.aspx?useLiveData=1&internal=0#nose");
Console.WriteLine("Uri.Scheme: "+uri.Scheme);
Console.WriteLine("Uri.UserInfo: "+uri.UserInfo);
Console.WriteLine("Uri.Host: "+uri.Host);
Console.WriteLine("Uri.Port: "+uri.Port);
Console.WriteLine("Uri.AbsolutePath: "+uri.AbsolutePath);
Console.WriteLine("Uri.Query: "+uri.Query);
Console.WriteLine("Uri.Fragment: "+uri.Fragment);
}
}
Outputs
Uri.Scheme: stackoverflow
Uri.UserInfo: iboyd:password01
Uri.Host: mail.stackoverflow.com
Uri.Port: 12386
Uri.AbsolutePath: /questions/SubmitQuestion.aspx
Uri.Query: ?useLiveData=1&internal=0
Uri.Fragment: #nose