Add an authority() method to folly::Uri.
authorStephane Sezer <sas@fb.com>
Mon, 23 Sep 2013 21:42:11 +0000 (14:42 -0700)
committerPeter Griess <pgriess@fb.com>
Tue, 15 Oct 2013 01:43:06 +0000 (18:43 -0700)
Summary: facebook::strings::URL has an authority() method. Having the same thing for folly::Uri help converting users of the URL class. This method just takes username, password, host, and port, and builds a string in the form <username>:<password>@<host>:<port>.

Test Plan: None.

Reviewed By: rajat@fb.com

FB internal diff: D977450

folly/Uri.cpp
folly/Uri.h

index a94c9308fb0f84fa3445bb435209d5a24b94fd28..c2bd85a2fe81db95dc303e583cb9f691eed443ad 100644 (file)
@@ -92,4 +92,26 @@ Uri::Uri(StringPiece str) : port_(0) {
   fragment_ = submatch(match, 4);
 }
 
+fbstring
+Uri::authority() const
+{
+  fbstring result(host());
+
+  if (port() != 0) {
+    result += fbstring(":") + to<fbstring>(port());
+  }
+
+  if (!username().empty()) {
+    fbstring userInformation(username());
+
+    if (!password().empty()) {
+      userInformation += fbstring(":") + password();
+    }
+
+    result = userInformation + "@" + result;
+  }
+
+  return result;
+}
+
 }  // namespace folly
index 5ed0a1b7af54aa221338c52c50328888eea42e0e..b036058d6cf102a9a3f92e90148c012bf83710ae 100644 (file)
@@ -53,6 +53,8 @@ class Uri {
   const fbstring& query() const { return query_; }
   const fbstring& fragment() const { return fragment_; }
 
+  fbstring authority() const;
+
   template <class String>
   String toString() const;