Luanti 5.15.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_ERR 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 std::string url = "";
43
44 // Identifies the caller (for asynchronous requests)
45 // Ignored by httpfetch_sync
47
48 // Some number that identifies the request
49 // (when the same caller issues multiple httpfetch_async calls)
50 u64 request_id = 0;
51
52 // Timeout for the whole transfer, in milliseconds
53 long timeout;
54
55 // Timeout for the connection phase, in milliseconds
57
58 // Indicates if this is multipart/form-data or
59 // application/x-www-form-urlencoded. Not allowed for GET.
60 bool multipart = false;
61
62 // Method to use
64
65 // Fields of the request
67
68 // Raw data of the request (instead of fields, ignored if multipart)
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 // User agent to send
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:113
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
u64 httpfetch_caller_alloc_secure()
Definition httpfetch.cpp:72
u64 httpfetch_caller_alloc()
Definition httpfetch.cpp:53
bool httpfetch_sync_interruptible(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result, long interval=100)
Definition httpfetch.cpp:757
void httpfetch_init(int parallel_limit)
Definition httpfetch.cpp:718
void httpfetch_caller_free(u64 caller)
Definition httpfetch.cpp:101
void httpfetch_cleanup()
Definition httpfetch.cpp:731
void httpfetch_async(const HTTPFetchRequest &fetch_request)
Definition httpfetch.cpp:745
#define HTTPFETCH_DISCARD
Definition httpfetch.h:12
std::unordered_map< std::string, std::string > StringMap
Definition string.h:66
Definition httpfetch.h:41
long connect_timeout
Definition httpfetch.h:56
bool multipart
Definition httpfetch.h:60
long timeout
Definition httpfetch.h:53
std::vector< std::string > extra_headers
Definition httpfetch.h:72
HttpMethod method
Definition httpfetch.h:63
u64 request_id
Definition httpfetch.h:50
std::string url
Definition httpfetch.h:42
std::string raw_data
Definition httpfetch.h:69
u64 caller
Definition httpfetch.h:46
StringMap fields
Definition httpfetch.h:66
std::string useragent
Definition httpfetch.h:75
HTTPFetchRequest()
Definition httpfetch.cpp:33
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