httpfetch.h 3.5 KB

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