GraphLab: Distributed Graph-Parallel API  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
web_util.cpp
1 /**
2  * Copyright (c) 2009 Carnegie Mellon University.
3  * All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an "AS
13  * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14  * express or implied. See the License for the specific language
15  * governing permissions and limitations under the License.
16  *
17  * For more about this software visit:
18  *
19  * http://www.graphlab.ml.cmu.edu
20  *
21  */
22 
23 #include <graphlab/util/web_util.hpp>
24 #include <graphlab/util/stl_util.hpp>
25 
26 
27 
28 namespace graphlab {
29  namespace web_util {
30 
31  std::string url_decode(const std::string& url) {
32 #define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
33  std::string ret_str;
34  for (size_t i = 0; i < url.size(); ++i) {
35  if (url[i] == '%' &&
36  (i+1 < url.size() && isxdigit(url[i+1])) &&
37  (i+1 < url.size() && isxdigit(url[i+2]))) {
38  const char a = tolower(url[i+1]);
39  const char b = tolower(url[i+2]);
40  const char new_char = ((HEXTOI(a) << 4) | HEXTOI(b));
41  i += 2;
42  ret_str.push_back(new_char);
43  } else if (url[i] == '+') {
44  ret_str.push_back(' ');
45  } else {
46  ret_str.push_back(url[i]);
47  }
48  }
49 #undef HEXTOI
50  return ret_str;
51  } // end of url decode
52 
53 
54 
55  std::map<std::string, std::string> parse_query(const std::string& query) {
56  std::vector<std::string> pairs = graphlab::strsplit(query, ",=", true);
57  std::map<std::string, std::string> map;
58  for(size_t i = 0; i+1 < pairs.size(); i+=2)
59  map[url_decode(pairs[i])] = url_decode(pairs[i+1]);
60  return map;
61  } // end of parse url query
62 
63  } // end of namespace web_util
64 
65 }; // end of namespace GraphLab
66