You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_server.cc 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "ps/comm/http_server.h"
  17. #include "ps/comm/http_message_handler.h"
  18. #include "ps/comm/comm_util.h"
  19. #ifdef WIN32
  20. #include <WinSock2.h>
  21. #endif
  22. #include <arpa/inet.h>
  23. #include <event.h>
  24. #include <event2/buffer.h>
  25. #include <event2/bufferevent.h>
  26. #include <event2/bufferevent_compat.h>
  27. #include <event2/http.h>
  28. #include <event2/http_compat.h>
  29. #include <event2/http_struct.h>
  30. #include <event2/listener.h>
  31. #include <event2/util.h>
  32. #include <cstdio>
  33. #include <cstdlib>
  34. #include <cstring>
  35. #include <functional>
  36. #include <regex>
  37. namespace mindspore {
  38. namespace ps {
  39. namespace comm {
  40. HttpServer::~HttpServer() { Stop(); }
  41. bool HttpServer::InitServer() {
  42. CommUtil::CheckIp(server_address_);
  43. event_base_ = event_base_new();
  44. MS_EXCEPTION_IF_NULL(event_base_);
  45. event_http_ = evhttp_new(event_base_);
  46. MS_EXCEPTION_IF_NULL(event_http_);
  47. int ret = evhttp_bind_socket(event_http_, server_address_.c_str(), server_port_);
  48. if (ret != 0) {
  49. MS_LOG(EXCEPTION) << "Http bind server addr:" << server_address_.c_str() << " port:" << server_port_ << "failed";
  50. }
  51. is_init_ = true;
  52. return true;
  53. }
  54. void HttpServer::SetTimeOut(int seconds) {
  55. MS_EXCEPTION_IF_NULL(event_http_);
  56. if (seconds < 0) {
  57. MS_LOG(EXCEPTION) << "The timeout seconds:" << seconds << "is less than 0!";
  58. }
  59. evhttp_set_timeout(event_http_, seconds);
  60. }
  61. void HttpServer::SetAllowedMethod(u_int16_t methods) {
  62. MS_EXCEPTION_IF_NULL(event_http_);
  63. evhttp_set_allowed_methods(event_http_, methods);
  64. }
  65. void HttpServer::SetMaxHeaderSize(size_t num) {
  66. MS_EXCEPTION_IF_NULL(event_http_);
  67. if (num < 0) {
  68. MS_LOG(EXCEPTION) << "The header num:" << num << "is less than 0!";
  69. }
  70. evhttp_set_max_headers_size(event_http_, num);
  71. }
  72. void HttpServer::SetMaxBodySize(size_t num) {
  73. MS_EXCEPTION_IF_NULL(event_http_);
  74. if (num < 0) {
  75. MS_LOG(EXCEPTION) << "The max body num:" << num << "is less than 0!";
  76. }
  77. evhttp_set_max_body_size(event_http_, num);
  78. }
  79. bool HttpServer::RegisterRoute(const std::string &url, OnRequestReceive *function) {
  80. if ((!is_init_) && (!InitServer())) {
  81. MS_LOG(EXCEPTION) << "Init http server failed!";
  82. }
  83. if (!function) {
  84. return false;
  85. }
  86. auto TransFunc = [](struct evhttp_request *req, void *arg) {
  87. MS_EXCEPTION_IF_NULL(req);
  88. MS_EXCEPTION_IF_NULL(arg);
  89. HttpMessageHandler httpReq(req);
  90. httpReq.InitHttpMessage();
  91. OnRequestReceive *func = reinterpret_cast<OnRequestReceive *>(arg);
  92. (*func)(&httpReq);
  93. };
  94. MS_EXCEPTION_IF_NULL(event_http_);
  95. // O SUCCESS,-1 ALREADY_EXIST,-2 FAILURE
  96. int ret = evhttp_set_cb(event_http_, url.c_str(), TransFunc, reinterpret_cast<void *>(function));
  97. if (ret == 0) {
  98. MS_LOG(INFO) << "Ev http register handle of:" << url.c_str() << " success.";
  99. } else if (ret == -1) {
  100. MS_LOG(WARNING) << "Ev http register handle of:" << url.c_str() << " exist.";
  101. } else {
  102. MS_LOG(ERROR) << "Ev http register handle of:" << url.c_str() << " failed.";
  103. return false;
  104. }
  105. return true;
  106. }
  107. bool HttpServer::UnRegisterRoute(const std::string &url) {
  108. MS_EXCEPTION_IF_NULL(event_http_);
  109. return (evhttp_del_cb(event_http_, url.c_str()) == 0);
  110. }
  111. bool HttpServer::Start() {
  112. MS_EXCEPTION_IF_NULL(event_base_);
  113. int ret = event_base_dispatch(event_base_);
  114. if (ret == 0) {
  115. MS_LOG(INFO) << "Event base dispatch success!";
  116. return true;
  117. } else if (ret == 1) {
  118. MS_LOG(ERROR) << "Event base dispatch failed with no events pending or active!";
  119. return false;
  120. } else if (ret == -1) {
  121. MS_LOG(ERROR) << "Event base dispatch failed with error occurred!";
  122. return false;
  123. } else {
  124. MS_LOG(EXCEPTION) << "Event base dispatch with unexpect error code!";
  125. }
  126. }
  127. void HttpServer::Stop() {
  128. if (event_http_) {
  129. evhttp_free(event_http_);
  130. event_http_ = nullptr;
  131. }
  132. if (event_base_) {
  133. event_base_free(event_base_);
  134. event_base_ = nullptr;
  135. }
  136. }
  137. } // namespace comm
  138. } // namespace ps
  139. } // namespace mindspore