2013-08-29 05:04:56 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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 Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
#include <vector>
|
2015-05-19 08:24:14 +02:00
|
|
|
#include "util/string.h"
|
2013-08-29 05:04:56 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2022-01-01 22:44:56 +01:00
|
|
|
// These can be used in place of "caller" in to specify special handling.
|
|
|
|
// Discard result (used as default value of "caller").
|
2013-08-29 05:04:56 +02:00
|
|
|
#define HTTPFETCH_DISCARD 0
|
2022-01-01 22:44:56 +01:00
|
|
|
// Indicates that the result should not be discarded when performing a
|
|
|
|
// synchronous request (since a real caller ID is not needed for synchronous
|
|
|
|
// requests because the result does not have to be retrieved later).
|
2013-11-09 22:49:27 +01:00
|
|
|
#define HTTPFETCH_SYNC 1
|
2022-01-01 22:44:56 +01:00
|
|
|
// Print response body to console if the server returns an error code.
|
|
|
|
#define HTTPFETCH_PRINT_ERR 2
|
|
|
|
// Start of regular allocated caller IDs.
|
|
|
|
#define HTTPFETCH_CID_START 3
|
2013-08-29 05:04:56 +02:00
|
|
|
|
2020-07-29 23:16:21 +02:00
|
|
|
// Methods
|
|
|
|
enum HttpMethod : u8
|
|
|
|
{
|
|
|
|
HTTP_GET,
|
|
|
|
HTTP_POST,
|
|
|
|
HTTP_PUT,
|
|
|
|
HTTP_DELETE,
|
|
|
|
};
|
|
|
|
|
2013-08-29 05:04:56 +02:00
|
|
|
struct HTTPFetchRequest
|
|
|
|
{
|
2017-06-17 19:11:28 +02:00
|
|
|
std::string url = "";
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// Identifies the caller (for asynchronous requests)
|
|
|
|
// Ignored by httpfetch_sync
|
2022-01-01 22:44:56 +01:00
|
|
|
u64 caller = HTTPFETCH_DISCARD;
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// Some number that identifies the request
|
|
|
|
// (when the same caller issues multiple httpfetch_async calls)
|
2022-01-01 22:44:56 +01:00
|
|
|
u64 request_id = 0;
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// Timeout for the whole transfer, in milliseconds
|
|
|
|
long timeout;
|
|
|
|
|
|
|
|
// Timeout for the connection phase, in milliseconds
|
|
|
|
long connect_timeout;
|
|
|
|
|
2014-06-19 22:00:22 +02:00
|
|
|
// Indicates if this is multipart/form-data or
|
|
|
|
// application/x-www-form-urlencoded. POST-only.
|
2017-06-17 19:11:28 +02:00
|
|
|
bool multipart = false;
|
2014-06-19 22:00:22 +02:00
|
|
|
|
2020-07-29 23:16:21 +02:00
|
|
|
// The Method to use default = GET
|
|
|
|
// Avaible methods GET, POST, PUT, DELETE
|
|
|
|
HttpMethod method = HTTP_GET;
|
|
|
|
|
|
|
|
// Fields of the request
|
|
|
|
StringMap fields;
|
2014-06-19 22:00:22 +02:00
|
|
|
|
2020-07-29 23:16:21 +02:00
|
|
|
// Raw data of the request overrides fields
|
|
|
|
std::string raw_data;
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// If not empty, should contain entries such as "Accept: text/html"
|
|
|
|
std::vector<std::string> extra_headers;
|
|
|
|
|
2017-04-23 10:24:00 +02:00
|
|
|
// useragent to use
|
2013-08-29 05:58:13 +02:00
|
|
|
std::string useragent;
|
|
|
|
|
2014-01-06 23:50:45 +01:00
|
|
|
HTTPFetchRequest();
|
2013-08-29 05:04:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct HTTPFetchResult
|
|
|
|
{
|
2017-06-17 19:11:28 +02:00
|
|
|
bool succeeded = false;
|
|
|
|
bool timeout = false;
|
|
|
|
long response_code = 0;
|
|
|
|
std::string data = "";
|
2013-08-29 05:04:56 +02:00
|
|
|
// The caller and request_id from the corresponding HTTPFetchRequest.
|
2022-01-01 22:44:56 +01:00
|
|
|
u64 caller = HTTPFETCH_DISCARD;
|
|
|
|
u64 request_id = 0;
|
2013-08-29 05:04:56 +02:00
|
|
|
|
2017-08-18 08:07:59 +02:00
|
|
|
HTTPFetchResult() = default;
|
2017-04-23 10:24:00 +02:00
|
|
|
|
2017-10-09 11:32:06 +02:00
|
|
|
HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
|
|
|
|
caller(fetch_request.caller), request_id(fetch_request.request_id)
|
2017-04-23 10:24:00 +02:00
|
|
|
{
|
|
|
|
}
|
2013-08-29 05:04:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Initializes the httpfetch module
|
|
|
|
void httpfetch_init(int parallel_limit);
|
|
|
|
|
|
|
|
// Stops the httpfetch thread and cleans up resources
|
|
|
|
void httpfetch_cleanup();
|
|
|
|
|
|
|
|
// Starts an asynchronous HTTP fetch request
|
2014-09-15 02:46:45 +02:00
|
|
|
void httpfetch_async(const HTTPFetchRequest &fetch_request);
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// If any fetch for the given caller ID is complete, removes it from the
|
2014-09-15 02:46:45 +02:00
|
|
|
// result queue, sets the fetch result and returns true. Otherwise returns false.
|
2022-01-01 22:44:56 +01:00
|
|
|
bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// Allocates a caller ID for httpfetch_async
|
|
|
|
// Not required if you want to set caller = HTTPFETCH_DISCARD
|
2022-01-01 22:44:56 +01:00
|
|
|
u64 httpfetch_caller_alloc();
|
2013-08-29 05:04:56 +02:00
|
|
|
|
2016-02-18 11:38:47 +01:00
|
|
|
// Allocates a non-predictable caller ID for httpfetch_async
|
2022-01-01 22:44:56 +01:00
|
|
|
u64 httpfetch_caller_alloc_secure();
|
2016-02-18 11:38:47 +01:00
|
|
|
|
2013-08-29 05:04:56 +02:00
|
|
|
// Frees a caller ID allocated with httpfetch_caller_alloc
|
|
|
|
// Note: This can be expensive, because the httpfetch thread is told
|
|
|
|
// to stop any ongoing fetches for the given caller.
|
2022-01-01 22:44:56 +01:00
|
|
|
void httpfetch_caller_free(u64 caller);
|
2013-08-29 05:04:56 +02:00
|
|
|
|
|
|
|
// Performs a synchronous HTTP request. This blocks and therefore should
|
|
|
|
// only be used from background threads.
|
2017-04-23 10:24:00 +02:00
|
|
|
void httpfetch_sync(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result);
|