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 // Can be used in place of "caller" in asynchronous transfers to discard result
27 // (used as default value of "caller")
28 #define HTTPFETCH_DISCARD 0
29 #define HTTPFETCH_SYNC 1
31 struct HTTPFetchRequest
35 // Identifies the caller (for asynchronous requests)
36 // Ignored by httpfetch_sync
37 unsigned long caller = HTTPFETCH_DISCARD;
39 // Some number that identifies the request
40 // (when the same caller issues multiple httpfetch_async calls)
41 unsigned long request_id = 0;
43 // Timeout for the whole transfer, in milliseconds
46 // Timeout for the connection phase, in milliseconds
49 // Indicates if this is multipart/form-data or
50 // application/x-www-form-urlencoded. POST-only.
51 bool multipart = false;
53 // POST fields. Fields are escaped properly.
54 // If this is empty a GET request is done instead.
55 StringMap post_fields;
57 // Raw POST data, overrides post_fields.
58 std::string post_data;
60 // If not empty, should contain entries such as "Accept: text/html"
61 std::vector<std::string> extra_headers;
64 std::string useragent;
69 struct HTTPFetchResult
71 bool succeeded = false;
73 long response_code = 0;
74 std::string data = "";
75 // The caller and request_id from the corresponding HTTPFetchRequest.
76 unsigned long caller = HTTPFETCH_DISCARD;
77 unsigned long request_id = 0;
79 HTTPFetchResult() = default;
81 HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
82 caller(fetch_request.caller), request_id(fetch_request.request_id)
87 // Initializes the httpfetch module
88 void httpfetch_init(int parallel_limit);
90 // Stops the httpfetch thread and cleans up resources
91 void httpfetch_cleanup();
93 // Starts an asynchronous HTTP fetch request
94 void httpfetch_async(const HTTPFetchRequest &fetch_request);
96 // If any fetch for the given caller ID is complete, removes it from the
97 // result queue, sets the fetch result and returns true. Otherwise returns false.
98 bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result);
100 // Allocates a caller ID for httpfetch_async
101 // Not required if you want to set caller = HTTPFETCH_DISCARD
102 unsigned long httpfetch_caller_alloc();
104 // Allocates a non-predictable caller ID for httpfetch_async
105 unsigned long httpfetch_caller_alloc_secure();
107 // Frees a caller ID allocated with httpfetch_caller_alloc
108 // Note: This can be expensive, because the httpfetch thread is told
109 // to stop any ongoing fetches for the given caller.
110 void httpfetch_caller_free(unsigned long caller);
112 // Performs a synchronous HTTP request. This blocks and therefore should
113 // only be used from background threads.
114 void httpfetch_sync(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result);