I figured it out. I used the library uriparser: http://uriparser.sourceforge.net/ and hastily implemented the function as follows. It does sparse error checking and may leak memory.
std::string urljoin(std::string &base, std::string &relative)
{
UriParserStateA state;
UriUriA uriOne;
UriUriA uriTwo;
state.uri = &uriOne;
if (uriParseUriA(&state, base.c_str()) != URI_SUCCESS)
{
return "";
}
state.uri = &uriTwo;
if (uriParseUriA(&state, relative.c_str()) != URI_SUCCESS)
{
uriFreeUriMembersA(&uriTwo);
return "";
}
UriUriA result;
if (uriAddBaseUriA(&result, &uriTwo, &uriOne) != URI_SUCCESS)
{
uriFreeUriMembersA(&result);
return "";
}
uriFreeUriMembersA(&uriOne);
uriFreeUriMembersA(&uriTwo);
int charsRequired;
uriToStringCharsRequiredA(&result, &charsRequired);
charsRequired++;
char *buf = (char*) malloc(charsRequired * sizeof(char)); if (uriToStringA(buf, &result, charsRequired, NULL) != URI_SUCCESS)
return "";
uriFreeUriMembersA(&result);
std::string ret(buf);
free(buf);
return ret;
}