-
Notifications
You must be signed in to change notification settings - Fork 2
/
url.cpp
53 lines (42 loc) · 1.36 KB
/
url.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
// Nonolith Connect
// https://github.com/nonolith/connect
// URL parsing
// Released under the terms of the GNU GPLv3+
// (C) 2012 Nonolith Labs, LLC
// Authors:
// Kevin Mehall <[email protected]>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include "url.hpp"
typedef std::string::const_iterator iterator_t;
using std::string;
string map_get(std::map<string, string>& map, const string key, const string def){
std::map<std::string, std::string>::iterator it = map.find(key);
if (it != map.end()){
return it->second;
}else{
return def;
}
}
void parse_query(const string& query, std::map<string, string>& map){
std::vector<std::string> vars;
boost::split(vars, query, boost::is_any_of("&"));
BOOST_FOREACH(string& pair, vars){
std::string::size_type sep = pair.find("=",0);
string key, value;
if (sep != std::string::npos) {
key = pair.substr(0, sep);
value = pair.substr(sep+1);
map[key] = value;
}
}
}
Url::Url(const string url){
iterator_t queryStart = std::find(url.begin(), url.end(), '?');
string path(url.begin(), queryStart);
boost::split(pathparts, path, boost::is_any_of("/"));
if (pathparts.at(pathparts.size()-1) == "") pathparts.pop_back(); // Remove trailing /
if (queryStart != url.end()) queryStart++; // skip '?'
string query(queryStart, url.end());
parse_query(query, params);
}