Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
httpfetch.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <vector>
23#include "util/string.h"
24#include "config.h"
25
26// These can be used in place of "caller" in to specify special handling.
27// Discard result (used as default value of "caller").
28#define HTTPFETCH_DISCARD 0
29// Indicates that the result should not be discarded when performing a
30// synchronous request (since a real caller ID is not needed for synchronous
31// requests because the result does not have to be retrieved later).
32#define HTTPFETCH_SYNC 1
33// Print response body to console if the server returns an error code.
34#define HTTPFETCH_PRINT_ERR 2
35// Start of regular allocated caller IDs.
36#define HTTPFETCH_CID_START 3
37
38namespace {
39 // lower bound for curl_timeout (see also settingtypes.txt)
40 constexpr long MIN_HTTPFETCH_TIMEOUT_INTERACTIVE = 1000;
41 // lower bound for curl_file_download_timeout
42 constexpr long MIN_HTTPFETCH_TIMEOUT = 5000;
43}
44
45// Methods
46enum HttpMethod : u8
47{
52};
53
55{
56 std::string url = "";
57
58 // Identifies the caller (for asynchronous requests)
59 // Ignored by httpfetch_sync
61
62 // Some number that identifies the request
63 // (when the same caller issues multiple httpfetch_async calls)
64 u64 request_id = 0;
65
66 // Timeout for the whole transfer, in milliseconds
67 long timeout;
68
69 // Timeout for the connection phase, in milliseconds
71
72 // Indicates if this is multipart/form-data or
73 // application/x-www-form-urlencoded. POST-only.
74 bool multipart = false;
75
76 // The Method to use default = GET
77 // Avaible methods GET, POST, PUT, DELETE
79
80 // Fields of the request
82
83 // Raw data of the request overrides fields
84 std::string raw_data;
85
86 // If not empty, should contain entries such as "Accept: text/html"
87 std::vector<std::string> extra_headers;
88
89 // useragent to use
90 std::string useragent;
91
93};
94
96{
97 bool succeeded = false;
98 bool timeout = false;
99 long response_code = 0;
100 std::string data = "";
101 // The caller and request_id from the corresponding HTTPFetchRequest.
103 u64 request_id = 0;
104
105 HTTPFetchResult() = default;
106
107 HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
108 caller(fetch_request.caller), request_id(fetch_request.request_id)
109 {
110 }
111};
112
113// Initializes the httpfetch module
114void httpfetch_init(int parallel_limit);
115
116// Stops the httpfetch thread and cleans up resources
117void httpfetch_cleanup();
118
119// Starts an asynchronous HTTP fetch request
120void httpfetch_async(const HTTPFetchRequest &fetch_request);
121
122// If any fetch for the given caller ID is complete, removes it from the
123// result queue, sets the fetch result and returns true. Otherwise returns false.
124bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
125
126// Allocates a caller ID for httpfetch_async
127// Not required if you want to set caller = HTTPFETCH_DISCARD
129
130// Allocates a non-predictable caller ID for httpfetch_async
132
133// Frees a caller ID allocated with httpfetch_caller_alloc
134// Note: This can be expensive, because the httpfetch thread is told
135// to stop any ongoing fetches for the given caller.
136void httpfetch_caller_free(u64 caller);
137
138// Performs a synchronous HTTP request that is interruptible if the current
139// thread is a Thread object. interval is the completion check interval in ms.
140// This blocks and therefore should only be used from background threads.
141// Returned is whether the request completed without interruption.
142bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request,
143 HTTPFetchResult &fetch_result, long interval = 100);
bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result)
Definition: httpfetch.cpp:123
HttpMethod
Definition: httpfetch.h:47
@ HTTP_PUT
Definition: httpfetch.h:50
@ HTTP_DELETE
Definition: httpfetch.h:51
@ HTTP_GET
Definition: httpfetch.h:48
@ HTTP_POST
Definition: httpfetch.h:49
u64 httpfetch_caller_alloc_secure()
Definition: httpfetch.cpp:82
u64 httpfetch_caller_alloc()
Definition: httpfetch.cpp:63
bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result, long interval=100)
Definition: httpfetch.cpp:765
void httpfetch_init(int parallel_limit)
Definition: httpfetch.cpp:702
void httpfetch_caller_free(u64 caller)
Definition: httpfetch.cpp:111
void httpfetch_cleanup()
Definition: httpfetch.cpp:720
void httpfetch_async(const HTTPFetchRequest &fetch_request)
Definition: httpfetch.cpp:734
#define HTTPFETCH_DISCARD
Definition: httpfetch.h:28
std::unordered_map< std::string, std::string > StringMap
Definition: string.h:78
Definition: httpfetch.h:55
long connect_timeout
Definition: httpfetch.h:70
bool multipart
Definition: httpfetch.h:74
long timeout
Definition: httpfetch.h:67
std::vector< std::string > extra_headers
Definition: httpfetch.h:87
HttpMethod method
Definition: httpfetch.h:78
u64 request_id
Definition: httpfetch.h:64
std::string url
Definition: httpfetch.h:56
std::string raw_data
Definition: httpfetch.h:84
u64 caller
Definition: httpfetch.h:60
StringMap fields
Definition: httpfetch.h:81
std::string useragent
Definition: httpfetch.h:90
HTTPFetchRequest()
Definition: httpfetch.cpp:43
Definition: httpfetch.h:96
HTTPFetchResult()=default
bool succeeded
Definition: httpfetch.h:97
u64 caller
Definition: httpfetch.h:102
HTTPFetchResult(const HTTPFetchRequest &fetch_request)
Definition: httpfetch.h:107
long response_code
Definition: httpfetch.h:99
std::string data
Definition: httpfetch.h:100
bool timeout
Definition: httpfetch.h:98
u64 request_id
Definition: httpfetch.h:103