requests.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. req->timeout = 600; // 10min
  54. if (req->File(filepath) != 200) return NULL;
  55. if (&headers != &DefaultHeaders) {
  56. req->headers = headers;
  57. }
  58. return request(req);
  59. }
  60. #ifndef WITHOUT_HTTP_CONTENT
  61. 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) {
  62. Request req(new HttpRequest);
  63. req->method = method;
  64. req->url = url;
  65. req->timeout = 600; // 10min
  66. req->content_type = MULTIPART_FORM_DATA;
  67. req->SetFormFile(name, filepath);
  68. for (auto& param : params) {
  69. req->SetFormData(param.first.c_str(), param.second);
  70. }
  71. if (&headers != &DefaultHeaders) {
  72. req->headers = headers;
  73. }
  74. return request(req);
  75. }
  76. #endif
  77. // see examples/wget.cpp
  78. typedef std::function<void(size_t received_bytes, size_t total_bytes)> download_progress_cb;
  79. HV_INLINE size_t downloadFile(const char* url, const char* filepath, download_progress_cb progress_cb = NULL) {
  80. // open file
  81. std::string filepath_download(filepath);
  82. filepath_download += ".download";
  83. HFile file;
  84. int ret = file.open(filepath_download.c_str(), "wb");
  85. if (ret != 0) {
  86. return 0;
  87. }
  88. // download
  89. Request req(new HttpRequest);
  90. req->method = HTTP_GET;
  91. req->url = url;
  92. req->timeout = 3600; // 1h
  93. size_t content_length = 0;
  94. size_t received_bytes = 0;
  95. req->http_cb = [&file, &content_length, &received_bytes, &progress_cb]
  96. (HttpMessage* resp, http_parser_state state, const char* data, size_t size) {
  97. if (!resp->headers["Location"].empty()) return;
  98. if (state == HP_HEADERS_COMPLETE) {
  99. content_length = hv::from_string<size_t>(resp->GetHeader("Content-Length"));
  100. } else if (state == HP_BODY) {
  101. if (data && size) {
  102. // write file
  103. file.write(data, size);
  104. received_bytes += size;
  105. if (progress_cb) {
  106. progress_cb(received_bytes, content_length);
  107. }
  108. }
  109. }
  110. };
  111. auto resp = request(req);
  112. file.close();
  113. if (resp == NULL || resp->status_code != 200) {
  114. return 0;
  115. }
  116. // check filesize
  117. if (content_length != 0) {
  118. if (hv_filesize(filepath_download.c_str()) == content_length) {
  119. rename(filepath_download.c_str(), filepath);
  120. } else {
  121. remove(filepath_download.c_str());
  122. return 0;
  123. }
  124. }
  125. return hv_filesize(filepath);
  126. }
  127. HV_INLINE Response head(const char* url, const http_headers& headers = DefaultHeaders) {
  128. return request(HTTP_HEAD, url, NoBody, headers);
  129. }
  130. HV_INLINE Response get(const char* url, const http_headers& headers = DefaultHeaders) {
  131. return request(HTTP_GET, url, NoBody, headers);
  132. }
  133. HV_INLINE Response post(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  134. return request(HTTP_POST, url, body, headers);
  135. }
  136. HV_INLINE Response put(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  137. return request(HTTP_PUT, url, body, headers);
  138. }
  139. HV_INLINE Response patch(const char* url, const http_body& body = NoBody, const http_headers& headers = DefaultHeaders) {
  140. return request(HTTP_PATCH, url, body, headers);
  141. }
  142. // delete is c++ keyword, we have to replace delete with Delete.
  143. HV_INLINE Response Delete(const char* url, const http_headers& headers = DefaultHeaders) {
  144. return request(HTTP_DELETE, url, NoBody, headers);
  145. }
  146. HV_INLINE int async(Request req, ResponseCallback resp_cb) {
  147. return http_client_send_async(req, std::move(resp_cb));
  148. }
  149. }
  150. #endif // HV_REQUESTS_H_