http_client.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef __RPC_HTTP_CLIENT_H__
  14. #define __RPC_HTTP_CLIENT_H__
  15. #include "workflow/WFTask.h"
  16. #include "workflow/HttpMessage.h"
  17. #include "workflow/WFTaskFactory.h"
  18. #include "rpc_options.h"
  19. #include "rpc_basic.h"
  20. #include "http_task.h"
  21. namespace srpc
  22. {
  23. #define HTTP_REDIRECT_DEFAULT 2
  24. #define HTTP_RETRY_DEFAULT 5
  25. #define HTTP_KEEPALIVE_DEFAULT (60 * 1000)
  26. struct HttpTaskParams
  27. {
  28. int send_timeout;
  29. int receive_timeout;
  30. int watch_timeout;
  31. int keep_alive_timeout;
  32. int redirect_max;
  33. int retry_max;
  34. };
  35. struct HttpClientParams
  36. {
  37. HttpTaskParams task_params;
  38. bool is_ssl;
  39. std::string url; // can be empty
  40. ParsedURI uri;
  41. };
  42. static constexpr struct HttpTaskParams HTTP_TASK_PARAMS_DEFAULT =
  43. {
  44. /* .send_timeout = */ -1,
  45. /* .receive_timeout = */ -1,
  46. /* .watch_timeout = */ 0,
  47. /* .keep_alive_timeout = */ HTTP_KEEPALIVE_DEFAULT,
  48. /* .retry_max = */ HTTP_REDIRECT_DEFAULT,
  49. /* .redirect_max = */ HTTP_RETRY_DEFAULT,
  50. };
  51. static const struct HttpClientParams HTTP_CLIENT_PARAMS_DEFAULT =
  52. {
  53. /* .task_params = */ HTTP_TASK_PARAMS_DEFAULT,
  54. /* .is_ssl = */ false,
  55. /* .url = */ "",
  56. /* .uri = */
  57. };
  58. class HttpClient
  59. {
  60. public:
  61. HttpClient() { }
  62. WFHttpTask *create_http_task(const std::string& url,
  63. int redirect_max,
  64. int retry_max,
  65. http_callback_t callback);
  66. WFHttpTask *create_http_task(const ParsedURI& uri,
  67. int redirect_max,
  68. int retry_max,
  69. http_callback_t callback);
  70. void add_filter(RPCFilter *filter);
  71. private:
  72. void callback(WFHttpTask *task);
  73. void init();
  74. void task_init(HttpClientTask *task);
  75. protected:
  76. HttpClientParams params;
  77. ParsedURI uri;
  78. private:
  79. std::mutex mutex;
  80. RPCModule *modules[SRPC_MODULE_MAX] = { 0 };
  81. };
  82. } // end namespace srpc
  83. #endif