Added HTTP status support, added curlpp initialization
This commit is contained in:
parent
f0c6004f30
commit
7d0a2d0af6
26
src/http.cpp
26
src/http.cpp
|
@ -24,6 +24,7 @@
|
|||
#include <curlpp/Easy.hpp>
|
||||
#include <curlpp/Options.hpp>
|
||||
#include <curlpp/Exception.hpp>
|
||||
#include <curlpp/Infos.hpp>
|
||||
#include "mastorss.hpp"
|
||||
|
||||
using std::string;
|
||||
|
@ -31,6 +32,11 @@ using std::cerr;
|
|||
|
||||
namespace curlopts = curlpp::options;
|
||||
|
||||
void curlpp_init()
|
||||
{
|
||||
curlpp::initialize();
|
||||
}
|
||||
|
||||
const std::uint16_t http_get(const string &feedurl, string &answer, const string &useragent)
|
||||
{
|
||||
try
|
||||
|
@ -43,10 +49,24 @@ const std::uint16_t http_get(const string &feedurl, string &answer, const string
|
|||
{
|
||||
"Connection: close",
|
||||
});
|
||||
request.setOpt<curlopts::FollowLocation>(true);
|
||||
request.setOpt<curlopts::WriteStream>(&oss);
|
||||
|
||||
|
||||
oss << request;
|
||||
answer = oss.str();
|
||||
request.perform();
|
||||
std::uint16_t ret = curlpp::infos::ResponseCode::get(request);
|
||||
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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -242,11 +242,12 @@ int main(int argc, char *argv[])
|
|||
std::uint16_t ret;
|
||||
|
||||
read_config(config, profile, instance, access_token, feedurl);
|
||||
curlpp_init();
|
||||
|
||||
string answer;
|
||||
string last_entry = config.get(profile + ".last_entry", "");
|
||||
std::vector<string> entries;
|
||||
//FIXME: User-Agent
|
||||
|
||||
ret = http_get(feedurl, answer, "mastorss/" + (string)global::version);
|
||||
if (ret != 0)
|
||||
{
|
||||
|
|
|
@ -15,5 +15,6 @@ std::vector<string> parse_website(const string &profile, const string &xml);
|
|||
// http.cpp
|
||||
const std::uint16_t http_get(const string &feedurl,
|
||||
string &answer, const string &useragent = "");
|
||||
void curlpp_init();
|
||||
|
||||
#endif // mastorss_HPP
|
||||
|
|
Loading…
Reference in New Issue
Block a user