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