Luanti 5.19.0-dev
Loading...
Searching...
No Matches
httpfetch.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <vector>
8#include "util/string.h"
9
10// These can be used in place of "caller" in to specify special handling.
11// Discard result (used as default value of "caller").
12#define HTTPFETCH_DISCARD 0
13// Indicates that the result should not be discarded when performing a
14// synchronous request (since a real caller ID is not needed for synchronous
15// requests because the result does not have to be retrieved later).
16#define HTTPFETCH_SYNC 1
17// Print response body to console if the server returns an error code.
18#define HTTPFETCH_PRINT_BODY 2
19// Start of regular allocated caller IDs.
20#define HTTPFETCH_CID_START 3
21
22namespace {
23 // lower bound for curl_timeout (see also settingtypes.txt)
24 constexpr long MIN_HTTPFETCH_TIMEOUT_INTERACTIVE = 1000;
25 // lower bound for curl_file_download_timeout
26 constexpr long MIN_HTTPFETCH_TIMEOUT = 5000;
27}
28
29// Methods
39
41{
42 // Target URL
43 std::string url;
44
45 // Identifies the caller (for asynchronous requests)
46 // Ignored by httpfetch_sync
48
49 // Some number that identifies the request
50 // (when the same caller issues multiple httpfetch_async calls)
51 u64 request_id = 0;
52
53 // Timeout for the whole transfer, in milliseconds
54 long timeout;
55
56 // Timeout for the connection phase, in milliseconds
58
59 // Indicates if this is multipart/form-data or
60 // application/x-www-form-urlencoded. Not allowed for GET.
61 bool multipart = false;
62
63 // Method to use
65
66 // Fields of the request
68
69 // Raw data of the request (instead of fields, ignored if multipart)
70 std::string raw_data;
71
72 // Additional headers to send, in the form of "Accept: text/html"
73 std::vector<std::string> extra_headers;
74
75 // User agent to send
76 std::string useragent;
77
78 // If true, don't print to errorstream when result is not OK.
79 bool quiet = false;
80
81 // If true, honor the 'bind_address' setting for this request
82 bool use_bind_address = false;
83
85};
86
88{
89 bool succeeded = false;
90 bool timeout = false;
91 long response_code = 0;
92 std::string data;
93 // The caller and request_id from the corresponding HTTPFetchRequest.
95 u64 request_id = 0;
96
98 bool good() const { return succeeded && response_code < 400; }
99
100 HTTPFetchResult() = default;
101
102 HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
103 caller(fetch_request.caller), request_id(fetch_request.request_id)
104 {
105 }
106};
107
108// Initializes the httpfetch module
109void httpfetch_init(int parallel_limit);
110
111// Stops the httpfetch thread and cleans up resources
112void httpfetch_cleanup();
113
114// Starts an asynchronous HTTP fetch request
115void httpfetch_async(const HTTPFetchRequest &fetch_request);
116
117// If any fetch for the given caller ID is complete, removes it from the
118// result queue, sets the fetch result and returns true. Otherwise returns false.
119bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
120
121// Allocates a caller ID for httpfetch_async
122// Not required if you want to set caller = HTTPFETCH_DISCARD
124
125// Allocates a non-predictable caller ID for httpfetch_async
127
128// Frees a caller ID allocated with httpfetch_caller_alloc
129// Note: This can be expensive, because the httpfetch thread is told
130// to stop any ongoing fetches for the given caller.
131void httpfetch_caller_free(u64 caller);
132
133// Performs a synchronous HTTP request that is interruptible if the current
134// thread is a Thread object. interval is the completion check interval in ms.
135// This blocks and therefore should only be used from background threads.
136// Returned is whether the request completed without interruption.
137bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request,
138 HTTPFetchResult &fetch_result, long interval = 100);
bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result)
Definition httpfetch.cpp:113
bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result, long interval)
Definition httpfetch.cpp:796
u64 httpfetch_caller_alloc_secure()
Definition httpfetch.cpp:72
u64 httpfetch_caller_alloc()
Definition httpfetch.cpp:53
void httpfetch_init(int parallel_limit)
Definition httpfetch.cpp:757
void httpfetch_caller_free(u64 caller)
Definition httpfetch.cpp:101
void httpfetch_cleanup()
Definition httpfetch.cpp:770
void httpfetch_async(const HTTPFetchRequest &fetch_request)
Definition httpfetch.cpp:784
HttpMethod
Definition httpfetch.h:31
@ HTTP_PUT
Definition httpfetch.h:35
@ HTTP_DELETE
Definition httpfetch.h:37
@ HTTP_GET
Definition httpfetch.h:32
@ HTTP_POST
Definition httpfetch.h:34
@ HTTP_HEAD
Definition httpfetch.h:33
@ HTTP_PATCH
Definition httpfetch.h:36
#define HTTPFETCH_DISCARD
Definition httpfetch.h:12
std::unordered_map< std::string, std::string > StringMap
Definition string.h:69
Definition httpfetch.h:41
long connect_timeout
Definition httpfetch.h:57
bool multipart
Definition httpfetch.h:61
bool quiet
Definition httpfetch.h:79
long timeout
Definition httpfetch.h:54
std::vector< std::string > extra_headers
Definition httpfetch.h:73
bool use_bind_address
Definition httpfetch.h:82
HttpMethod method
Definition httpfetch.h:64
u64 request_id
Definition httpfetch.h:51
std::string url
Definition httpfetch.h:43
std::string raw_data
Definition httpfetch.h:70
u64 caller
Definition httpfetch.h:47
StringMap fields
Definition httpfetch.h:67
std::string useragent
Definition httpfetch.h:76
HTTPFetchRequest()
Definition httpfetch.cpp:33
Definition httpfetch.h:88
HTTPFetchResult()=default
bool succeeded
Definition httpfetch.h:89
u64 caller
Definition httpfetch.h:94
HTTPFetchResult(const HTTPFetchRequest &fetch_request)
Definition httpfetch.h:102
long response_code
Definition httpfetch.h:91
std::string data
Definition httpfetch.h:92
bool good() const
returns true if the request succeeded with a non-error status code
Definition httpfetch.h:98
bool timeout
Definition httpfetch.h:90
u64 request_id
Definition httpfetch.h:95