httpfetch.h 3.7 KB

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