Electroneum
Loading...
Searching...
No Matches
net_parse_helpers.h
Go to the documentation of this file.
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
28
29
30#pragma once
31#include "http_base.h"
32#include "reg_exp_definer.h"
33
34#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
35#define ELECTRONEUM_DEFAULT_LOG_CATEGORY "net"
36
37namespace epee
38{
39namespace net_utils
40{
41
42 inline bool parse_uri_query(const std::string& query, std::list<std::pair<std::string, std::string> >& params)
43 {
44 enum state
45 {
46 st_param_name,
47 st_param_val
48 };
49 state st = st_param_name;
50 std::string::const_iterator start_it = query.begin();
51 std::pair<std::string, std::string> e;
52 for(std::string::const_iterator it = query.begin(); it != query.end(); it++)
53 {
54 switch(st)
55 {
56 case st_param_name:
57 if(*it == '=')
58 {
59 e.first.assign(start_it, it);
60 start_it = it;++start_it;
61 st = st_param_val;
62 }
63 break;
64 case st_param_val:
65 if(*it == '&')
66 {
67 e.second.assign(start_it, it);
68 start_it = it;++start_it;
69 params.push_back(e);
70 e.first.clear();e.second.clear();
71 st = st_param_name;
72 }
73 break;
74 default:
75 LOG_ERROR("Unknown state " << (int)st);
76 return false;
77 }
78 }
79 if(st == st_param_name)
80 {
81 if(start_it != query.end())
82 {
83 e.first.assign(start_it, query.end());
84 params.push_back(e);
85 }
86 }else
87 {
88 if(start_it != query.end())
89 e.second.assign(start_it, query.end());
90
91 if(e.first.size())
92 params.push_back(e);
93 }
94 return true;
95 }
96
97 inline
98 bool parse_uri(const std::string uri, http::uri_content& content)
99 {
100
102 content.m_query_params.clear();
103 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^([^?#]*)(\\?([^#]*))?(#(.*))?", boost::regex::icase | boost::regex::normal);
104
105 boost::smatch result;
106 if(!(boost::regex_search(uri, result, rexp_match_uri, boost::match_default) && result[0].matched))
107 {
108 LOG_PRINT_L1("[PARSE URI] regex not matched for uri: " << uri);
109 content.m_path = uri;
110 return true;
111 }
112 if(result[1].matched)
113 {
114 content.m_path = result[1];
115 }
116 if(result[3].matched)
117 {
118 content.m_query = result[3];
119 }
120 if(result[5].matched)
121 {
122 content.m_fragment = result[5];
123 }
124 if(content.m_query.size())
125 {
126 parse_uri_query(content.m_query, content.m_query_params);
127 }
128 return true;
129 }
130
131
132 inline
133 bool parse_url(const std::string url_str, http::url_content& content)
134 {
135
137 //STATIC_REGEXP_EXPR_1(rexp_match_uri, "^([^?#]*)(\\?([^#]*))?(#(.*))?", boost::regex::icase | boost::regex::normal);
138 STATIC_REGEXP_EXPR_1(rexp_match_uri, "^((.*?)://)?(([^/:]*)(:(\\d+))?)(.*)?", boost::regex::icase | boost::regex::normal);
139 // 12 34 5 6 7
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 true;
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 if(result[6].matched)
157 {
158 content.port = boost::lexical_cast<uint64_t>(result[6]);
159 }
160 if(result[7].matched)
161 {
162 content.uri = result[7];
163 return parse_uri(result[7], content.m_uri_content);
164 }
165
166 return true;
167 }
168
169}
170}
#define LOG_PRINT_L1(x)
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
bool parse_uri(const std::string uri, http::uri_content &content)
bool parse_url(const std::string url_str, http::url_content &content)
bool parse_uri_query(const std::string &query, std::list< std::pair< std::string, std::string > > &params)
#define STATIC_REGEXP_EXPR_1(var_name, xpr_text, reg_exp_flags)
std::list< std::pair< std::string, std::string > > m_query_params
Definition http_base.h:143