http_client.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef HTTP_CLIENT_H_
  2. #define HTTP_CLIENT_H_
  3. #include "hexport.h"
  4. #include "HttpMessage.h"
  5. /*
  6. #include <stdio.h>
  7. #include "http_client.h"
  8. int main(int argc, char* argv[]) {
  9. HttpRequest req;
  10. req.method = HTTP_GET;
  11. req.url = "http://www.example.com";
  12. HttpResponse res;
  13. int ret = http_client_send(&req, &res);
  14. printf("%s\n", req.Dump(true,true).c_str());
  15. if (ret != 0) {
  16. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  17. }
  18. else {
  19. printf("%s\n", res.Dump(true,true).c_str());
  20. }
  21. return ret;
  22. }
  23. */
  24. #define DEFAULT_HTTP_TIMEOUT 10 // s
  25. typedef struct http_client_s http_client_t;
  26. HV_EXPORT http_client_t* http_client_new(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0);
  27. HV_EXPORT int http_client_del(http_client_t* cli);
  28. HV_EXPORT const char* http_client_strerror(int errcode);
  29. HV_EXPORT int http_client_set_timeout(http_client_t* cli, int timeout);
  30. // common headers
  31. HV_EXPORT int http_client_clear_headers(http_client_t* cli);
  32. HV_EXPORT int http_client_set_header(http_client_t* cli, const char* key, const char* value);
  33. HV_EXPORT int http_client_del_header(http_client_t* cli, const char* key);
  34. HV_EXPORT const char* http_client_get_header(http_client_t* cli, const char* key);
  35. // sync
  36. HV_EXPORT int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp);
  37. // async
  38. // Intern will start an event-loop thread when http_client_send_async first called,
  39. // http_client_del will destroy the thread.
  40. HV_EXPORT int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponsePtr resp,
  41. HttpResponseCallback cb = NULL, void* userdata = NULL);
  42. // top-level api
  43. // http_client_new -> http_client_send -> http_client_del
  44. HV_EXPORT int http_client_send(HttpRequest* req, HttpResponse* resp);
  45. // http_client_send_async(&default_async_client, ...)
  46. HV_EXPORT int http_client_send_async(HttpRequestPtr req, HttpResponsePtr resp,
  47. HttpResponseCallback cb = NULL, void* userdata = NULL);
  48. #endif // HTTP_CLIENT_H_