2019-04-20 04:47:53 +02:00
|
|
|
/* This file is part of gitea2rss.
|
|
|
|
* Copyright © 2019 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
2019-08-09 22:38:28 +02:00
|
|
|
#include <memory>
|
2019-08-25 04:22:32 +02:00
|
|
|
#include <regex>
|
2019-08-09 22:38:28 +02:00
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
|
|
|
#include <Poco/StreamCopier.h>
|
|
|
|
#include <Poco/URI.h>
|
|
|
|
#include <Poco/Exception.h>
|
|
|
|
#include <Poco/Environment.h>
|
2019-04-20 04:47:53 +02:00
|
|
|
#include "version.hpp"
|
|
|
|
#include "gitea2rss.hpp"
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2019-08-09 22:38:28 +02:00
|
|
|
using std::istream;
|
|
|
|
using std::unique_ptr;
|
|
|
|
using std::make_unique;
|
2019-08-25 04:22:32 +02:00
|
|
|
using std::regex;
|
|
|
|
using std::regex_search;
|
|
|
|
using std::smatch;
|
2019-08-09 22:38:28 +02:00
|
|
|
using Poco::Net::HTTPClientSession;
|
|
|
|
using Poco::Net::HTTPSClientSession;
|
|
|
|
using Poco::Net::HTTPRequest;
|
|
|
|
using Poco::Net::HTTPResponse;
|
|
|
|
using Poco::Net::HTTPMessage;
|
|
|
|
using Poco::StreamCopier;
|
|
|
|
using Poco::Environment;
|
2019-04-20 04:47:53 +02:00
|
|
|
|
2019-08-09 22:38:28 +02:00
|
|
|
void set_proxy()
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-08-25 04:22:32 +02:00
|
|
|
HTTPSClientSession::ProxyConfig proxyconfig;
|
|
|
|
string env_proxy = Environment::get("http_proxy");
|
|
|
|
regex re_proxy("^(?:https?://)?(?:([^:]+):?([^@]*)@)?" // user:password
|
|
|
|
"([^:]+):([[:digit:]]+/?)"); // host:port
|
|
|
|
smatch match;
|
2019-04-20 10:00:15 +02:00
|
|
|
|
2019-08-25 04:22:32 +02:00
|
|
|
if (regex_search(env_proxy, match, re_proxy))
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
2019-08-25 05:05:36 +02:00
|
|
|
string username, password;
|
|
|
|
Poco::URI::decode(match[1].str(), username);
|
|
|
|
Poco::URI::decode(match[2].str(), password);
|
|
|
|
|
2019-08-25 04:22:32 +02:00
|
|
|
proxyconfig.host = match[3].str();
|
|
|
|
proxyconfig.port = std::stoi(match[4].str());
|
2019-08-25 05:05:36 +02:00
|
|
|
proxyconfig.username = username;
|
|
|
|
proxyconfig.password = password;
|
2019-08-09 22:38:28 +02:00
|
|
|
|
2019-08-25 04:22:32 +02:00
|
|
|
HTTPSClientSession::setGlobalProxyConfig(proxyconfig);
|
2019-08-09 22:38:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception &)
|
|
|
|
{
|
|
|
|
// No proxy found, no problem.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const string get_http(const string &url)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Poco::URI poco_uri(url);
|
|
|
|
string path = poco_uri.getPathAndQuery();
|
|
|
|
if (path.empty())
|
|
|
|
{
|
|
|
|
path = "/";
|
|
|
|
}
|
2019-04-20 10:00:15 +02:00
|
|
|
|
2019-08-09 22:38:28 +02:00
|
|
|
unique_ptr<HTTPClientSession> session;
|
|
|
|
if (poco_uri.getScheme() == "https")
|
|
|
|
{
|
|
|
|
session = make_unique<HTTPSClientSession>(poco_uri.getHost(),
|
|
|
|
poco_uri.getPort());
|
|
|
|
}
|
|
|
|
else if (poco_uri.getScheme() == "http")
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
2019-08-09 22:38:28 +02:00
|
|
|
session = make_unique<HTTPClientSession>(poco_uri.getHost(),
|
|
|
|
poco_uri.getPort());
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
|
|
|
else
|
2019-08-09 22:38:28 +02:00
|
|
|
{
|
|
|
|
throw Poco::Exception("Protocol not supported.");
|
|
|
|
}
|
|
|
|
|
|
|
|
HTTPRequest request(HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1);
|
|
|
|
request.set("User-Agent", string("gitea2rss/") + global::version);
|
|
|
|
|
|
|
|
HTTPResponse response;
|
|
|
|
|
|
|
|
session->sendRequest(request);
|
|
|
|
istream &rs = session->receiveResponse(response);
|
|
|
|
|
|
|
|
// Not using the constants because some are too new for Debian stretch.
|
|
|
|
switch (response.getStatus())
|
|
|
|
{
|
|
|
|
case 301: // HTTPResponse::HTTP_MOVED_PERMANENTLY
|
|
|
|
case 308: // HTTPResponse::HTTP_PERMANENT_REDIRECT
|
|
|
|
case 302: // HTTPResponse::HTTP_FOUND
|
|
|
|
case 303: // HTTPResponse::HTTP_SEE_OTHER
|
|
|
|
case 307: // HTTPResponse::HTTP_TEMPORARY_REDIRECT
|
|
|
|
{
|
|
|
|
string location = response.get("Location");
|
|
|
|
if (location.substr(0, 4) != "http")
|
|
|
|
{
|
|
|
|
location = poco_uri.getScheme() + "://" + poco_uri.getHost()
|
|
|
|
+ location;
|
|
|
|
}
|
|
|
|
return get_http(location);
|
|
|
|
}
|
|
|
|
case HTTPResponse::HTTP_OK:
|
|
|
|
{
|
|
|
|
string answer;
|
|
|
|
StreamCopier::copyToString(rs, answer);
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
default:
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
|
|
|
if (cgi)
|
|
|
|
{
|
2019-08-09 22:38:28 +02:00
|
|
|
cout << "Status: " << response.getStatus() << endl;
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
2019-08-09 22:38:28 +02:00
|
|
|
cerr << "HTTP Error: " << response.getStatus() << endl;
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-09 22:38:28 +02:00
|
|
|
catch (const Poco::Exception &e)
|
2019-04-20 04:47:53 +02:00
|
|
|
{
|
2019-08-09 22:38:28 +02:00
|
|
|
cerr << "Error: " << e.displayText() << endl;
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|
|
|
|
|
2019-08-09 22:38:28 +02:00
|
|
|
return "";
|
2019-04-20 04:47:53 +02:00
|
|
|
}
|