Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

146 lines
4.9KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "mk_httpclient.h"
  11. #include "Util/logger.h"
  12. #include "Http/HttpDownloader.h"
  13. #include "Http/HttpRequester.h"
  14. using namespace std;
  15. using namespace toolkit;
  16. using namespace mediakit;
  17. API_EXPORT mk_http_downloader API_CALL mk_http_downloader_create() {
  18. HttpDownloader::Ptr *obj(new HttpDownloader::Ptr(new HttpDownloader()));
  19. return (mk_http_downloader) obj;
  20. }
  21. API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx) {
  22. assert(ctx);
  23. HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx;
  24. delete obj;
  25. }
  26. API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data) {
  27. assert(ctx && url && file);
  28. HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx;
  29. (*obj)->setOnResult([cb, user_data](const SockException &ex, const string &filePath) {
  30. if (cb) {
  31. cb(user_data, ex.getErrCode(), ex.what(), filePath.data());
  32. }
  33. });
  34. (*obj)->startDownload(url, file, false);
  35. }
  36. ///////////////////////////////////////////HttpRequester/////////////////////////////////////////////
  37. API_EXPORT mk_http_requester API_CALL mk_http_requester_create(){
  38. HttpRequester::Ptr *ret = new HttpRequester::Ptr(new HttpRequester);
  39. return ret;
  40. }
  41. API_EXPORT void API_CALL mk_http_requester_clear(mk_http_requester ctx){
  42. assert(ctx);
  43. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  44. (*obj)->clear();
  45. }
  46. API_EXPORT void API_CALL mk_http_requester_release(mk_http_requester ctx){
  47. assert(ctx);
  48. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  49. delete obj;
  50. }
  51. API_EXPORT void API_CALL mk_http_requester_set_method(mk_http_requester ctx,const char *method){
  52. assert(ctx);
  53. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  54. (*obj)->setMethod(method);
  55. }
  56. template <typename C = StrCaseMap>
  57. static C get_http_header( const char *response_header[]){
  58. C header;
  59. for (int i = 0; response_header[i] != NULL;) {
  60. auto key = response_header[i];
  61. auto value = response_header[i + 1];
  62. if (key && value) {
  63. i += 2;
  64. header.emplace(key,value);
  65. continue;
  66. }
  67. break;
  68. }
  69. return header;
  70. }
  71. API_EXPORT void API_CALL mk_http_requester_set_body(mk_http_requester ctx, mk_http_body body){
  72. assert(ctx && body);
  73. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  74. HttpBody::Ptr *body_obj = (HttpBody::Ptr *)body;
  75. (*obj)->setBody(*body_obj);
  76. }
  77. API_EXPORT void API_CALL mk_http_requester_set_header(mk_http_requester ctx, const char *header[]){
  78. assert(ctx && header);
  79. auto header_obj = get_http_header(header);
  80. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  81. (*obj)->setHeader(header_obj);
  82. }
  83. API_EXPORT void API_CALL mk_http_requester_add_header(mk_http_requester ctx,const char *key,const char *value,int force){
  84. assert(ctx && key && value);
  85. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  86. (*obj)->addHeader(key,value,force);
  87. }
  88. API_EXPORT const char* API_CALL mk_http_requester_get_response_status(mk_http_requester ctx){
  89. assert(ctx);
  90. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  91. return (*obj)->response().Url().c_str();
  92. }
  93. API_EXPORT const char* API_CALL mk_http_requester_get_response_header(mk_http_requester ctx,const char *key){
  94. assert(ctx);
  95. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  96. return (*obj)->response()[key].c_str();
  97. }
  98. API_EXPORT const char* API_CALL mk_http_requester_get_response_body(mk_http_requester ctx, size_t *length){
  99. assert(ctx);
  100. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  101. if(length){
  102. *length = (*obj)->response().Content().size();
  103. }
  104. return (*obj)->response().Content().c_str();
  105. }
  106. API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester ctx){
  107. assert(ctx);
  108. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  109. return (mk_parser)&((*obj)->response());
  110. }
  111. API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data){
  112. assert(ctx && cb);
  113. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  114. (*obj)->setOnResult([cb, user_data](const SockException &ex, const Parser &res) {
  115. cb(user_data, ex.getErrCode(), ex.what());
  116. });
  117. }
  118. API_EXPORT void API_CALL mk_http_requester_start(mk_http_requester ctx,const char *url, float timeout_second){
  119. assert(ctx && url && url[0] && timeout_second > 0);
  120. HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx;
  121. (*obj)->setCompleteTimeout(timeout_second * 1000);
  122. (*obj)->sendRequest(url);
  123. }