Aws::String urlEncodeSegment()

in src/aws-cpp-sdk-core/source/http/URI.cpp [33:70]


Aws::String urlEncodeSegment(const Aws::String& segment, bool rfcEncoded = false)
{
    // consolidates legacy escaping logic into one local method
    if (rfcEncoded || s_compliantRfc3986Encoding)
    {
        return StringUtils::URLEncode(segment.c_str());
    }
    else
    {
        Aws::StringStream ss;
        ss << std::hex << std::uppercase;
        for(unsigned char c : segment) // alnum results in UB if the value of c is not unsigned char & is not EOF
        {
            // RFC 3986 §2.3 unreserved characters
            if (StringUtils::IsAlnum(c))
            {
                ss << c;
                continue;
            }
            switch(c)
            {
                // §2.3 unreserved characters
                // The path section of the URL allows unreserved characters to appear unescaped
                case '-': case '_': case '.': case '~':
                // RFC 3986 §2.2 Reserved characters
                // NOTE: this implementation does not accurately implement the RFC on purpose to accommodate for
                // discrepancies in the implementations of URL encoding between AWS services for legacy reasons.
                case '$': case '&': case ',':
                case ':': case '=': case '@':
                    ss << c;
                    break;
                default:
                    ss << '%' << std::setfill('0') << std::setw(2) << (int)c << std::setw(0);
            }
        }
        return ss.str();
    }
}