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.

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