http_client.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. 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. // timeout: s
  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. // SSL/TLS
  75. int setSslCtx(hssl_ctx_t ssl_ctx) {
  76. return http_client_set_ssl_ctx(_client, ssl_ctx);
  77. }
  78. int newSslCtx(hssl_ctx_opt_t* opt) {
  79. return http_client_new_ssl_ctx(_client, opt);
  80. }
  81. // headers
  82. int clearHeaders() {
  83. return http_client_clear_headers(_client);
  84. }
  85. int setHeader(const char* key, const char* value) {
  86. return http_client_set_header(_client, key, value);
  87. }
  88. int delHeader(const char* key) {
  89. return http_client_del_header(_client, key);
  90. }
  91. const char* getHeader(const char* key) {
  92. return http_client_get_header(_client, key);
  93. }
  94. // http_proxy
  95. int setHttpProxy(const char* host, int port) {
  96. return http_client_set_http_proxy(_client, host, port);
  97. }
  98. // https_proxy
  99. int setHttpsProxy(const char* host, int port) {
  100. return http_client_set_https_proxy(_client, host, port);
  101. }
  102. // no_proxy
  103. int addNoProxy(const char* host) {
  104. return http_client_add_no_proxy(_client, host);
  105. }
  106. // sync
  107. int send(HttpRequest* req, HttpResponse* resp) {
  108. return http_client_send(_client, req, resp);
  109. }
  110. // async
  111. int sendAsync(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL) {
  112. return http_client_send_async(_client, req, resp_cb);
  113. }
  114. // close
  115. int close() {
  116. return http_client_close(_client);
  117. }
  118. private:
  119. http_client_t* _client;
  120. };
  121. }
  122. #endif // HV_HTTP_CLIENT_H_