std::string curl_easy::escape()

in src/Windows/curl_easy.cpp [482:523]


std::string curl_easy::escape(const char* url, int length)
{
    std::string escapedHeader;
    if (length == 0)
    {
        length = static_cast<int>(strlen(url));
    }

    for (int i = 0; i < length; i += 1)
    {
        char ch = url[i];
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
            (ch >= '0' && ch <= '9') || (ch == '-') || (ch == '.') ||
            (ch == '~') || (ch == '?') || (ch == '_'))
        {
            escapedHeader.push_back(ch);
        }
        else
        {
            escapedHeader.push_back('%');
            char firstNibble = HexAsciiFromUInt8(ch >> 4);
            if (firstNibble < 0)
            {
                std::stringstream ss;
                ss << "Bogus hex value " << (uint8_t)ch << "(" << ch
                   << ") in URL encoding in header " << url;
                throw_on_error(EBADMSG, ss.str());
            }
            escapedHeader.push_back(firstNibble);
            char secondNibble = HexAsciiFromUInt8(ch & 0x0f);
            if (secondNibble < 0)
            {
                std::stringstream ss;
                ss << "Bogus hex value " << (uint8_t)ch << "(" << ch
                   << ") in URL encoding in header " << url;
                throw_on_error(EBADMSG, ss.str());
            }
            escapedHeader.push_back(secondNibble);
        }
    }
    return escapedHeader;
}