• 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 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 
27 namespace 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          */
~Uri()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          * Normalize the path of this URI.
79          */
80         std::string Normalize() const;
81 
82         /**
83          * Gets the protocol part of the URI.
84          */
85         std::string GetScheme() const;
86 
87         /**
88          * Gets the decoding permission component part of this URI.
89          */
90         std::string GetAuthority() const;
91 
92         /**
93          * Gets the decoding scheme-specific part of the URI.
94          */
95         std::string GetSsp() const;
96 
97         /**
98          * Obtains the user information part of the URI.
99          */
100         std::string GetUserinfo() const;
101 
102         /**
103          * Gets the hostname portion of the URI without a port.
104          */
105         std::string GetHost() const;
106 
107         /**
108          * Gets the hostname portion of the URI without a port.
109          */
110         std::string GetPort() const;
111 
112         /**
113          * Gets the path portion of the URI.
114          */
115         std::string GetPath() const;
116 
117         /**
118          * Gets the query portion of the URI.
119          */
120         std::string GetQuery() const;
121 
122         /**
123          * Gets the fragment part of the URI.
124          */
125         std::string GetFragment() const;
126 
127     private:
128         void PreliminaryWork() const;
129         void AnalysisUri();
130         void SpecialPath();
131         void AnalysisFragment(size_t pos);
132         void AnalysisQuery(size_t pos);
133         void AnalysisScheme(size_t pos);
134         void AnalysisHostAndPath();
135         void AnalysisPath(size_t pos);
136         void AnalysisUserInfo(size_t pos);
137         void AnalysisIPV6();
138 
139         bool CheckCharacter(std::string data, std::bitset<MAX_BIT_SIZE> rule, bool flag) const;
140         bool AnalysisPort(size_t pos);
141         bool AnalysisIPV4();
142 
143         std::string Split(std::string path) const;
144 
145     private:
146         UriData uriData_;
147         std::string data_;
148         std::string inputUri_;
149         std::string errStr_;
150     };
151 } // namespace OHOS::Uri
152 #endif // URI_JS_URI_H_
153