Debian Codegraph @

contrib/epee/src/net_parse_helpers.cpp

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27 #include "net/net_parse_helpers.h"
28 #include "net/http_base.h"
29 #include "misc_log_ex.h"
30 #include "reg_exp_definer.h"
31 #include <boost/lexical_cast.hpp>
32
33 #undef MONERO_DEFAULT_LOG_CATEGORY
34 #define MONERO_DEFAULT_LOG_CATEGORY "net"
35
36 namespace epee
37 {
38 namespace net_utils
39 {
40
41 static bool parse_uri_query(const std::string& query, std::list<std::pair<std::string, std::string> >& params)
42 {
43 enum state
44 {
45 st_param_name,
46 st_param_val
47 };
48 state st = st_param_name;
49 std::string::const_iterator start_it = query.begin();
50 std::pair<std::string, std::string> e;
51 for(std::string::const_iterator it = query.begin(); it != query.end(); ++it)
52 {
53 switch(st)
54 {
55 case st_param_name:
56 if(*it == '=')
57 {
58 e.first.assign(start_it, it);
59 start_it = it;++start_it;
60 st = st_param_val;
61 }
62 break;
63 case st_param_val:
64 if(*it == '&')
65 {
66 e.second.assign(start_it, it);
67 start_it = it;++start_it;
68 params.push_back(e);
69 e.first.clear();e.second.clear();
70 st = st_param_name;
71 }
72 break;
73 default:
74 LOG_ERROR("Unknown state " << (int)st);
75 return false;
76 }
77 }
78 if(st == st_param_name)
79 {
80 if(start_it != query.end())
81 {
82 e.first.assign(start_it, query.end());
83 params.push_back(e);
84 }
85 }else
86 {
87 if(start_it != query.end())
88 e.second.assign(start_it, query.end());
89
90 if(e.first.size())
91 params.push_back(e);
92 }
93 return true;
94 }
95
96 static bool parse_port(const std::string& port_str, uint64_t& out_port)
97 {
98 out_port = 0;
99 return boost::conversion::try_lexical_convert(port_str, out_port) && out_port <= 65535;
100 }
101
102 bool parse_uri(const std::string uri, http::uri_content& content)
103 {
104
105 ///iframe_test.html?api_url=http://api.vk.com/api.php&api_id=3289090&api_settings=1&viewer_id=562964060&viewer_type=0&sid=0aad8d1c5713130f9ca0076f2b7b47e532877424961367d81e7fa92455f069be7e21bc3193cbd0be11895&secret=368ebbc0ef&access_token=668bc03f43981d883f73876ffff4aa8564254b359cc745dfa1b3cde7bdab2e94105d8f6d8250717569c0a7&user_id=0&group_id=0&is_app_user=1&auth_key=d2f7a895ca5ff3fdb2a2a8ae23fe679a&language=0&parent_language=0&ad_info=ElsdCQBaQlxiAQRdFUVUXiN2AVBzBx5pU1BXIgZUJlIEAWcgAUoLQg==&referrer=unknown&lc_name=9834b6a3&hash=
106 content.m_query_params.clear();
107 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^([^?#]*)(\\?([^#]*))?(#(.*))?", boost::regex::icase | boost::regex::normal);
108
109 boost::smatch result;
110 if(!(boost::regex_search(uri, result, rexp_match_uri, boost::match_default) && result[0].matched))
111 {
112 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << uri);
113 content.m_path = uri;
114 return true;
115 }
116 if(result[1].matched)
117 {
118 content.m_path = result[1];
119 }
120 if(result[3].matched)
121 {
122 content.m_query = result[3];
123 }
124 if(result[5].matched)
125 {
126 content.m_fragment = result[5];
127 }
128 if(content.m_query.size())
129 {
130 parse_uri_query(content.m_query, content.m_query_params);
131 }
132 return true;
133 }
134
135 bool parse_url_ipv6(const std::string url_str, http::url_content& content)
136 {
137 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^(([^:]*?)://)?(\\[(.*)\\](:(\\d+))?)(.*)?", boost::regex::icase | boost::regex::normal);
138 // 12 3 4 5 6 7
139
140 content.port = 0;
141 boost::smatch result;
142 if(!(boost::regex_search(url_str, result, rexp_match_uri, boost::match_default) && result[0].matched))
143 {
144 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << rexp_match_uri);
145 //content.m_path = uri;
146 return false;
147 }
148 if(result[2].matched)
149 {
150 content.schema = result[2];
151 }
152 if(result[4].matched)
153 {
154 content.host = result[4];
155 }
156 else // if host not matched, matching should be considered failed
157 {
158 return false;
159 }
160 if(result[6].matched)
161 {
162 if (!parse_port(result[6].str(), content.port))
163 return false;
164 }
165 if(result[7].matched)
166 {
167 content.uri = result[7];
168 return parse_uri(result[7], content.m_uri_content);
169 }
170
171 return true;
172 }
173
174 bool parse_url(const std::string url_str, http::url_content& content)
175 {
176
177 if (parse_url_ipv6(url_str, content)) return true;
178
179 ///iframe_test.html?api_url=http://api.vk.com/api.php&api_id=3289090&api_settings=1&viewer_id=562964060&viewer_type=0&sid=0aad8d1c5713130f9ca0076f2b7b47e532877424961367d81e7fa92455f069be7e21bc3193cbd0be11895&secret=368ebbc0ef&access_token=668bc03f43981d883f73876ffff4aa8564254b359cc745dfa1b3cde7bdab2e94105d8f6d8250717569c0a7&user_id=0&group_id=0&is_app_user=1&auth_key=d2f7a895ca5ff3fdb2a2a8ae23fe679a&language=0&parent_language=0&ad_info=ElsdCQBaQlxiAQRdFUVUXiN2AVBzBx5pU1BXIgZUJlIEAWcgAUoLQg==&referrer=unknown&lc_name=9834b6a3&hash=
180 //STATIC_REGEXP_EXPR_1(rexp_match_uri, "^([^?#]*)(\\?([^#]*))?(#(.*))?", boost::regex::icase | boost::regex::normal);
181 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^(([^:]*?)://)?(([^/:]*)(:(\\d+))?)(.*)?", boost::regex::icase | boost::regex::normal);
182 // 12 34 5 6 7
183 content.port = 0;
184 boost::smatch result;
185 if(!(boost::regex_search(url_str, result, rexp_match_uri, boost::match_default) && result[0].matched))
186 {
187 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << rexp_match_uri);
188 //content.m_path = uri;
189 return true;
190 }
191 if(result[2].matched)
192 {
193 content.schema = result[2];
194 }
195 if(result[4].matched)
196 {
197 content.host = result[4];
198 }
199 if(result[6].matched)
200 {
201 if (!parse_port(result[6].str(), content.port))
202 return false;
203 }
204 if(result[7].matched)
205 {
206 content.uri = result[7];
207 return parse_uri(result[7], content.m_uri_content);
208 }
209
210 return true;
211 }
212
213 }
214 }