Replaced own urlencode() with curlpp::escape()

This commit is contained in:
tastytea 2018-02-22 03:04:15 +01:00
parent 6d6601e98a
commit 505849ee2b
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
7 changed files with 24 additions and 44 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.7) cmake_minimum_required (VERSION 3.7)
project (mastodon-cpp project (mastodon-cpp
VERSION 0.3.1 VERSION 0.3.2
LANGUAGES CXX LANGUAGES CXX
) )

View File

@ -1,6 +1,6 @@
**mastodon-cpp** is a C++ wrapper for the Mastodon API. The aim is to be as simple as possible. **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. 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 # Install
@ -88,24 +88,24 @@ If you use a debug build, you get more verbose error messages.
# TODO # TODO
* Version 0.1.0 * Version 0.1.0
* [x] Implement all GET calls * [x] Implement all GET calls
* [x] Usable error handling * [x] Usable error handling
* [x] Network stuff * [x] Network stuff
* [x] Comprehensive example * [x] Comprehensive example
* Version 0.2.0 * Version 0.2.0
* [x] Escape user input * [x] Escape user input
* [x] Implement all PATCH calls * [x] Implement all PATCH calls
* [x] Implement all POST calls * [x] Implement all POST calls
* [x] Implement all PUT calls * [x] Implement all PUT calls
* [x] Implement all DELETE calls * [x] Implement all DELETE calls
* Version 0.3.0 * Version 0.3.0
* [x] Handle HTTP statuses 301, 302, 307 and 308 * [x] Handle HTTP statuses 301, 302, 307 and 308
* [x] Support registering as an application * [x] Support registering as an application
* Version 0.4.0 * Version 0.4.0
* [ ] Handle X-RateLimit header * [ ] Handle X-RateLimit header
* [ ] Streaming API * [ ] Streaming API
* Later * Later
* [ ] Asynchronous I/O * [ ] Asynchronous I/O
* [ ] Improve error reporting * [ ] Improve error reporting

View File

@ -17,6 +17,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <curlpp/cURLpp.hpp>
#include "macros.hpp" #include "macros.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
@ -55,7 +56,7 @@ const std::uint16_t API::del(const Mastodon::API::v1 &call,
const parametermap &parameters) const parametermap &parameters)
{ {
string strcall = ""; string strcall = "";
const string argument_encoded = urlencode(argument); const string argument_encoded = curlpp::escape(argument);
switch (call) switch (call)
{ {

View File

@ -17,6 +17,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <curlpp/cURLpp.hpp>
#include "macros.hpp" #include "macros.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
@ -100,7 +101,7 @@ const std::uint16_t API::get(const Mastodon::API::v1 &call,
{ {
string strcall = ""; string strcall = "";
bool firstparam = true; bool firstparam = true;
const string argument_encoded = urlencode(argument); const string argument_encoded = curlpp::escape(argument);
switch (call) switch (call)
{ {

View File

@ -17,6 +17,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <curlpp/cURLpp.hpp>
#include "macros.hpp" #include "macros.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
@ -84,7 +85,7 @@ const std::uint16_t API::post(const Mastodon::API::v1 &call,
const parametermap &parameters, string &answer) const parametermap &parameters, string &answer)
{ {
string strcall = ""; string strcall = "";
const string argument_encoded = urlencode(argument); const string argument_encoded = curlpp::escape(argument);
switch (call) switch (call)
{ {

View File

@ -17,6 +17,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <curlpp/cURLpp.hpp>
#include "macros.hpp" #include "macros.hpp"
#include "mastodon-cpp.hpp" #include "mastodon-cpp.hpp"
@ -28,7 +29,7 @@ const std::uint16_t API::put(const Mastodon::API::v1 &call,
const parametermap &parameters, string &answer) const parametermap &parameters, string &answer)
{ {
string strcall = ""; string strcall = "";
const string argument_encoded = urlencode(argument); const string argument_encoded = curlpp::escape(argument);
switch (call) switch (call)
{ {

View File

@ -49,30 +49,6 @@ const string API::get_useragent() const
return _useragent; 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) const string API::maptostr(const parametermap &map, const bool &firstparam)
{ {
string result = ""; string result = "";
@ -86,7 +62,7 @@ const string API::maptostr(const parametermap &map, const bool &firstparam)
{ {
if (it.second.size() == 1) if (it.second.size() == 1)
{ {
result += (delim + it.first + "=" + urlencode(it.second.front())); result += (delim + it.first + "=" + curlpp::escape(it.second.front()));
if (delim == '?') if (delim == '?')
{ {
delim = '&'; delim = '&';
@ -96,7 +72,7 @@ const string API::maptostr(const parametermap &map, const bool &firstparam)
{ {
for (const string &str : it.second) for (const string &str : it.second)
{ {
result += (delim + it.first + "[]=" + urlencode(str)); result += (delim + it.first + "[]=" + curlpp::escape(str));
if (delim == '?') if (delim == '?')
{ {
delim = '&'; delim = '&';
@ -182,8 +158,8 @@ const std::uint16_t API::register_app1(const string &instance,
client_secret = match[1].str(); client_secret = match[1].str();
url = "https://" + instance + "/oauth/authorize" + url = "https://" + instance + "/oauth/authorize" +
"?scope=" + urlencode(scopes) + "&response_type=code" + "?scope=" + curlpp::escape(scopes) + "&response_type=code" +
"&redirect_uri=" + urlencode(redirect_uri) + "&redirect_uri=" + curlpp::escape(redirect_uri) +
"&client_id=" + client_id; "&client_id=" + client_id;
if (!website.empty()) if (!website.empty())
{ {