Future<T>::then([](T&&)), aka next()
[folly.git] / folly / Uri.cpp
index f7bf80fafeec1a8fbcca141082284bee316e678a..f4618d8750b792785a36dca071e235611cd53297 100644 (file)
@@ -91,26 +91,6 @@ Uri::Uri(StringPiece str) : port_(0) {
   }
 
   query_ = submatch(match, 3);
-  if (!query_.empty()) {
-    // Parse query string
-    static const boost::regex queryParamRegex(
-      "(^|&)([^=&]*)=?([^=&]*)(?=(&|$))");
-    boost::cregex_iterator paramBeginItr(
-      match[3].first,
-      match[3].second,
-      queryParamRegex);
-    boost::cregex_iterator paramEndItr;
-    for(auto itr = paramBeginItr; itr != paramEndItr; itr++) {
-      if (itr->length(2) == 0) {
-        // key is empty, ignore it
-        continue;
-      }
-      queryParams_.emplace_back(
-        fbstring((*itr)[2].first, (*itr)[2].second), // parameter name
-        fbstring((*itr)[3].first, (*itr)[3].second)  // parameter value
-      );
-    }
-  }
   fragment_ = submatch(match, 4);
 }
 
@@ -150,4 +130,30 @@ fbstring Uri::hostname() const {
   return host_;
 }
 
+const std::vector<std::pair<fbstring, fbstring>>& Uri::getQueryParams() {
+  if (!query_.empty() && queryParams_.empty()) {
+    // Parse query string
+    static const boost::regex queryParamRegex(
+        "(^|&)" /*start of query or start of parameter "&"*/
+        "([^=&]*)=?" /*parameter name and "=" if value is expected*/
+        "([^=&]*)" /*parameter value*/
+        "(?=(&|$))" /*forward reference, next should be end of query or
+                      start of next parameter*/);
+    boost::cregex_iterator paramBeginItr(
+        query_.data(), query_.data() + query_.size(), queryParamRegex);
+    boost::cregex_iterator paramEndItr;
+    for (auto itr = paramBeginItr; itr != paramEndItr; itr++) {
+      if (itr->length(2) == 0) {
+        // key is empty, ignore it
+        continue;
+      }
+      queryParams_.emplace_back(
+          fbstring((*itr)[2].first, (*itr)[2].second), // parameter name
+          fbstring((*itr)[3].first, (*itr)[3].second) // parameter value
+          );
+    }
+  }
+  return queryParams_;
+}
+
 }  // namespace folly