http_client.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef HV_HTTP_CLIENT_H_
  2. #define HV_HTTP_CLIENT_H_
  3. #include "hexport.h"
  4. #include "hssl.h"
  5. #include "HttpMessage.h"
  6. /*
  7. #include <stdio.h>
  8. #include "http_client.h"
  9. int main(int argc, char* argv[]) {
  10. HttpRequest req;
  11. req.method = HTTP_GET;
  12. req.url = "http://www.example.com";
  13. HttpResponse res;
  14. int ret = http_client_send(&req, &res);
  15. printf("%s\n", req.Dump(true,true).c_str());
  16. if (ret != 0) {
  17. printf("* Failed:%s:%d\n", http_client_strerror(ret), ret);
  18. }
  19. else {
  20. printf("%s\n", res.Dump(true,true).c_str());
  21. }
  22. return ret;
  23. }
  24. */
  25. #define DEFAULT_HTTP_TIMEOUT 60 // s
  26. typedef struct http_client_s http_client_t;
  27. HV_EXPORT http_client_t* http_client_new(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0);
  28. HV_EXPORT int http_client_close(http_client_t* cli);
  29. HV_EXPORT int http_client_del(http_client_t* cli);
  30. HV_EXPORT const char* http_client_strerror(int errcode);
  31. HV_EXPORT int http_client_set_timeout(http_client_t* cli, int timeout);
  32. // SSL/TLS
  33. HV_EXPORT int http_client_set_ssl_ctx(http_client_t* cli, hssl_ctx_t ssl_ctx);
  34. // hssl_ctx_new(opt) -> http_client_set_ssl_ctx
  35. HV_EXPORT int http_client_new_ssl_ctx(http_client_t* cli, hssl_ctx_opt_t* opt);
  36. // common headers
  37. HV_EXPORT int http_client_clear_headers(http_client_t* cli);
  38. HV_EXPORT int http_client_set_header(http_client_t* cli, const char* key, const char* value);
  39. HV_EXPORT int http_client_del_header(http_client_t* cli, const char* key);
  40. HV_EXPORT const char* http_client_get_header(http_client_t* cli, const char* key);
  41. // http_proxy
  42. HV_EXPORT int http_client_set_http_proxy(http_client_t* cli, const char* host, int port);
  43. // https_proxy
  44. HV_EXPORT int http_client_set_https_proxy(http_client_t* cli, const char* host, int port);
  45. // no_proxy
  46. HV_EXPORT int http_client_add_no_proxy(http_client_t* cli, const char* host);
  47. // sync
  48. HV_EXPORT int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp);
  49. // async
  50. // Intern will start an EventLoopThread when http_client_send_async first called,
  51. // http_client_del will destroy the thread.
  52. HV_EXPORT int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  53. // top-level api
  54. // http_client_new -> http_client_send -> http_client_del
  55. HV_EXPORT int http_client_send(HttpRequest* req, HttpResponse* resp);
  56. // http_client_send_async(&default_async_client, ...)
  57. HV_EXPORT int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  58. namespace hv {
  59. class HttpClient {
  60. public:
  61. HttpClient(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0) {
  62. _client = http_client_new(host, port, https);
  63. }
  64. ~HttpClient() {
  65. if (_client) {
  66. http_client_del(_client);
  67. _client = NULL;
  68. }
  69. }
  70. // timeout: s
  71. int setTimeout(int timeout) {
  72. return http_client_set_timeout(_client, timeout);
  73. }
  74. // headers
  75. int clearHeaders() {
  76. return http_client_clear_headers(_client);
  77. }
  78. int setHeader(const char* key, const char* value) {
  79. return http_client_set_header(_client, key, value);
  80. }
  81. int delHeader(const char* key) {
  82. return http_client_del_header(_client, key);
  83. }
  84. const char* getHeader(const char* key) {
  85. return http_client_get_header(_client, key);
  86. }
  87. // http_proxy
  88. int setHttpProxy(const char* host, int port) {
  89. return http_client_set_http_proxy(_client, host, port);
  90. }
  91. // https_proxy
  92. int setHttpsProxy(const char* host, int port) {
  93. return http_client_set_https_proxy(_client, host, port);
  94. }
  95. // no_proxy
  96. int addNoProxy(const char* host) {
  97. return http_client_add_no_proxy(_client, host);
  98. }
  99. // sync
  100. int send(HttpRequest* req, HttpResponse* resp) {
  101. return http_client_send(_client, req, resp);
  102. }
  103. // async
  104. int sendAsync(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL) {
  105. return http_client_send_async(_client, req, resp_cb);
  106. }
  107. // close
  108. int close() {
  109. return http_client_close(_client);
  110. }
  111. private:
  112. http_client_t* _client;
  113. };
  114. }
  115. #endif // HV_HTTP_CLIENT_H_