2018-01-08 16:22:45 +01:00
|
|
|
#pragma region Copyright (c) 2014-2018 OpenRCT2 Developers
|
2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
|
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
|
|
|
|
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 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, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* A full copy of the GNU General Public License can be found in licence.txt
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
#include "Http.h"
|
2018-03-14 12:44:16 +01:00
|
|
|
#include <cstring>
|
2018-01-08 16:22:45 +01:00
|
|
|
#include <exception>
|
|
|
|
|
#include <memory>
|
2018-03-14 12:44:16 +01:00
|
|
|
#include <thread>
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
#ifndef DISABLE_HTTP
|
|
|
|
|
|
2017-02-03 13:11:58 +00:00
|
|
|
#include "../Version.h"
|
2018-01-08 16:22:45 +01:00
|
|
|
#include "../core/Console.hpp"
|
2015-05-25 20:36:40 +01:00
|
|
|
|
2017-06-12 18:00:15 +01:00
|
|
|
#ifdef _WIN32
|
2018-01-08 16:22:45 +01:00
|
|
|
// cURL includes windows.h, but we don't need all of it.
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2016-01-13 22:37:13 +00:00
|
|
|
#endif
|
2015-09-12 18:17:25 +01:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
2016-08-29 19:25:53 +01:00
|
|
|
#define OPENRCT2_USER_AGENT "OpenRCT2/" OPENRCT2_VERSION
|
2015-11-08 22:03:35 +00:00
|
|
|
|
2018-06-11 11:18:41 +02:00
|
|
|
namespace OpenRCT2::Network::Http
|
2017-03-30 18:25:35 +01:00
|
|
|
{
|
|
|
|
|
|
2018-06-11 11:18:41 +02:00
|
|
|
Http::Http()
|
2015-05-25 20:36:40 +01:00
|
|
|
{
|
2016-10-30 03:15:27 +01:00
|
|
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
2015-05-25 20:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 11:18:41 +02:00
|
|
|
Http::~Http()
|
2015-05-25 20:36:40 +01:00
|
|
|
{
|
2016-10-30 03:15:27 +01:00
|
|
|
curl_global_cleanup();
|
2015-05-25 20:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
static size_t writeData(const char * src, size_t size, size_t nmemb, void * userdata)
|
2015-05-25 20:36:40 +01:00
|
|
|
{
|
2018-01-08 16:22:45 +01:00
|
|
|
size_t realsize = size * nmemb;
|
|
|
|
|
Response * res = static_cast<Response *>(userdata);
|
|
|
|
|
res->body += std::string(src, src + realsize);
|
2015-05-25 20:36:40 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
return realsize;
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
static size_t header_callback(const char * src, size_t size, size_t nitems, void * userdata)
|
2016-10-30 03:15:27 +01:00
|
|
|
{
|
2018-01-08 16:22:45 +01:00
|
|
|
size_t realsize = nitems * size;
|
|
|
|
|
Response * res = static_cast<Response *>(userdata);
|
2016-10-30 03:15:27 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
auto line = std::string(src, src + realsize);
|
|
|
|
|
auto pos = line.find(':');
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
std::string key = line.substr(0, pos);
|
|
|
|
|
// substract 4 chars for ": " and "\r\n"
|
|
|
|
|
std::string value = line.substr(pos + 2, line.size() - pos - 4);
|
|
|
|
|
res->header[key] = value;
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
return realsize;
|
|
|
|
|
}
|
2016-10-30 03:15:27 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
struct WriteThis
|
|
|
|
|
{
|
|
|
|
|
const char * readptr;
|
|
|
|
|
size_t sizeleft;
|
|
|
|
|
};
|
2016-10-30 03:15:27 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
static size_t read_callback(void * dst, size_t size, size_t nmemb, void * userp)
|
|
|
|
|
{
|
|
|
|
|
WriteThis * wt = static_cast<WriteThis *>(userp);
|
|
|
|
|
size_t buffer_size = size * nmemb;
|
2016-10-30 03:15:27 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
if (wt->sizeleft)
|
|
|
|
|
{
|
|
|
|
|
size_t copy_this_much = wt->sizeleft;
|
|
|
|
|
if (copy_this_much > buffer_size)
|
|
|
|
|
copy_this_much = buffer_size;
|
|
|
|
|
memcpy(dst, wt->readptr, copy_this_much);
|
|
|
|
|
|
|
|
|
|
wt->readptr += copy_this_much;
|
|
|
|
|
wt->sizeleft -= copy_this_much;
|
|
|
|
|
return copy_this_much;
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-10-30 03:15:27 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
Response Do(const Request & req)
|
|
|
|
|
{
|
|
|
|
|
CURL * curl = curl_easy_init();
|
|
|
|
|
std::shared_ptr<void> _(nullptr, [curl](...) { curl_easy_cleanup(curl); });
|
|
|
|
|
|
|
|
|
|
if (!curl)
|
|
|
|
|
std::runtime_error("Failed to initialize curl");
|
|
|
|
|
|
|
|
|
|
Response res;
|
|
|
|
|
|
|
|
|
|
if (req.method == Method::POST || req.method == Method::PUT)
|
|
|
|
|
{
|
|
|
|
|
WriteThis wt;
|
|
|
|
|
wt.readptr = req.body.c_str();
|
|
|
|
|
wt.sizeleft = req.body.size();
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_READDATA, &wt);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)wt.sizeleft);
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
if (req.forceIPv4)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
|
|
|
|
|
|
|
|
|
|
if (req.method == Method::POST)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1L);
|
|
|
|
|
|
|
|
|
|
if (req.method == Method::PUT)
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
|
|
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, req.url.c_str());
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeData);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&res);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)&res);
|
2016-10-30 03:15:27 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, OPENRCT2_USER_AGENT);
|
2018-01-08 16:22:45 +01:00
|
|
|
|
|
|
|
|
curl_slist * chunk = nullptr;
|
|
|
|
|
std::shared_ptr<void> __(nullptr, [chunk](...) { curl_slist_free_all(chunk); });
|
|
|
|
|
for (auto header : req.header)
|
2018-02-11 21:51:46 +01:00
|
|
|
{
|
2018-01-08 16:22:45 +01:00
|
|
|
std::string hs = header.first + ": " + header.second;
|
|
|
|
|
chunk = curl_slist_append(chunk, hs.c_str());
|
2018-02-11 21:51:46 +01:00
|
|
|
}
|
2018-01-08 16:22:45 +01:00
|
|
|
if (req.header.size() != 0)
|
|
|
|
|
{
|
|
|
|
|
if (chunk == nullptr)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Failed to set headers");
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
2018-01-08 16:22:45 +01:00
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
CURLcode curl_code = curl_easy_perform(curl);
|
|
|
|
|
if (curl_code != CURLE_OK)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("Failed to perform request");
|
|
|
|
|
}
|
2016-10-30 03:15:27 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
// gets freed by curl_easy_cleanup
|
|
|
|
|
char * content_type;
|
|
|
|
|
long code;
|
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &content_type);
|
|
|
|
|
res.status = static_cast<Status>(code);
|
|
|
|
|
res.content_type = std::string(content_type);
|
|
|
|
|
|
|
|
|
|
return res;
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
void DoAsync(const Request & req, std::function<void(Response & res)> fn)
|
2016-10-30 03:15:27 +01:00
|
|
|
{
|
2018-01-08 16:22:45 +01:00
|
|
|
auto thread = std::thread([=]() {
|
|
|
|
|
Response res;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-06-11 11:18:41 +02:00
|
|
|
res = Do(req);
|
2018-01-08 16:22:45 +01:00
|
|
|
}
|
|
|
|
|
catch (std::exception & e)
|
|
|
|
|
{
|
|
|
|
|
res.error = e.what();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fn(res);
|
|
|
|
|
});
|
2017-03-30 18:25:35 +01:00
|
|
|
thread.detach();
|
2016-10-30 03:15:27 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 11:18:41 +02:00
|
|
|
size_t DownloadPark(const char * url, void ** outData)
|
2016-10-30 03:15:27 +01:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
// Download park to buffer in memory
|
2018-01-08 16:22:45 +01:00
|
|
|
Request request;
|
|
|
|
|
request.url = url;
|
|
|
|
|
request.method = Method::GET;
|
2017-06-06 23:42:28 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
Response res;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
res = Do(request);
|
|
|
|
|
if (res.status != Status::OK)
|
|
|
|
|
std::runtime_error("bad http status");
|
|
|
|
|
}
|
|
|
|
|
catch (std::exception & e)
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine("Failed to download '%s', cause %s", request.url.c_str(), e.what());
|
2017-08-15 10:07:44 +02:00
|
|
|
*outData = nullptr;
|
2017-06-06 23:42:28 +01:00
|
|
|
return 0;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-11-08 22:03:35 +00:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
size_t dataSize = res.body.size() - 1;
|
|
|
|
|
void * data = malloc(dataSize);
|
|
|
|
|
if (data == nullptr)
|
|
|
|
|
{
|
2017-06-06 23:42:28 +01:00
|
|
|
Console::Error::WriteLine("Failed to allocate memory for downloaded park.");
|
2018-01-08 16:22:45 +01:00
|
|
|
return 0;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-05-25 20:36:40 +01:00
|
|
|
|
2018-01-08 16:22:45 +01:00
|
|
|
memcpy(data, res.body.c_str(), dataSize);
|
2017-06-06 23:42:28 +01:00
|
|
|
*outData = data;
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2017-06-06 23:42:28 +01:00
|
|
|
return dataSize;
|
2015-05-25 20:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-11 11:18:41 +02:00
|
|
|
} // namespace OpenRCT2::Network
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2015-09-24 22:47:14 +02:00
|
|
|
#endif
|