Better is_ascii_host_char.

GitOrigin-RevId: 25c42be7223691ae9623061ed495b99940b1d79a
This commit is contained in:
levlam 2018-05-18 21:12:39 +03:00
parent 3675e38605
commit 2a3d1494aa

View File

@ -29,13 +29,12 @@
namespace td { namespace td {
static bool is_ascii_host_char(char c) { static bool is_ascii_host_char(char c) {
return is_alnum(c) || c == '-'; return static_cast<unsigned char>(c) <= 127;
} }
static bool is_ascii_host(Slice host) { static bool is_ascii_host(Slice host) {
for (auto c : host) { for (auto c : host) {
// ':' and '@' are not allowed in a host name anyway, so we can skip them if (!is_ascii_host_char(c)) {
if (!is_ascii_host_char(c) && c != '.' && c != ':' && c != '@') {
return false; return false;
} }
} }
@ -55,7 +54,7 @@ Result<string> idn_to_ascii(CSlice host) {
wchar_t punycode[256]; wchar_t punycode[256];
int result_length = IdnToAscii(IDN_ALLOW_UNASSIGNED, whost.c_str(), whost.size(), punycode, 255); int result_length = IdnToAscii(IDN_ALLOW_UNASSIGNED, whost.c_str(), whost.size(), punycode, 255);
if (result_length == 0) { if (result_length == 0) {
return Status::Error("Host can't be punycoded"); return Status::Error("Host can't be converted to ASCII");
} }
TRY_RESULT(idn_host, from_wstring(punycode, result_length)); TRY_RESULT(idn_host, from_wstring(punycode, result_length));