URLParser.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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)
  11. match[1] = scratch;
  12. // get the host
  13. if (sscanf(url, "htt%*[^:]://%512[^/#?]", scratch) > 0)
  14. match[2] = scratch;
  15. // get the path
  16. url = strstr(url, match[2].c_str()) + match[2].size();
  17. if (sscanf(url, "/%512[^?]", scratch) > 0)
  18. match[3] = scratch;
  19. else if (*url && *url != '?' && *url != '#')
  20. url++;
  21. // get the query
  22. if (match[3].size())
  23. url += match[3].size() + 1;
  24. if (sscanf(url, "?%512[^#]", scratch) > 0)
  25. match[4] = scratch;
  26. // get the hash
  27. if (match[4].size())
  28. url += match[4].size() + 1;
  29. if (sscanf(url, "#%512s", scratch) > 0)
  30. match[5] = scratch;
  31. // fix the acquired items
  32. match[3] = "/" + match[3];
  33. if (match[4].size())
  34. match[4] = "?" + match[4];
  35. // need at least schema and host
  36. if (match[1].size() == 0 || match[2].size() == 0)
  37. match.clear();
  38. }
  39. #else
  40. const std::regex URLParser::urlParseRegex = std::regex(
  41. "^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
  42. #endif
  43. } // namespace bell