c++ html encode,urlencode - Encode/Decode URLs in C++ - Stack Overflow

This version is pure C and can optionally normalize the resource path. Using it with C++ is trivial:

#include

#include

int main(int argc, char** argv)

{

const std::string src("/some.url/foo/../bar/%2e/");

std::cout << "src=\"" << src << "\"" << std::endl;

// either do it the C++ conformant way:

char* dst_buf = new char[src.size() + 1];

urldecode(dst_buf, src.c_str(), 1);

std::string dst1(dst_buf);

delete[] dst_buf;

std::cout << "dst1=\"" << dst1 << "\"" << std::endl;

// or in-place with the &[0] trick to skip the new/delete

std::string dst2;

dst2.resize(src.size() + 1);

dst2.resize(urldecode(&dst2[0], src.c_str(), 1));

std::cout << "dst2=\"" << dst2 << "\"" << std::endl;

}

Outputs:

src="/some.url/foo/../bar/%2e/"

dst1="/some.url/bar/"

dst2="/some.url/bar/"

And the actual function:

#include

#include

/**

* decode a percent-encoded C string with optional path normalization

*

* The buffer pointed to by @dst must be at least strlen(@src) bytes.

* Decoding stops at the first character from @src that decodes to null.

* Path normalization will remove redundant slashes and slash+dot sequences,

* as well as removing path components when slash+dot+dot is found. It will

* keep the root slash (if one was present) and will stop normalization

* at the first questionmark found (so query parameters won't be normalized).

*

* @param dst destination buffer

* @param src source buffer

* @param normalize perform path normalization if nonzero

* @return number of valid characters in @dst

* @author Johan Lindh

* @legalese BSD licensed (http://opensource.org/licenses/BSD-2-Clause)

*/

ptrdiff_t urldecode(char* dst, const char* src, int normalize)

{

char* org_dst = dst;

int slash_dot_dot = 0;

char ch, a, b;

do {

ch = *src++;

if (ch == '%' && isxdigit(a = src[0]) && isxdigit(b = src[1])) {

if (a < 'A') a -= '0';

else if(a < 'a') a -= 'A' - 10;

else a -= 'a' - 10;

if (b < 'A') b -= '0';

else if(b < 'a') b -= 'A' - 10;

else b -= 'a' - 10;

ch = 16 * a + b;

src += 2;

}

if (normalize) {

switch (ch) {

case '/':

if (slash_dot_dot < 3) {

/* compress consecutive slashes and remove slash-dot */

dst -= slash_dot_dot;

slash_dot_dot = 1;

break;

}

/* fall-through */

case '?':

/* at start of query, stop normalizing */

if (ch == '?')

normalize = 0;

/* fall-through */

case '\0':

if (slash_dot_dot > 1) {

/* remove trailing slash-dot-(dot) */

dst -= slash_dot_dot;

/* remove parent directory if it was two dots */

if (slash_dot_dot == 3)

while (dst > org_dst && *--dst != '/')

/* empty body */;

slash_dot_dot = (ch == '/') ? 1 : 0;

/* keep the root slash if any */

if (!slash_dot_dot && dst == org_dst && *dst == '/')

++dst;

}

break;

case '.':

if (slash_dot_dot == 1 || slash_dot_dot == 2) {

++slash_dot_dot;

break;

}

/* fall-through */

default:

slash_dot_dot = 0;

}

}

*dst++ = ch;

} while(ch);

return (dst - org_dst) - 1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值