URLParser.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. // get schema [http(s)]://
  8. if (sscanf(url, "%[^:]:/", scratch) > 0) match[1] = scratch;
  9. // get host http(s)://[host]
  10. if (sscanf(url, "htt%*[^:]://%512[^/#]", scratch) > 0) match[2] = scratch;
  11. // get the path
  12. url = strstr(url, match[2].c_str());
  13. if (!url || *url == '\0') return;
  14. url += match[2].size();
  15. if (sscanf(url, "/%512[^?]", scratch) > 0) match[3] = scratch;
  16. // get the query
  17. if (match[3].size()) url += match[3].size() + 1;
  18. if (sscanf(url, "?%512[^#]", scratch) > 0) match[4] = scratch;
  19. // get the hash
  20. if (match[4].size()) url += match[4].size() + 1;
  21. if (sscanf(url, "#%512s", scratch) > 0) match[5] = scratch;
  22. // fix the acquired items
  23. match[3] = "/" + match[3];
  24. if (match[4].size()) match[4] = "?" + match[4];
  25. }
  26. #else
  27. const std::regex URLParser::urlParseRegex = std::regex(
  28. "^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(\\?(?:[^#]*))?(#(?:.*))?");
  29. #endif
  30. }