android的urlencode,urlencode.cc

/*

* Copyright 2008 The WebRTC Project Authors. All rights reserved.

*

* Use of this source code is governed by a BSD-style license

* that can be found in the LICENSE file in the root of the source

* tree. An additional intellectual property rights grant can be found

* in the file PATENTS. All contributing project authors may

* be found in the AUTHORS file in the root of the source tree.

*/

#include "webrtc/base/urlencode.h"

#include "webrtc/base/common.h"

#include "webrtc/base/stringutils.h"

static int HexPairValue(const char * code) {

int value = 0;

for (const char * pch = code; pch < code + 2; ++pch) {

value <<= 4;

int digit = *pch;

if (digit >= '0' && digit <= '9') {

value += digit - '0';

}

else if (digit >= 'A' && digit <= 'F') {

value += digit - 'A' + 10;

}

else if (digit >= 'a' && digit <= 'f') {

value += digit - 'a' + 10;

}

else {

return -1;

}

}

return value;

}

static int InternalUrlDecode(const char *source, char *dest,

bool encode_space_as_plus) {

char * start = dest;

while (*source) {

switch (*source) {

case '+':

if (encode_space_as_plus) {

*(dest++) = ' ';

} else {

*dest++ = *source;

}

break;

case '%':

if (source[1] && source[2]) {

int value = HexPairValue(source + 1);

if (value >= 0) {

*(dest++) = static_cast(value);

source += 2;

}

else {

*dest++ = '?';

}

}

else {

*dest++ = '?';

}

break;

default:

*dest++ = *source;

}

source++;

}

*dest = 0;

return static_cast(dest - start);

}

static bool IsValidUrlChar(char ch, bool unsafe_only) {

if (unsafe_only) {

return !(ch <= ' ' || strchr("\\\"^&`<>[]{}", ch));

} else {

return isalnum(ch) || strchr("-_.!~*'()", ch);

}

}

namespace rtc {

int UrlDecode(const char *source, char *dest) {

return InternalUrlDecode(source, dest, true);

}

int UrlDecodeWithoutEncodingSpaceAsPlus(const char *source, char *dest) {

return InternalUrlDecode(source, dest, false);

}

int InternalUrlEncode(const char *source, char *dest, unsigned int max,

bool encode_space_as_plus, bool unsafe_only) {

static const char *digits = "0123456789ABCDEF";

if (max == 0) {

return 0;

}

char *start = dest;

while (static_cast(dest - start) < max && *source) {

unsigned char ch = static_cast(*source);

if (*source == ' ' && encode_space_as_plus && !unsafe_only) {

*dest++ = '+';

} else if (IsValidUrlChar(ch, unsafe_only)) {

*dest++ = *source;

} else {

if (static_cast(dest - start) + 4 > max) {

break;

}

*dest++ = '%';

*dest++ = digits[(ch >> 4) & 0x0F];

*dest++ = digits[ ch & 0x0F];

}

source++;

}

ASSERT(static_cast(dest - start) < max);

*dest = 0;

return static_cast(dest - start);

}

int UrlEncode(const char *source, char *dest, unsigned max) {

return InternalUrlEncode(source, dest, max, true, false);

}

int UrlEncodeWithoutEncodingSpaceAsPlus(const char *source, char *dest,

unsigned max) {

return InternalUrlEncode(source, dest, max, false, false);

}

int UrlEncodeOnlyUnsafeChars(const char *source, char *dest, unsigned max) {

return InternalUrlEncode(source, dest, max, false, true);

}

std::string

InternalUrlDecodeString(const std::string & encoded,

bool encode_space_as_plus) {

size_t needed_length = encoded.length() + 1;

char* buf = STACK_ARRAY(char, needed_length);

InternalUrlDecode(encoded.c_str(), buf, encode_space_as_plus);

return buf;

}

std::string

UrlDecodeString(const std::string & encoded) {

return InternalUrlDecodeString(encoded, true);

}

std::string

UrlDecodeStringWithoutEncodingSpaceAsPlus(const std::string & encoded) {

return InternalUrlDecodeString(encoded, false);

}

std::string

InternalUrlEncodeString(const std::string & decoded,

bool encode_space_as_plus,

bool unsafe_only) {

int needed_length = static_cast(decoded.length()) * 3 + 1;

char* buf = STACK_ARRAY(char, needed_length);

InternalUrlEncode(decoded.c_str(), buf, needed_length,

encode_space_as_plus, unsafe_only);

return buf;

}

std::string

UrlEncodeString(const std::string & decoded) {

return InternalUrlEncodeString(decoded, true, false);

}

std::string

UrlEncodeStringWithoutEncodingSpaceAsPlus(const std::string & decoded) {

return InternalUrlEncodeString(decoded, false, false);

}

std::string

UrlEncodeStringForOnlyUnsafeChars(const std::string & decoded) {

return InternalUrlEncodeString(decoded, false, true);

}

} // namespace rtc

普通文本

|

182行

|

4.76 KB

/*

* Copyright 2008 The WebRTC Project Authors. All rights reserved.

*

* Use of this source code is governed by a BSD-style license

* that can be found in the LICENSE file in the root of the source

* tree. An additional intellectual property rights grant can be found

* in the file PATENTS. All contributing project authors may

* be found in the AUTHORS file in the root of the source tree.

*/

#include "webrtc/base/urlencode.h"

#include "webrtc/base/common.h"

#include "webrtc/base/stringutils.h"

static int HexPairValue(const char * code) {

int value = 0;

for (const char * pch = code; pch < code + 2; ++pch) {

value <<= 4;

int digit = *pch;

if (digit >= '0' && digit <= '9') {

value += digit - '0';

}

else if (digit >= 'A' && digit <= 'F') {

value += digit - 'A' + 10;

}

else if (digit >= 'a' && digit <= 'f') {

value += digit - 'a' + 10;

}

else {

return -1;

}

}

return value;

}

static int InternalUrlDecode(const char *source, char *dest,

bool encode_space_as_plus) {

char * start = dest;

while (*source) {

switch (*source) {

case '+':

if (encode_space_as_plus) {

*(dest++) = ' ';

} else {

*dest++ = *source;

}

break;

case '%':

if (source[1] && source[2]) {

int value = HexPairValue(source + 1);

if (value >= 0) {

*(dest++) = static_cast(value);

source += 2;

}

else {

*dest++ = '?';

}

}

else {

*dest++ = '?';

}

break;

default:

*dest++ = *source;

}

source++;

}

*dest = 0;

return static_cast(dest - start);

}

static bool IsValidUrlChar(char ch, bool unsafe_only) {

if (unsafe_only) {

return !(ch <= ' ' || strchr("\\\"^&`<>[]{}", ch));

} else {

return isalnum(ch) || strchr("-_.!~*'()", ch);

}

}

namespace rtc {

int UrlDecode(const char *source, char *dest) {

return InternalUrlDecode(source, dest, true);

}

int UrlDecodeWithoutEncodingSpaceAsPlus(const char *source, char *dest) {

return InternalUrlDecode(source, dest, false);

}

int InternalUrlEncode(const char *source, char *dest, unsigned int max,

bool encode_space_as_plus, bool unsafe_only) {

static const char *digits = "0123456789ABCDEF";

if (max == 0) {

return 0;

}

char *start = dest;

while (static_cast(dest - start) < max && *source) {

unsigned char ch = static_cast(*source);

if (*source == ' ' && encode_space_as_plus && !unsafe_only) {

*dest++ = '+';

} else if (IsValidUrlChar(ch, unsafe_only)) {

*dest++ = *source;

} else {

if (static_cast(dest - start) + 4 > max) {

break;

}

*dest++ = '%';

*dest++ = digits[(ch >> 4) & 0x0F];

*dest++ = digits[ ch & 0x0F];

}

source++;

}

ASSERT(static_cast(dest - start) < max);

*dest = 0;

return static_cast(dest - start);

}

int UrlEncode(const char *source, char *dest, unsigned max) {

return InternalUrlEncode(source, dest, max, true, false);

}

int UrlEncodeWithoutEncodingSpaceAsPlus(const char *source, char *dest,

unsigned max) {

return InternalUrlEncode(source, dest, max, false, false);

}

int UrlEncodeOnlyUnsafeChars(const char *source, char *dest, unsigned max) {

return InternalUrlEncode(source, dest, max, false, true);

}

std::string

InternalUrlDecodeString(const std::string & encoded,

bool encode_space_as_plus) {

size_t needed_length = encoded.length() + 1;

char* buf = STACK_ARRAY(char, needed_length);

InternalUrlDecode(encoded.c_str(), buf, encode_space_as_plus);

return buf;

}

std::string

UrlDecodeString(const std::string & encoded) {

return InternalUrlDecodeString(encoded, true);

}

std::string

UrlDecodeStringWithoutEncodingSpaceAsPlus(const std::string & encoded) {

return InternalUrlDecodeString(encoded, false);

}

std::string

InternalUrlEncodeString(const std::string & decoded,

bool encode_space_as_plus,

bool unsafe_only) {

int needed_length = static_cast(decoded.length()) * 3 + 1;

char* buf = STACK_ARRAY(char, needed_length);

InternalUrlEncode(decoded.c_str(), buf, needed_length,

encode_space_as_plus, unsafe_only);

return buf;

}

std::string

UrlEncodeString(const std::string & decoded) {

return InternalUrlEncodeString(decoded, true, false);

}

std::string

UrlEncodeStringWithoutEncodingSpaceAsPlus(const std::string & decoded) {

return InternalUrlEncodeString(decoded, false, false);

}

std::string

UrlEncodeStringForOnlyUnsafeChars(const std::string & decoded) {

return InternalUrlEncodeString(decoded, false, true);

}

} // namespace rtc

### 回答1: `HttpUtility.UrlEncode`是一个.NET Framework类库中的方法,用于将字符串编码为URL编码格式。该方法用于将Web应用程序中的参数值、表单数据、查询字符串等转换为URL格式,可以避免URL中出现特殊字符导致解析错误的问题。例如,将空格替换为“%20”,将“&”替换为“%26”等。 ### 回答2: httputility.urlencode是一个在ASP.NET中经常使用的方法,主要用于将字符串转换为URL编码格式。使用这个方法可以确保URL中所有的字符都可以被正确的传递和识别,避免出现多种问题,如页面无法打开、参数值丢失等等。 通常情况下,httputility.urlencode主要用于将请求数据作为URL参数传递到服务器端。在这个过程中,客户端的字符会经过URL编码后以URL参数的形式传递到服务器端。在服务器端接收到这个URL参数后,再用httputility.urldecode方法进行解码,得到原始字符串数据。 需要注意的是,虽然可以使用其他方法进行URL编码(如JavaScript中的encodeURI()和encodeURIComponent()方法),但在ASP.NET中建议使用httputility.urlencode方法,因为它针对ASP.NET的机制进行了优化,能够很好地处理各种编码格式的问题。 使用httputility.urlencode方法时,可以指定编码的方式。常见的编码方式有UTF-8、GB2312等等。在使用过程中需要根据具体的使用场景来选择合适的编码方式,以确保数据能够被正确的处理。 综上所述,httputility.urlencode是一个在ASP.NET中非常有用的方法,可以确保传输的数据能够被正确的识别和处理,避免各种数据传输问题的出现。在编写ASP.NET应用程序时,建议充分利用这个方法来处理URL编码相关的问题。 ### 回答3: httputility.urlencode是一个ASP.NET框架中的类,主要用于将字符串编码为URL格式。URL编码在很多场景下都非常有用,比如对于包含特殊字符(如空格、井号、加号等)的字符串,需要将其转换为URL可能接受的形式,以避免出现歧义甚至错误。下面将从原理、用法和注意事项三个方面介绍httputility.urlencode。 一、原理 URL编码将特殊字符转换成具有特殊含义的16进制值,并在其前加上%。编码后的字符串只由以下字符构成:字母(A~Z、a~z)、数字(0~9)和这些特殊符号(- _ . ! ~ * ' ( ))。例如,对于字符串"hello world!",编码后的结果为"hello%20world%21",其中%20对应空格,%21对应感叹号。 二、用法 使用httputility.urlencode非常简单,只需在需要进行URL编码的字符串上调用其静态方法,即可得到编码后的结果。可以在ASP.NET Web Forms应用程序中的页面代码中使用它,也可以在ASP.NET MVC应用程序中的控制器代码中使用它。以下是一些使用httputility.urlencode方法的示例代码: 1. 对字符串进行编码: string encodedString = HttpUtility.UrlEncode("hello world!"); 2. 对URL中的查询字符串参数进行编码: string encodedQueryString = HttpUtility.UrlEncode("keyword=hello world&category=news"); 3. 在ASP.NET Web Forms应用程序中,在重定向到另一个页面时,对查询字符串参数进行编码: Response.Redirect("search.aspx?" + HttpUtility.UrlEncode("keyword=hello world&category=news")); 三、注意事项 虽然httputility.urlencode方法很好用,但需要注意一些问题: 1. URL编码是可逆的,也就是说,可以通过httputility.urldecode方法将编码后的字符串还原为原始字符串。但不建议依赖这种方式来保护应用程序的安全性,因为它并不能完全防止恶意攻击。 2. 如果将编码后的字符串作为URL的一部分,需要先验证其长度是否超过允许的最大长度,以免出现“过长的URL”错误。 3. 在请求或响应中使用URL编码时,需要注意使用正确的字符集,以避免出现乱码或不兼容的现象。在ASP.NET中,可以使用httputility.urlencode方法的不同重载来指定字符集。 综上所述,httputility.urlencode是一个很实用的ASP.NET类,用于将包含特殊字符的字符串转换为可以安全传输的URL编码形式。使用它需要注意一些细节和安全问题,但只要正确使用,它可以使Web应用程序更加健壮和安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值