void Url::Parse(string_view str) {
  std::string tmp = ToString(str);
  boost::trim_left(tmp);

  std::size_t p = std::string::npos;

  p = tmp.find("://");
  if (p != std::string::npos) {
    scheme_ = tmp.substr(0, p);
    tmp = tmp.substr(p + 3);
  }

  p = tmp.find('/');
  if (p != std::string::npos) {
    host_ = tmp.substr(0, p);

    tmp = tmp.substr(p);

    p = tmp.find('?');
    if (p != std::string::npos) {
      path_ = tmp.substr(0, p);
      query_ = tmp.substr(p + 1);
    } else {
      path_ = tmp;
    }
  } else {
    path_ = "";

    p = tmp.find('?');
    if (p != std::string::npos) {
      host_ = tmp.substr(0, p);
      query_ = tmp.substr(p + 1);
    } else {
      host_ = tmp;
    }
  }

  if (!host_.empty()) {
    // Check if there's a port.
    p = host_.find_last_of(':');
    if (p != std::string::npos) {
      // For IPv6: [::1]:8080
      std::size_t bracket = host_.find_last_of(']');
      if (bracket == std::string::npos || p > bracket) {
        port_ = host_.substr(p + 1);
        host_ = host_.substr(0, p);
      }
    }
  }
}
void Url::Parse(string_view str) {
  std::string tmp = ToString(str);
  boost::trim_left(tmp);

  std::size_t p = std::string::npos;

  p = tmp.find("://");
  if (p != std::string::npos) {
    scheme_ = tmp.substr(0, p);
    tmp = tmp.substr(p + 3);
  }

  p = tmp.find('/');
  if (p != std::string::npos) {
    host_ = tmp.substr(0, p);

    tmp = tmp.substr(p);

    p = tmp.find('?');
    if (p != std::string::npos) {
      path_ = tmp.substr(0, p);
      query_ = tmp.substr(p + 1);
    } else {
      path_ = tmp;
    }
  } else {
    path_ = "";

    p = tmp.find('?');
    if (p != std::string::npos) {
      host_ = tmp.substr(0, p);
      query_ = tmp.substr(p + 1);
    } else {
      host_ = tmp;
    }
  }

  if (!host_.empty()) {
     
    p = host_.find_last_of(':');
    if (p != std::string::npos) {
       
      std::size_t bracket = host_.find_last_of(']');
      if (bracket == std::string::npos || p > bracket) {
        port_ = host_.substr(p + 1);
        host_ = host_.substr(0, p);
      }
    }
  }
}