Debian Codegraph @

monero/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 bool parse_uri(const std::string uri, http::uri_content& content)
97 {
98
99 ///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=
100 content.m_query_params.clear();
101 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^([^?#]*)(\\?([^#]*))?(#(.*))?", boost::regex::icase | boost::regex::normal);
102
103 boost::smatch result;
104 if(!(boost::regex_search(uri, result, rexp_match_uri, boost::match_default) && result[0].matched))
105 {
106 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << uri);
107 content.m_path = uri;
108 return true;
109 }
110 if(result[1].matched)
111 {
112 content.m_path = result[1];
113 }
114 if(result[3].matched)
115 {
116 content.m_query = result[3];
117 }
118 if(result[5].matched)
119 {
120 content.m_fragment = result[5];
121 }
122 if(content.m_query.size())
123 {
124 parse_uri_query(content.m_query, content.m_query_params);
125 }
126 return true;
127 }
128
129 bool parse_url_ipv6(const std::string url_str, http::url_content& content)
130 {
131 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^(([^:]*?)://)?(\\[(.*)\\](:(\\d+))?)(.*)?", boost::regex::icase | boost::regex::normal);
132 // 12 3 4 5 6 7
133
134 content.port = 0;
135 boost::smatch result;
136 if(!(boost::regex_search(url_str, result, rexp_match_uri, boost::match_default) && result[0].matched))
137 {
138 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << rexp_match_uri);
139 //content.m_path = uri;
140 return false;
141 }
142 if(result[2].matched)
143 {
144 content.schema = result[2];
145 }
146 if(result[4].matched)
147 {
148 content.host = result[4];
149 }
150 else // if host not matched, matching should be considered failed
151 {
152 return false;
153 }
154 if(result[6].matched)
155 {
156 content.port = boost::lexical_cast<uint64_t>(result[6]);
157 }
158 if(result[7].matched)
159 {
160 content.uri = result[7];
161 return parse_uri(result[7], content.m_uri_content);
162 }
163
164 return true;
165 }
166
167 bool parse_url(const std::string url_str, http::url_content& content)
168 {
169
170 if (parse_url_ipv6(url_str, content)) return true;
171
172 ///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=
173 //STATIC_REGEXP_EXPR_1(rexp_match_uri, "^([^?#]*)(\\?([^#]*))?(#(.*))?", boost::regex::icase | boost::regex::normal);
174 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^(([^:]*?)://)?(([^/:]*)(:(\\d+))?)(.*)?", boost::regex::icase | boost::regex::normal);
175 // 12 34 5 6 7
176 content.port = 0;
177 boost::smatch result;
178 if(!(boost::regex_search(url_str, result, rexp_match_uri, boost::match_default) && result[0].matched))
179 {
180 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << rexp_match_uri);
181 //content.m_path = uri;
182 return true;
183 }
184 if(result[2].matched)
185 {
186 content.schema = result[2];
187 }
188 if(result[4].matched)
189 {
190 content.host = result[4];
191 }
192 if(result[6].matched)
193 {
194 content.port = boost::lexical_cast<uint64_t>(result[6]);
195 }
196 if(result[7].matched)
197 {
198 content.uri = result[7];
199 return parse_uri(result[7], content.m_uri_content);
200 }
201
202 return true;
203 }
204
205 }
206 }