Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Last active December 17, 2015 18:59
Show Gist options
  • Save hoehrmann/5657552 to your computer and use it in GitHub Desktop.
Save hoehrmann/5657552 to your computer and use it in GitHub Desktop.
This command line utility prints out how WinINet, which is used by Internet Explorer, processes URLs, via InternetCombineUrl which combines a base reference with a relative reference, and via InternetCrackUrl, which splits URLs into various components. Originally http://lists.w3.org/Archives/Public/www-archive/2011Aug/0000.html
#undef UNICODE
#include <Windows.h>
#include <WinInet.h>
#include <stdio.h>
#pragma comment(lib, "wininet.lib")
int
main(int argc, char *argv[]) {
if (argc != 2 && argc != 3) {
printf("Usage: `%s <URL>`, or `%s <Base> <Relative>`\n",
argv[0], argv[0]);
return 1;
}
DWORD length = 1024;
char temp[1024];
char* absolute = argv[1];
if (argc == 3) {
if (!InternetCombineUrl(argv[1], argv[2], temp, &length, 0))
return GetLastError();
absolute = temp;
}
URL_COMPONENTS parts;
memset(&parts, 0, sizeof(parts));
parts.dwStructSize = sizeof(parts);
parts.dwSchemeLength = 1;
parts.dwHostNameLength = 1;
parts.dwUserNameLength = 1;
parts.dwPasswordLength = 1;
parts.dwUrlPathLength = 1;
parts.dwExtraInfoLength = 1;
if (!InternetCrackUrl(absolute, strlen(absolute), 0, &parts))
return GetLastError();
printf(""
"Absolute: %s\n"
"Scheme: %.*s\n"
"HostName: %.*s\n"
"UserName: %.*s\n"
"Password: %.*s\n"
"UrlPath: %.*s\n"
"ExtraInfo: %.*s\n",
absolute,
parts.dwSchemeLength,
parts.lpszScheme,
parts.dwHostNameLength,
parts.lpszHostName,
parts.dwUserNameLength,
parts.lpszUserName,
parts.dwPasswordLength,
parts.lpszPassword,
parts.dwUrlPathLength,
parts.lpszUrlPath,
parts.dwExtraInfoLength,
parts.lpszExtraInfo
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment