move url_has_argument to escape_string and added unit tests

This commit is contained in:
Arvid Norberg 2007-12-02 19:10:45 +00:00
parent 9727634f09
commit a7b5250058
4 changed files with 42 additions and 19 deletions

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_ESCAPE_STRING_HPP_INCLUDED
#include <string>
#include <boost/optional.hpp>
#include "libtorrent/config.hpp"
namespace libtorrent
@ -43,10 +44,12 @@ namespace libtorrent
std::string TORRENT_EXPORT escape_path(const char* str, int len);
// encodes a string using the base64 scheme
TORRENT_EXPORT std::string base64encode(const std::string& s);
TORRENT_EXPORT std::string base64encode(std::string const& s);
// encodes a string using the base32 scheme
TORRENT_EXPORT std::string base32encode(const std::string& s);
TORRENT_EXPORT std::string base32encode(std::string const& s);
TORRENT_EXPORT boost::optional<std::string> url_has_argument(
std::string const& url, std::string argument);
}
#endif // TORRENT_ESCAPE_STRING_HPP_INCLUDED

View File

@ -38,7 +38,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include <iomanip>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <boost/optional.hpp>
#include "libtorrent/assert.hpp"
@ -85,7 +86,6 @@ namespace libtorrent
return ret;
}
std::string escape_string(const char* str, int len)
{
TORRENT_ASSERT(str != 0);
@ -220,7 +220,6 @@ namespace libtorrent
unsigned char inbuf[5];
unsigned char outbuf[8];
std::cerr << "base32(" << s << ") = ";
std::string ret;
for (std::string::const_iterator i = s.begin(); i != s.end();)
{
@ -261,8 +260,29 @@ namespace libtorrent
ret += '=';
}
}
std::cerr << ret << std::endl;
return ret;
}
boost::optional<std::string> url_has_argument(
std::string const& url, std::string argument)
{
size_t i = url.find('?');
if (i == std::string::npos) return boost::optional<std::string>();
++i;
argument += '=';
if (url.compare(i, argument.size(), argument) == 0)
{
size_t pos = i + argument.size();
return url.substr(pos, url.find('&', pos) - pos);
}
argument.insert(0, "&");
i = url.find(argument, i);
if (i == std::string::npos) return boost::optional<std::string>();
size_t pos = i + argument.size();
return url.substr(pos, url.find('&', pos) - pos);
}
}

View File

@ -89,19 +89,6 @@ namespace
namespace
{
bool url_has_argument(std::string const& url, std::string argument)
{
size_t i = url.find('?');
if (i == std::string::npos) return false;
argument += '=';
if (url.compare(i + 1, argument.size(), argument) == 0) return true;
argument.insert(0, "&");
return url.find(argument, i)
!= std::string::npos;
}
char to_lower(char c) { return std::tolower(c); }
}

View File

@ -91,6 +91,8 @@ int test_main()
TEST_CHECK(base64encode("fooba") == "Zm9vYmE=");
TEST_CHECK(base64encode("foobar") == "Zm9vYmFy");
// base32 test vectors from http://www.faqs.org/rfcs/rfc4648.html
TEST_CHECK(base32encode("") == "");
TEST_CHECK(base32encode("f") == "MY======");
TEST_CHECK(base32encode("fo") == "MZXQ====");
@ -99,6 +101,17 @@ int test_main()
TEST_CHECK(base32encode("fooba") == "MZXW6YTB");
TEST_CHECK(base32encode("foobar") == "MZXW6YTBOI======");
// url_has_argument
TEST_CHECK(!url_has_argument("http://127.0.0.1/test", "test"));
TEST_CHECK(!url_has_argument("http://127.0.0.1/test?foo=24", "bar"));
TEST_CHECK(*url_has_argument("http://127.0.0.1/test?foo=24", "foo") == "24");
TEST_CHECK(*url_has_argument("http://127.0.0.1/test?foo=24&bar=23", "foo") == "24");
TEST_CHECK(*url_has_argument("http://127.0.0.1/test?foo=24&bar=23", "bar") == "23");
TEST_CHECK(*url_has_argument("http://127.0.0.1/test?foo=24&bar=23&a=e", "bar") == "23");
TEST_CHECK(*url_has_argument("http://127.0.0.1/test?foo=24&bar=23&a=e", "a") == "e");
TEST_CHECK(!url_has_argument("http://127.0.0.1/test?foo=24&bar=23&a=e", "b"));
// HTTP request parser
http_parser parser;