http_client.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_del(http_client_t* cli);
  28. HV_EXPORT const char* http_client_strerror(int errcode);
  29. // timeout: s
  30. HV_EXPORT int http_client_set_timeout(http_client_t* cli, int timeout);
  31. // SSL/TLS
  32. HV_EXPORT int http_client_set_ssl_ctx(http_client_t* cli, hssl_ctx_t ssl_ctx);
  33. // hssl_ctx_new(opt) -> http_client_set_ssl_ctx
  34. HV_EXPORT int http_client_new_ssl_ctx(http_client_t* cli, hssl_ctx_opt_t* opt);
  35. // common headers
  36. HV_EXPORT int http_client_clear_headers(http_client_t* cli);
  37. HV_EXPORT int http_client_set_header(http_client_t* cli, const char* key, const char* value);
  38. HV_EXPORT int http_client_del_header(http_client_t* cli, const char* key);
  39. HV_EXPORT const char* http_client_get_header(http_client_t* cli, const char* key);
  40. // http_proxy
  41. HV_EXPORT int http_client_set_http_proxy(http_client_t* cli, const char* host, int port);
  42. // https_proxy
  43. HV_EXPORT int http_client_set_https_proxy(http_client_t* cli, const char* host, int port);
  44. // no_proxy
  45. HV_EXPORT int http_client_add_no_proxy(http_client_t* cli, const char* host);
  46. // sync
  47. HV_EXPORT int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp);
  48. // async
  49. // Intern will start an EventLoopThread when http_client_send_async first called,
  50. // http_client_del will destroy the thread.
  51. HV_EXPORT int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  52. // top-level api
  53. // http_client_new -> http_client_send -> http_client_del
  54. HV_EXPORT int http_client_send(HttpRequest* req, HttpResponse* resp);
  55. // http_client_send_async(&default_async_client, ...)
  56. HV_EXPORT int http_client_send_async(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL);
  57. // low-level api
  58. // @retval >=0 connfd, <0 error
  59. HV_EXPORT int http_client_connect(http_client_t* cli, const char* host, int port, int https, int timeout);
  60. HV_EXPORT int http_client_send_header(http_client_t* cli, HttpRequest* req);
  61. HV_EXPORT int http_client_send_data(http_client_t* cli, const char* data, int size);
  62. HV_EXPORT int http_client_recv_data(http_client_t* cli, char* data, int size);
  63. HV_EXPORT int http_client_recv_response(http_client_t* cli, HttpResponse* resp);
  64. HV_EXPORT int http_client_close(http_client_t* cli);
  65. namespace hv {
  66. class HttpClient {
  67. public:
  68. HttpClient(const char* host = NULL, int port = DEFAULT_HTTP_PORT, int https = 0) {
  69. _client = http_client_new(host, port, https);
  70. }
  71. ~HttpClient() {
  72. if (_client) {
  73. http_client_del(_client);
  74. _client = NULL;
  75. }
  76. }
  77. // timeout: s
  78. int setTimeout(int timeout) {
  79. return http_client_set_timeout(_client, timeout);
  80. }
  81. // SSL/TLS
  82. int setSslCtx(hssl_ctx_t ssl_ctx) {
  83. return http_client_set_ssl_ctx(_client, ssl_ctx);
  84. }
  85. int newSslCtx(hssl_ctx_opt_t* opt) {
  86. return http_client_new_ssl_ctx(_client, opt);
  87. }
  88. // headers
  89. int clearHeaders() {
  90. return http_client_clear_headers(_client);
  91. }
  92. int setHeader(const char* key, const char* value) {
  93. return http_client_set_header(_client, key, value);
  94. }
  95. int delHeader(const char* key) {
  96. return http_client_del_header(_client, key);
  97. }
  98. const char* getHeader(const char* key) {
  99. return http_client_get_header(_client, key);
  100. }
  101. // http_proxy
  102. int setHttpProxy(const char* host, int port) {
  103. return http_client_set_http_proxy(_client, host, port);
  104. }
  105. // https_proxy
  106. int setHttpsProxy(const char* host, int port) {
  107. return http_client_set_https_proxy(_client, host, port);
  108. }
  109. // no_proxy
  110. int addNoProxy(const char* host) {
  111. return http_client_add_no_proxy(_client, host);
  112. }
  113. // sync
  114. int send(HttpRequest* req, HttpResponse* resp) {
  115. return http_client_send(_client, req, resp);
  116. }
  117. // async
  118. int sendAsync(HttpRequestPtr req, HttpResponseCallback resp_cb = NULL) {
  119. return http_client_send_async(_client, req, std::move(resp_cb));
  120. }
  121. // low-level api
  122. int connect(const char* host, int port = DEFAULT_HTTP_PORT, int https = 0, int timeout = DEFAULT_HTTP_CONNECT_TIMEOUT) {
  123. return http_client_connect(_client, host, port, https, timeout);
  124. }
  125. int sendHeader(HttpRequest* req) {
  126. return http_client_send_header(_client, req);
  127. }
  128. int sendData(const char* data, int size) {
  129. return http_client_send_data(_client, data, size);
  130. }
  131. int recvData(char* data, int size) {
  132. return http_client_recv_data(_client, data, size);
  133. }
  134. int recvResponse(HttpResponse* resp) {
  135. return http_client_recv_response(_client, resp);
  136. }
  137. int close() {
  138. return http_client_close(_client);
  139. }
  140. private:
  141. http_client_t* _client;
  142. };
  143. }
  144. #endif // HV_HTTP_CLIENT_H_