File: uri.md

package info (click to toggle)
jsoncons 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,584 kB
  • sloc: cpp: 136,382; sh: 33; makefile: 5
file content (199 lines) | stat: -rw-r--r-- 6,062 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
### jsoncons::uri

```cpp
#include <jsoncons/utility/uri.hpp>

```
The class `uri` represents a Uniform Resource Identifier (URI) reference.

#### Constructor

    uri();

    explicit uri(jsoncons::string_view str);
Constructs a `uri` by parsing the given string.

    uri(jsoncons::string_view scheme,
        jsoncons::string_view userinfo,
        jsoncons::string_view host,
        jsoncons::string_view port,
        jsoncons::string_view path,
        jsoncons::string_view query = "",
        jsoncons::string_view fragment = "")
Constructs a `uri` from the given parts. It is assumed that the parts
are not already %-encoded, encoding is performed during construction.
 
    uri(const uri& other, uri_fragment_part_t, jsoncons::string_view fragment);
Constructs a `uri` from `other`, replacing it's fragment part with `fragment`.
It is assumed that `fragment` is not already %-encoded, encoding is performed 
during construction.

    uri(const uri& other);
Copy constructor.

    uri(uri&& other) noexcept;
Move constructor.

#### Assignment
    
    uri& operator=(const uri& other);
Copy assignment.
    
    uri& operator=(uri&& other) noexcept;
Move assignment.

#### Member functions

    jsoncons::string_view scheme() const noexcept;
Returns the scheme part of this URI. The scheme is the first part of the URI, before the `:` character.

    std::string userinfo() const;
Returns the decoded userinfo part of this URI.

    jsoncons::string_view encoded_userinfo() const noexcept;
Returns the encoded userinfo part of this URI.

    jsoncons::string_view host() const noexcept;
Returns the host part of this URI.

    jsoncons::string_view port() const noexcept;
Returns the port number of this URI.

    std::string authority() const;
Returns the decoded authority part of this URI.

    jsoncons::string_view encoded_authority() const noexcept;
Returns the encoded authority part of this URI.

    std::string path() const;
Returns the decoded path part of this URI.

    jsoncons::string_view encoded_path() const noexcept;
Returns the encoded path part of this URI.

    std::string query() const;
Returns the decoded query part of this URI.

    jsoncons::string_view encoded_query() const noexcept;
Returns the encoded query part of this URI.

    std::string fragment() const;
Returns the decoded fragment part of this URI.

    jsoncons::string_view encoded_fragment() const noexcept;
Returns the encoded fragment part of this URI.

    bool is_absolute() const noexcept;
Returns true if this URI is absolute, false if it is relative.
An absolute URI has a scheme part.

    bool is_opaque() const noexcept;
Returns true if this URI is opaque, otherwise false.
An opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character ('/').

    uri base() const noexcept;
Returns the base uri. The base uri includes the scheme, userinfo, host, port, and path parts,
but not the query or fragment.     

    bool has_scheme() const noexcept;
Returns true if this URI has a scheme part, otherwise false.

    bool has_userinfo() const noexcept;
Returns true if this URI has a userinfo part, otherwise false.

    bool has_authority() const noexcept;
Returns true if this URI has an authority part, otherwise false.

    bool has_host() const noexcept;
Returns true if this URI has a host part, otherwise false.

    bool has_port() const noexcept;
Returns true if this URI has a port number, otherwise false.

    bool has_path() const noexcept;
Returns true if this URI has a path part, otherwise false.

    bool has_query() const noexcept;
Returns true if this URI has a query part, otherwise false.

    bool has_fragment() const noexcept;
Returns true if this URI has a fragment part, otherwise false.

    uri resolve(jsoncons::string_view reference) const;
Resolve `reference` as a URI relative to this URI.

    uri resolve(const uri& reference) const;
Resolve `reference` as a URI relative to this URI.

    const std::string& string() const noexcept;
Returns a URI string.

    static uri parse(jsoncons::string_view str, std::error_code& ec);
Creates a `uri` by parsing the given string. If a parse error is
encountered, returns a default constructed `uri` and sets `ec`.

    friend std::ostream& operator<<(std::ostream& os, const uri& uri_);

### Examples

#### Parts
  
```cpp
#include <jsoncons/utility/uri.hpp>

int main()
{
    jsoncons::uri uri{ "https://github.com/danielaparker/jsoncons/tree/master/doc/ref/corelib/utility/uri.md#Examples" };

    std::cout << "uri: " << uri << "\n";
    std::cout << "base: " << uri.base() << "\n";
    std::cout << "scheme: " << uri.scheme() << "\n";
    std::cout << "authority: " << uri.authority() << "\n";
    std::cout << "userinfo: " << uri.userinfo() << "\n";
    std::cout << "path: " << uri.path() << "\n";
    std::cout << "query: " << uri.query() << "\n";
    std::cout << "fragment: " << uri.fragment() << "\n";
}
```

Output:

```
uri: https://github.com/danielaparker/jsoncons/tree/master/doc/ref/corelib/utility/uri.md#Examples
base: https://github.com/danielaparker/jsoncons/tree/master/doc/ref/corelib/utility/uri.md
scheme: https
authority: github.com
userinfo:
path: /danielaparker/jsoncons/tree/master/doc/ref/corelib/utility/uri.md
query:
fragment: Examples
```

#### Resolve reference relative to a base URI
  
```cpp
#include <jsoncons/utility/uri.hpp>

int main()
{
    jsoncons::uri uri{ "https://github.com/danielaparker/jsoncons/" };

    auto uri1 = uri.resolve("tree/master/doc/ref/corelib/utility/uri.md#Examples");
    std::cout << "(1) " << uri1 << "\n";

    auto uri2 = uri1.resolve("../../../../../../blob/master/include/jsoncons/utility/uri.hpp");
    std::cout << "(2) " << uri2 << "\n";

    auto uri3 = uri2.resolve("file:///~calendar");
    std::cout << "(3) " << uri3 << "\n";
}
```

Output:

```
(1) https://github.com/danielaparker/jsoncons/tree/master/doc/ref/corelib/utility/uri.md#Examples
(2) https://github.com/danielaparker/jsoncons/blob/master/include/jsoncons/utility/uri.hpp
(3) file:///~calendar
```