forked from snowplow/snowplow-cpp-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcracked_url.cpp
More file actions
98 lines (77 loc) · 2.42 KB
/
Copy pathcracked_url.cpp
File metadata and controls
98 lines (77 loc) · 2.42 KB
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
/*
Copyright (c) 2016 Snowplow Analytics Ltd. All rights reserved.
This program is licensed to you under the Apache License Version 2.0,
and you may not use this file except in compliance with the Apache License Version 2.0.
You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing,
software distributed under the Apache License Version 2.0 is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
#include "cracked_url.hpp"
CrackedUrl::CrackedUrl(const string & url) {
string cleaned_url = url;
if (regex_match(cleaned_url, regex("^https?://.+")) == false) {
cleaned_url = string("http://") + url;
}
regex r_host("(https?)://([^\\s\\.]+\\.[^\\s/]+)(/.*)?");
regex r_hostname_port("([^:]+):(\\d+)");
smatch match;
if (regex_search(cleaned_url, match, r_host)) {
string protocol = match.str(1);
string hostname_port = match.str(2);
this->path = match.str(3);
smatch host_match;
if (regex_search(hostname_port, host_match, r_hostname_port)) {
this->hostname = host_match.str(1);
string port = host_match.str(2);
this->port = stoi(port);
this->use_default_port = false;
} else {
this->hostname = hostname_port; // it's just a hostname
this->port = 0;
this->use_default_port = true;
}
this->is_https = protocol == "https";
this->error_code = 0;
this->is_valid = true;
} else {
this->error_code = -1;
this->is_valid = false;
}
}
string CrackedUrl::get_hostname() {
return this->hostname;
}
string CrackedUrl::get_path() {
return this->path;
}
bool CrackedUrl::get_is_https() {
return this->is_https;
}
bool CrackedUrl::get_is_valid() {
return this->is_valid;
}
int CrackedUrl::get_error_code() {
return this->error_code;
}
unsigned int CrackedUrl::get_port() {
return this->port;
}
bool CrackedUrl::get_use_default_port() {
return this->use_default_port;
}
string CrackedUrl::to_string() {
stringstream s;
if (this->get_is_https()) {
s << "https://";
} else {
s << "http://";
}
s << this->get_hostname();
if (!this->get_use_default_port()) {
s << ":" << std::to_string(this->get_port());
}
s << this->get_path();
return s.str();
}