http_client.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef HV_HTTP_CLIENT_H_
  2. #define HV_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 60 // 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_close(http_client_t* cli);
  28. HV_EXPORT int http_client_del(http_client_t* cli);
  29. HV_EXPORT const char* http_client_strerror(int errcode);
  30. HV_EXPORT int http_client_set_timeout(http_client_t* cli, int timeout);
  31. // common headers
  32. HV_EXPORT int http_client_clear_headers(http_client_t* cli);
  33. HV_EXPORT int http_client_set_header(http_client_t* cli, const char* key, const char* value);
  34. HV_EXPORT int http_client_del_header(http_client_t* cli, const char* key);
  35. HV_EXPORT const char* http_client_get_header(http_client_t* cli, const char* key);
  36. // http_proxy
  37. HV_EXPORT int http_client_set_http_proxy(http_client_t* cli, const char* host, int port);
  38. // https_proxy
  39. HV_EXPORT int http_client_set_https_proxy(http_client_t* cli, const char* host, int port);
  40. // no_proxy
  41. HV_EXPORT int http_client_add_no_proxy(http_client_t* cli, const char* host);
  42. // sync
  43. HV_EXPORT int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp);
  44. // async
  45. // Intern will start an EventLoopThread when http_client_send_async first called,
  46. // http_client_del will destroy the thread.
  47. HV_EXPORT int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  48. // top-level api
  49. // http_client_new -> http_client_send -> http_client_del
  50. HV_EXPORT int http_client_send(HttpRequest* req, HttpResponse* resp);
  51. // http_client_send_async(&default_async_client, ...)
  52. HV_EXPORT int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  53. namespace hv {
  54. class HttpClient {
  55. public:
  56. HttpClient(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0) {
  57. _client = http_client_new(host, port, https);
  58. }
  59. ~HttpClient() {
  60. if (_client) {
  61. http_client_del(_client);
  62. _client = NULL;
  63. }
  64. }
  65. // timeout: s
  66. int setTimeout(int timeout) {
  67. return http_client_set_timeout(_client, timeout);
  68. }
  69. // headers
  70. int clearHeaders() {
  71. return http_client_clear_headers(_client);
  72. }
  73. int setHeader(const char* key, const char* value) {
  74. return http_client_set_header(_client, key, value);
  75. }
  76. int delHeader(const char* key) {
  77. return http_client_del_header(_client, key);
  78. }
  79. const char* getHeader(const char* key) {
  80. return http_client_get_header(_client, key);
  81. }
  82. // http_proxy
  83. int setHttpProxy(const char* host, int port) {
  84. return http_client_set_http_proxy(_client, host, port);
  85. }
  86. // https_proxy
  87. int setHttpsProxy(const char* host, int port) {
  88. return http_client_set_https_proxy(_client, host, port);
  89. }
  90. // no_proxy
  91. int addNoProxy(const char* host) {
  92. return http_client_add_no_proxy(_client, host);
  93. }
  94. // sync
  95. int send(HttpRequest* req, HttpResponse* resp) {
  96. return http_client_send(_client, req, resp);
  97. }
  98. // async
  99. int sendAsync(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL) {
  100. return http_client_send_async(_client, req, resp_cb);
  101. }
  102. // close
  103. int close() {
  104. return http_client_close(_client);
  105. }
  106. private:
  107. http_client_t* _client;
  108. };
  109. }
  110. #endif // HV_HTTP_CLIENT_H_