mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-18 04:23:20 +01:00
improve master server to POST json
This commit is contained in:
@@ -16,6 +16,14 @@ void http_dispose() { }
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <curl/curl.h>
|
||||
|
||||
#define MIME_TYPE_APPLICATION_JSON "application/json"
|
||||
|
||||
typedef struct {
|
||||
char *ptr;
|
||||
int length;
|
||||
int position;
|
||||
} read_buffer;
|
||||
|
||||
typedef struct {
|
||||
char *ptr;
|
||||
int length;
|
||||
@@ -32,6 +40,22 @@ void http_dispose()
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
static size_t http_request_read_func(void *ptr, size_t size, size_t nmemb, void *userdata)
|
||||
{
|
||||
read_buffer *readBuffer = (read_buffer*)userdata;
|
||||
|
||||
size_t remainingBytes = readBuffer->length - readBuffer->position;
|
||||
size_t readBytes = size * nmemb;
|
||||
if (readBytes > remainingBytes) {
|
||||
readBytes = remainingBytes;
|
||||
}
|
||||
|
||||
memcpy(ptr, readBuffer->ptr + readBuffer->position, readBytes);
|
||||
|
||||
readBuffer->position += readBytes;
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
static size_t http_request_write_func(void *ptr, size_t size, size_t nmemb, void *userdata)
|
||||
{
|
||||
write_buffer *writeBuffer = (write_buffer*)userdata;
|
||||
@@ -54,33 +78,55 @@ static size_t http_request_write_func(void *ptr, size_t size, size_t nmemb, void
|
||||
return newBytesLength;
|
||||
}
|
||||
|
||||
http_json_response *http_request_json(const char *url)
|
||||
http_json_response *http_request_json(const http_json_request *request)
|
||||
{
|
||||
CURL *curl;
|
||||
CURLcode curlResult;
|
||||
http_json_response *response;
|
||||
read_buffer readBuffer;
|
||||
write_buffer writeBuffer;
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (curl == NULL)
|
||||
return NULL;
|
||||
|
||||
if (request->body != NULL) {
|
||||
readBuffer.ptr = json_dumps(request->body, JSON_COMPACT);
|
||||
readBuffer.length = strlen(readBuffer.ptr);
|
||||
readBuffer.position = 0;
|
||||
}
|
||||
|
||||
writeBuffer.ptr = NULL;
|
||||
writeBuffer.length = 0;
|
||||
writeBuffer.capacity = 0;
|
||||
|
||||
curl_slist *headers = NULL;
|
||||
headers = curl_slist_append(headers, "Accept: application/json");
|
||||
headers = curl_slist_append(headers, "Accept: " MIME_TYPE_APPLICATION_JSON);
|
||||
if (request->body != NULL) {
|
||||
headers = curl_slist_append(headers, "Content-Type: " MIME_TYPE_APPLICATION_JSON);
|
||||
|
||||
char contentLengthHeaderValue[64];
|
||||
snprintf(contentLengthHeaderValue, sizeof(contentLengthHeaderValue), "Content-Length: %d", readBuffer.length);
|
||||
headers = curl_slist_append(headers, contentLengthHeaderValue);
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, readBuffer.ptr);
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, request->method);
|
||||
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_CAINFO, "curl-ca-bundle.crt");
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, request->url);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &writeBuffer);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_request_write_func);
|
||||
|
||||
curlResult = curl_easy_perform(curl);
|
||||
|
||||
if (request->body != NULL) {
|
||||
free(readBuffer.ptr);
|
||||
}
|
||||
|
||||
if (curlResult != CURLE_OK) {
|
||||
log_error("HTTP request failed: %s.", curl_easy_strerror(curlResult));
|
||||
if (writeBuffer.ptr != NULL)
|
||||
@@ -115,24 +161,27 @@ http_json_response *http_request_json(const char *url)
|
||||
return response;
|
||||
}
|
||||
|
||||
void http_request_json_async(const char *url, void (*callback)(http_json_response*))
|
||||
void http_request_json_async(const http_json_request *request, void (*callback)(http_json_response*))
|
||||
{
|
||||
struct TempThreadArgs {
|
||||
char *url;
|
||||
http_json_request request;
|
||||
void (*callback)(http_json_response*);
|
||||
};
|
||||
|
||||
TempThreadArgs *args = (TempThreadArgs*)malloc(sizeof(TempThreadArgs));
|
||||
args->url = _strdup(url);
|
||||
args->request.url = _strdup(request->url);
|
||||
args->request.method = request->method;
|
||||
args->request.body = json_deep_copy(request->body);
|
||||
args->callback = callback;
|
||||
|
||||
SDL_Thread *thread = SDL_CreateThread([](void *ptr) -> int {
|
||||
TempThreadArgs *args = (TempThreadArgs*)ptr;
|
||||
|
||||
http_json_response *response = http_request_json(args->url);
|
||||
http_json_response *response = http_request_json(&args->request);
|
||||
args->callback(response);
|
||||
|
||||
free(args->url);
|
||||
free((char*)args->request.url);
|
||||
json_decref((json_t*)args->request.body);
|
||||
free(args);
|
||||
return 0;
|
||||
}, NULL, args);
|
||||
|
||||
Reference in New Issue
Block a user