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 2014/05/05 07:15:43 UTC

[2/2] git commit: Introduced 'Libprocess-From' to identify a libprocess "message".

Introduced 'Libprocess-From' to identify a libprocess "message".

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


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

Branch: refs/heads/master
Commit: 9926af07bb33873a801dd86a8e1229d1ea6c5588
Parents: d5fe51c
Author: Benjamin Hindman <be...@gmail.com>
Authored: Fri Apr 11 18:17:22 2014 -0600
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sun May 4 20:24:11 2014 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/src/encoder.hpp             | 12 ++-
 3rdparty/libprocess/src/process.cpp             | 81 ++++++++++++--------
 3rdparty/libprocess/src/tests/process_tests.cpp | 75 +++++++++++++++++-
 3 files changed, 131 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/9926af07/3rdparty/libprocess/src/encoder.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/encoder.hpp b/3rdparty/libprocess/src/encoder.hpp
index 7f4593b..9c5aa81 100644
--- a/3rdparty/libprocess/src/encoder.hpp
+++ b/3rdparty/libprocess/src/encoder.hpp
@@ -106,8 +106,16 @@ public:
     std::ostringstream out;
 
     if (message != NULL) {
-      out << "POST /" << message->to.id << "/" << message->name
-          << " HTTP/1.0\r\n"
+      out << "POST ";
+      // Nothing keeps the 'id' component of a PID from being an empty
+      // string which would create a malformed path that has two
+      // '//' unless we check for it explicitly.
+      // TODO(benh): Make the 'id' part of a PID optional so when it's
+      // missing it's clear that we're simply addressing an ip:port.
+      if (message->to.id != "") {
+        out << "/" << message->to.id;
+      }
+      out << "/" << message->name << " HTTP/1.0\r\n"
           << "User-Agent: libprocess/" << message->from << "\r\n"
           << "Connection: Keep-Alive\r\n";
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/9926af07/3rdparty/libprocess/src/process.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/process.cpp b/3rdparty/libprocess/src/process.cpp
index c14ecce..58bae5b 100644
--- a/3rdparty/libprocess/src/process.cpp
+++ b/3rdparty/libprocess/src/process.cpp
@@ -813,9 +813,12 @@ static void transport(Message* message, ProcessBase* sender = NULL)
 
 static bool libprocess(Request* request)
 {
-  return request->method == "POST" &&
-    request->headers.count("User-Agent") > 0 &&
-    request->headers["User-Agent"].find("libprocess/") == 0;
+  return
+    (request->method == "POST" &&
+     request->headers.contains("User-Agent") &&
+     request->headers["User-Agent"].find("libprocess/") == 0) ||
+    (request->method == "POST" &&
+     request->headers.contains("Libprocess-From"));
 }
 
 
@@ -823,44 +826,54 @@ static Message* parse(Request* request)
 {
   // TODO(benh): Do better error handling (to deal with a malformed
   // libprocess message, malicious or otherwise).
-  const string& agent = request->headers["User-Agent"];
-  const string& identifier = "libprocess/";
-  size_t index = agent.find(identifier);
-  if (index != string::npos) {
-    // Okay, now determine 'from'.
-    const UPID from(agent.substr(index + identifier.size(), agent.size()));
-
-    // Now determine 'to'.
-    index = request->path.find('/', 1);
-    index = index != string::npos ? index - 1 : string::npos;
-
-    // Decode possible percent-encoded 'to'.
-    Try<string> decode = http::decode(request->path.substr(1, index));
-
-    if (decode.isError()) {
-      VLOG(2) << "Failed to decode URL path: " << decode.get();
-      return NULL;
-    }
 
-    const UPID to(decode.get(), __ip__, __port__);
+  // First try and determine 'from'.
+  Option<UPID> from = None();
+
+  if (request->headers.contains("Libprocess-From")) {
+    from = UPID(strings::trim(request->headers["Libprocess-From"]));
+  } else {
+    // Try and get 'from' from the User-Agent.
+    const string& agent = request->headers["User-Agent"];
+    const string& identifier = "libprocess/";
+    size_t index = agent.find(identifier);
+    if (index != string::npos) {
+      from = UPID(agent.substr(index + identifier.size(), agent.size()));
+    }
+  }
 
-    // And now determine 'name'.
-    index = index != string::npos ? index + 2: request->path.size();
-    const string& name = request->path.substr(index);
+  if (from.isNone()) {
+    return NULL;
+  }
 
-    VLOG(2) << "Parsed message name '" << name
-            << "' for " << to << " from " << from;
+  // Now determine 'to'.
+  size_t index = request->path.find('/', 1);
+  index = index != string::npos ? index - 1 : string::npos;
 
-    Message* message = new Message();
-    message->name = name;
-    message->from = from;
-    message->to = to;
-    message->body = request->body;
+  // Decode possible percent-encoded 'to'.
+  Try<string> decode = http::decode(request->path.substr(1, index));
 
-    return message;
+  if (decode.isError()) {
+    VLOG(2) << "Failed to decode URL path: " << decode.get();
+    return NULL;
   }
 
-  return NULL;
+  const UPID to(decode.get(), __ip__, __port__);
+
+  // And now determine 'name'.
+  index = index != string::npos ? index + 2: request->path.size();
+  const string& name = request->path.substr(index);
+
+  VLOG(2) << "Parsed message name '" << name
+          << "' for " << to << " from " << from.get();
+
+  Message* message = new Message();
+  message->name = name;
+  message->from = from.get();
+  message->to = to;
+  message->body = request->body;
+
+  return message;
 }
 
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/9926af07/3rdparty/libprocess/src/tests/process_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/process_tests.cpp b/3rdparty/libprocess/src/tests/process_tests.cpp
index 41b57a9..ebce48c 100644
--- a/3rdparty/libprocess/src/tests/process_tests.cpp
+++ b/3rdparty/libprocess/src/tests/process_tests.cpp
@@ -1425,7 +1425,7 @@ TEST(Process, remote)
 
 
 // Like the 'remote' test but uses http::post.
-TEST(Process, http)
+TEST(Process, http1)
 {
   ASSERT_TRUE(GTEST_IS_THREADSAFE);
 
@@ -1455,6 +1455,79 @@ TEST(Process, http)
 }
 
 
+// Like 'http1' but using a 'Libprocess-From' header.
+TEST(Process, http2)
+{
+  ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+  RemoteProcess process;
+  spawn(process);
+
+  // Create a receiving socket so we can get messages back.
+  int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
+  ASSERT_LE(0, s);
+
+  // Set up socket.
+  sockaddr_in addr;
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family = PF_INET;
+  addr.sin_addr.s_addr = INADDR_ANY;
+  addr.sin_port = 0;
+
+  ASSERT_EQ(0, ::bind(s, (sockaddr*) &addr, sizeof(addr)));
+
+  // Create a UPID for 'Libprocess-From' based on the IP and port we
+  // got assigned.
+  socklen_t addrlen = sizeof(addr);
+  ASSERT_EQ(0, getsockname(s, (sockaddr*) &addr, &addrlen));
+
+  UPID from("", addr.sin_addr.s_addr, ntohs(addr.sin_port));
+
+  ASSERT_EQ(0, listen(s, 1));
+
+  Future<UPID> pid;
+  Future<string> body;
+  EXPECT_CALL(process, handler(_, _))
+    .WillOnce(DoAll(FutureArg<0>(&pid),
+                    FutureArg<1>(&body)));
+
+  hashmap<string, string> headers;
+  headers["Libprocess-From"] = stringify(from);
+
+  Future<http::Response> response =
+    http::post(process.self(), "handler", headers, "hello world");
+
+  AWAIT_READY(response);
+  ASSERT_EQ(http::statuses[202], response.get().status);
+
+  AWAIT_READY(body);
+  ASSERT_EQ("hello world", body.get());
+
+  AWAIT_READY(pid);
+  ASSERT_EQ(from, pid.get());
+
+  // Now post a message as though it came from the process.
+  const string name = "reply";
+  post(process.self(), from, name);
+
+  // Accept the incoming connection.
+  memset(&addr, 0, sizeof(addr));
+  addrlen = sizeof(addr);
+
+  int c = ::accept(s, (sockaddr*) &addr, &addrlen);
+  ASSERT_LT(0, c);
+
+  const string data = "POST /" + name + " HTTP/1.0";
+  EXPECT_SOME_EQ(data, os::read(c, data.size()));
+
+  close(c);
+  close(s);
+
+  terminate(process);
+  wait(process);
+}
+
+
 int foo()
 {
   return 1;