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:41 UTC
[14/20] mesos git commit: Added a URL abstraction.
Added a URL abstraction.
Review: https://reviews.apache.org/r/29533
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/3c285f14
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/3c285f14
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/3c285f14
Branch: refs/heads/master
Commit: 3c285f14fa2f8af8a0109480299dca0b7f93d47d
Parents: e369699
Author: Benjamin Hindman <be...@gmail.com>
Authored: Tue Dec 16 13:14:42 2014 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sat Feb 7 14:42:46 2015 -0800
----------------------------------------------------------------------
3rdparty/libprocess/include/process/http.hpp | 75 +++++++++++++++++++++++
3rdparty/libprocess/src/tests/http_tests.cpp | 38 ++++++++++++
2 files changed, 113 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/3c285f14/3rdparty/libprocess/include/process/http.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/http.hpp b/3rdparty/libprocess/include/process/http.hpp
index 689f56d..819cee1 100644
--- a/3rdparty/libprocess/include/process/http.hpp
+++ b/3rdparty/libprocess/include/process/http.hpp
@@ -3,18 +3,22 @@
#include <limits.h>
#include <stdint.h>
+#include <unistd.h>
#include <cctype>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <string>
+#include <vector>
#include <process/pid.hpp>
#include <stout/error.hpp>
+#include <stout/foreach.hpp>
#include <stout/hashmap.hpp>
#include <stout/json.hpp>
+#include <stout/net.hpp>
#include <stout/none.hpp>
#include <stout/numify.hpp>
#include <stout/option.hpp>
@@ -493,6 +497,77 @@ inline Try<std::string> decode(const std::string& s)
}
+// Represents a Uniform Resource Locator:
+// scheme://domain|ip:port/path?query#fragment
+struct URL
+{
+ URL(const std::string& _scheme,
+ const std::string& _domain,
+ const uint16_t _port = 80,
+ const std::string& _path = "/",
+ const hashmap<std::string, std::string>& _query =
+ hashmap<std::string, std::string>(),
+ const Option<std::string>& _fragment = None())
+ : scheme(_scheme),
+ domain(_domain),
+ port(_port),
+ path(_path),
+ query(_query),
+ fragment(_fragment) {}
+
+ URL(const std::string& _scheme,
+ const net::IP& _ip,
+ const uint16_t _port = 80,
+ const std::string& _path = "/",
+ const hashmap<std::string, std::string>& _query =
+ hashmap<std::string, std::string>(),
+ const Option<std::string>& _fragment = None())
+ : scheme(_scheme),
+ ip(_ip),
+ port(_port),
+ path(_path),
+ query(_query),
+ fragment(_fragment) {}
+
+ std::string scheme;
+ // TODO(benh): Consider using unrestricted union for 'domain' and 'ip'.
+ Option<std::string> domain;
+ Option<net::IP> ip;
+ uint16_t port;
+ std::string path;
+ hashmap<std::string, std::string> query;
+ Option<std::string> fragment;
+};
+
+
+inline std::ostream& operator << (
+ std::ostream& stream,
+ const URL& url)
+{
+ stream << url.scheme << "://";
+
+ if (url.domain.isSome()) {
+ stream << url.domain.get();
+ } else if (url.ip.isSome()) {
+ stream << url.ip.get();
+ }
+
+ stream << ":" << url.port;
+
+ stream << "/" << strings::remove(url.path, "/", strings::PREFIX);
+
+ if (!url.query.empty()) {
+ stream << "?" << query::encode(url.query);
+ }
+
+ if (url.fragment.isSome()) {
+ stream << "#" << url.fragment.get();
+ }
+
+ return stream;
+}
+
+
// Asynchronously sends an HTTP GET request to the process with the
// given UPID and returns the HTTP response from the process.
Future<Response> get(
http://git-wip-us.apache.org/repos/asf/mesos/blob/3c285f14/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 ec64e60..73949b8 100644
--- a/3rdparty/libprocess/src/tests/http_tests.cpp
+++ b/3rdparty/libprocess/src/tests/http_tests.cpp
@@ -19,11 +19,14 @@
#include <stout/none.hpp>
#include <stout/nothing.hpp>
#include <stout/os.hpp>
+#include <stout/stringify.hpp>
#include "encoder.hpp"
using namespace process;
+using process::http::URL;
+
using process::network::Socket;
using std::string;
@@ -389,3 +392,38 @@ TEST(HTTP, QueryEncodeDecode)
EXPECT_SOME_EQ(HashmapStringString({{"a&b=c", "d&e=fg"}}),
http::query::decode("a%26b%3Dc=d%26e%3Dfg"));
}
+
+
+TEST(URLTest, stringification)
+{
+ EXPECT_EQ("http://mesos.apache.org:80/",
+ stringify(URL("http", "mesos.apache.org")));
+
+ EXPECT_EQ("https://mesos.apache.org:8080/",
+ stringify(URL("https", "mesos.apache.org", 8080)));
+
+ Try<net::IP> ip = net::IP::fromDotDecimal("172.158.1.23");
+ ASSERT_SOME(ip);
+
+ EXPECT_EQ("http://172.158.1.23:8080/",
+ stringify(URL("http", ip.get(), 8080)));
+
+ EXPECT_EQ("http://172.158.1.23:80/path",
+ stringify(URL("http", ip.get(), 80, "/path")));
+
+ hashmap<string, string> query;
+ query["foo"] = "bar";
+ query["baz"] = "bam";
+
+ EXPECT_EQ("http://172.158.1.23:80/?baz=bam&foo=bar",
+ stringify(URL("http", ip.get(), 80, "/", query)));
+
+ EXPECT_EQ("http://172.158.1.23:80/path?baz=bam&foo=bar",
+ stringify(URL("http", ip.get(), 80, "/path", query)));
+
+ EXPECT_EQ("http://172.158.1.23:80/?baz=bam&foo=bar#fragment",
+ stringify(URL("http", ip.get(), 80, "/", query, "fragment")));
+
+ EXPECT_EQ("http://172.158.1.23:80/path?baz=bam&foo=bar#fragment",
+ stringify(URL("http", ip.get(), 80, "/path", query, "fragment")));
+}