BellSocket.cpp 583 B

123456789101112131415161718192021
  1. // Copyright (c) Kuba Szczodrzyński 2021-12-21.
  2. #include "BellSocket.h"
  3. #include <cstring>
  4. void bell::Socket::open(const std::string &url) {
  5. auto *urlStr = url.c_str();
  6. bool https = urlStr[4] == 's';
  7. uint16_t port = https ? 443 : 80;
  8. auto *hostname = urlStr + (https ? 8 : 7);
  9. auto *hostnameEnd = strchr(hostname, ':');
  10. auto *path = strchr(hostname, '/');
  11. if (hostnameEnd == nullptr) {
  12. hostnameEnd = path;
  13. } else {
  14. port = strtol(hostnameEnd + 1, nullptr, 10);
  15. }
  16. auto hostnameStr = std::string(hostname, (const char *)hostnameEnd);
  17. this->open(hostnameStr, port);
  18. }