You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2015/02/08 16:51:33 UTC

[06/20] mesos git commit: Added HTTP query string encode / decode.

Added HTTP query string encode / decode.

Review: https://reviews.apache.org/r/30441


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/93bed983
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/93bed983
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/93bed983

Branch: refs/heads/master
Commit: 93bed983262e4a78865b2c50548df9b2e9d19b43
Parents: dc9be77
Author: Cody Maloney <co...@mesosphere.io>
Authored: Fri Jan 30 17:57:29 2015 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sat Feb 7 14:42:45 2015 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/http.hpp | 23 ++------
 3rdparty/libprocess/src/decoder.hpp          |  8 ++-
 3rdparty/libprocess/src/http.cpp             | 71 ++++++++++++++++++++++-
 3rdparty/libprocess/src/tests/http_tests.cpp | 40 +++++++++++++
 4 files changed, 120 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/93bed983/3rdparty/libprocess/include/process/http.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/http.hpp b/3rdparty/libprocess/include/process/http.hpp
index 9cf05ac..1d1ad41 100644
--- a/3rdparty/libprocess/include/process/http.hpp
+++ b/3rdparty/libprocess/include/process/http.hpp
@@ -380,12 +380,12 @@ inline Try<hashmap<std::string, std::string> > parse(
 
 namespace query {
 
-// Parses an HTTP query string into a map. For example:
+// Decodes an HTTP query string into a map. For example:
 //
-//   parse("foo=1;bar=2;baz;foo=3")
+//   decode("foo=1&bar=%20&baz&foo=3")
 //
 // Would return a map with the following:
-//   bar: "2"
+//   bar: " "
 //   baz: ""
 //   foo: "3"
 //
@@ -394,22 +394,9 @@ namespace query {
 // http://en.wikipedia.org/wiki/Query_string
 // TODO(bmahler): If needed, investigate populating the query map inline
 // for better performance.
-inline hashmap<std::string, std::string> parse(const std::string& query)
-{
-  hashmap<std::string, std::string> result;
+Try<hashmap<std::string, std::string>> decode(const std::string& query);
 
-  const std::vector<std::string>& tokens = strings::tokenize(query, ";&");
-  foreach (const std::string& token, tokens) {
-    const std::vector<std::string>& pairs = strings::split(token, "=");
-    if (pairs.size() == 2) {
-      result[pairs[0]] = pairs[1];
-    } else if (pairs.size() == 1) {
-      result[pairs[0]] = "";
-    }
-  }
-
-  return result;
-}
+std::string encode(const hashmap<std::string, std::string>& query);
 
 } // namespace query {
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/93bed983/3rdparty/libprocess/src/decoder.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/decoder.hpp b/3rdparty/libprocess/src/decoder.hpp
index 8168464..45f3d7f 100644
--- a/3rdparty/libprocess/src/decoder.hpp
+++ b/3rdparty/libprocess/src/decoder.hpp
@@ -120,12 +120,16 @@ private:
 //     std::cout << "http::Request:" << std::endl;
 //     std::cout << "  method: " << decoder->request->method << std::endl;
 //     std::cout << "  path: " << decoder->request->path << std::endl;
+
     // Parse the query key/values.
-    Try<std::string> decoded = http::decode(decoder->query);
+    Try<hashmap<std::string, std::string>> decoded =
+      http::query::decode(decoder->query);
+
     if (decoded.isError()) {
       return 1;
     }
-    decoder->request->query = http::query::parse(decoded.get());
+
+    decoder->request->query =  decoded.get();
 
     Option<std::string> encoding =
       decoder->request->headers.get("Content-Encoding");

http://git-wip-us.apache.org/repos/asf/mesos/blob/93bed983/3rdparty/libprocess/src/http.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/http.cpp b/3rdparty/libprocess/src/http.cpp
index 869b205..ec9823e 100644
--- a/3rdparty/libprocess/src/http.cpp
+++ b/3rdparty/libprocess/src/http.cpp
@@ -32,6 +32,56 @@ namespace http {
 
 hashmap<uint16_t, string> statuses;
 
+namespace query {
+
+Try<hashmap<std::string, std::string>> decode(const std::string& query)
+{
+  hashmap<std::string, std::string> result;
+
+  const std::vector<std::string>& tokens = strings::tokenize(query, ";&");
+  foreach (const std::string& token, tokens) {
+    const std::vector<std::string>& pairs = strings::split(token, "=", 2);
+    if (pairs.size() == 0) {
+      continue;
+    }
+
+    Try<std::string> key = http::decode(pairs[0]);
+    if (key.isError()) {
+      return Error(key.error());
+    }
+
+    if (pairs.size() == 2) {
+      Try<std::string> value = http::decode(pairs[1]);
+      if (value.isError()) {
+        return Error(value.error());
+      }
+      result[key.get()] = value.get();
+
+    } else if (pairs.size() == 1) {
+      result[key.get()] = "";
+    }
+  }
+
+  return result;
+}
+
+
+std::string encode(const hashmap<std::string, std::string>& query)
+{
+  std::string output;
+
+  foreachpair (const std::string& key, const std::string& value, query) {
+    output += http::encode(key);
+    if (!value.empty()) {
+      output += "=" + http::encode(value);
+    }
+    output += '&';
+  }
+  return strings::remove(output, "&", strings::SUFFIX);
+}
+
+} // namespace query {
+
 namespace internal {
 
 Future<Response> decode(const string& buffer)
@@ -161,8 +211,25 @@ Future<Response> get(
     const Option<string>& query,
     const Option<hashmap<string, string> >& headers)
 {
-  return internal::request(
-      upid, "GET", path, query, headers, None(), None());
+  URL url("http", net::IP(upid.address.ip), upid.address.port, upid.id);
+
+  if (path.isSome()) {
+    // TODO(benh): Get 'query' and/or 'fragment' out of 'path'.
+    url.path = strings::join("/", url.path, path.get());
+  }
+
+  if (query.isSome()) {
+    Try<hashmap<string, string>> decode = http::query::decode(
+        strings::remove(query.get(), "?", strings::PREFIX));
+
+    if (decode.isError()) {
+      return Failure("Failed to decode HTTP query string: " + decode.error());
+    }
+
+    url.query = decode.get();
+  }
+
+  return get(url, headers);
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/93bed983/3rdparty/libprocess/src/tests/http_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/http_tests.cpp b/3rdparty/libprocess/src/tests/http_tests.cpp
index c71a91d..f7084ef 100644
--- a/3rdparty/libprocess/src/tests/http_tests.cpp
+++ b/3rdparty/libprocess/src/tests/http_tests.cpp
@@ -353,3 +353,43 @@ TEST(HTTP, Post)
   terminate(process);
   wait(process);
 }
+
+TEST(HTTP, QueryEncodeDecode)
+{
+  // If we use Type<a, b> directly inside a macro without surrounding
+  // parenthesis the comma will be eaten by the macro rather than the
+  // template. Typedef to avoid the problem.
+  typedef hashmap<string, string> HashmapStringString;
+
+  EXPECT_EQ("",
+            http::query::encode(HashmapStringString({})));
+
+  EXPECT_EQ("foo=bar",
+            http::query::encode(HashmapStringString({{"foo", "bar"}})));
+
+  EXPECT_EQ("c%7E%2Fasdf=%25asdf&a()=b%2520",
+            http::query::encode(
+                HashmapStringString({{"a()", "b%20"}, {"c~/asdf", "%asdf"}})));
+
+  EXPECT_EQ("d",
+            http::query::encode(HashmapStringString({{"d", ""}})));
+
+  EXPECT_EQ("a%26b%3Dc=d%26e%3Dfg",
+            http::query::encode(HashmapStringString({{"a&b=c", "d&e=fg"}})));
+
+  // Explicitly not testing decoding failures.
+  EXPECT_SOME_EQ(HashmapStringString(),
+                 http::query::decode(""));
+
+  EXPECT_SOME_EQ(HashmapStringString({{"foo", "bar"}}),
+                 http::query::decode("foo=bar"));
+
+  EXPECT_SOME_EQ(HashmapStringString({{"a()", "b%20"}, {"c~/asdf", "%asdf"}}),
+                 http::query::decode("c%7E%2Fasdf=%25asdf&a()=b%2520"));
+
+  EXPECT_SOME_EQ(HashmapStringString({{"d", ""}}),
+                 http::query::decode("d"));
+
+  EXPECT_SOME_EQ(HashmapStringString({{"a&b=c", "d&e=fg"}}),
+                 http::query::decode("a%26b%3Dc=d%26e%3Dfg"));
+}