C++ url encode decode
来源:互联网 发布:网络直播的起源与发展 编辑:程序博客网 时间:2024/05/17 12:03
鉴于有时要用C++ 写CGI,url encode、decode是必不可少的,有必要单独整理下。这里吐槽下,C++做web真是笨重,没有python、php爽。
http://blog.csdn.net/nanjunxiao/article/details/9303619之前写过一个简单的encode,这里补充更新下。
#include <string>#include <iostream>using namespace std;//static const char safe[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };static const char safe[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};std::string encode(const std::string &uri){string ret;const unsigned char *ptr = (const unsigned char *)uri.c_str();ret.reserve(uri.length());for (; *ptr ; ++ptr){if (!safe[*ptr]) {char buf[5];memset(buf, 0, 5);#ifdef WIN32_snprintf_s(buf, 5, "%%%X", (*ptr));#elsesnprintf(buf, 5, "%%%X", (*ptr));#endifret.append(buf); }else if (*ptr==' '){ret+='+';}else{ret += *ptr;}}return ret;}std::string decode(const std::string &uri){//Note from RFC1630: "Sequences which start with a percent sign//but are not followed by two hexadecimal characters (0-9,A-F) are reserved//for future extension"const unsigned char *ptr = (const unsigned char *)uri.c_str();string ret;ret.reserve(uri.length());for (; *ptr; ++ptr){if (*ptr == '%'){if (*(ptr + 1)){char a = *(ptr + 1);char b = *(ptr + 2);if (!((a >= 0x30 && a < 0x40) || (a >= 0x41 && a < 0x47))) continue;if (!((b >= 0x30 && b < 0x40) || (b >= 0x41 && b < 0x47))) continue;char buf[3];buf[0] = a;buf[1] = b;buf[2] = 0;ret += (char)strtoul(buf, NULL, 16);ptr += 2;continue;}}if (*ptr=='+'){ret+=' ';continue;}ret += *ptr;}return ret;}
- Javascript url decode, encode
- oracle url encode decode
- C++ url encode decode
- url encode decode
- ExtJs encode & decode & url
- Url decode & encode
- oracle url encode decode
- url encode 和 decode
- URL encode 与 URL decode 的C语言实现
- URL encode 与 URL decode 的C语言实现
- Objective-C url encode/decode url加密解密
- URL encode 与 URL decode 的C语言实现
- URL Encode and URL Decode
- URL解码(Decode)/编码(Encode)
- URL解码(Decode)/编码(Encode)
- URL解码(Decode)/编码(Encode)
- C++ URL encode/decode (gb2312)
- C++ URL encode/decode (gb2312)
- 【字符串操作】04.判断一个字符串是否为回文字符串
- 两点三次埃尔米特插值法
- Linux的简单shell脚本中修改文件操作
- 飘逸的python - 鲜为人知的参数
- 对话框进度条的使用
- C++ url encode decode
- vb.net Emgu.CV 旋转摄像头视频
- weixin
- uva 1484 Alice and Bob's Trip (树形dp)
- [leetcode刷题系列]Sort Colors
- hrbust1828 剪纸条(DP)
- [Leap Motion开发系列]理解C++应用例子
- CSS hack原理
- Excel 和 SQL