re-added HTTP status code checking

This commit is contained in:
tastytea 2018-02-10 12:01:55 +01:00
parent 01996113de
commit 10808b8114
Signed by: tastytea
GPG Key ID: 59346E0EA35C67E5
3 changed files with 22 additions and 4 deletions

View File

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

View File

@ -93,7 +93,7 @@ If you use a debug build, you get more verbose error messages.
* [x] Implement all PUT calls
* [x] Implement all DELETE calls
* Version 0.3.0
* [x] Handle HTTP statuses 301 & 302
* [ ] Handle HTTP statuses 301 & 302
* [x] Support registering as an application
* Later
* [ ] Asynchronous I/O

View File

@ -22,6 +22,7 @@
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Infos.hpp>
#include "macros.hpp"
#include "mastodon-cpp.hpp"
@ -64,6 +65,8 @@ const std::uint16_t API::http::request_sync(const method &meth,
"Connection: close",
"Authorization: Bearer " + _access_token
});
request.setOpt<curlopts::FollowLocation>(true);
request.setOpt<curlopts::WriteStream>(&oss);
if (!formdata.empty())
{
request.setOpt<curlopts::HttpPost>(formdata);
@ -87,8 +90,23 @@ const std::uint16_t API::http::request_sync(const method &meth,
break;
}
oss << request;
answer = oss.str();
request.perform();
std::uint16_t ret = curlpp::infos::ResponseCode::get(request);
ttdebug << "Response code: " << ret << '\n';
if (ret == 200 || ret == 302 || ret == 307)
{ // OK or Found or Temporary Redirect
answer = oss.str();
}
else if (ret == 301 || ret == 308)
{
// Moved Permanently or Permanent Redirect
// FIXME: The new URL should be passed back somehow
answer = oss.str();
}
else
{
return ret;
}
}
catch (curlpp::RuntimeError &e)
{