http_client.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) 2023 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "http_client.h"
  14. #include "http_task.h"
  15. #include "http_module.h"
  16. namespace srpc
  17. {
  18. WFHttpTask *HttpClient::create_http_task(const std::string& url,
  19. int redirect_max,
  20. int retry_max,
  21. http_callback_t callback)
  22. {
  23. std::list<RPCModule *> module;
  24. for (int i = 0; i < SRPC_MODULE_MAX; i++)
  25. {
  26. if (this->modules[i])
  27. module.push_back(this->modules[i]);
  28. }
  29. auto&& cb = std::bind(&HttpClient::callback, this, std::placeholders::_1);
  30. HttpClientTask *task = new HttpClientTask(redirect_max, retry_max,
  31. cb, std::move(module));
  32. ParsedURI uri;
  33. URIParser::parse(url, uri);
  34. task->init(std::move(uri));
  35. task->set_keep_alive(HTTP_KEEPALIVE_DEFAULT);
  36. // task->set_url(url);
  37. task->user_callback_ = std::move(callback);
  38. return task;
  39. }
  40. WFHttpTask *HttpClient::create_http_task(const ParsedURI& uri,
  41. int redirect_max,
  42. int retry_max,
  43. http_callback_t callback)
  44. {
  45. std::list<RPCModule *> module;
  46. for (int i = 0; i < SRPC_MODULE_MAX; i++)
  47. {
  48. if (this->modules[i])
  49. module.push_back(this->modules[i]);
  50. }
  51. auto&& cb = std::bind(&HttpClient::callback, this, std::placeholders::_1);
  52. HttpClientTask *task = new HttpClientTask(redirect_max, retry_max,
  53. cb, std::move(module));
  54. task->init(uri);
  55. task->set_keep_alive(HTTP_KEEPALIVE_DEFAULT);
  56. // task->set_url(url);
  57. task->user_callback_ = std::move(callback);
  58. return task;
  59. }
  60. void HttpClient::init()
  61. {
  62. URIParser::parse(this->params.url, this->params.uri);
  63. if (this->params.uri.scheme &&
  64. strcasecmp(this->params.uri.scheme, "https") == 0)
  65. {
  66. this->params.is_ssl = true;
  67. }
  68. }
  69. void HttpClient::callback(WFHttpTask *task)
  70. {
  71. HttpClientTask *client_task = (HttpClientTask *)task;
  72. RPCModuleData *resp_data = client_task->mutable_module_data();
  73. http_get_header_module_data(task->get_resp(), *resp_data);
  74. for (int i = 0; i < SRPC_MODULE_MAX; i++)
  75. {
  76. if (this->modules[i])
  77. this->modules[i]->client_task_end(task, *resp_data);
  78. }
  79. if (client_task->user_callback_)
  80. client_task->user_callback_(task);
  81. }
  82. void HttpClient::add_filter(RPCFilter *filter)
  83. {
  84. int type = filter->get_module_type();
  85. this->mutex.lock();
  86. if (type < SRPC_MODULE_MAX && type >= 0)
  87. {
  88. RPCModule *module = this->modules[type];
  89. if (!module)
  90. {
  91. switch (type)
  92. {
  93. case RPCModuleTypeTrace:
  94. module = new HttpTraceModule();
  95. break;
  96. case RPCModuleTypeMetrics:
  97. module = new HttpMetricsModule();
  98. break;
  99. default:
  100. break;
  101. }
  102. this->modules[type] = module;
  103. }
  104. if (module)
  105. module->add_filter(filter);
  106. }
  107. this->mutex.unlock();
  108. return;
  109. }
  110. } // end namespace srpc