1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Hechong.xyf
17 * \date Nov 2020
18 * \brief Interface of URI Parser
19 */
20
21#ifndef __AILEGO_ENCODING_URI_H__
22#define __AILEGO_ENCODING_URI_H__
23
24#include <string>
25
26namespace ailego {
27
28/*! URI module
29 */
30class Uri {
31 public:
32 //! Constructor (STL-string)
33 Uri(const std::string &str) {
34 Uri::Parse(str, this);
35 }
36
37 //! Constructor (C-string)
38 Uri(const char *str) {
39 Uri::Parse(str, this);
40 }
41
42 //! Constructor (Empty)
43 Uri(void) {}
44
45 //! Test if the Uri is valid
46 bool is_valid(void) const {
47 return valid_;
48 }
49
50 //! Retrieve the scheme part of URI
51 const std::string &scheme(void) const {
52 return scheme_;
53 }
54
55 //! Retrieve the authority part of URI
56 const std::string &authority(void) const {
57 return authority_;
58 }
59
60 //! Retrieve the username part of URI
61 const std::string &username(void) const {
62 return username_;
63 }
64
65 //! Retrieve the password part of URI
66 const std::string &password(void) const {
67 return password_;
68 }
69
70 //! Retrieve the host part of URI
71 const std::string &host(void) const {
72 return host_;
73 }
74
75 //! Retrieve the port part of URI
76 int32_t port(void) const {
77 return port_;
78 }
79
80 //! Retrieve the path part of URI
81 const std::string &path(void) const {
82 return path_;
83 }
84
85 //! Retrieve the query part of URI
86 const std::string &query(void) const {
87 return query_;
88 }
89
90 //! Retrieve the fragment part of URI
91 const std::string &fragment(void) const {
92 return fragment_;
93 }
94
95 //! Parse a URI string (C-string)
96 static bool Parse(const char *str, Uri *out);
97
98 //! Parse a URI string (STL-string)
99 static bool Parse(const std::string &str, Uri *out) {
100 return Uri::Parse(str.c_str(), out);
101 }
102
103 private:
104 bool valid_{false};
105 std::string scheme_{};
106 std::string authority_{};
107 std::string username_{};
108 std::string password_{};
109 std::string host_{};
110 int32_t port_{};
111 std::string path_{};
112 std::string query_{};
113 std::string fragment_{};
114};
115
116
117} // namespace ailego
118
119#endif //__AILEGO_ENCODING_URI_H__
120