• 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 class NWebUrlResourceResponse {
24 public:
25     /**
26      * @brief Construct a resource response with the given parameters.
27      *
28      * @param mime_type the resource response's MIME type, for example {
29      * "text/html"}.
30      * @param encoding the resource response's character encoding, for example
31      * {"utf-8"}.
32      * @param status_code the status code needs to be in the ranges [100, 299],
33      * [400, 599]. Causing a redirect by specifying a 3xx code is not supported.
34      * @param reason_phrase the phrase describing the status code, for example
35      * "OK". Must be non-empty.
36      * @param response_headers the resource response's headers represented as a
37      * mapping of header name -> header value.
38      * @param input_stream the input stream that provides the resource response's
39      * data.
40      */
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)41     NWebUrlResourceResponse(const std::string& mime_type,
42                             const std::string& encoding,
43                             const int status_code,
44                             const std::string& reason_phrase,
45                             const std::map<std::string, std::string>& response_headers,
46                             std::string& input_stream)
47         : mime_type_(mime_type),
48           encoding_(encoding),
49           status_code_(status_code),
50           reason_phrase_(reason_phrase),
51           response_headers_(response_headers),
52           input_stream_(input_stream) {}
53 
NWebUrlResourceResponse(const std::string & mime_type,const std::string & encoding,std::string & input_stream)54     NWebUrlResourceResponse(const std::string& mime_type,
55                             const std::string& encoding,
56                             std::string& input_stream)
57         : mime_type_(mime_type),
58           encoding_(encoding),
59           input_stream_(input_stream) {}
60 
61     ~NWebUrlResourceResponse() = default;
62 
63     /**
64      * @brief get input stream
65      *
66      * @retval inputstream string
67      */
ResponseData()68     const std::string& ResponseData()
69     {
70         return input_stream_;
71     }
72 
73     /**
74      * @brief set input stream
75      *
76      * @param input_stream set inputstream for example: fread(buf, 1, sizeof(buf),
77      * file)
78      */
PutResponseData(std::string & input_stream)79     void PutResponseData(std::string& input_stream)
80     {
81         input_stream_ = input_stream;
82     }
83 
84     /**
85      * @brief Construct a resource response with the given parameters.
86      *
87      * @param encoding encoding { "utf-8" }
88      */
PutResponseEncoding(const std::string & encoding)89     void PutResponseEncoding(const std::string& encoding)
90     {
91         encoding_ = encoding;
92     }
93 
94     /**
95      * @brief get encoding
96      *
97      * @retval encoding the resource response's encoding
98      */
ResponseEncoding()99     std::string ResponseEncoding()
100     {
101         return encoding_;
102     }
103 
104     /**
105      * @brief Construct a resource response with the given parameters.
106      *
107      * @param mime_type mime_type{ "text/html" }
108      */
PutResponseMimeType(const std::string & mime_type)109     void PutResponseMimeType(const std::string& mime_type)
110     {
111         mime_type_ = mime_type;
112     }
113 
114     /**
115      * @brief Get mimetype
116      *
117      * @retval mimetype The resource response's MIME type
118      */
ResponseMimeType()119     std::string ResponseMimeType()
120     {
121         return mime_type_;
122     }
123 
124     /**
125      * @brief Set ResponseHeaders
126      *
127      * @param response_headers response header
128      */
PutResponseHeaders(const std::map<std::string,std::string> & response_headers)129     void PutResponseHeaders(const std::map<std::string, std::string>& response_headers)
130     {
131         response_headers_ = response_headers;
132     }
133 
134     /**
135      * @brief Get ResponseHeaders
136      *
137      * @retval response headers
138      */
ResponseHeaders()139     const std::map<std::string, std::string>& ResponseHeaders()
140     {
141         return response_headers_;
142     }
143 
144     /**
145      * @brief Set StatusCode And ReasonPhrase
146      *
147      * @param status_code status code
148      * @param reasonphrase reason phrase
149      */
PutResponseStateAndStatuscode(int status_code,std::string reason_phrase)150     void PutResponseStateAndStatuscode(int status_code,
151                                     std::string reason_phrase)
152     {
153         status_code_ = status_code;
154         reason_phrase_ = reason_phrase;
155     }
156 
157     /**
158      * @brief Get status code
159      *
160      * @retval status code
161     */
ResponseStatusCode()162     int ResponseStatusCode()
163     {
164         return status_code_;
165     }
166 
167     /**
168      * @brief Get ReasonPhrase
169      *
170      * @retval errorcode reason
171     */
ResponseStatus()172     std::string ResponseStatus()
173     {
174         return reason_phrase_;
175     }
176 
177 private:
178     std::string mime_type_;
179     std::string encoding_;
180     int status_code_ = 200;
181     std::string reason_phrase_;
182     std::map<std::string, std::string> response_headers_;
183     std::string& input_stream_;
184 };
185 } // namespace OHOS::NWeb
186 
187 #endif // NWEB_URL_RESOURCE_RESPONSE_H
188