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