Exemple #1
0
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");
	}
}
Exemple #2
0
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;
	
}
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);
    }
}