http_client.h 3.7 KB

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