Files
OpenRCT2-Web/src/openrct2/network/Http.cpp
T

330 lines
9.4 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
/*****************************************************************************
* 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
2017-03-30 18:25:35 +01:00
#include <thread>
2015-05-25 20:36:40 +01:00
extern "C" {
#include "http.h"
2015-05-25 20:36:40 +01:00
}
#ifdef DISABLE_HTTP
void http_init() { }
void http_dispose() { }
#else
#include "../core/Console.hpp"
2015-12-14 22:23:12 +01:00
#include "../core/Math.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
#include "../Version.h"
2015-05-25 20:36:40 +01:00
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
// cURL includes windows.h, but we don't need all of it.
#define WIN32_LEAN_AND_MEAN
#endif
2015-09-12 18:17:25 +01:00
#include <curl/curl.h>
2015-11-08 22:03:35 +00:00
#define MIME_TYPE_APPLICATION_JSON "application/json"
2016-08-29 19:25:53 +01:00
#define OPENRCT2_USER_AGENT "OpenRCT2/" OPENRCT2_VERSION
2015-11-08 22:03:35 +00:00
2017-03-30 18:25:35 +01:00
struct HttpRequest2
{
void * Tag = nullptr;
std::string Method;
std::string Url;
http_data_type Type;
size_t Size = 0;
union
{
char * Buffer = 0;
json_t * Json;
} Body;
HttpRequest2() { }
HttpRequest2(const HttpRequest2 &request)
{
Tag = request.Tag;
Method = request.Method;
Url = request.Url;
Type = request.Type;
Size = request.Size;
if (request.Type == HTTP_DATA_JSON)
{
Body.Json = json_deep_copy(request.Body.Json);
}
else
{
Body.Buffer = new char[request.Size];
memcpy(Body.Buffer, request.Body.Buffer, request.Size);
}
}
explicit HttpRequest2(const http_request_t * request)
{
Tag = request->tag;
Method = std::string(request->method);
Url = std::string(request->url);
Type = request->type;
Size = request->size;
if (request->type == HTTP_DATA_JSON)
{
Body.Json = json_deep_copy(request->root);
}
else
{
Body.Buffer = new char[request->size];
memcpy(Body.Buffer, request->body, request->size);
}
}
~HttpRequest2()
{
if (Type == HTTP_DATA_JSON)
{
json_decref(Body.Json);
}
else
{
delete Body.Buffer;
}
}
};
2016-05-12 23:57:40 +02:00
typedef struct read_buffer {
char *ptr;
size_t length;
size_t position;
2015-11-08 22:03:35 +00:00
} read_buffer;
2016-05-12 23:57:40 +02:00
typedef struct write_buffer {
char *ptr;
size_t length;
size_t capacity;
2015-05-25 20:36:40 +01:00
} write_buffer;
void http_init()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
2015-05-25 20:36:40 +01:00
}
void http_dispose()
{
curl_global_cleanup();
2015-05-25 20:36:40 +01:00
}
static size_t http_request_write_func(void *ptr, size_t size, size_t nmemb, void *userdata)
{
write_buffer *writeBuffer = (write_buffer*)userdata;
2015-05-25 20:36:40 +01:00
size_t newBytesLength = size * nmemb;
if (newBytesLength > 0) {
size_t newCapacity = writeBuffer->capacity;
size_t newLength = writeBuffer->length + newBytesLength;
while (newLength > newCapacity) {
newCapacity = Math::Max<size_t>(4096, newCapacity * 2);
}
if (newCapacity != writeBuffer->capacity) {
writeBuffer->ptr = (char*)realloc(writeBuffer->ptr, newCapacity);
writeBuffer->capacity = newCapacity;
}
memcpy(writeBuffer->ptr + writeBuffer->length, ptr, newBytesLength);
writeBuffer->length = newLength;
}
return newBytesLength;
}
2017-03-30 18:25:35 +01:00
static http_response_t *http_request(const HttpRequest2 &request)
{
CURL *curl;
CURLcode curlResult;
http_response_t *response;
read_buffer readBuffer = { 0 };
write_buffer writeBuffer;
curl = curl_easy_init();
2017-08-15 10:07:44 +02:00
if (curl == nullptr)
return nullptr;
2017-08-15 10:07:44 +02:00
if (request.Type == HTTP_DATA_JSON && request.Body.Json != nullptr) {
2017-03-30 18:25:35 +01:00
readBuffer.ptr = json_dumps(request.Body.Json, JSON_COMPACT);
readBuffer.length = strlen(readBuffer.ptr);
readBuffer.position = 0;
2017-08-15 10:07:44 +02:00
} else if (request.Type == HTTP_DATA_RAW && request.Body.Buffer != nullptr) {
2017-03-30 18:25:35 +01:00
readBuffer.ptr = request.Body.Buffer;
readBuffer.length = request.Size;
readBuffer.position = 0;
}
2017-08-15 10:07:44 +02:00
writeBuffer.ptr = nullptr;
writeBuffer.length = 0;
writeBuffer.capacity = 0;
2017-08-15 10:07:44 +02:00
curl_slist *headers = nullptr;
2017-03-30 18:25:35 +01:00
if (request.Type == HTTP_DATA_JSON) {
headers = curl_slist_append(headers, "Accept: " MIME_TYPE_APPLICATION_JSON);
2017-08-15 10:07:44 +02:00
if (request.Body.Json != nullptr) {
headers = curl_slist_append(headers, "Content-Type: " MIME_TYPE_APPLICATION_JSON);
}
}
2017-08-15 10:07:44 +02:00
if (readBuffer.ptr != nullptr) {
char contentLengthHeaderValue[64];
snprintf(contentLengthHeaderValue, sizeof(contentLengthHeaderValue), "Content-Length: %zu", readBuffer.length);
headers = curl_slist_append(headers, contentLengthHeaderValue);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, readBuffer.ptr);
}
2017-03-30 18:25:35 +01:00
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request.Method.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_USERAGENT, OPENRCT2_USER_AGENT);
2017-03-30 18:25:35 +01:00
curl_easy_setopt(curl, CURLOPT_URL, request.Url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeBuffer);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_request_write_func);
curlResult = curl_easy_perform(curl);
2017-08-15 10:07:44 +02:00
if (request.Type == HTTP_DATA_JSON && request.Body.Json != nullptr) {
free(readBuffer.ptr);
}
if (curlResult != CURLE_OK) {
log_error("HTTP request failed: %s.", curl_easy_strerror(curlResult));
2017-08-15 10:07:44 +02:00
if (writeBuffer.ptr != nullptr)
free(writeBuffer.ptr);
2017-08-15 10:07:44 +02:00
return nullptr;
}
2017-01-16 15:29:49 +01:00
long httpStatusCode;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpStatusCode);
char* contentType;
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &contentType);
// Null terminate the response buffer
writeBuffer.length++;
writeBuffer.ptr = (char*)realloc(writeBuffer.ptr, writeBuffer.length);
writeBuffer.capacity = writeBuffer.length;
writeBuffer.ptr[writeBuffer.length - 1] = 0;
2017-08-15 10:07:44 +02:00
response = nullptr;
// Parse as JSON if response is JSON
2017-08-15 10:07:44 +02:00
if (contentType != nullptr && strstr(contentType, "json") != nullptr) {
json_t *root;
json_error_t error;
root = json_loads(writeBuffer.ptr, 0, &error);
2017-08-15 10:07:44 +02:00
if (root != nullptr) {
response = (http_response_t*) malloc(sizeof(http_response_t));
2017-03-30 18:25:35 +01:00
response->tag = request.Tag;
2017-01-04 22:17:08 +01:00
response->status_code = (sint32) httpStatusCode;
response->root = root;
response->type = HTTP_DATA_JSON;
response->size = writeBuffer.length;
}
free(writeBuffer.ptr);
} else {
response = (http_response_t*) malloc(sizeof(http_response_t));
2017-03-30 18:25:35 +01:00
response->tag = request.Tag;
2017-01-04 22:17:08 +01:00
response->status_code = (sint32) httpStatusCode;
response->body = writeBuffer.ptr;
response->type = HTTP_DATA_RAW;
response->size = writeBuffer.length;
}
curl_easy_cleanup(curl);
return response;
}
2017-03-30 18:25:35 +01:00
void http_request_async(const http_request_t * request, void (*callback)(http_response_t*))
{
2017-03-30 18:25:35 +01:00
auto request2 = HttpRequest2(request);
auto thread = std::thread([](const HttpRequest2 &req, void(*callback2)(http_response_t*)) -> void
{
http_response_t * response = http_request(req);
callback2(response);
}, std::move(request2), callback);
thread.detach();
}
void http_request_dispose(http_response_t *response)
{
2017-08-15 10:07:44 +02:00
if (response->type == HTTP_DATA_JSON && response->root != nullptr)
json_decref(response->root);
2017-08-15 10:07:44 +02:00
else if (response->type == HTTP_DATA_RAW && response->body != nullptr)
free(response->body);
free(response);
}
const char *http_get_extension_from_url(const char *url, const char *fallback)
{
const char *extension = strrchr(url, '.');
// Assume a save file by default if no valid extension can be determined
2017-08-15 10:07:44 +02:00
if (extension == nullptr || strchr(extension, '/') != nullptr) {
return fallback;
} else {
return extension;
}
}
2017-06-06 23:42:28 +01:00
size_t http_download_park(const char * url, void * * outData)
{
2017-06-06 23:24:18 +02:00
// Download park to buffer in memory
HttpRequest2 request;
request.Url = url;
request.Method = "GET";
request.Type = HTTP_DATA_NONE;
2017-06-06 23:24:18 +02:00
http_response_t *response = http_request(request);
2017-08-15 10:07:44 +02:00
if (response == nullptr || response->status_code != 200) {
2017-06-06 23:24:18 +02:00
Console::Error::WriteLine("Failed to download '%s'", request.Url.c_str());
2017-08-15 10:07:44 +02:00
if (response != nullptr) {
2017-06-06 23:24:18 +02:00
http_request_dispose(response);
}
2017-06-06 23:42:28 +01:00
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
2017-06-06 23:42:28 +01:00
size_t dataSize = response->size - 1;
void * data = malloc(dataSize);
2017-08-15 10:07:44 +02:00
if (data == nullptr) {
2017-06-06 23:42:28 +01:00
dataSize = 0;
Console::Error::WriteLine("Failed to allocate memory for downloaded park.");
} else {
memcpy(data, response->body, dataSize);
2017-06-06 23:24:18 +02:00
}
2015-05-25 20:36:40 +01:00
2017-06-06 23:24:18 +02:00
http_request_dispose(response);
2015-11-08 22:03:35 +00:00
2017-06-06 23:42:28 +01:00
*outData = data;
return dataSize;
2015-05-25 20:36:40 +01:00
}
2015-09-24 22:47:14 +02:00
#endif