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.h 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef MINDSPORE_CCSRC_PS_COMM_HTTP_SERVER_H_
  17. #define MINDSPORE_CCSRC_PS_COMM_HTTP_SERVER_H_
  18. #include "ps/comm/http_message_handler.h"
  19. #include <event2/buffer.h>
  20. #include <event2/event.h>
  21. #include <event2/http.h>
  22. #include <event2/keyvalq_struct.h>
  23. #include <event2/listener.h>
  24. #include <event2/util.h>
  25. #include <cstdio>
  26. #include <cstdlib>
  27. #include <cstring>
  28. #include <functional>
  29. #include <string>
  30. namespace mindspore {
  31. namespace ps {
  32. namespace comm {
  33. typedef enum eHttpMethod {
  34. HM_GET = 1 << 0,
  35. HM_POST = 1 << 1,
  36. HM_HEAD = 1 << 2,
  37. HM_PUT = 1 << 3,
  38. HM_DELETE = 1 << 4,
  39. HM_OPTIONS = 1 << 5,
  40. HM_TRACE = 1 << 6,
  41. HM_CONNECT = 1 << 7,
  42. HM_PATCH = 1 << 8
  43. } HttpMethod;
  44. class HttpServer {
  45. public:
  46. // Server address only support IPV4 now, and should be in format of "x.x.x.x"
  47. explicit HttpServer(const std::string &address, std::uint16_t port)
  48. : server_address_(address), server_port_(port), event_base_(nullptr), event_http_(nullptr), is_init_(false) {}
  49. ~HttpServer();
  50. using OnRequestReceive = std::function<void(HttpMessageHandler *)>;
  51. bool InitServer();
  52. void SetTimeOut(int seconds = 5);
  53. // Default allowed methods: GET, POST, HEAD, PUT, DELETE
  54. void SetAllowedMethod(u_int16_t methods);
  55. // Default to ((((unsigned long long)0xffffffffUL) << 32) | 0xffffffffUL)
  56. void SetMaxHeaderSize(std::size_t num);
  57. // Default to ((((unsigned long long)0xffffffffUL) << 32) | 0xffffffffUL)
  58. void SetMaxBodySize(std::size_t num);
  59. // Return: true if success, false if failed, check log to find failure reason
  60. bool RegisterRoute(const std::string &url, OnRequestReceive *func);
  61. bool UnRegisterRoute(const std::string &url);
  62. bool Start();
  63. void Stop();
  64. private:
  65. std::string server_address_;
  66. std::uint16_t server_port_;
  67. struct event_base *event_base_;
  68. struct evhttp *event_http_;
  69. bool is_init_;
  70. };
  71. } // namespace comm
  72. } // namespace ps
  73. } // namespace mindspore
  74. #endif // MINDSPORE_CCSRC_PS_COMM_HTTP_SERVER_H_