URLParser.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "URLParser.h"
  2. namespace bell {
  3. #ifdef BELL_DISABLE_REGEX
  4. void URLParser::parse(const char* url, std::vector<std::string>& match) {
  5. match[0] = url;
  6. char scratch[512];
  7. /* Parsing the following (http|https://[host][/path][?query]#hash] as in regex
  8. * below. This needs to be changed if you update that regex */
  9. // get the schema
  10. if (sscanf(url, "%[^:]:/", scratch) > 0) match[1] = scratch;
  11. // get the host
  12. if (sscanf(url, "htt%*[^:]://%512[^/#?]", scratch) > 0) match[2] = scratch;
  13. // get the path
  14. url = strstr(url, match[2].c_str()) + match[2].size();
  15. if (sscanf(url, "/%512[^?]", scratch) > 0) match[3] = scratch;
  16. else if (*url && *url != '?' && *url != '#') url++;
  17. // get the query
  18. if (match[3].size()) url += match[3].size() + 1;
  19. if (sscanf(url, "?%512[^#]", scratch) > 0) match[4] = scratch;
  20. // get the hash
  21. if (match[4].size()) url += match[4].size() + 1;
  22. if (sscanf(url, "#%512s", scratch) > 0) match[5] = scratch;
  23. // fix the acquired items
  24. match[3] = "/" + match[3];
  25. if (match[4].size()) match[4] = "?" + match[4];
  26. // need at least schema and host
  27. if (match[1].size() == 0 || match[2].size() == 0) match.clear();
  28. }
  29. #else
  30. const std::regex URLParser::urlParseRegex = std::regex(
  31. "^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
  32. #endif
  33. }