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 17 #ifndef MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_MESSAGE_HANDLER_H_ 18 #define MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_MESSAGE_HANDLER_H_ 19 20 #include <event2/buffer.h> 21 #include <event2/event.h> 22 #include <event2/http.h> 23 #include <event2/keyvalq_struct.h> 24 #include <event2/listener.h> 25 #include <event2/util.h> 26 #include <cstdio> 27 #include <cstdlib> 28 #include <cstring> 29 #include <functional> 30 #include <string> 31 #include <list> 32 #include <map> 33 #include <memory> 34 #include <vector> 35 #include "ps/core/comm_util.h" 36 #include "utils/log_adapter.h" 37 #include "ps/core/communicator/request_process_result_code.h" 38 #include "nlohmann/json.hpp" 39 #include "include/backend/distributed/ps/constants.h" 40 41 namespace mindspore { 42 namespace ps { 43 namespace core { 44 using HttpHeaders = std::map<std::string, std::list<std::string>>; 45 46 class HttpMessageHandler { 47 public: HttpMessageHandler()48 HttpMessageHandler() 49 : event_request_(nullptr), 50 event_uri_(nullptr), 51 path_params_{0}, 52 head_params_(nullptr), 53 post_params_{0}, 54 post_param_parsed_(false), 55 post_message_(nullptr), 56 body_(nullptr), 57 resp_headers_(nullptr), 58 resp_buf_(nullptr), 59 resp_code_(HTTP_OK), 60 content_len_(0), 61 event_base_(nullptr), 62 offset_(0) {} 63 64 virtual ~HttpMessageHandler() = default; 65 66 void InitHttpMessage(); 67 68 std::string GetRequestUri() const; 69 std::string GetRequestHost(); 70 const char *GetHostByUri() const; 71 std::string GetHeadParam(const std::string &key) const; 72 std::string GetPathParam(const std::string &key) const; 73 std::string GetPostParam(const std::string &key); 74 bool GetPostMsg(size_t *len, uint8_t **buffer); 75 std::string GetUriPath() const; 76 std::string GetRequestPath(); 77 std::string GetUriQuery() const; 78 79 // It will return -1 if no port set 80 int GetUriPort() const; 81 82 // Useless to get from a request url, fragment is only for browser to locate sth. 83 std::string GetUriFragment() const; 84 85 void AddRespHeadParam(const std::string &key, const std::string &val); 86 void AddRespHeaders(const HttpHeaders &headers); 87 void AddRespString(const std::string &str); 88 void SetRespCode(int code); 89 90 // Make sure code and all response body has finished set 91 void SendResponse(); 92 void QuickResponse(int code, const void *body, size_t len); 93 void QuickResponseInference(int code, const void *body, size_t len, evbuffer_ref_cleanup_cb cb); 94 void SimpleResponse(int code, const HttpHeaders &headers, const std::string &body); 95 void ErrorResponse(int code, const RequestProcessResult &status); 96 97 // If message is empty, libevent will use default error code message instead 98 void RespError(int nCode, const std::string &message); 99 // Body length should no more than MAX_POST_BODY_LEN, default 64kB 100 void ParsePostParam(); 101 RequestProcessResult ParsePostMessageToJson(); 102 void ReceiveMessage(const void *buffer, size_t num); 103 void set_content_len(const uint64_t &len); 104 uint64_t content_len() const; 105 const event_base *http_base() const; 106 void set_http_base(const struct event_base *base); 107 void set_request(const struct evhttp_request *req); 108 const struct evhttp_request *request() const; 109 void InitBodySize(); 110 std::shared_ptr<std::vector<char>> body(); 111 void set_body(const std::shared_ptr<std::vector<char>> &body); 112 nlohmann::json request_message() const; 113 RequestProcessResult ParseValueFromKey(const std::string &key, uint32_t *const value); 114 115 // Parse node ids when receiving an http request for scale in 116 RequestProcessResult ParseNodeIdsFromKey(const std::string &key, std::vector<std::string> *const value); 117 118 private: 119 struct evhttp_request *event_request_; 120 const struct evhttp_uri *event_uri_; 121 struct evkeyvalq path_params_; 122 struct evkeyvalq *head_params_; 123 struct evkeyvalq post_params_; 124 bool post_param_parsed_; 125 std::unique_ptr<std::string> post_message_; 126 std::shared_ptr<std::vector<char>> body_; 127 struct evkeyvalq *resp_headers_; 128 struct evbuffer *resp_buf_; 129 int resp_code_; 130 uint64_t content_len_; 131 struct event_base *event_base_; 132 uint64_t offset_; 133 nlohmann::json request_message_; 134 }; 135 } // namespace core 136 } // namespace ps 137 } // namespace mindspore 138 #endif // MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_HTTP_MESSAGE_HANDLER_H_ 139