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_message_handler.h 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_MESSAGE_HANDLER_H_
  17. #define MINDSPORE_CCSRC_PS_COMM_HTTP_MESSAGE_HANDLER_H_
  18. #include <event2/buffer.h>
  19. #include <event2/event.h>
  20. #include <event2/http.h>
  21. #include <event2/keyvalq_struct.h>
  22. #include <event2/listener.h>
  23. #include <event2/util.h>
  24. #include <cstdio>
  25. #include <cstdlib>
  26. #include <cstring>
  27. #include <functional>
  28. #include <string>
  29. #include <list>
  30. #include <map>
  31. #include <memory>
  32. #include "utils/log_adapter.h"
  33. namespace mindspore {
  34. namespace ps {
  35. namespace comm {
  36. using HttpHeaders = std::map<std::string, std::list<std::string>>;
  37. class HttpMessageHandler {
  38. public:
  39. explicit HttpMessageHandler(struct evhttp_request *req)
  40. : event_request_(req),
  41. event_uri_(nullptr),
  42. path_params_{0},
  43. head_params_(nullptr),
  44. post_params_{0},
  45. post_param_parsed_(false),
  46. body_(nullptr),
  47. resp_headers_(nullptr),
  48. resp_buf_(nullptr),
  49. resp_code_(HTTP_OK) {}
  50. ~HttpMessageHandler();
  51. void InitHttpMessage();
  52. std::string GetRequestUri();
  53. std::string GetRequestHost();
  54. std::string GetHeadParam(const std::string &key);
  55. std::string GetPathParam(const std::string &key);
  56. std::string GetPostParam(const std::string &key);
  57. std::string GetPostMsg();
  58. std::string GetUriPath();
  59. std::string GetUriQuery();
  60. // It will return -1 if no port set
  61. int GetUriPort();
  62. // Useless to get from a request url, fragment is only for browser to locate sth.
  63. std::string GetUriFragment();
  64. void AddRespHeadParam(const std::string &key, const std::string &val);
  65. void AddRespHeaders(const HttpHeaders &headers);
  66. void AddRespString(const std::string &str);
  67. void SetRespCode(int code);
  68. // Make sure code and all response body has finished set
  69. void SendResponse();
  70. void QuickResponse(int code, const std::string &body);
  71. void SimpleResponse(int code, const HttpHeaders &headers, const std::string &body);
  72. // If message is empty, libevent will use default error code message instead
  73. void RespError(int nCode, const std::string &message);
  74. private:
  75. struct evhttp_request *event_request_;
  76. const struct evhttp_uri *event_uri_;
  77. struct evkeyvalq path_params_;
  78. struct evkeyvalq *head_params_;
  79. struct evkeyvalq post_params_;
  80. bool post_param_parsed_;
  81. std::unique_ptr<std::string> body_;
  82. struct evkeyvalq *resp_headers_;
  83. struct evbuffer *resp_buf_;
  84. int resp_code_;
  85. // Body length should no more than MAX_POST_BODY_LEN, default 64kB
  86. void ParsePostParam();
  87. };
  88. } // namespace comm
  89. } // namespace ps
  90. } // namespace mindspore
  91. #endif // MINDSPORE_CCSRC_PS_COMM_HTTP_MESSAGE_HANDLER_H_