Implement httpfetch module and initialize it from main()
[oweals/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
31 struct HTTPFetchRequest
32 {
33         std::string url;
34
35         // Identifies the caller (for asynchronous requests)
36         // Ignored by httpfetch_sync
37         unsigned long caller;
38
39         // Some number that identifies the request
40         // (when the same caller issues multiple httpfetch_async calls)
41         unsigned long request_id;
42
43         // Timeout for the whole transfer, in milliseconds
44         long timeout;
45
46         // Timeout for the connection phase, in milliseconds
47         long connect_timeout;
48
49         // POST data (should be application/x-www-form-urlencoded
50         // unless a Content-Type header is specified in extra_headers)
51         // If this is empty a GET request is done instead.
52         std::string post_fields;
53
54         // If not empty, should contain entries such as "Accept: text/html"
55         std::vector<std::string> extra_headers;
56
57         HTTPFetchRequest()
58         {
59                 url = "";
60                 caller = HTTPFETCH_DISCARD;
61                 request_id = 0;
62                 timeout = 0;
63                 connect_timeout = 0;
64         }
65 };
66
67 struct HTTPFetchResult
68 {
69         bool succeeded;
70         bool timeout;
71         long response_code;
72         std::string data;
73         // The caller and request_id from the corresponding HTTPFetchRequest.
74         unsigned long caller;
75         unsigned long request_id;
76
77         HTTPFetchResult()
78         {
79                 succeeded = false;
80                 timeout = false;
81                 response_code = 0;
82                 data = "";
83                 caller = HTTPFETCH_DISCARD;
84                 request_id = 0;
85         }
86
87         HTTPFetchResult(const HTTPFetchRequest &fetchrequest)
88         {
89                 succeeded = false;
90                 timeout = false;
91                 response_code = 0;
92                 data = "";
93                 caller = fetchrequest.caller;
94                 request_id = fetchrequest.request_id;
95         }
96 };
97
98 // Initializes the httpfetch module
99 void httpfetch_init(int parallel_limit);
100
101 // Stops the httpfetch thread and cleans up resources
102 void httpfetch_cleanup();
103
104 // Starts an asynchronous HTTP fetch request
105 void httpfetch_async(const HTTPFetchRequest &fetchrequest);
106
107 // If any fetch for the given caller ID is complete, removes it from the
108 // result queue, sets fetchresult and returns true. Otherwise returns false.
109 bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetchresult);
110
111 // Allocates a caller ID for httpfetch_async
112 // Not required if you want to set caller = HTTPFETCH_DISCARD
113 unsigned long httpfetch_caller_alloc();
114
115 // Frees a caller ID allocated with httpfetch_caller_alloc
116 // Note: This can be expensive, because the httpfetch thread is told
117 // to stop any ongoing fetches for the given caller.
118 void httpfetch_caller_free(unsigned long caller);
119
120 // Performs a synchronous HTTP request. This blocks and therefore should
121 // only be used from background threads.
122 void httpfetch_sync(const HTTPFetchRequest &fetchrequest,
123                 HTTPFetchResult &fetchresult);
124
125
126 #endif // !HTTPFETCH_HEADER