From 9b8cf190d9e7547fc9b09444077092657f24e917 Mon Sep 17 00:00:00 2001 From: tastytea Date: Wed, 17 Apr 2019 08:26:26 +0200 Subject: [PATCH] Added more error checking, report HTTP errors when in CGI mode. --- src/main.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cef6499..f9d113b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,6 +43,8 @@ using std::chrono::system_clock; namespace curlopts = curlpp::options; +bool cgi = false; + // Fetch HTTP document. const string get_http(const string &url) { @@ -67,7 +69,11 @@ const string get_http(const string &url) } else { - cerr << "HTTP error: " << std::to_string(ret) << endl; + if (cgi) + { + cout << "Status: " << std::to_string(ret) << endl; + } + cerr << "HTTP Error: " << std::to_string(ret) << endl; } } catch (const std::exception &e) @@ -110,12 +116,21 @@ int main(int argc, char *argv[]) const char *envbaseurl = std::getenv("GITEA2RSS_BASEURL"); if (envbaseurl == nullptr) { + cout << "Status: 500 Internal Server Error\n\n"; cerr << "Error: GITEA2RSS_BASEURL not set\n"; return 1; } + cgi = true; + const size_t pos = query.find("repo="); + if (pos == std::string::npos) + { + cout << "Status: 400 Bad Request\n\n"; + return 1; + } + url = string(envbaseurl) + "/" + query.substr(query.find('=', pos) + 1); - cout << "Content-Type: application/rss+xml\n\n"; + cout << "Content-Type: application/rss+xml\n"; } else if (argc < 2) { @@ -144,6 +159,10 @@ int main(int argc, char *argv[]) } Json::Value json; data >> json; + if (cgi) + { + cout << endl; + } cout << "\n" @@ -171,14 +190,6 @@ int main(int argc, char *argv[]) << "
\n" << release["body"].asString()
              << "\n
]]>\n"; - // for (const Json::Value &file : release["assets"]) - // { - // cout << " \n"; - // } - cout << " \n"; }