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 URI_JS_URI_H
17#define URI_JS_URI_H
18
19#include <bitset>
20#include <cstdlib>
21#include <regex>
22#include <string>
23#include <vector>
24#include "napi/native_api.h"
25#include "napi/native_node_api.h"
26
27namespace OHOS::Uri {
28    constexpr int MAX_BIT_SIZE = 128;
29    struct UriData {
30        int port = -1;
31        std::string scheme = "";
32        std::string userInfo = "";
33        std::string host = "";
34        std::string query = "";
35        std::string fragment = "";
36        std::string path = "";
37        std::string authority = "";
38        std::string SchemeSpecificPart = "";
39    };
40
41    class Uri {
42    public:
43        /**
44         * URI constructor, which is used to instantiate a URI object.
45         *
46         * @param input Constructs a URI by parsing a given string.
47         */
48        explicit Uri(const std::string input);
49
50        /**
51         * The destructor of the Uri.
52         */
53        virtual ~Uri() {}
54
55        /**
56         * Tests whether this URI is equivalent to other URI objects.
57         *
58         * @param other URI object to be compared
59         */
60        bool Equals(const Uri other) const;
61
62        /**
63         * Indicates whether this URI is an absolute URI.
64         */
65        bool IsAbsolute() const;
66
67        /**
68         * Determine whether parsing failed.
69         */
70        std::string IsFailed() const;
71
72        /**
73         * Returns the serialized URI as a string.
74         */
75        std::string ToString() const;
76
77        /**
78         * Indicates whether this URI is an relative URI.
79         */
80        bool IsRelative() const;
81
82        /**
83         * Indicates whether this URI is an opaque URI.
84         */
85        bool IsOpaque() const;
86
87        /**
88         * Indicates whether this URI is an hierarchical URI.
89         */
90        bool IsHierarchical() const;
91
92        /**
93         * Add key and value to Uri's query
94         */
95        std::string AddQueryValue(const std::string key, const std::string value) const;
96
97        /**
98         * Add pathSegment to Uri's segment
99         */
100        std::string AddSegment(const std::string pathSegment) const;
101
102        /**
103         * Gets the all Segment of the URI.
104         */
105        std::vector<std::string> GetSegment() const;
106
107        /**
108         * Normalize the path of this URI.
109         */
110        std::string Normalize() const;
111
112        /**
113         * Gets the protocol part of the URI.
114         */
115        std::string GetScheme() const;
116
117        /**
118         * Gets the decoding permission component part of this URI.
119         */
120        std::string GetAuthority() const;
121
122        /**
123         * Gets the decoding scheme-specific part of the URI.
124         */
125        std::string GetSsp() const;
126
127        /**
128         * Obtains the user information part of the URI.
129         */
130        std::string GetUserinfo() const;
131
132        /**
133         * Gets the hostname portion of the URI without a port.
134         */
135        std::string GetHost() const;
136
137        /**
138         * Gets the hostname portion of the URI without a port.
139         */
140        std::string GetPort() const;
141
142        /**
143         * Gets the path portion of the URI.
144         */
145        std::string GetPath() const;
146
147        /**
148         * Gets the query portion of the URI.
149         */
150        std::string GetQuery() const;
151
152        /**
153         * Gets the fragment part of the URI.
154         */
155        std::string GetFragment() const;
156
157        /**
158         * Clear Uri's query
159         */
160        std::string ClearQuery() const;
161
162    private:
163        void PreliminaryWork() const;
164        void AnalysisUri();
165        void SpecialPath();
166        void AnalysisFragment(size_t pos);
167        void AnalysisQuery(size_t pos);
168        void AnalysisScheme(size_t pos);
169        void AnalysisHostAndPath();
170        void AnalysisHost(bool isLawfulProt);
171        void AnalysisPath(size_t pos);
172        void AnalysisUserInfo(size_t pos);
173        void AnalysisIPV6();
174        void AssignSchemeSpecificPart();
175
176        bool CheckCharacter(std::string data, std::bitset<MAX_BIT_SIZE> rule, bool flag) const;
177        bool AnalysisPort(size_t pos);
178        bool AnalysisIPV4();
179
180        std::string Split(const std::string &path) const;
181        std::string BuildUriString(const std::string str, const std::string param) const;
182
183    private:
184        UriData uriData_;
185        std::string data_ {};
186        std::string inputUri_ {};
187        std::string errStr_ {};
188    };
189} // namespace OHOS::Uri
190#endif // URI_JS_URI_H
191