Return new URL on HTTP 301 and 308

This commit is contained in:
tastytea 2018-02-17 20:01:51 +01:00
parent db6139f2a2
commit 065b9bc6d8
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
12 changed files with 85 additions and 22 deletions

View File

@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.7)
include(GNUInstallDirs) include(GNUInstallDirs)
project (mastodon-cpp project (mastodon-cpp
VERSION 0.2.14 VERSION 0.2.15
LANGUAGES CXX LANGUAGES CXX
) )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")

View File

@ -74,6 +74,7 @@ After you did a `make install`, a project consisting of one file can be compiled
| 0 | No error | | 0 | No error |
| 1 | Invalid call | | 1 | Invalid call |
| 2 | Not implemented | | 2 | Not implemented |
| 3 | URL changed (HTTP 301 or 308) |
| 100 - 999 | HTTP status codes | | 100 - 999 | HTTP status codes |
| 65535 | Unknown error | | 65535 | Unknown error |
@ -99,13 +100,14 @@ If you use a debug build, you get more verbose error messages.
* [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
* [ ] Handle HTTP statuses 301 & 302 * [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
## Status of implementation ## Status of implementation

View File

@ -68,6 +68,12 @@ int main(int argc, char *argv[])
return ret; return ret;
} }
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -56,6 +56,12 @@ int main(int argc, char *argv[])
} }
} }
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -104,6 +104,12 @@ int main(int argc, char *argv[])
pt::write_json(filename, config); pt::write_json(filename, config);
} }
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -62,6 +62,12 @@ int main(int argc, char *argv[])
{ {
std::cout << answer << '\n'; std::cout << answer << '\n';
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -36,6 +36,12 @@ int main(int argc, char *argv[])
{ {
// std::cout << answer << '\n'; // std::cout << answer << '\n';
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -53,6 +53,12 @@ int main(int argc, char *argv[])
return ret; return ret;
} }
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << answer << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -71,6 +71,12 @@ int main(int argc, char *argv[])
return ret; return ret;
} }
} }
else if (ret == 3)
{
std::cerr << "The URL has permanently changed.\n" <<
"New URL: " << url << '\n';
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -99,8 +99,9 @@ const std::uint16_t API::http::request_sync(const method &meth,
} }
else if (ret == 301 || ret == 308) else if (ret == 301 || ret == 308)
{ // Moved Permanently or Permanent Redirect { // Moved Permanently or Permanent Redirect
// FIXME: The new URL should be passed back somehow // return new URL
answer = oss.str(); answer = curlpp::infos::EffectiveUrl::get(request);
return 3;
} }
else else
{ {

View File

@ -192,6 +192,11 @@ const std::uint16_t API::register_app1(const string &instance,
return 0; return 0;
} }
else if (ret == 3)
{
url = answer;
return ret;
}
else else
{ {
std::cerr << "Error code: " << ret << '\n'; std::cerr << "Error code: " << ret << '\n';

View File

@ -44,9 +44,7 @@ namespace Mastodon
* | 0 | No error | * | 0 | No error |
* | 1 | Invalid call | * | 1 | Invalid call |
* | 2 | Not implemented | * | 2 | Not implemented |
* | 16 | Connection failed | * | 3 | URL changed (HTTP 301 or 308) |
* | 17 | TLS error |
* | 18 | Invalid response from server |
* | 100 - 999 | HTTP status codes | * | 100 - 999 | HTTP status codes |
* | 65535 | Unknown exception | * | 65535 | Unknown exception |
*/ */
@ -184,7 +182,8 @@ public:
* @param client_secret Returned * @param client_secret Returned
* @param url Returned, used to generate code for register_app2 * @param url Returned, used to generate code for register_app2
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and url is set to the new URL.
*/ */
const std::uint16_t register_app1(const std::string &instance, const std::uint16_t register_app1(const std::string &instance,
const std::string &client_name, const std::string &client_name,
@ -235,7 +234,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, const std::uint16_t get(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
@ -250,7 +250,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, const std::uint16_t get(const Mastodon::API::v1 &call,
const parametermap &parameters, const parametermap &parameters,
@ -265,7 +266,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t get(const Mastodon::API::v1 &call, const std::uint16_t get(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
@ -279,7 +281,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t get(const std::string &call, const std::uint16_t get(const std::string &call,
std::string &answer); std::string &answer);
@ -294,7 +297,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t patch(const Mastodon::API::v1 &call, const std::uint16_t patch(const Mastodon::API::v1 &call,
const parametermap &parameters, const parametermap &parameters,
@ -307,7 +311,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t post(const Mastodon::API::v1 &call, const std::uint16_t post(const Mastodon::API::v1 &call,
std::string &answer); std::string &answer);
@ -320,7 +325,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error an * @param answer The answer from the server. Usually JSON. On error an
* empty string. * empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t post(const Mastodon::API::v1 &call, const std::uint16_t post(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
@ -337,7 +343,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t post(const Mastodon::API::v1 &call, const std::uint16_t post(const Mastodon::API::v1 &call,
const parametermap &parameters, const parametermap &parameters,
@ -354,7 +361,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t post(const Mastodon::API::v1 &call, const std::uint16_t post(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
@ -371,7 +379,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t post(const std::string &call, const std::uint16_t post(const std::string &call,
const parametermap &parameters, const parametermap &parameters,
@ -387,7 +396,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t put(const Mastodon::API::v1 &call, const std::uint16_t put(const Mastodon::API::v1 &call,
const std::string &argument, const std::string &argument,
@ -403,7 +413,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t put(const std::string &call, const std::uint16_t put(const std::string &call,
const parametermap &parameters, const parametermap &parameters,
@ -454,7 +465,8 @@ public:
* @param answer The answer from the server. Usually JSON. On error * @param answer The answer from the server. Usually JSON. On error
* an empty string. * an empty string.
* *
* @return @ref error "Error code". * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t del(const std::string &call, const std::uint16_t del(const std::string &call,
const parametermap &parameters, const parametermap &parameters,
@ -511,7 +523,8 @@ private:
* @param formdata The form data for PATCH and POST request. * @param formdata The form data for PATCH and POST request.
* @param answer The answer from the server * @param answer The answer from the server
* *
* @return Error code. See README.md for details. * @return @ref error "Error code". If the URL has permanently changed, 3
* is returned and answer is set to the new URL.
*/ */
const std::uint16_t request_sync(const method &meth, const std::uint16_t request_sync(const method &meth,
const std::string &path, const std::string &path,