X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FUri.cpp;h=bc94a31fb33a6cd688225ee97e9354eee6f5dffd;hb=9276f2a5646a94eda765c92b171b98c499313213;hp=f7bf80fafeec1a8fbcca141082284bee316e678a;hpb=1e5e33a557fc1f2b7f1322b928566fe71bcf26ad;p=folly.git diff --git a/folly/Uri.cpp b/folly/Uri.cpp index f7bf80fa..bc94a31f 100644 --- a/folly/Uri.cpp +++ b/folly/Uri.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2016 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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 @@ -60,6 +60,7 @@ 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( @@ -84,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); @@ -91,26 +93,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 +132,30 @@ fbstring Uri::hostname() const { 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