1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 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 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NWEB_URL_RESOURCE_RESPONSE_H 17 #define NWEB_URL_RESOURCE_RESPONSE_H 18 19 #include <map> 20 #include <string> 21 22 namespace OHOS::NWeb { 23 24 class NWebResourceReadyCallback { 25 public: ~NWebResourceReadyCallback()26 virtual ~NWebResourceReadyCallback() {}; 27 virtual void Continue() = 0; 28 virtual void Cancel() = 0; 29 }; 30 31 class NWebUrlResourceResponse { 32 public: 33 /** 34 * @brief Construct a resource response with the given parameters. 35 * 36 * @param mime_type the resource response's MIME type, for example { 37 * "text/html"}. 38 * @param encoding the resource response's character encoding, for example 39 * {"utf-8"}. 40 * @param status_code the status code needs to be in the ranges [100, 299], 41 * [400, 599]. Causing a redirect by specifying a 3xx code is not supported. 42 * @param reason_phrase the phrase describing the status code, for example 43 * "OK". Must be non-empty. 44 * @param response_headers the resource response's headers represented as a 45 * mapping of header name -> header value. 46 * @param input_stream the input stream that provides the resource response's 47 * data. 48 */ NWebUrlResourceResponse(const std::string & mime_type,const std::string & encoding,const int status_code,const std::string & reason_phrase,const std::map<std::string,std::string> & response_headers,std::string & input_stream)49 NWebUrlResourceResponse(const std::string& mime_type, 50 const std::string& encoding, 51 const int status_code, 52 const std::string& reason_phrase, 53 const std::map<std::string, std::string>& response_headers, 54 std::string& input_stream) 55 : mime_type_(mime_type), 56 encoding_(encoding), 57 status_code_(status_code), 58 reason_phrase_(reason_phrase), 59 response_headers_(response_headers), 60 input_stream_(input_stream) {} 61 NWebUrlResourceResponse(const std::string & mime_type,const std::string & encoding,std::string & input_stream)62 NWebUrlResourceResponse(const std::string& mime_type, 63 const std::string& encoding, 64 std::string& input_stream) 65 : mime_type_(mime_type), 66 encoding_(encoding), 67 input_stream_(input_stream) {} 68 69 ~NWebUrlResourceResponse() = default; 70 71 /** 72 * @brief get input stream 73 * 74 * @retval inputstream string 75 */ ResponseData()76 const std::string& ResponseData() 77 { 78 return input_stream_; 79 } 80 81 /** 82 * @brief set input stream 83 * 84 * @param input_stream set inputstream for example: fread(buf, 1, sizeof(buf), 85 * file) 86 */ PutResponseData(std::string & input_stream)87 void PutResponseData(std::string& input_stream) 88 { 89 input_stream_ = input_stream; 90 fd_ = 0; 91 isFileFd_ = false; 92 } 93 94 /** 95 * @brief Construct a resource response with the given parameters. 96 * 97 * @param encoding encoding { "utf-8" } 98 */ PutResponseEncoding(const std::string & encoding)99 void PutResponseEncoding(const std::string& encoding) 100 { 101 encoding_ = encoding; 102 } 103 104 /** 105 * @brief get encoding 106 * 107 * @retval encoding the resource response's encoding 108 */ ResponseEncoding()109 std::string ResponseEncoding() 110 { 111 return encoding_; 112 } 113 114 /** 115 * @brief Construct a resource response with the given parameters. 116 * 117 * @param mime_type mime_type{ "text/html" } 118 */ PutResponseMimeType(const std::string & mime_type)119 void PutResponseMimeType(const std::string& mime_type) 120 { 121 mime_type_ = mime_type; 122 } 123 124 /** 125 * @brief Get mimetype 126 * 127 * @retval mimetype The resource response's MIME type 128 */ ResponseMimeType()129 std::string ResponseMimeType() 130 { 131 return mime_type_; 132 } 133 134 /** 135 * @brief Set ResponseHeaders 136 * 137 * @param response_headers response header 138 */ PutResponseHeaders(const std::map<std::string,std::string> & response_headers)139 void PutResponseHeaders(const std::map<std::string, std::string>& response_headers) 140 { 141 response_headers_ = response_headers; 142 } 143 144 /** 145 * @brief Get ResponseHeaders 146 * 147 * @retval response headers 148 */ ResponseHeaders()149 const std::map<std::string, std::string>& ResponseHeaders() 150 { 151 return response_headers_; 152 } 153 154 /** 155 * @brief Set StatusCode And ReasonPhrase 156 * 157 * @param status_code status code 158 * @param reasonphrase reason phrase 159 */ PutResponseStateAndStatuscode(int status_code,std::string reason_phrase)160 void PutResponseStateAndStatuscode(int status_code, 161 std::string reason_phrase) 162 { 163 status_code_ = status_code; 164 reason_phrase_ = reason_phrase; 165 } 166 167 /** 168 * @brief Get status code 169 * 170 * @retval status code 171 */ ResponseStatusCode()172 int ResponseStatusCode() 173 { 174 return status_code_; 175 } 176 177 /** 178 * @brief Get ReasonPhrase 179 * 180 * @retval errorcode reason 181 */ ResponseStatus()182 std::string ResponseStatus() 183 { 184 return reason_phrase_; 185 } 186 PutResponseDataStatus(bool isDataReady)187 void PutResponseDataStatus(bool isDataReady) 188 { 189 isDataReady_ = isDataReady; 190 if (isDataReady_ == true && readyCallback_ != nullptr) { 191 readyCallback_->Continue(); 192 readyCallback_ = nullptr; 193 } 194 } 195 ResponseDataStatus()196 bool ResponseDataStatus() const 197 { 198 return isDataReady_; 199 } 200 ResponseIsFileHandle()201 bool ResponseIsFileHandle() const 202 { 203 return isFileFd_; 204 } 205 PutResponseFileHandle(int fd)206 void PutResponseFileHandle(int fd) 207 { 208 fd_ = fd; 209 isFileFd_ = true; 210 input_stream_.clear(); 211 } 212 ResponseFileHandle()213 int ResponseFileHandle() const 214 { 215 return fd_; 216 } 217 PutResponseReadyCallback(std::shared_ptr<NWebResourceReadyCallback> readyCallback)218 void PutResponseReadyCallback(std::shared_ptr<NWebResourceReadyCallback> readyCallback) 219 { 220 readyCallback_ = readyCallback; 221 } 222 223 private: 224 std::string mime_type_; 225 std::string encoding_; 226 int status_code_ = 200; 227 std::string reason_phrase_; 228 std::map<std::string, std::string> response_headers_; 229 std::string input_stream_; 230 int fd_ = -1; 231 bool isFileFd_ = false; 232 bool isDataReady_ = true; 233 std::shared_ptr<NWebResourceReadyCallback> readyCallback_; 234 }; 235 } // namespace OHOS::NWeb 236 237 #endif // NWEB_URL_RESOURCE_RESPONSE_H 238