httpfetch.h 4.0 KB

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