Luanti 5.10.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#include "config.h"
10
11// These can be used in place of "caller" in to specify special handling.
12// Discard result (used as default value of "caller").
13#define HTTPFETCH_DISCARD 0
14// Indicates that the result should not be discarded when performing a
15// synchronous request (since a real caller ID is not needed for synchronous
16// requests because the result does not have to be retrieved later).
17#define HTTPFETCH_SYNC 1
18// Print response body to console if the server returns an error code.
19#define HTTPFETCH_PRINT_ERR 2
20// Start of regular allocated caller IDs.
21#define HTTPFETCH_CID_START 3
22
23namespace {
24 // lower bound for curl_timeout (see also settingtypes.txt)
25 constexpr long MIN_HTTPFETCH_TIMEOUT_INTERACTIVE = 1000;
26 // lower bound for curl_file_download_timeout
27 constexpr long MIN_HTTPFETCH_TIMEOUT = 5000;
28}
29
30// Methods
38
40{
41 std::string url = "";
42
43 // Identifies the caller (for asynchronous requests)
44 // Ignored by httpfetch_sync
46
47 // Some number that identifies the request
48 // (when the same caller issues multiple httpfetch_async calls)
49 u64 request_id = 0;
50
51 // Timeout for the whole transfer, in milliseconds
52 long timeout;
53
54 // Timeout for the connection phase, in milliseconds
56
57 // Indicates if this is multipart/form-data or
58 // application/x-www-form-urlencoded. POST-only.
59 bool multipart = false;
60
61 // The Method to use default = GET
62 // Avaible methods GET, POST, PUT, DELETE
64
65 // Fields of the request
67
68 // Raw data of the request overrides fields
69 std::string raw_data;
70
71 // If not empty, should contain entries such as "Accept: text/html"
72 std::vector<std::string> extra_headers;
73
74 // useragent to use
75 std::string useragent;
76
78};
79
81{
82 bool succeeded = false;
83 bool timeout = false;
84 long response_code = 0;
85 std::string data = "";
86 // The caller and request_id from the corresponding HTTPFetchRequest.
88 u64 request_id = 0;
89
90 HTTPFetchResult() = default;
91
92 HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
93 caller(fetch_request.caller), request_id(fetch_request.request_id)
94 {
95 }
96};
97
98// Initializes the httpfetch module
99void httpfetch_init(int parallel_limit);
100
101// Stops the httpfetch thread and cleans up resources
102void httpfetch_cleanup();
103
104// Starts an asynchronous HTTP fetch request
105void httpfetch_async(const HTTPFetchRequest &fetch_request);
106
107// If any fetch for the given caller ID is complete, removes it from the
108// result queue, sets the fetch result and returns true. Otherwise returns false.
109bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
110
111// Allocates a caller ID for httpfetch_async
112// Not required if you want to set caller = HTTPFETCH_DISCARD
114
115// Allocates a non-predictable caller ID for httpfetch_async
117
118// Frees a caller ID allocated with httpfetch_caller_alloc
119// Note: This can be expensive, because the httpfetch thread is told
120// to stop any ongoing fetches for the given caller.
121void httpfetch_caller_free(u64 caller);
122
123// Performs a synchronous HTTP request that is interruptible if the current
124// thread is a Thread object. interval is the completion check interval in ms.
125// This blocks and therefore should only be used from background threads.
126// Returned is whether the request completed without interruption.
127bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request,
128 HTTPFetchResult &fetch_result, long interval = 100);
bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result)
Definition httpfetch.cpp:115
HttpMethod
Definition httpfetch.h:32
@ HTTP_PUT
Definition httpfetch.h:35
@ HTTP_DELETE
Definition httpfetch.h:36
@ HTTP_GET
Definition httpfetch.h:33
@ HTTP_POST
Definition httpfetch.h:34
u64 httpfetch_caller_alloc_secure()
Definition httpfetch.cpp:74
u64 httpfetch_caller_alloc()
Definition httpfetch.cpp:55
bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result, long interval=100)
Definition httpfetch.cpp:744
void httpfetch_init(int parallel_limit)
Definition httpfetch.cpp:694
void httpfetch_caller_free(u64 caller)
Definition httpfetch.cpp:103
void httpfetch_cleanup()
Definition httpfetch.cpp:712
void httpfetch_async(const HTTPFetchRequest &fetch_request)
Definition httpfetch.cpp:726
#define HTTPFETCH_DISCARD
Definition httpfetch.h:13
std::unordered_map< std::string, std::string > StringMap
Definition string.h:65
Definition httpfetch.h:40
long connect_timeout
Definition httpfetch.h:55
bool multipart
Definition httpfetch.h:59
long timeout
Definition httpfetch.h:52
std::vector< std::string > extra_headers
Definition httpfetch.h:72
HttpMethod method
Definition httpfetch.h:63
u64 request_id
Definition httpfetch.h:49
std::string url
Definition httpfetch.h:41
std::string raw_data
Definition httpfetch.h:69
u64 caller
Definition httpfetch.h:45
StringMap fields
Definition httpfetch.h:66
std::string useragent
Definition httpfetch.h:75
HTTPFetchRequest()
Definition httpfetch.cpp:35
Definition httpfetch.h:81
HTTPFetchResult()=default
bool succeeded
Definition httpfetch.h:82
u64 caller
Definition httpfetch.h:87
HTTPFetchResult(const HTTPFetchRequest &fetch_request)
Definition httpfetch.h:92
long response_code
Definition httpfetch.h:84
std::string data
Definition httpfetch.h:85
bool timeout
Definition httpfetch.h:83
u64 request_id
Definition httpfetch.h:88