requests.h 5.2 KB

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