]> git.lizzy.rs Git - minetest.git/blob - src/httpfetch.h
Fix red background missing in deathscreen
[minetest.git] / src / httpfetch.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef HTTPFETCH_HEADER
21 #define HTTPFETCH_HEADER
22
23 #include <string>
24 #include <vector>
25 #include "config.h"
26
27 // Can be used in place of "caller" in asynchronous transfers to discard result
28 // (used as default value of "caller")
29 #define HTTPFETCH_DISCARD 0
30 #define HTTPFETCH_SYNC 1
31
32 struct HTTPFetchRequest
33 {
34         std::string url;
35
36         // Identifies the caller (for asynchronous requests)
37         // Ignored by httpfetch_sync
38         unsigned long caller;
39
40         // Some number that identifies the request
41         // (when the same caller issues multiple httpfetch_async calls)
42         unsigned long request_id;
43
44         // Timeout for the whole transfer, in milliseconds
45         long timeout;
46
47         // Timeout for the connection phase, in milliseconds
48         long connect_timeout;
49
50         // POST data (should be application/x-www-form-urlencoded
51         // unless a Content-Type header is specified in extra_headers)
52         // If this is empty a GET request is done instead.
53         std::string post_fields;
54
55         // If not empty, should contain entries such as "Accept: text/html"
56         std::vector<std::string> extra_headers;
57
58         //useragent to use
59         std::string useragent;
60
61         HTTPFetchRequest();
62 };
63
64 struct HTTPFetchResult
65 {
66         bool succeeded;
67         bool timeout;
68         long response_code;
69         std::string data;
70         // The caller and request_id from the corresponding HTTPFetchRequest.
71         unsigned long caller;
72         unsigned long request_id;
73
74         HTTPFetchResult()
75         {
76                 succeeded = false;
77                 timeout = false;
78                 response_code = 0;
79                 data = "";
80                 caller = HTTPFETCH_DISCARD;
81                 request_id = 0;
82         }
83
84         HTTPFetchResult(const HTTPFetchRequest &fetchrequest)
85         {
86                 succeeded = false;
87                 timeout = false;
88                 response_code = 0;
89                 data = "";
90                 caller = fetchrequest.caller;
91                 request_id = fetchrequest.request_id;
92         }
93
94 };
95
96 // Initializes the httpfetch module
97 void httpfetch_init(int parallel_limit);
98
99 // Stops the httpfetch thread and cleans up resources
100 void httpfetch_cleanup();
101
102 // Starts an asynchronous HTTP fetch request
103 void httpfetch_async(const HTTPFetchRequest &fetchrequest);
104
105 // If any fetch for the given caller ID is complete, removes it from the
106 // result queue, sets fetchresult and returns true. Otherwise returns false.
107 bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetchresult);
108
109 // Allocates a caller ID for httpfetch_async
110 // Not required if you want to set caller = HTTPFETCH_DISCARD
111 unsigned long httpfetch_caller_alloc();
112
113 // Frees a caller ID allocated with httpfetch_caller_alloc
114 // Note: This can be expensive, because the httpfetch thread is told
115 // to stop any ongoing fetches for the given caller.
116 void httpfetch_caller_free(unsigned long caller);
117
118 // Performs a synchronous HTTP request. This blocks and therefore should
119 // only be used from background threads.
120 void httpfetch_sync(const HTTPFetchRequest &fetchrequest,
121                 HTTPFetchResult &fetchresult);
122
123
124 #endif // !HTTPFETCH_HEADER