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 <memory> 21 #include <string> 22 23 namespace OHOS::NWeb { 24 25 class NWebResourceReadyCallback { 26 public: ~NWebResourceReadyCallback()27 virtual ~NWebResourceReadyCallback() {} 28 virtual void Continue() = 0; 29 virtual void Cancel() = 0; 30 }; 31 32 enum class NWebResponseDataType : int32_t { 33 NWEB_STRING_TYPE, 34 NWEB_FILE_TYPE, 35 NWEB_RESOURCE_URL_TYPE, 36 NWEB_BUFFER_TYPE, 37 }; 38 39 class NWebUrlResourceResponse { 40 public: 41 virtual ~NWebUrlResourceResponse() = default; 42 43 /** 44 * @brief get input stream 45 * 46 * @retval inputstream string 47 */ 48 virtual std::string ResponseData() = 0; 49 50 /** 51 * @brief set input stream 52 * 53 * @param input_stream set inputstream for example: fread(buf, 1, sizeof(buf), 54 * file) 55 */ 56 virtual void PutResponseData(const std::string& input_stream) = 0; 57 58 /** 59 * @brief Construct a resource response with the given parameters. 60 * 61 * @param encoding encoding { "utf-8" } 62 */ 63 virtual void PutResponseEncoding(const std::string& encoding) = 0; 64 65 /** 66 * @brief get encoding 67 * 68 * @retval encoding the resource response's encoding 69 */ 70 virtual std::string ResponseEncoding() = 0; 71 72 /** 73 * @brief Construct a resource response with the given parameters. 74 * 75 * @param mime_type mime_type{ "text/html" } 76 */ 77 virtual void PutResponseMimeType(const std::string& mime_type) = 0; 78 79 /** 80 * @brief Get mimetype 81 * 82 * @retval mimetype The resource response's MIME type 83 */ 84 virtual std::string ResponseMimeType() = 0; 85 86 /** 87 * @brief Set ResponseHeaders 88 * 89 * @param response_headers response header 90 */ 91 virtual void PutResponseHeaders(const std::map<std::string, std::string>& response_headers) = 0; 92 93 /** 94 * @brief Get ResponseHeaders 95 * 96 * @retval response headers 97 */ 98 virtual std::map<std::string, std::string> ResponseHeaders() = 0; 99 100 /** 101 * @brief Set StatusCode And ReasonPhrase 102 * 103 * @param status_code status code 104 * @param reasonphrase reason phrase 105 */ 106 virtual void PutResponseStateAndStatuscode(int status_code, const std::string& reason_phrase) = 0; 107 108 /** 109 * @brief Get status code 110 * 111 * @retval status code 112 */ 113 virtual int ResponseStatusCode() = 0; 114 115 /** 116 * @brief Get ReasonPhrase 117 * 118 * @retval errorcode reason 119 */ 120 virtual std::string ResponseStatus() = 0; 121 122 virtual void PutResponseDataStatus(bool isDataReady) = 0; 123 124 virtual bool ResponseDataStatus() = 0; 125 126 virtual bool ResponseIsFileHandle() = 0; 127 128 virtual void PutResponseFileHandle(int fd) = 0; 129 130 virtual int ResponseFileHandle() = 0; 131 132 virtual void PutResponseResourceUrl(const std::string& url) = 0; 133 134 virtual std::string ResponseResourceUrl() = 0; 135 136 virtual NWebResponseDataType ResponseDataType() = 0; 137 138 virtual void PutResponseReadyCallback(std::shared_ptr<NWebResourceReadyCallback> readyCallback) = 0; 139 140 virtual void PutResponseDataBuffer(char* buffer, size_t bufferSize) = 0; 141 142 virtual char* GetResponseDataBuffer() = 0; 143 144 virtual size_t GetResponseDataBufferSize() = 0; 145 }; 146 147 } // namespace OHOS::NWeb 148 149 #endif // NWEB_URL_RESOURCE_RESPONSE_H 150