3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "util/string.h"
26 // These can be used in place of "caller" in to specify special handling.
27 // Discard result (used as default value of "caller").
28 #define HTTPFETCH_DISCARD 0
29 // Indicates that the result should not be discarded when performing a
30 // synchronous request (since a real caller ID is not needed for synchronous
31 // requests because the result does not have to be retrieved later).
32 #define HTTPFETCH_SYNC 1
33 // Print response body to console if the server returns an error code.
34 #define HTTPFETCH_PRINT_ERR 2
35 // Start of regular allocated caller IDs.
36 #define HTTPFETCH_CID_START 3
47 struct HTTPFetchRequest
51 // Identifies the caller (for asynchronous requests)
52 // Ignored by httpfetch_sync
53 u64 caller = HTTPFETCH_DISCARD;
55 // Some number that identifies the request
56 // (when the same caller issues multiple httpfetch_async calls)
59 // Timeout for the whole transfer, in milliseconds
62 // Timeout for the connection phase, in milliseconds
65 // Indicates if this is multipart/form-data or
66 // application/x-www-form-urlencoded. POST-only.
67 bool multipart = false;
69 // The Method to use default = GET
70 // Avaible methods GET, POST, PUT, DELETE
71 HttpMethod method = HTTP_GET;
73 // Fields of the request
76 // Raw data of the request overrides fields
79 // If not empty, should contain entries such as "Accept: text/html"
80 std::vector<std::string> extra_headers;
83 std::string useragent;
88 struct HTTPFetchResult
90 bool succeeded = false;
92 long response_code = 0;
93 std::string data = "";
94 // The caller and request_id from the corresponding HTTPFetchRequest.
95 u64 caller = HTTPFETCH_DISCARD;
98 HTTPFetchResult() = default;
100 HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
101 caller(fetch_request.caller), request_id(fetch_request.request_id)
106 // Initializes the httpfetch module
107 void httpfetch_init(int parallel_limit);
109 // Stops the httpfetch thread and cleans up resources
110 void httpfetch_cleanup();
112 // Starts an asynchronous HTTP fetch request
113 void httpfetch_async(const HTTPFetchRequest &fetch_request);
115 // If any fetch for the given caller ID is complete, removes it from the
116 // result queue, sets the fetch result and returns true. Otherwise returns false.
117 bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
119 // Allocates a caller ID for httpfetch_async
120 // Not required if you want to set caller = HTTPFETCH_DISCARD
121 u64 httpfetch_caller_alloc();
123 // Allocates a non-predictable caller ID for httpfetch_async
124 u64 httpfetch_caller_alloc_secure();
126 // Frees a caller ID allocated with httpfetch_caller_alloc
127 // Note: This can be expensive, because the httpfetch thread is told
128 // to stop any ongoing fetches for the given caller.
129 void httpfetch_caller_free(u64 caller);
131 // Performs a synchronous HTTP request. This blocks and therefore should
132 // only be used from background threads.
133 void httpfetch_sync(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result);