From 505849ee2b36d17011251815804dfff2e0e71f3d Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 22 Feb 2018 03:04:15 +0100 Subject: [PATCH] Replaced own urlencode() with curlpp::escape() --- CMakeLists.txt | 2 +- README.md | 22 +++++++++++----------- src/api_delete.cpp | 3 ++- src/api_get.cpp | 3 ++- src/api_post.cpp | 3 ++- src/api_put.cpp | 3 ++- src/mastodon-cpp.cpp | 32 ++++---------------------------- 7 files changed, 24 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e56d32..ce10416 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project (mastodon-cpp - VERSION 0.3.1 + VERSION 0.3.2 LANGUAGES CXX ) diff --git a/README.md b/README.md index d7b453a..adf3105 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ **mastodon-cpp** is a C++ wrapper for the Mastodon API. The aim is to be as simple as possible. The library takes care of the network stuff. You submit a query and get the raw JSON. -All versions below 1.0.0 (SOVERSION 0) are considered insecure, unstable and can change any time. +All versions below 1.0.0 (SOVERSION 0) are considered unstable and can change any time. # Install @@ -88,24 +88,24 @@ If you use a debug build, you get more verbose error messages. # TODO - * Version 0.1.0 +* Version 0.1.0 * [x] Implement all GET calls * [x] Usable error handling * [x] Network stuff * [x] Comprehensive example - * Version 0.2.0 - * [x] Escape user input - * [x] Implement all PATCH calls - * [x] Implement all POST calls - * [x] Implement all PUT calls - * [x] Implement all DELETE calls - * Version 0.3.0 +* Version 0.2.0 + * [x] Escape user input + * [x] Implement all PATCH calls + * [x] Implement all POST calls + * [x] Implement all PUT calls + * [x] Implement all DELETE calls +* Version 0.3.0 * [x] Handle HTTP statuses 301, 302, 307 and 308 * [x] Support registering as an application - * Version 0.4.0 +* Version 0.4.0 * [ ] Handle X-RateLimit header * [ ] Streaming API - * Later +* Later * [ ] Asynchronous I/O * [ ] Improve error reporting diff --git a/src/api_delete.cpp b/src/api_delete.cpp index d06d766..d6d28b7 100644 --- a/src/api_delete.cpp +++ b/src/api_delete.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "macros.hpp" #include "mastodon-cpp.hpp" @@ -55,7 +56,7 @@ const std::uint16_t API::del(const Mastodon::API::v1 &call, const parametermap ¶meters) { string strcall = ""; - const string argument_encoded = urlencode(argument); + const string argument_encoded = curlpp::escape(argument); switch (call) { diff --git a/src/api_get.cpp b/src/api_get.cpp index 9f96418..1f7a286 100644 --- a/src/api_get.cpp +++ b/src/api_get.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "macros.hpp" #include "mastodon-cpp.hpp" @@ -100,7 +101,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call, { string strcall = ""; bool firstparam = true; - const string argument_encoded = urlencode(argument); + const string argument_encoded = curlpp::escape(argument); switch (call) { diff --git a/src/api_post.cpp b/src/api_post.cpp index 3b769e7..cf9af08 100644 --- a/src/api_post.cpp +++ b/src/api_post.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "macros.hpp" #include "mastodon-cpp.hpp" @@ -84,7 +85,7 @@ const std::uint16_t API::post(const Mastodon::API::v1 &call, const parametermap ¶meters, string &answer) { string strcall = ""; - const string argument_encoded = urlencode(argument); + const string argument_encoded = curlpp::escape(argument); switch (call) { diff --git a/src/api_put.cpp b/src/api_put.cpp index 713bdbf..692430b 100644 --- a/src/api_put.cpp +++ b/src/api_put.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "macros.hpp" #include "mastodon-cpp.hpp" @@ -28,7 +29,7 @@ const std::uint16_t API::put(const Mastodon::API::v1 &call, const parametermap ¶meters, string &answer) { string strcall = ""; - const string argument_encoded = urlencode(argument); + const string argument_encoded = curlpp::escape(argument); switch (call) { diff --git a/src/mastodon-cpp.cpp b/src/mastodon-cpp.cpp index 5f3c206..d0c0e5b 100644 --- a/src/mastodon-cpp.cpp +++ b/src/mastodon-cpp.cpp @@ -49,30 +49,6 @@ const string API::get_useragent() const return _useragent; } -const std::string API::urlencode(const string &str) const -{ - std::ostringstream oss; - - for (const std::uint8_t &b: str) - { - // Check for unreserved characters (RFC 3986 section 2.3) - if ((b >= 0x30 && b <= 0x39) || // 0-9 - (b >= 0x41 && b <= 0x5A) || // A-Z - (b >= 0x61 && b <= 0x7A) || // a-z - b == 0x2D || b == 0x2E || // -, . - b == 0x5F || b == 0x7E) // _, ~ - { - oss << b; - } - else - { - oss << '%' << std::hex << std::uppercase << (int)(unsigned char)b; - } - } - - return oss.str(); -} - const string API::maptostr(const parametermap &map, const bool &firstparam) { string result = ""; @@ -86,7 +62,7 @@ const string API::maptostr(const parametermap &map, const bool &firstparam) { if (it.second.size() == 1) { - result += (delim + it.first + "=" + urlencode(it.second.front())); + result += (delim + it.first + "=" + curlpp::escape(it.second.front())); if (delim == '?') { delim = '&'; @@ -96,7 +72,7 @@ const string API::maptostr(const parametermap &map, const bool &firstparam) { for (const string &str : it.second) { - result += (delim + it.first + "[]=" + urlencode(str)); + result += (delim + it.first + "[]=" + curlpp::escape(str)); if (delim == '?') { delim = '&'; @@ -182,8 +158,8 @@ const std::uint16_t API::register_app1(const string &instance, client_secret = match[1].str(); url = "https://" + instance + "/oauth/authorize" + - "?scope=" + urlencode(scopes) + "&response_type=code" + - "&redirect_uri=" + urlencode(redirect_uri) + + "?scope=" + curlpp::escape(scopes) + "&response_type=code" + + "&redirect_uri=" + curlpp::escape(redirect_uri) + "&client_id=" + client_id; if (!website.empty()) {