requests.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef HV_REQUESTS_H_
  2. #define HV_REQUESTS_H_
  3. /*
  4. * Inspired by python requests
  5. *
  6. * @code
  7. #include "requests.h"
  8. int main() {
  9. auto resp = requests::get("http://127.0.0.1:8080/ping");
  10. if (resp == NULL) {
  11. printf("request failed!\n");
  12. } else {
  13. printf("%d %s\r\n", resp->status_code, resp->status_message());
  14. printf("%s\n", resp->body.c_str());
  15. }
  16. resp = requests::post("http://127.0.0.1:8080/echo", "hello,world!");
  17. if (resp == NULL) {
  18. printf("request failed!\n");
  19. } else {
  20. printf("%d %s\r\n", resp->status_code, resp->status_message());
  21. printf("%s\n", resp->body.c_str());
  22. }
  23. return 0;
  24. }
  25. **/
  26. #include <memory>
  27. #include "http_client.h"
  28. namespace requests {
  29. typedef HttpRequestPtr Request;
  30. typedef HttpResponsePtr Response;
  31. typedef HttpResponseCallback ResponseCallback;
  32. HV_INLINE Response request(Request req) {
  33. Response resp(new HttpResponse);
  34. int ret = http_client_send(req.get(), resp.get());
  35. return ret ? NULL : resp;
  36. }
  37. HV_INLINE Response request(http_method method, const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  38. Request req(new HttpRequest);
  39. req->method = method;
  40. req->url = url;
  41. if (&body != &NoBody) {
  42. req->body = body;
  43. }
  44. if (&headers != &DefaultHeaders) {
  45. req->headers = headers;
  46. }
  47. return request(req);
  48. }
  49. HV_INLINE Response uploadFile(const char* url, const char* filepath, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders) {
  50. Request req(new HttpRequest);
  51. req->method = method;
  52. req->url = url;
  53. if (req->File(filepath) != 200) return NULL;
  54. if (&headers != &DefaultHeaders) {
  55. req->headers = headers;
  56. }
  57. return request(req);
  58. }
  59. #ifndef WITHOUT_HTTP_CONTENT
  60. HV_INLINE Response uploadFormFile(const char* url, const char* name, const char* filepath, std::map<std::string, std::string>& params = hv::empty_map, http_method method = HTTP_POST, const http_headers& headers = DefaultHeaders) {
  61. Request req(new HttpRequest);
  62. req->method = method;
  63. req->url = url;
  64. req->timeout = 600; // 10min
  65. req->content_type = MULTIPART_FORM_DATA;
  66. req->SetFormFile(name, filepath);
  67. for (auto& param : params) {
  68. req->SetFormData(param.first.c_str(), param.second);
  69. }
  70. if (&headers != &DefaultHeaders) {
  71. req->headers = headers;
  72. }
  73. return request(req);
  74. }
  75. #endif
  76. // see examples/wget.cpp
  77. typedef std::function<void(size_t received_bytes, size_t total_bytes)> download_progress_cb;
  78. HV_INLINE size_t downloadFile(const char* url, const char* filepath, download_progress_cb progress_cb = NULL) {
  79. // open file
  80. std::string filepath_download(filepath);
  81. filepath_download += ".download";
  82. HFile file;
  83. int ret = file.open(filepath_download.c_str(), "wb");
  84. if (ret != 0) {
  85. return 0;
  86. }
  87. // download
  88. Request req(new HttpRequest);
  89. req->method = HTTP_GET;
  90. req->url = url;
  91. req->timeout = 3600; // 1h
  92. size_t content_length = 0;
  93. size_t received_bytes = 0;
  94. req->http_cb = [&file, &content_length, &received_bytes, &progress_cb]
  95. (HttpMessage* resp, http_parser_state state, const char* data, size_t size) {
  96. if (state == HP_HEADERS_COMPLETE) {
  97. content_length = hv::from_string<size_t>(resp->GetHeader("Content-Length"));
  98. } else if (state == HP_BODY) {
  99. if (data && size) {
  100. // write file
  101. file.write(data, size);
  102. received_bytes += size;
  103. if (progress_cb) {
  104. progress_cb(received_bytes, content_length);
  105. }
  106. }
  107. }
  108. };
  109. auto resp = request(req);
  110. file.close();
  111. if (resp == NULL || resp->status_code != 200) {
  112. return 0;
  113. }
  114. // check filesize
  115. if (content_length != 0) {
  116. if (hv_filesize(filepath_download.c_str()) == content_length) {
  117. rename(filepath_download.c_str(), filepath);
  118. } else {
  119. remove(filepath_download.c_str());
  120. return 0;
  121. }
  122. }
  123. return hv_filesize(filepath);
  124. }
  125. HV_INLINE Response head(const char* url, const http_headers& headers = DefaultHeaders) {
  126. return request(HTTP_HEAD, url, NoBody, headers);
  127. }
  128. HV_INLINE Response get(const char* url, const http_headers& headers = DefaultHeaders) {
  129. return request(HTTP_GET, url, NoBody, headers);
  130. }
  131. HV_INLINE Response post(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  132. return request(HTTP_POST, url, body, headers);
  133. }
  134. HV_INLINE Response put(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  135. return request(HTTP_PUT, url, body, headers);
  136. }
  137. HV_INLINE Response patch(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  138. return request(HTTP_PATCH, url, body, headers);
  139. }
  140. // delete is c++ keyword, we have to replace delete with Delete.
  141. HV_INLINE Response Delete(const char* url, const http_headers& headers = DefaultHeaders) {
  142. return request(HTTP_DELETE, url, NoBody, headers);
  143. }
  144. HV_INLINE int async(Request req, ResponseCallback resp_cb) {
  145. return http_client_send_async(req, std::move(resp_cb));
  146. }
  147. }
  148. #endif // HV_REQUESTS_H_