http_task.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_TASK_H__
  14. #define __RPC_HTTP_TASK_H__
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <string>
  19. #include "workflow/HttpUtil.h"
  20. #include "workflow/WFTaskFactory.h"
  21. #include "workflow/WFGlobal.h"
  22. #include "rpc_module.h"
  23. namespace srpc
  24. {
  25. // copy part of workflow/src/factory/HttpTaskImpl.cc
  26. class HttpClientTask : public WFComplexClientTask<protocol::HttpRequest,
  27. protocol::HttpResponse>
  28. {
  29. public:
  30. HttpClientTask(int redirect_max,
  31. int retry_max,
  32. http_callback_t&& callback,
  33. std::list<RPCModule *>&& modules);
  34. RPCModuleData *mutable_module_data() { return &module_data_; }
  35. void set_module_data(RPCModuleData data) { module_data_ = std::move(data); }
  36. int get_retry_times() const { return retry_times_; }
  37. void set_url(std::string url) { this->url_ = std::move(url); }
  38. std::string get_uri_host() const;
  39. std::string get_uri_port() const;
  40. std::string get_uri_scheme() const;
  41. std::string get_url() const;
  42. /*
  43. // similar to opentracing: log({{"event", "error"}, {"message", "application log"}});
  44. void log(const RPCLogVector& fields);
  45. // Baggage Items, which are just key:value pairs that cross process boundaries
  46. void add_baggage(const std::string& key, const std::string& value);
  47. bool get_baggage(const std::string& key, std::string& value);
  48. */
  49. protected:
  50. virtual CommMessageOut *message_out();
  51. virtual CommMessageIn *message_in();
  52. virtual int keep_alive_timeout();
  53. virtual bool init_success();
  54. virtual void init_failed();
  55. virtual bool finish_once();
  56. protected:
  57. bool need_redirect(ParsedURI& uri);
  58. bool redirect_url(protocol::HttpResponse *client_resp, ParsedURI& uri);
  59. void set_empty_request();
  60. void check_response();
  61. public:
  62. http_callback_t user_callback_;
  63. private:
  64. std::string url_;
  65. int redirect_max_;
  66. int redirect_count_;
  67. RPCModuleData module_data_;
  68. std::list<RPCModule *> modules_ = { NULL };
  69. };
  70. class HttpServerTask : public WFServerTask<protocol::HttpRequest,
  71. protocol::HttpResponse>
  72. {
  73. public:
  74. HttpServerTask(CommService *service,
  75. std::function<void (WFHttpTask *)>& process,
  76. std::list<RPCModule *>&& modules) :
  77. WFServerTask(service, WFGlobal::get_scheduler(), process),
  78. req_is_alive_(false),
  79. req_has_keep_alive_header_(false),
  80. modules_(std::move(modules))
  81. {}
  82. void set_is_ssl(bool is_ssl) { this->is_ssl_ = is_ssl; }
  83. void set_listen_port(unsigned short port) { this->listen_port_ = port; }
  84. bool is_ssl() const { return this->is_ssl_; }
  85. unsigned short listen_port() const { return this->listen_port_; }
  86. class ModuleSeries : public WFServerTask<protocol::HttpRequest,
  87. protocol::HttpResponse>::Series
  88. {
  89. public:
  90. ModuleSeries(WFServerTask<protocol::HttpRequest,
  91. protocol::HttpResponse> *task) :
  92. WFServerTask<protocol::HttpRequest,
  93. protocol::HttpResponse>::Series(task),
  94. module_data_(NULL)
  95. {}
  96. RPCModuleData *get_module_data() { return module_data_; }
  97. void set_module_data(RPCModuleData *data) { module_data_ = data; }
  98. bool has_module_data() const { return !!module_data_; }
  99. void clear_module_data() { module_data_ = NULL; }
  100. private:
  101. RPCModuleData *module_data_;
  102. };
  103. //TODO:
  104. // bool get_remote(std::string& ip, unsigned short *port) const;
  105. RPCModuleData *mutable_module_data() { return &module_data_; }
  106. void set_module_data(RPCModuleData data) { module_data_ = std::move(data); }
  107. protected:
  108. virtual void handle(int state, int error);
  109. virtual CommMessageOut *message_out();
  110. protected:
  111. bool req_is_alive_;
  112. bool req_has_keep_alive_header_;
  113. std::string req_keep_alive_;
  114. RPCModuleData module_data_;
  115. std::list<RPCModule *> modules_;
  116. bool is_ssl_;
  117. unsigned short listen_port_;
  118. };
  119. } // end namespace srpc
  120. #endif