1 /*
2  * Copyright (c) 2021-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 OHOS_ABILITY_BASE_URI_H
17 #define OHOS_ABILITY_BASE_URI_H
18 
19 #include <string>
20 #include <vector>
21 #include "parcel.h"
22 
23 namespace OHOS {
24 class Uri : public Parcelable {
25 public:
26     explicit Uri(const std::string& uriString);
27     ~Uri();
28 
29     /**
30      * Get the Scheme part.
31      *
32      * @return the scheme string.
33      */
34     std::string GetScheme();
35 
36     /**
37      * Get the Ssp part.
38      *
39      * @return the SchemeSpecificPart string.
40      */
41     std::string GetSchemeSpecificPart();
42 
43     /**
44      * Get the GetAuthority part.
45      *
46      * @return the authority string.
47      */
48     std::string GetAuthority();
49 
50     /**
51      * Get the Host part.
52      *
53      * @return the host string.
54      */
55     std::string GetHost();
56 
57     /**
58      * Get the Port part.
59      *
60      * @return the port number.
61      */
62     int GetPort();
63 
64     /**
65      * Get the User part.
66      *
67      * @return the user string.
68      */
69     std::string GetUserInfo();
70 
71     /**
72      * Get the Query part.
73      *
74      * @return the query string.
75      */
76     std::string GetQuery();
77 
78     /**
79      * Get the Path part.
80      *
81      * @return the path string.
82      */
83     std::string GetPath();
84 
85     /**
86      * Get the path segments.
87      *
88      * @param the path segments of Uri.
89      */
90     void GetPathSegments(std::vector<std::string>& segments);
91 
92     /**
93      * Get the Fragment part.
94      *
95      * @return the fragment string.
96      */
97     std::string GetFragment();
98 
99     /**
100      * Returns true if this URI is hierarchical like "http://www.example.com".
101      * Absolute URIs are hierarchical if the scheme-specific part starts with a '/'.
102      * Relative URIs are always hierarchical.
103      *
104      * @return true if this URI is hierarchical, false if it's opaque.
105      */
106     bool IsHierarchical();
107 
108     /**
109      * Returns true if this URI is opaque like "mailto:nobody@ohos.com".
110      * The scheme-specific part of an opaque URI cannot start with a '/'.
111      *
112      * @return true if this URI is opaque, false if it's hierarchical.
113      */
114     bool IsOpaque();
115 
116     /**
117      * Returns true if this URI is absolute, i.e.&nbsp;if it contains an explicit scheme.
118      *
119      * @return true if this URI is absolute, false if it's relative.
120      */
121     bool IsAbsolute();
122 
123     /**
124      * Returns true if this URI is relative, i.e.&nbsp;if it doesn't contain an explicit scheme.
125      *
126      * @return true if this URI is relative, false if it's absolute.
127      */
128     bool IsRelative();
129 
130     /**
131      * Check whether the other is the same as this.
132      *
133      * @return true if the same string.
134      */
135     bool Equals(const Uri& other) const;
136 
137     /**
138      * Compare to other uri.
139      *
140      * @return the string compare result.
141      */
142     int CompareTo(const Uri& other) const;
143 
144     /**
145      * Convert to a string object.
146      *
147      * @return a string object.
148      */
149     std::string ToString() const;
150 
151     /**
152      * override the == method.
153      *
154      * @return true if the same content, false not the same content.
155      */
156     bool operator==(const Uri& other) const;
157 
158     /**
159      * Override Parcelable' interface.
160      *
161      * @return true if parcel write success, false write fail.
162      */
163     virtual bool Marshalling(Parcel& parcel) const override;
164 
165     /**
166      * Support the Ummarshlling method for construct object by  Parcel read.
167      *
168      * @return the uri object address.
169      */
170     static Uri* Unmarshalling(Parcel& parcel);
171 
172 private:
173     bool CheckScheme();
174     std::string ParseScheme();
175     std::string ParseSsp();
176     std::string ParseAuthority();
177     std::string ParseUserInfo();
178     std::string ParseHost();
179     int ParsePort();
180     std::string ParsePath(size_t ssi);
181     std::string ParsePath();
182     std::string ParseQuery();
183     std::string ParseFragment();
184 
185     /**
186     * Finds the first ':'.
187     *
188     * @return the pos of the ':', string::npos if none found.
189     */
190     size_t FindSchemeSeparator();
191 
192     /**
193      * Finds the first '#'.
194      *
195      * @return the pos of the '#', string::npos if none found.
196      */
197     size_t FindFragmentSeparator();
198 
199     std::string uriString_;
200     std::string scheme_;
201     std::string ssp_;
202     std::string authority_;
203     std::string host_;
204     int port_;
205     std::string userInfo_;
206     std::string query_;
207     std::string path_;
208     std::string fragment_;
209     size_t cachedSsi_;
210     size_t cachedFsi_;
211 };
212 } // namespace OHOS
213 #endif // OHOS_ABILITY_BASE_URI_H_
214