httpfetch.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <vector>
  18. #include "util/string.h"
  19. #include "config.h"
  20. // These can be used in place of "caller" in to specify special handling.
  21. // Discard result (used as default value of "caller").
  22. #define HTTPFETCH_DISCARD 0
  23. // Indicates that the result should not be discarded when performing a
  24. // synchronous request (since a real caller ID is not needed for synchronous
  25. // requests because the result does not have to be retrieved later).
  26. #define HTTPFETCH_SYNC 1
  27. // Print response body to console if the server returns an error code.
  28. #define HTTPFETCH_PRINT_ERR 2
  29. // Start of regular allocated caller IDs.
  30. #define HTTPFETCH_CID_START 3
  31. namespace {
  32. // lower bound for curl_timeout (see also settingtypes.txt)
  33. constexpr long MIN_HTTPFETCH_TIMEOUT_INTERACTIVE = 1000;
  34. // lower bound for curl_file_download_timeout
  35. constexpr long MIN_HTTPFETCH_TIMEOUT = 5000;
  36. }
  37. // Methods
  38. enum HttpMethod : u8
  39. {
  40. HTTP_GET,
  41. HTTP_POST,
  42. HTTP_PUT,
  43. HTTP_DELETE,
  44. };
  45. struct HTTPFetchRequest
  46. {
  47. std::string url = "";
  48. // Identifies the caller (for asynchronous requests)
  49. // Ignored by httpfetch_sync
  50. u64 caller = HTTPFETCH_DISCARD;
  51. // Some number that identifies the request
  52. // (when the same caller issues multiple httpfetch_async calls)
  53. u64 request_id = 0;
  54. // Timeout for the whole transfer, in milliseconds
  55. long timeout;
  56. // Timeout for the connection phase, in milliseconds
  57. long connect_timeout;
  58. // Indicates if this is multipart/form-data or
  59. // application/x-www-form-urlencoded. POST-only.
  60. bool multipart = false;
  61. // The Method to use default = GET
  62. // Avaible methods GET, POST, PUT, DELETE
  63. HttpMethod method = HTTP_GET;
  64. // Fields of the request
  65. StringMap fields;
  66. // Raw data of the request overrides fields
  67. std::string raw_data;
  68. // If not empty, should contain entries such as "Accept: text/html"
  69. std::vector<std::string> extra_headers;
  70. // useragent to use
  71. std::string useragent;
  72. HTTPFetchRequest();
  73. };
  74. struct HTTPFetchResult
  75. {
  76. bool succeeded = false;
  77. bool timeout = false;
  78. long response_code = 0;
  79. std::string data = "";
  80. // The caller and request_id from the corresponding HTTPFetchRequest.
  81. u64 caller = HTTPFETCH_DISCARD;
  82. u64 request_id = 0;
  83. HTTPFetchResult() = default;
  84. HTTPFetchResult(const HTTPFetchRequest &fetch_request) :
  85. caller(fetch_request.caller), request_id(fetch_request.request_id)
  86. {
  87. }
  88. };
  89. // Initializes the httpfetch module
  90. void httpfetch_init(int parallel_limit);
  91. // Stops the httpfetch thread and cleans up resources
  92. void httpfetch_cleanup();
  93. // Starts an asynchronous HTTP fetch request
  94. void httpfetch_async(const HTTPFetchRequest &fetch_request);
  95. // If any fetch for the given caller ID is complete, removes it from the
  96. // result queue, sets the fetch result and returns true. Otherwise returns false.
  97. bool httpfetch_async_get(u64 caller, HTTPFetchResult &fetch_result);
  98. // Allocates a caller ID for httpfetch_async
  99. // Not required if you want to set caller = HTTPFETCH_DISCARD
  100. u64 httpfetch_caller_alloc();
  101. // Allocates a non-predictable caller ID for httpfetch_async
  102. u64 httpfetch_caller_alloc_secure();
  103. // Frees a caller ID allocated with httpfetch_caller_alloc
  104. // Note: This can be expensive, because the httpfetch thread is told
  105. // to stop any ongoing fetches for the given caller.
  106. void httpfetch_caller_free(u64 caller);
  107. // Performs a synchronous HTTP request. This blocks and therefore should
  108. // only be used from background threads.
  109. void httpfetch_sync(const HTTPFetchRequest &fetch_request, HTTPFetchResult &fetch_result);