dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Free Software Foundation, Inc. |
| 3 | * |
| 4 | * |
| 5 | * This software is distributed under the terms of the GNU Affero Public License. |
| 6 | * See the COPYING file in the main directory for details. |
| 7 | * |
| 8 | * This use of this software may be subject to additional restrictions. |
| 9 | * See the LEGAL file in the main directory for details. |
| 10 | |
| 11 | This program is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU Affero General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU Affero General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU Affero General Public License |
| 22 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | */ |
| 25 | |
| 26 | #include <URLEncode.h> |
| 27 | #include <string> |
| 28 | #include <string.h> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | using namespace std; |
| 32 | |
| 33 | //based on javascript encodeURIComponent() |
| 34 | string URLEncode(const string &c) |
| 35 | { |
| 36 | static const char *digits = "01234567890ABCDEF"; |
| 37 | string retVal=""; |
kurtis.heimerl | 1a2d244 | 2012-12-22 04:30:56 +0000 | [diff] [blame] | 38 | for (size_t i=0; i<c.length(); i++) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 39 | { |
| 40 | const char ch = c[i]; |
| 41 | if (isalnum(ch) || strchr("-_.!~'()",ch)) { |
| 42 | retVal += ch; |
| 43 | } else { |
| 44 | retVal += '%'; |
| 45 | retVal += digits[(ch>>4) & 0x0f]; |
| 46 | retVal += digits[ch & 0x0f]; |
| 47 | } |
| 48 | } |
| 49 | return retVal; |
| 50 | } |
| 51 | |