+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