• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum class NWebResponseDataType : int32_t {
32     NWEB_STRING_TYPE,
33     NWEB_FILE_TYPE,
34     NWEB_RESOURCE_URL_TYPE,
35 };
36 
37 class NWebUrlResourceResponse {
38 public:
39     /**
40      * @brief Construct a resource response with the given parameters.
41      *
42      * @param mime_type the resource response's MIME type, for example {
43      * "text/html"}.
44      * @param encoding the resource response's character encoding, for example
45      * {"utf-8"}.
46      * @param status_code the status code needs to be in the ranges [100, 299],
47      * [400, 599]. Causing a redirect by specifying a 3xx code is not supported.
48      * @param reason_phrase the phrase describing the status code, for example
49      * "OK". Must be non-empty.
50      * @param response_headers the resource response's headers represented as a
51      * mapping of header name -> header value.
52      * @param input_stream the input stream that provides the resource response's
53      * data.
54      */
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)55     NWebUrlResourceResponse(const std::string& mime_type,
56                             const std::string& encoding,
57                             const int status_code,
58                             const std::string& reason_phrase,
59                             const std::map<std::string, std::string>& response_headers,
60                             std::string& input_stream)
61         : mime_type_(mime_type),
62           encoding_(encoding),
63           status_code_(status_code),
64           reason_phrase_(reason_phrase),
65           response_headers_(response_headers),
66           input_stream_(input_stream) {}
67 
NWebUrlResourceResponse(const std::string & mime_type,const std::string & encoding,std::string & input_stream)68     NWebUrlResourceResponse(const std::string& mime_type,
69                             const std::string& encoding,
70                             std::string& input_stream)
71         : mime_type_(mime_type),
72           encoding_(encoding),
73           input_stream_(input_stream) {}
74 
75     ~NWebUrlResourceResponse() = default;
76 
77     /**
78      * @brief get input stream
79      *
80      * @retval inputstream string
81      */
ResponseData()82     const std::string& ResponseData()
83     {
84         return input_stream_;
85     }
86 
87     /**
88      * @brief set input stream
89      *
90      * @param input_stream set inputstream for example: fread(buf, 1, sizeof(buf),
91      * file)
92      */
PutResponseData(std::string & input_stream)93     void PutResponseData(std::string& input_stream)
94     {
95         input_stream_ = input_stream;
96         fd_ = 0;
97         isFileFd_ = false;
98         dataType_ = NWebResponseDataType::NWEB_STRING_TYPE;
99     }
100 
101     /**
102      * @brief Construct a resource response with the given parameters.
103      *
104      * @param encoding encoding { "utf-8" }
105      */
PutResponseEncoding(const std::string & encoding)106     void PutResponseEncoding(const std::string& encoding)
107     {
108         encoding_ = encoding;
109     }
110 
111     /**
112      * @brief get encoding
113      *
114      * @retval encoding the resource response's encoding
115      */
ResponseEncoding()116     std::string ResponseEncoding()
117     {
118         return encoding_;
119     }
120 
121     /**
122      * @brief Construct a resource response with the given parameters.
123      *
124      * @param mime_type mime_type{ "text/html" }
125      */
PutResponseMimeType(const std::string & mime_type)126     void PutResponseMimeType(const std::string& mime_type)
127     {
128         mime_type_ = mime_type;
129     }
130 
131     /**
132      * @brief Get mimetype
133      *
134      * @retval mimetype The resource response's MIME type
135      */
ResponseMimeType()136     std::string ResponseMimeType()
137     {
138         return mime_type_;
139     }
140 
141     /**
142      * @brief Set ResponseHeaders
143      *
144      * @param response_headers response header
145      */
PutResponseHeaders(const std::map<std::string,std::string> & response_headers)146     void PutResponseHeaders(const std::map<std::string, std::string>& response_headers)
147     {
148         response_headers_ = response_headers;
149     }
150 
151     /**
152      * @brief Get ResponseHeaders
153      *
154      * @retval response headers
155      */
ResponseHeaders()156     const std::map<std::string, std::string>& ResponseHeaders()
157     {
158         return response_headers_;
159     }
160 
161     /**
162      * @brief Set StatusCode And ReasonPhrase
163      *
164      * @param status_code status code
165      * @param reasonphrase reason phrase
166      */
PutResponseStateAndStatuscode(int status_code,std::string reason_phrase)167     void PutResponseStateAndStatuscode(int status_code,
168                                     std::string reason_phrase)
169     {
170         status_code_ = status_code;
171         reason_phrase_ = reason_phrase;
172     }
173 
174     /**
175      * @brief Get status code
176      *
177      * @retval status code
178     */
ResponseStatusCode()179     int ResponseStatusCode()
180     {
181         return status_code_;
182     }
183 
184     /**
185      * @brief Get ReasonPhrase
186      *
187      * @retval errorcode reason
188     */
ResponseStatus()189     std::string ResponseStatus()
190     {
191         return reason_phrase_;
192     }
193 
PutResponseDataStatus(bool isDataReady)194     void PutResponseDataStatus(bool isDataReady)
195     {
196         isDataReady_ = isDataReady;
197         if (isDataReady_ == true && readyCallback_ != nullptr) {
198             readyCallback_->Continue();
199             readyCallback_ = nullptr;
200         }
201     }
202 
ResponseDataStatus()203     bool ResponseDataStatus() const
204     {
205         return isDataReady_;
206     }
207 
ResponseIsFileHandle()208     bool ResponseIsFileHandle() const
209     {
210         return isFileFd_;
211     }
212 
PutResponseFileHandle(int fd)213     void PutResponseFileHandle(int fd)
214     {
215         dataType_ = NWebResponseDataType::NWEB_FILE_TYPE;
216         fd_ = fd;
217         isFileFd_ = true;
218         input_stream_.clear();
219     }
220 
ResponseFileHandle()221     int ResponseFileHandle() const
222     {
223         return fd_;
224     }
225 
PutResponseResourceUrl(const std::string & url)226     void PutResponseResourceUrl(const std::string& url)
227     {
228         resource_url_ = url;
229         dataType_ = NWebResponseDataType::NWEB_RESOURCE_URL_TYPE;
230     }
231 
ResponseResourceUrl()232     std::string ResponseResourceUrl() const
233     {
234         return resource_url_;
235     }
236 
ResponseDataType()237     NWebResponseDataType ResponseDataType() const
238     {
239         return dataType_;
240     }
241 
PutResponseReadyCallback(std::shared_ptr<NWebResourceReadyCallback> readyCallback)242     void PutResponseReadyCallback(std::shared_ptr<NWebResourceReadyCallback> readyCallback)
243     {
244         readyCallback_ = readyCallback;
245     }
246 
247 private:
248     std::string mime_type_;
249     std::string encoding_;
250     int status_code_ = 200;
251     std::string reason_phrase_;
252     std::map<std::string, std::string> response_headers_;
253     std::string input_stream_;
254     int fd_ = -1;
255     std::string resource_url_;
256     NWebResponseDataType dataType_ = NWebResponseDataType::NWEB_STRING_TYPE;
257     bool isFileFd_ = false;
258     bool isDataReady_ = true;
259     std::shared_ptr<NWebResourceReadyCallback> readyCallback_;
260 };
261 } // namespace OHOS::NWeb
262 
263 #endif // NWEB_URL_RESOURCE_RESPONSE_H
264