This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.
services/http/util/urlencode.cpp@0:dcf3c92487ca, 2011-07-20 (annotated)
- Committer:
- idinor
- Date:
- Wed Jul 20 11:45:39 2011 +0000
- Revision:
- 0:dcf3c92487ca
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
idinor | 0:dcf3c92487ca | 1 | |
idinor | 0:dcf3c92487ca | 2 | /* |
idinor | 0:dcf3c92487ca | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
idinor | 0:dcf3c92487ca | 4 | |
idinor | 0:dcf3c92487ca | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
idinor | 0:dcf3c92487ca | 6 | of this software and associated documentation files (the "Software"), to deal |
idinor | 0:dcf3c92487ca | 7 | in the Software without restriction, including without limitation the rights |
idinor | 0:dcf3c92487ca | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
idinor | 0:dcf3c92487ca | 9 | copies of the Software, and to permit persons to whom the Software is |
idinor | 0:dcf3c92487ca | 10 | furnished to do so, subject to the following conditions: |
idinor | 0:dcf3c92487ca | 11 | |
idinor | 0:dcf3c92487ca | 12 | The above copyright notice and this permission notice shall be included in |
idinor | 0:dcf3c92487ca | 13 | all copies or substantial portions of the Software. |
idinor | 0:dcf3c92487ca | 14 | |
idinor | 0:dcf3c92487ca | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
idinor | 0:dcf3c92487ca | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
idinor | 0:dcf3c92487ca | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
idinor | 0:dcf3c92487ca | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
idinor | 0:dcf3c92487ca | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
idinor | 0:dcf3c92487ca | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
idinor | 0:dcf3c92487ca | 21 | THE SOFTWARE. |
idinor | 0:dcf3c92487ca | 22 | */ |
idinor | 0:dcf3c92487ca | 23 | |
idinor | 0:dcf3c92487ca | 24 | #include "url.h" |
idinor | 0:dcf3c92487ca | 25 | #include <stdlib.h> |
idinor | 0:dcf3c92487ca | 26 | #include <ctype.h> |
idinor | 0:dcf3c92487ca | 27 | |
idinor | 0:dcf3c92487ca | 28 | //From http://www.geekhideout.com/urlcode.shtml |
idinor | 0:dcf3c92487ca | 29 | |
idinor | 0:dcf3c92487ca | 30 | char from_hex(char ch); |
idinor | 0:dcf3c92487ca | 31 | char to_hex(char code); |
idinor | 0:dcf3c92487ca | 32 | |
idinor | 0:dcf3c92487ca | 33 | /* Converts a hex character to its integer value */ |
idinor | 0:dcf3c92487ca | 34 | char from_hex(char ch) { |
idinor | 0:dcf3c92487ca | 35 | return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; |
idinor | 0:dcf3c92487ca | 36 | } |
idinor | 0:dcf3c92487ca | 37 | |
idinor | 0:dcf3c92487ca | 38 | /* Converts an integer value to its hex character*/ |
idinor | 0:dcf3c92487ca | 39 | char to_hex(char code) { |
idinor | 0:dcf3c92487ca | 40 | static char hex[] = "0123456789abcdef"; |
idinor | 0:dcf3c92487ca | 41 | return hex[code & 15]; |
idinor | 0:dcf3c92487ca | 42 | } |
idinor | 0:dcf3c92487ca | 43 | |
idinor | 0:dcf3c92487ca | 44 | /* Returns a url-encoded version of str */ |
idinor | 0:dcf3c92487ca | 45 | /* IMPORTANT: be sure to free() the returned string after use */ |
idinor | 0:dcf3c92487ca | 46 | char *url_encode(char *str) { |
idinor | 0:dcf3c92487ca | 47 | char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
idinor | 0:dcf3c92487ca | 48 | while (*pstr) { |
idinor | 0:dcf3c92487ca | 49 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
idinor | 0:dcf3c92487ca | 50 | *pbuf++ = *pstr; |
idinor | 0:dcf3c92487ca | 51 | else if (*pstr == ' ') |
idinor | 0:dcf3c92487ca | 52 | *pbuf++ = '+'; |
idinor | 0:dcf3c92487ca | 53 | else |
idinor | 0:dcf3c92487ca | 54 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
idinor | 0:dcf3c92487ca | 55 | pstr++; |
idinor | 0:dcf3c92487ca | 56 | } |
idinor | 0:dcf3c92487ca | 57 | *pbuf = '\0'; |
idinor | 0:dcf3c92487ca | 58 | return buf; |
idinor | 0:dcf3c92487ca | 59 | } |
idinor | 0:dcf3c92487ca | 60 | |
idinor | 0:dcf3c92487ca | 61 | /* Returns a url-decoded version of str */ |
idinor | 0:dcf3c92487ca | 62 | /* IMPORTANT: be sure to free() the returned string after use */ |
idinor | 0:dcf3c92487ca | 63 | char *url_decode(char *str) { |
idinor | 0:dcf3c92487ca | 64 | char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf; |
idinor | 0:dcf3c92487ca | 65 | while (*pstr) { |
idinor | 0:dcf3c92487ca | 66 | if (*pstr == '%') { |
idinor | 0:dcf3c92487ca | 67 | if (pstr[1] && pstr[2]) { |
idinor | 0:dcf3c92487ca | 68 | *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); |
idinor | 0:dcf3c92487ca | 69 | pstr += 2; |
idinor | 0:dcf3c92487ca | 70 | } |
idinor | 0:dcf3c92487ca | 71 | } else if (*pstr == '+') { |
idinor | 0:dcf3c92487ca | 72 | *pbuf++ = ' '; |
idinor | 0:dcf3c92487ca | 73 | } else { |
idinor | 0:dcf3c92487ca | 74 | *pbuf++ = *pstr; |
idinor | 0:dcf3c92487ca | 75 | } |
idinor | 0:dcf3c92487ca | 76 | pstr++; |
idinor | 0:dcf3c92487ca | 77 | } |
idinor | 0:dcf3c92487ca | 78 | *pbuf = '\0'; |
idinor | 0:dcf3c92487ca | 79 | return buf; |
idinor | 0:dcf3c92487ca | 80 | } |