Test UnboundedBlockingQueue in ThreadPoolExecutorTest
[folly.git] / folly / Uri.cpp
index 9d4ad2acf37535f7ba5487bdeea051b45bf73ec2..d1bdfae73b736c1d59bc0bb5e8f357a71c1f7281 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include "folly/Uri.h"
+#include <folly/Uri.h>
+
+#include <cctype>
 
-#include <ctype.h>
 #include <boost/regex.hpp>
 
 namespace folly {
 
 namespace {
 
-fbstring submatch(const boost::cmatch& m, size_t idx) {
+std::string submatch(const boost::cmatch& m, int idx) {
   auto& sub = m[idx];
-  return fbstring(sub.first, sub.second);
+  return std::string(sub.first, sub.second);
 }
 
 template <class String>
 void toLower(String& s) {
   for (auto& c : s) {
-    c = tolower(c);
+    c = char(tolower(c));
   }
 }
 
-}  // namespace
+} // namespace
 
-Uri::Uri(StringPiece str) : port_(0) {
+Uri::Uri(StringPiece str) : hasAuthority_(false), port_(0) {
   static const boost::regex uriRegex(
       "([a-zA-Z][a-zA-Z0-9+.-]*):"  // scheme:
       "([^?#]*)"                    // authority and path
@@ -60,12 +61,13 @@ Uri::Uri(StringPiece str) : port_(0) {
                           authorityAndPathMatch,
                           authorityAndPathRegex)) {
     // Does not start with //, doesn't have authority
-    path_ = authorityAndPath.fbstr();
+    hasAuthority_ = false;
+    path_ = authorityAndPath.str();
   } else {
     static const boost::regex authorityRegex(
         "(?:([^@:]*)(?::([^@]*))?@)?"  // username, password
-        "(\\[[^\\]]*\\]|[^\\[:]*)"     // host (IP-literal, dotted-IPv4, or
-                                       // named host)
+        "(\\[[^\\]]*\\]|[^\\[:]*)"     // host (IP-literal (e.g. '['+IPv6+']',
+                                       // dotted-IPv4, or named host)
         "(?::(\\d*))?");               // port
 
     auto authority = authorityAndPathMatch[1];
@@ -84,6 +86,7 @@ Uri::Uri(StringPiece str) : port_(0) {
       port_ = to<uint16_t>(port);
     }
 
+    hasAuthority_ = true;
     username_ = submatch(authorityMatch, 1);
     password_ = submatch(authorityMatch, 2);
     host_ = submatch(authorityMatch, 3);
@@ -94,8 +97,8 @@ Uri::Uri(StringPiece str) : port_(0) {
   fragment_ = submatch(match, 4);
 }
 
-fbstring Uri::authority() const {
-  fbstring result;
+std::string Uri::authority() const {
+  std::string result;
 
   // Port is 5 characters max and we have up to 3 delimiters.
   result.reserve(host().size() + username().size() + password().size() + 8);
@@ -121,4 +124,39 @@ fbstring Uri::authority() const {
   return result;
 }
 
-}  // namespace folly
+std::string Uri::hostname() const {
+  if (host_.size() > 0 && host_[0] == '[') {
+    // If it starts with '[', then it should end with ']', this is ensured by
+    // regex
+    return host_.substr(1, host_.size() - 2);
+  }
+  return host_;
+}
+
+const std::vector<std::pair<std::string, std::string>>& 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(
+          std::string((*itr)[2].first, (*itr)[2].second), // parameter name
+          std::string((*itr)[3].first, (*itr)[3].second) // parameter value
+          );
+    }
+  }
+  return queryParams_;
+}
+
+} // namespace folly