core: Implementing FaceUri abstraction

Change-Id: Ia4e0e9bb1a8b8c440ae97d45f77f792a50f87e85
Refs: #1195
diff --git a/daemon/core/face-uri.cpp b/daemon/core/face-uri.cpp
new file mode 100644
index 0000000..a30bb7b
--- /dev/null
+++ b/daemon/core/face-uri.cpp
@@ -0,0 +1,62 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "face-uri.hpp"
+#include "core/logger.hpp"
+#include <boost/regex.hpp>
+
+NFD_LOG_INIT("FaceUri");
+
+namespace nfd {
+
+FaceUri::FaceUri(const std::string& uri)
+{
+  if (!parse(uri))
+    {
+      throw Error("Malformed URI: " + uri);
+    }
+}
+
+bool
+FaceUri::parse(const std::string& uri)
+{
+  m_scheme.clear();
+  m_domain.clear();
+  m_port.clear();
+  
+  boost::regex protocolExp("(\\w+\\d?)://(.+)");
+  boost::smatch protocolMatch;
+  if (!boost::regex_match(uri, protocolMatch, protocolExp))
+    {
+      return false;
+    }
+  m_scheme = protocolMatch[1];
+
+  const std::string& remote = protocolMatch[2];
+
+  boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [stuff]:port
+  boost::regex v4Exp("^((\\d+\\.){3}\\d+)(:(\\d+))?$");
+  boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // stuff:port
+
+  boost::smatch match;
+  if (boost::regex_match(remote, match, v6Exp) ||
+      boost::regex_match(remote, match, v4Exp) ||
+      boost::regex_match(remote, match, hostExp))
+    {
+      m_domain = match[1];
+      m_port   = match[4];
+    }
+  else
+    {
+      return false;
+    }
+  
+  NFD_LOG_DEBUG("URI [" << uri << "] parsed into: "
+                << m_scheme << ", " << m_domain << ", " << m_port);
+  return true;
+}
+
+} // namespace nfd
diff --git a/daemon/core/face-uri.hpp b/daemon/core/face-uri.hpp
new file mode 100644
index 0000000..91c7a30
--- /dev/null
+++ b/daemon/core/face-uri.hpp
@@ -0,0 +1,89 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NFD_CORE_FACE_URI_H
+#define NFD_CORE_FACE_URI_H
+
+#include "common.hpp"
+
+namespace nfd {
+
+class FaceUri
+{
+public:
+  class Error : public std::runtime_error
+  {
+  public:
+    Error(const std::string& what) : std::runtime_error(what) {}
+  };
+
+  FaceUri();
+  
+  /** \brief Construct URI object
+   *
+   * Expected format: scheme://domain:port/path?query_string#fragment_id
+   *
+   * \throw FaceUri::Error if URI cannot be parsed
+   */
+  explicit
+  FaceUri(const std::string& uri);
+
+  /** \brief Exception-safe parsing of URI
+   */
+  bool
+  parse(const std::string& uri);
+
+  /** \brief Get scheme (protocol) extracted from URI
+   */
+  const std::string&
+  getScheme() const;
+
+  /** \brief Get domain extracted from URI
+   */
+  const std::string&
+  getDomain() const;
+
+  /** \brief Get port extracted from URI
+   *
+   *  \return Extracted port or empty string if port wasn't present
+   */
+  const std::string&
+  getPort() const;
+
+  // other getters should be added, when necessary
+
+private:
+  std::string m_scheme;
+  std::string m_domain;
+  std::string m_port;
+};
+
+inline
+FaceUri::FaceUri()
+{
+}
+
+inline const std::string&
+FaceUri::getScheme() const
+{
+  return m_scheme;
+}
+
+inline const std::string&
+FaceUri::getDomain() const
+{
+  return m_domain;
+}
+
+inline const std::string&
+FaceUri::getPort() const
+{
+  return m_port;
+}
+
+} // namespace nfd
+
+#endif // NFD_CORE_FACE_URI_H
diff --git a/tests/core/face-uri.cpp b/tests/core/face-uri.cpp
new file mode 100644
index 0000000..0b6a633
--- /dev/null
+++ b/tests/core/face-uri.cpp
@@ -0,0 +1,58 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "core/face-uri.hpp"
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+  BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363"));
+  BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error);
+  BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error);
+
+  FaceUri uri;
+  BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false);
+  
+  BOOST_CHECK(uri.parse("udp://hostname:80"));
+  BOOST_CHECK_EQUAL(uri.getScheme(), "udp");
+  BOOST_CHECK_EQUAL(uri.getDomain(), "hostname");
+  BOOST_CHECK_EQUAL(uri.getPort(), "80");
+
+  BOOST_CHECK(uri.parse("udp4://192.0.2.1:20"));
+  BOOST_CHECK_EQUAL(uri.getScheme(), "udp4");
+  BOOST_CHECK_EQUAL(uri.getDomain(), "192.0.2.1");
+  BOOST_CHECK_EQUAL(uri.getPort(), "20");
+
+  BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363"));
+  BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
+  BOOST_CHECK_EQUAL(uri.getDomain(), "2001:db8:3f9:0::1");
+  BOOST_CHECK_EQUAL(uri.getPort(), "6363");
+  
+  BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363"));
+  BOOST_CHECK_EQUAL(uri.getScheme(), "udp6");
+  BOOST_CHECK_EQUAL(uri.getDomain(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3");
+  BOOST_CHECK_EQUAL(uri.getPort(), "6363");
+
+  BOOST_CHECK(uri.parse("tcp://random.host.name"));
+  BOOST_CHECK_EQUAL(uri.getScheme(), "tcp");
+  BOOST_CHECK_EQUAL(uri.getDomain(), "random.host.name");
+  BOOST_CHECK_EQUAL(uri.getPort(), "");
+
+  BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false);
+  BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false);
+  BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false);  
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd