AttestationResult ParseURL()

in client-library/src/Attestation/AttestationClient/lib/AttestationLibUtils.cpp [645:680]


    AttestationResult ParseURL(const std::string& url,
                               std::string& domain) {
        AttestationResult result(AttestationResult::ErrorCode::SUCCESS);
        if (url.empty()) {
            return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_INVALID_INPUT_PARAMETER,
                                        "Invalid input parameter");
        }

        std::string sanitized_url = url;
        // trim the url from left and right ends
        boost::trim_left(sanitized_url);
        boost::trim_right(sanitized_url);

        std::string path, dns, protocol, port, query;
        int offset = 0;
        size_t path_idx, port_idx, query_idx;
        offset = offset == 0 && sanitized_url.compare(0, 8, "https://") == 0 ? 8 : offset;
        offset = offset == 0 && sanitized_url.compare(0, 7, "http://") == 0 ? 7 : offset;
        path_idx = sanitized_url.find_first_of('/', offset + 1);
        path = path_idx == std::string::npos ? "" : sanitized_url.substr(path_idx);
        dns = std::string(sanitized_url.begin() + offset, path_idx != std::string::npos ? sanitized_url.begin() + path_idx : sanitized_url.end());
        port = (port_idx = dns.find(":")) != std::string::npos ? dns.substr(port_idx + 1) : "";
        dns = dns.substr(0, port_idx != std::string::npos ? port_idx : dns.length());
        protocol = offset > 0 ? sanitized_url.substr(0, offset - 3) : "";
        query = (query_idx = path.find("?")) != std::string::npos ? path.substr(query_idx + 1) : "";
        path = query_idx != std::string::npos ? path.substr(0, query_idx) : path;
        if (dns.empty()) {
            return LogErrorAndGetResult(AttestationResult::ErrorCode::ERROR_PARSING_DNS_INFO,
                "Error extracting DNS info from URL");
        }

        CLIENT_LOG_INFO("Attestation URL info - protocol {%s}, domain {%s}", 
                            protocol.c_str(), dns.c_str());
        domain = dns;
        return result;
    }