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.

network.hpp 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. PRIVATE void init();
  16. PRIVATE void destroy();
  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