diff --git a/include/libtorrent/string_util.hpp b/include/libtorrent/string_util.hpp index 4ae4034e5..abd4e0073 100644 --- a/include/libtorrent/string_util.hpp +++ b/include/libtorrent/string_util.hpp @@ -53,6 +53,11 @@ namespace libtorrent { // internal inline bool is_digit(char c) { return c >= '0' && c <= '9'; } + inline void ensure_trailing_slash(std::string& url) + { + if (url.empty() || url[url.size() - 1] != '/') + url += '/'; + } TORRENT_EXTRA_EXPORT bool is_print(char c); TORRENT_EXTRA_EXPORT bool is_space(char c); diff --git a/src/http_parser.cpp b/src/http_parser.cpp index bc7531c3d..3c19612f5 100644 --- a/src/http_parser.cpp +++ b/src/http_parser.cpp @@ -121,8 +121,7 @@ namespace libtorrent { // however, we may still need to insert a '/' in case neither side // has one. We know the location doesn't start with a / already. // so, if the referrer doesn't end with one, add it. - if (url.empty() || url[url.size() - 1] != '/') - url += '/'; + ensure_trailing_slash(url); url += location; } return url; diff --git a/src/torrent.cpp b/src/torrent.cpp index f4503d016..96bb13a1c 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -265,7 +265,8 @@ namespace libtorrent { // correct URLs to end with a "/" for multi-file torrents std::string& url = ws.back().url; - if (multi_file && url[url.size()-1] != '/') url += '/'; + if (multi_file) + ensure_trailing_slash(url); } for (auto const& e : p.http_seeds) diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 9da7f9a77..2b3cba2fc 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -1460,7 +1460,8 @@ namespace libtorrent { { web_seed_entry ent(maybe_url_encode(url_seeds.string_value().to_string()) , web_seed_entry::url_seed); - if ((m_flags & multifile) && ent.url[ent.url.size() - 1] != '/') ent.url += '/'; + if (m_flags & multifile) + ensure_trailing_slash(ent.url); m_web_seeds.push_back(ent); } else if (url_seeds && url_seeds.type() == bdecode_node::list_t) @@ -1474,7 +1475,8 @@ namespace libtorrent { if (url.string_length() == 0) continue; web_seed_entry ent(maybe_url_encode(url.string_value().to_string()) , web_seed_entry::url_seed); - if ((m_flags & multifile) && ent.url[ent.url.size() - 1] != '/') ent.url += '/'; + if (m_flags & multifile) + ensure_trailing_slash(ent.url); if (!unique.insert(ent.url).second) continue; m_web_seeds.push_back(ent); } diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 13a376db9..df67247c2 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -95,8 +95,8 @@ web_peer_connection::web_peer_connection(peer_connection_args const& pack { // handle incorrect .torrent files which are multi-file // but have web seeds not ending with a slash - if (m_path.empty() || m_path[m_path.size() - 1] != '/') m_path += '/'; - if (m_url.empty() || m_url[m_url.size() - 1] != '/') m_url += '/'; + ensure_trailing_slash(m_path); + ensure_trailing_slash(m_url); } else {