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