X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FUri.cpp;h=9bff152b1c45f518830fdb4b600492c845aad4fd;hp=9b1151a8f114e3f37de5266bfa1553da7ce1aaf0;hb=fbb8a926d8cc510a3ad870b96e1bd38b510034fd;hpb=2c3828bc4ea09ee0ebe104114df3dcb07ce3cbae diff --git a/folly/Uri.cpp b/folly/Uri.cpp index 9b1151a8..9bff152b 100644 --- a/folly/Uri.cpp +++ b/folly/Uri.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "folly/Uri.h" +#include #include #include @@ -37,7 +37,7 @@ void toLower(String& s) { } // 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 @@ -47,7 +47,7 @@ Uri::Uri(StringPiece str) : port_(0) { boost::cmatch match; if (UNLIKELY(!boost::regex_match(str.begin(), str.end(), match, uriRegex))) { - throw std::invalid_argument("invalid URI"); + throw std::invalid_argument(to("invalid URI ", str)); } scheme_ = submatch(match, 1); @@ -60,12 +60,13 @@ Uri::Uri(StringPiece str) : port_(0) { authorityAndPathMatch, authorityAndPathRegex)) { // Does not start with //, doesn't have authority + hasAuthority_ = false; path_ = authorityAndPath.fbstr(); } 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]; @@ -74,7 +75,9 @@ Uri::Uri(StringPiece str) : port_(0) { authority.second, authorityMatch, authorityRegex)) { - throw std::invalid_argument("invalid URI authority"); + throw std::invalid_argument( + to("invalid URI authority ", + StringPiece(authority.first, authority.second))); } StringPiece port(authorityMatch[4].first, authorityMatch[4].second); @@ -82,6 +85,7 @@ Uri::Uri(StringPiece str) : port_(0) { port_ = to(port); } + hasAuthority_ = true; username_ = submatch(authorityMatch, 1); password_ = submatch(authorityMatch, 2); host_ = submatch(authorityMatch, 3); @@ -119,4 +123,39 @@ fbstring Uri::authority() const { return result; } +fbstring 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>& 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