* @brief URI-encode a character string (AWS specific version, see spec) * * @see AWS spec: http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html * * @todo Consider reusing / converting to TSStringPercentEncode() using a custom map to account for the AWS specific rules. * Currently we don't build a library/archive so we could link with the unit-test binary. Also us
| 79 | * @return encoded string. |
| 80 | */ |
| 81 | String |
| 82 | uriEncode(const String &in, bool isObjectName) |
| 83 | { |
| 84 | std::stringstream result; |
| 85 | |
| 86 | for (char i : in) { |
| 87 | if (isalnum(i) || i == '-' || i == '_' || i == '.' || i == '~') { |
| 88 | /* URI encode every byte except the unreserved characters: |
| 89 | * 'A'-'Z', 'a'-'z', '0'-'9', '-', '.', '_', and '~'. */ |
| 90 | result << i; |
| 91 | } else if (i == ' ') { |
| 92 | /* The space character is a reserved character and must be encoded as "%20" (and not as "+"). */ |
| 93 | result << "%20"; |
| 94 | } else if (isObjectName && i == '/') { |
| 95 | /* Encode the forward slash character, '/', everywhere except in the object key name. */ |
| 96 | result << "/"; |
| 97 | } else if (i == '+') { |
| 98 | /* Only written in the example code, but a plus sign is treated as a space regardless of the position and it must be encoded |
| 99 | * as "%20" instead of "%2B" */ |
| 100 | result << "%20"; |
| 101 | } else { |
| 102 | /* Letters in the hexadecimal value must be upper-case, for example "%1A". */ |
| 103 | result << "%" << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(i); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return result.str(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @brief checks if the string is URI-encoded (AWS specific encoding version, see spec) |
no test coverage detected