2018-01-08 16:22:45 +01:00
|
|
|
/*****************************************************************************
|
2020-02-04 00:11:38 +01:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2018-01-08 16:22:45 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2018-01-08 16:22:45 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2018-01-08 16:22:45 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-06-11 11:18:41 +02:00
|
|
|
#pragma once
|
2018-01-08 16:22:45 +01:00
|
|
|
|
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include "../common.h"
|
2018-06-22 23:02:47 +02:00
|
|
|
|
2018-07-21 16:17:06 +02:00
|
|
|
# include <functional>
|
|
|
|
# include <map>
|
|
|
|
# include <string>
|
2020-02-04 00:11:38 +01:00
|
|
|
# include <thread>
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2020-02-04 00:11:38 +01:00
|
|
|
namespace Http
|
2018-01-08 16:22:45 +01:00
|
|
|
{
|
2018-06-22 23:02:47 +02:00
|
|
|
enum class Status
|
|
|
|
{
|
2019-02-04 19:37:49 +01:00
|
|
|
OK = 200,
|
|
|
|
NotFound = 404
|
2018-06-22 23:02:47 +02:00
|
|
|
};
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
enum class Method
|
|
|
|
{
|
|
|
|
GET,
|
|
|
|
POST,
|
|
|
|
PUT
|
|
|
|
};
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
struct Response
|
|
|
|
{
|
|
|
|
Status status;
|
|
|
|
std::string content_type;
|
|
|
|
std::string body = "";
|
|
|
|
std::map<std::string, std::string> header = {};
|
|
|
|
std::string error = "";
|
|
|
|
};
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
struct Request
|
|
|
|
{
|
|
|
|
std::string url;
|
|
|
|
std::map<std::string, std::string> header = {};
|
|
|
|
Method method = Method::GET;
|
|
|
|
std::string body = "";
|
|
|
|
bool forceIPv4 = false;
|
|
|
|
};
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2018-06-22 23:02:47 +02:00
|
|
|
Response Do(const Request& req);
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2020-02-04 00:11:38 +01:00
|
|
|
inline void DoAsync(const Request& req, std::function<void(Response& res)> fn)
|
|
|
|
{
|
|
|
|
auto thread = std::thread([=]() {
|
|
|
|
Response res;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
res = Do(req);
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
res.error = e.what();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fn(res);
|
|
|
|
});
|
|
|
|
thread.detach();
|
|
|
|
}
|
|
|
|
} // namespace Http
|
2018-01-08 16:22:45 +01:00
|
|
|
|
|
|
|
#endif // DISABLE_HTTP
|