void CHttpUrl::ParsingProtocol(std::string const& url) { auto pos = url.find("://"); if (pos == std::string::npos) { throw CUrlParsingError("Protocol parsing error"); } std::string protocol = url.substr(0, pos); if (MapOfProtocol.find(protocol) == MapOfProtocol.end()) { throw CUrlParsingError("Protocol parsing error"); } m_pos = pos + 3; m_protocol = MapOfProtocol.find(protocol)->second; }
CHttpUrl::CHttpUrl(std::string const & domain, std::string const & document, Protocol protocol, unsigned short port) : m_domain(domain) , m_document(document) , m_protocol(protocol) , m_port(port) { if (!IsDomainCorrect()) { throw CUrlParsingError("Invalid domain"); } }
void CHttpUrl::ParsingPort(std::string const& url) { if (url[m_pos] == ':') { auto pos = url.find('/', m_pos); std::string port; ++m_pos; if (pos == std::string::npos) { port = url.substr(m_pos, url.size() - m_pos + 1); } else { port = url.substr(m_pos, pos - m_pos ); } m_pos = pos; //auto notDigit = [](char const& ch) { return isdigit(ch); }; if (port.size() > 3 || !(std::any_of(port.begin(), port.end(), isdigit))) { throw CUrlParsingError("Port parsing error"); } if (port.size() == 0) { SetDefoultPort(); } m_port = static_cast<unsigned short>(std::atoi(port.c_str())); } else if (url[m_pos] == '/') { SetDefoultPort(); } else { throw CUrlParsingError("Port parsing error"); } }
unsigned short CHttpUrl::ParsePort(boost::string_ref const &url, size_t index) { std::string port; if (index == url.size() || (index < url.size() && !isdigit(url[index]))) { throw CUrlParsingError("Unknown port in the url address"); } for (; index != url.size(); ++index) { if (!isdigit(url[index])) { break; } port += url[index]; } return static_cast<unsigned short>(atoi(port.c_str())); }
void CHttpUrl::ParsingDomain(std::string const& url) { auto pos = url.find(':', m_pos); if (pos == std::string::npos) { pos = url.find('/', m_pos); pos = (pos == std::string::npos ? url.size() : pos); } auto domain = url.substr(m_pos, pos - m_pos); m_pos = pos; if (!IsDomainCorrect()) { throw CUrlParsingError("Invalid domain"); } m_domain = domain; }
void CHttpUrl::ParseUrl(std::string const &url) { try { boost::string_ref refUrl(url); size_t begin = 0; m_protocol = CheckProtocol(refUrl, begin); if (m_protocol == Protocol::HTTPS) { m_port = 443u; } else { m_port = 80u; } m_domain = ValidateDomainName(ParseDomainName(url.substr(begin), begin)); m_document = ValidateDocumentName(url.substr(begin)); } catch (std::invalid_argument const &e) { throw CUrlParsingError(e.what()); } }
CHttpUrl::CHttpUrl(std::string const &url) { const std::string http("http://"); const std::string https("https://"); unsigned long protocolPosition; std::string domain; std::string document; Protocol protocol; unsigned short port; if (IsProtocolInUrl(http, url)) { protocolPosition = url.substr(0, http.size()).find_last_of(http); protocol = HTTP; } else if (IsProtocolInUrl(https, url)) { protocolPosition = url.substr(0, https.size()).find_last_of(https); protocol = HTTPS; } else { throw CUrlParsingError( "You should pass a url with protocol at the beginning. For example: <https://www.amazon.com>"); } std::string str = url.substr(protocolPosition); auto portPosition = str.find_first_of(":"); if (portPosition == std::string::npos) { auto positionOfDocument = str.substr(1).find_first_of("/"); if (positionOfDocument != std::string::npos) { document = str.substr(positionOfDocument + 1); } if (IsDomainCorrect(str.substr(1, positionOfDocument))) { domain = str.substr(1, positionOfDocument); } else { throw CUrlParsingError("Domain can't contain a whitespaces"); } domain = str.substr(1, positionOfDocument); if (protocol == HTTPS) { port = 443; InitializePartsOfUrl(domain, document, protocol, port); } else { InitializePartsOfUrl(domain, document, protocol); } } else { auto positionOfDocument = str.substr(1).find_first_of("/"); std::string expectedPort = str.substr(portPosition + 1, positionOfDocument - portPosition); if (!IsNumber(expectedPort) || !IsPortCorrect((atoi(expectedPort.c_str())))) { throw CUrlParsingError("Unknown port"); } port = static_cast<unsigned short>(atoi(expectedPort.c_str())); if (positionOfDocument != std::string::npos) { document = str.substr(positionOfDocument + 1); } if (IsDomainCorrect(str.substr(1, portPosition - 1))) { domain = str.substr(1, portPosition - 1); } else { throw CUrlParsingError("Domain can't contain a whitespaces"); } InitializePartsOfUrl(domain, document, protocol, port); } }