Partial ton:// URLs support.

GitOrigin-RevId: eeaaab10574dc636e9de18cc482f2398f6732fa1
This commit is contained in:
levlam 2019-09-27 01:03:13 +03:00
parent 9b0530e704
commit 79330c3881
3 changed files with 13 additions and 8 deletions

View File

@ -1181,7 +1181,7 @@ string get_first_url(Slice text, const vector<MessageEntity> &entities) {
break; break;
case MessageEntity::Type::Url: { case MessageEntity::Type::Url: {
Slice url = utf8_utf16_substr(text, entity.offset, entity.length); Slice url = utf8_utf16_substr(text, entity.offset, entity.length);
if (begins_with(url, "tg:") || is_plain_domain(url)) { if (begins_with(url, "ton:") || begins_with(url, "tg:") || is_plain_domain(url)) {
continue; continue;
} }
return url.str(); return url.str();
@ -1199,7 +1199,7 @@ string get_first_url(Slice text, const vector<MessageEntity> &entities) {
case MessageEntity::Type::PreCode: case MessageEntity::Type::PreCode:
break; break;
case MessageEntity::Type::TextUrl: case MessageEntity::Type::TextUrl:
if (begins_with(entity.argument, "tg:")) { if (begins_with(entity.argument, "ton:") || begins_with(entity.argument, "tg:")) {
continue; continue;
} }
return entity.argument; return entity.argument;

View File

@ -277,20 +277,25 @@ string get_emoji_fingerprint(uint64 num) {
Result<string> check_url(Slice url) { Result<string> check_url(Slice url) {
bool is_tg = false; bool is_tg = false;
bool is_ton = false;
if (begins_with(url, "tg://")) { if (begins_with(url, "tg://")) {
url.remove_prefix(5); url.remove_prefix(5);
is_tg = true; is_tg = true;
} else if (begins_with(url, "tg:")) { } else if (begins_with(url, "tg:")) {
url.remove_prefix(3); url.remove_prefix(3);
is_tg = true; is_tg = true;
} else { } else if (begins_with(url, "ton://")) {
is_tg = false; url.remove_prefix(6);
is_ton = true;
} else if (begins_with(url, "ton:")) {
url.remove_prefix(4);
is_ton = true;
} }
TRY_RESULT(http_url, parse_url(url)); TRY_RESULT(http_url, parse_url(url));
if (is_tg) { if (is_tg || is_ton) {
if (begins_with(url, "http://") || http_url.protocol_ == HttpUrl::Protocol::HTTPS || !http_url.userinfo_.empty() || if (begins_with(url, "http://") || http_url.protocol_ == HttpUrl::Protocol::HTTPS || !http_url.userinfo_.empty() ||
http_url.specified_port_ != 0 || http_url.is_ipv6_) { http_url.specified_port_ != 0 || http_url.is_ipv6_) {
return Status::Error("Wrong tg URL"); return Status::Error(is_tg ? Slice("Wrong tg URL") : Slice("Wrong ton URL"));
} }
Slice query(http_url.query_); Slice query(http_url.query_);
@ -298,7 +303,7 @@ Result<string> check_url(Slice url) {
if (query[1] == '?') { if (query[1] == '?') {
query.remove_prefix(1); query.remove_prefix(1);
} }
return PSTRING() << "tg://" << http_url.host_ << query; return PSTRING() << (is_tg ? "tg" : "ton") << "://" << http_url.host_ << query;
} }
if (url.find('.') == string::npos) { if (url.find('.') == string::npos) {

View File

@ -33,7 +33,7 @@ int32 get_vector_hash(const vector<uint32> &numbers) TD_WARN_UNUSED_RESULT;
// returns emoji corresponding to the specified number // returns emoji corresponding to the specified number
string get_emoji_fingerprint(uint64 num); string get_emoji_fingerprint(uint64 num);
// checks whether url is a valid tg or HTTP(S) URL and returns its in a canonical form // checks whether url is a valid tg, ton or HTTP(S) URL and returns its in a canonical form
Result<string> check_url(Slice url); Result<string> check_url(Slice url);
} // namespace td } // namespace td