You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.1KB

  1. #pragma once
  2. #include <map>
  3. #include <jansson.h>
  4. #include <common.hpp>
  5. namespace rack {
  6. /** Networking functions for HTTP requests, downloads, and URLs */
  7. namespace network {
  8. typedef std::map<std::string, std::string> CookieMap;
  9. enum Method {
  10. METHOD_GET,
  11. METHOD_POST,
  12. METHOD_PUT,
  13. METHOD_DELETE,
  14. };
  15. void init();
  16. /** Requests a JSON API URL over HTTP(S), using the data as the query (GET) or the body (POST, etc)
  17. Caller must json_decref() if return value is non-NULL.
  18. */
  19. json_t* requestJson(Method method, const std::string& url, json_t* dataJ = NULL, const CookieMap& cookies = {});
  20. /** Returns true if downloaded successfully.
  21. If `progress` is non-NULL, the value is updated from 0 to 1 while downloading.
  22. */
  23. bool requestDownload(const std::string& url, const std::string& filename, float* progress, const CookieMap& cookies = {});
  24. /** URL-encodes a string. */
  25. std::string encodeUrl(const std::string& s);
  26. /** Returns the path portion of the URL.
  27. Example:
  28. urlPath("https://example.com/foo/index.html") // Returns "/foo/index.html"
  29. */
  30. std::string urlPath(const std::string& url);
  31. } // namespace network
  32. } // namespace rack