Remove/make private the default ***Holder constructor to allow compile time detection...
[folly.git] / folly / Uri.h
index 29711197272656836f90121241dcbf6e83e1805b..09e2f6776698133a04f97b99dc8a57baa174c333 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 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.
  */
 
-#ifndef FOLLY_URI_H_
+#pragma once
 #define FOLLY_URI_H_
 
-#include "folly/String.h"
+#include <folly/String.h>
+#include <vector>
 
 namespace folly {
 
@@ -47,7 +48,21 @@ class Uri {
   const fbstring& scheme() const { return scheme_; }
   const fbstring& username() const { return username_; }
   const fbstring& password() const { return password_; }
+  /**
+   * Get host part of URI. If host is an IPv6 address, square brackets will be
+   * returned, for example: "[::1]".
+   */
   const fbstring& host() const { return host_; }
+  /**
+   * Get host part of URI. If host is an IPv6 address, square brackets will not
+   * be returned, for exmaple "::1"; otherwise it returns the same thing as
+   * host().
+   *
+   * hostname() is what one needs to call if passing the host to any other tool
+   * or API that connects to that host/port; e.g. getaddrinfo() only understands
+   * IPv6 host without square brackets
+   */
+  fbstring hostname() const;
   uint16_t port() const { return port_; }
   const fbstring& path() const { return path_; }
   const fbstring& query() const { return query_; }
@@ -61,21 +76,46 @@ class Uri {
   std::string str() const { return toString<std::string>(); }
   fbstring fbstr() const { return toString<fbstring>(); }
 
-  void setPort(uint16_t port) {port_ = port;}
+  void setPort(uint16_t port) {
+    hasAuthority_ = true;
+    port_ = port;
+  }
+
+  /**
+   * Get query parameters as key-value pairs.
+   * e.g. for URI containing query string:  key1=foo&key2=&key3&=bar&=bar=
+   * In returned list, there are 3 entries:
+   *     "key1" => "foo"
+   *     "key2" => ""
+   *     "key3" => ""
+   * Parts "=bar" and "=bar=" are ignored, as they are not valid query
+   * parameters. "=bar" is missing parameter name, while "=bar=" has more than
+   * one equal signs, we don't know which one is the delimiter for key and
+   * value.
+   *
+   * Note, this method is not thread safe, it might update internal state, but
+   * only the first call to this method update the state. After the first call
+   * is finished, subsequent calls to this method are thread safe.
+   *
+   * @return  query parameter key-value pairs in a vector, each element is a
+   *          pair of which the first element is parameter name and the second
+   *          one is parameter value
+   */
+  const std::vector<std::pair<fbstring, fbstring>>& getQueryParams();
 
  private:
   fbstring scheme_;
   fbstring username_;
   fbstring password_;
   fbstring host_;
+  bool hasAuthority_;
   uint16_t port_;
   fbstring path_;
   fbstring query_;
   fbstring fragment_;
+  std::vector<std::pair<fbstring, fbstring>> queryParams_;
 };
 
 }  // namespace folly
 
-#include "folly/Uri-inl.h"
-
-#endif /* FOLLY_URI_H_ */
+#include <folly/Uri-inl.h>