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