forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uriparser.cpp
426 lines (364 loc) · 9.98 KB
/
uriparser.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2018 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
// STL includes
#include <algorithm>
#include <map>
#include <string>
#include "uriparser.hpp"
#ifdef TEST
#define TEST1 1
#endif
#ifdef TEST1
#include <iostream>
#endif
using namespace std;
map<string, UriParser::Type> g_types;
// Map construction using the initializer list is only available starting from C++11.
// This dummy structure is used instead.
struct UriParserInit
{
UriParserInit()
{
g_types["file"] = UriParser::FILE;
g_types["udp"] = UriParser::UDP;
g_types["tcp"] = UriParser::TCP;
g_types["srt"] = UriParser::SRT;
g_types["rtmp"] = UriParser::RTMP;
g_types["http"] = UriParser::HTTP;
g_types["rtp"] = UriParser::RTP;
g_types[""] = UriParser::UNKNOWN;
}
} g_uriparser_init;
UriParser::UriParser(const string& strUrl, DefaultExpect exp)
: m_uriType(UNKNOWN)
{
m_expect = exp;
Parse(strUrl, exp);
}
UriParser::~UriParser(void)
{
}
string UriParser::makeUri()
{
// Reassemble parts into the URI
string prefix = "";
if (m_proto != "")
{
prefix = m_proto + "://";
}
std::ostringstream out;
out << prefix << m_host;
if ((m_port == "" || m_port == "0") && m_expect == EXPECT_FILE)
{
// Do not add port
}
else
{
out << ":" << m_port;
}
if (m_path != "")
{
if (m_path[0] != '/')
out << "/";
out << m_path;
}
if (!m_mapQuery.empty())
{
out << "?";
query_it i = m_mapQuery.begin();
for (;;)
{
out << i->first << "=" << i->second;
++i;
if (i == m_mapQuery.end())
break;
out << "&";
}
}
m_origUri = out.str();
return m_origUri;
}
string UriParser::proto(void) const
{
return m_proto;
}
UriParser::Type UriParser::type() const
{
return m_uriType;
}
string UriParser::host(void) const
{
return m_host;
}
string UriParser::port(void) const
{
return m_port;
}
unsigned short int UriParser::portno(void) const
{
// This returns port in numeric version. Fallback to 0.
try
{
int i = atoi(m_port.c_str());
if ( i <= 0 || i > 65535 )
return 0;
return i;
}
catch (...)
{
return 0;
}
}
string UriParser::path(void) const
{
return m_path;
}
string UriParser::queryValue(const string& strKey) const
{
return m_mapQuery.at(strKey);
}
// NOTE: handles percent encoded single byte ASCII / Latin-1 characters but not unicode characters and encodings
static string url_decode(const string& str)
{
string ret;
size_t prev_idx;
size_t idx;
for (prev_idx = 0; string::npos != (idx = str.find('%', prev_idx)); prev_idx = idx + 3)
{
char tmp[3];
unsigned hex;
if (idx + 2 >= str.size()) // bad percent encoding
break;
tmp[0] = str[idx + 1];
tmp[1] = str[idx + 2];
tmp[2] = '\0';
if (!isxdigit((unsigned char) tmp[0]) || // bad percent encoding
!isxdigit((unsigned char) tmp[1]) ||
1 != sscanf(tmp, "%x", &hex) ||
0 == hex)
break;
ret += str.substr(prev_idx, idx - prev_idx);
ret += (char) hex;
}
ret += str.substr(prev_idx, str.size() - prev_idx);
return ret;
}
void UriParser::Parse(const string& strUrl, DefaultExpect exp)
{
int iQueryStart = -1;
size_t idx = strUrl.find("?");
if (idx != string::npos)
{
m_host = strUrl.substr(0, idx);
iQueryStart = (int)(idx + 1);
}
else
{
m_host = strUrl;
}
idx = m_host.find("://");
if (idx != string::npos)
{
m_proto = m_host.substr(0, idx);
transform(m_proto.begin(), m_proto.end(), m_proto.begin(), [](char c){ return tolower(c); });
m_host = m_host.substr(idx + 3, m_host.size() - (idx + 3));
}
// Handle the IPv6 specification in square brackets.
// This actually handles anything specified in [] so potentially
// you can also specify the usual hostname here as well. If the
// whole host results to have [] at edge positions, they are stripped,
// otherwise they remain. In both cases the search for the colon
// separating the port specification starts only after ].
const size_t i6pos = m_host.find("[");
size_t i6end = string::npos;
// Search for the "path" part only behind the closed bracket,
// if both open and close brackets were found
size_t path_since = 0;
if (i6pos != string::npos)
{
i6end = m_host.find("]", i6pos);
if (i6end != string::npos)
path_since = i6end;
}
idx = m_host.find("/", path_since);
if (idx != string::npos)
{
m_path = m_host.substr(idx, m_host.size() - idx);
m_host = m_host.substr(0, idx);
}
// Check special things in the HOST entry.
size_t atp = m_host.find('@');
if (atp != string::npos)
{
string realhost = m_host.substr(atp+1);
string prehost;
if ( atp > 0 )
{
prehost = m_host.substr(0, atp-0);
size_t colon = prehost.find(':');
if ( colon != string::npos )
{
string pw = prehost.substr(colon+1);
string user;
if ( colon > 0 )
user = prehost.substr(0, colon-0);
m_mapQuery["user"] = user;
m_mapQuery["password"] = pw;
}
else
{
m_mapQuery["user"] = prehost;
}
}
else
{
m_mapQuery["multicast"] = "1";
}
m_host = realhost;
}
bool stripbrackets = false;
size_t hostend = 0;
if (i6pos != string::npos)
{
// IPv6 IP address. Find the terminating ]
hostend = m_host.find("]", i6pos);
idx = m_host.rfind(":");
if (hostend != string::npos)
{
// Found the end. But not necessarily it was
// at the beginning. If it was at the beginning,
// strip them from the host name.
size_t lasthost = idx;
if (idx != string::npos && idx < hostend)
{
idx = string::npos;
lasthost = m_host.size();
}
if (i6pos == 0 && hostend == lasthost - 1)
{
stripbrackets = true;
}
}
}
else
{
idx = m_host.rfind(":");
}
if (idx != string::npos)
{
m_port = m_host.substr(idx + 1, m_host.size() - (idx + 1));
// Extract host WITHOUT stripping brackets
m_host = m_host.substr(0, idx);
}
if (stripbrackets)
{
if (!hostend)
hostend = m_host.size() - 1;
m_host = m_host.substr(1, hostend - 1);
}
if ( m_port == "" && m_host != "" )
{
// Check if the host-but-no-port has specified
// a single integer number. If so
// We need to use C86 strtol, cannot use C++11
const char* beg = m_host.c_str();
const char* end = m_host.c_str() + m_host.size();
char* eos = 0;
long val = strtol(beg, &eos, 10);
if ( val > 0 && eos == end )
{
m_port = m_host;
m_host = "";
}
}
string strQueryPair;
while (iQueryStart > -1)
{
idx = strUrl.find("&", iQueryStart);
if (idx != string::npos)
{
strQueryPair = strUrl.substr(iQueryStart, idx - iQueryStart);
iQueryStart = (int)(idx + 1);
}
else
{
strQueryPair = strUrl.substr(iQueryStart, strUrl.size() - iQueryStart);
iQueryStart = (int)idx;
}
idx = strQueryPair.find("=");
if (idx != string::npos)
{
m_mapQuery[url_decode(strQueryPair.substr(0, idx))] = url_decode(strQueryPair.substr(idx + 1, strQueryPair.size() - (idx + 1)));
}
}
if (m_proto == "file")
{
if ( m_path.size() > 3 && m_path.substr(0, 3) == "/./" )
m_path = m_path.substr(3);
}
// Post-parse fixes
// Treat empty protocol as a file. In this case, merge the host and path.
if (exp == EXPECT_FILE && m_proto == "" && m_port == "")
{
m_proto = "file";
m_path = m_host + m_path;
m_host = "";
}
const auto proto_it = g_types.find(m_proto);
// Default-constructed UNKNOWN will be used if not found.
if (proto_it != g_types.end())
{
m_uriType = proto_it->second;
}
m_origUri = strUrl;
}
#ifdef TEST
#include <vector>
using namespace std;
int main( int argc, char** argv )
{
if ( argc < 2 )
{
return 0;
}
UriParser parser (argv[1], UriParser::EXPECT_HOST);
std::vector<std::string> args;
if (argc > 2)
{
copy(argv+2, argv+argc, back_inserter(args));
}
(void)argc;
cout << "PARSING URL: " << argv[1] << endl;
cout << "SCHEME INDEX: " << int(parser.type()) << endl;
cout << "PROTOCOL: " << parser.proto() << endl;
cout << "HOST: " << parser.host() << endl;
cout << "PORT (string): " << parser.port() << endl;
cout << "PORT (numeric): " << parser.portno() << endl;
cout << "PATH: " << parser.path() << endl;
cout << "PARAMETERS:\n";
for (auto& p: parser.parameters())
{
cout << "\t" << p.first << " = " << p.second << endl;
}
if (!args.empty())
{
for (string& s: args)
{
vector<string> keyval;
Split(s, '=', back_inserter(keyval));
if (keyval.size() < 2)
keyval.push_back("");
parser[keyval[0]] = keyval[1];
}
cout << "REASSEMBLED: " << parser.makeUri() << endl;
}
return 0;
}
#endif