http_server.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_server.h"
  14. #include "http_task.h"
  15. #include "http_module.h"
  16. #ifdef _WIN32
  17. #include <workflow/PlatformSocket.h>
  18. #else
  19. #include <arpa/inet.h>
  20. #include <sys/socket.h>
  21. #endif
  22. namespace srpc
  23. {
  24. void HttpServer::add_filter(RPCFilter *filter)
  25. {
  26. int type = filter->get_module_type();
  27. this->mutex.lock();
  28. if (type < SRPC_MODULE_MAX && type >= 0)
  29. {
  30. RPCModule *module = this->modules[type];
  31. if (!module)
  32. {
  33. switch (type)
  34. {
  35. case RPCModuleTypeTrace:
  36. module = new HttpTraceModule();
  37. break;
  38. case RPCModuleTypeMetrics:
  39. module = new HttpMetricsModule();
  40. break;
  41. default:
  42. break;
  43. }
  44. this->modules[type] = module;
  45. }
  46. if (module)
  47. module->add_filter(filter);
  48. }
  49. this->mutex.unlock();
  50. return;
  51. }
  52. CommSession *HttpServer::new_session(long long seq, CommConnection *conn)
  53. {
  54. HttpServerTask *task;
  55. std::list<RPCModule *> module;
  56. for (int i = 0; i < SRPC_MODULE_MAX; i++)
  57. {
  58. if (this->modules[i])
  59. module.push_back(this->modules[i]);
  60. }
  61. task = new HttpServerTask(this, this->process, std::move(module));
  62. task->set_keep_alive(this->params.keep_alive_timeout);
  63. task->set_receive_timeout(this->params.receive_timeout);
  64. task->get_req()->set_size_limit(this->params.request_size_limit);
  65. task->set_is_ssl(this->get_ssl_ctx() ? true : false);
  66. task->set_listen_port(this->get_listen_port());
  67. return task;
  68. }
  69. unsigned short HttpServer::get_listen_port()
  70. {
  71. if (this->listen_port == 0)
  72. {
  73. struct sockaddr_storage addr;
  74. socklen_t l = sizeof addr;
  75. this->get_listen_addr((struct sockaddr *)&addr, &l);
  76. if (addr.ss_family == AF_INET)
  77. {
  78. struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
  79. this->listen_port = ntohs(sin->sin_port);
  80. }
  81. else // if (addr.ss_family == AF_INET6)
  82. {
  83. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
  84. this->listen_port = ntohs(sin6->sin6_port);
  85. }
  86. }
  87. return this->listen_port;
  88. }
  89. } // namespace srpc