| 194 | } |
| 195 | |
| 196 | bool ParseRestfulMappings(const butil::StringPiece& mappings, |
| 197 | std::vector<RestfulMapping>* list) { |
| 198 | if (list == NULL) { |
| 199 | LOG(ERROR) << "Param[list] is NULL"; |
| 200 | return false; |
| 201 | } |
| 202 | list->clear(); |
| 203 | list->reserve(8); |
| 204 | butil::StringSplitter sp( |
| 205 | mappings.data(), mappings.data() + mappings.size(), ','); |
| 206 | int nmappings = 0; |
| 207 | for (; sp; ++sp) { |
| 208 | ++nmappings; |
| 209 | size_t i = 0; |
| 210 | const char* p = sp.field(); |
| 211 | const size_t n = sp.length(); |
| 212 | bool added_sth = false; |
| 213 | for (; i < n; ++i) { |
| 214 | // find = |
| 215 | if (p[i] != '=') { |
| 216 | continue; |
| 217 | } |
| 218 | const size_t equal_sign_pos = i; |
| 219 | for (; i < n && p[i] == '='; ++i) {} // skip repeated = |
| 220 | // If the = ends with >, it's the arrow that we're finding. |
| 221 | // otherwise just skip and keep searching. |
| 222 | if (i < n && p[i] == '>') { |
| 223 | RestfulMapping m; |
| 224 | // Parse left part of the arrow as url path. |
| 225 | butil::StringPiece path(sp.field(), equal_sign_pos); |
| 226 | if (!ParseRestfulPath(path, &m.path)) { |
| 227 | LOG(ERROR) << "Fail to parse path=`" << path << '\''; |
| 228 | return false; |
| 229 | } |
| 230 | // Treat right part of the arrow as method_name. |
| 231 | butil::StringPiece method_name_piece(p + i + 1, n - (i + 1)); |
| 232 | method_name_piece.trim_spaces(); |
| 233 | if (method_name_piece.empty()) { |
| 234 | LOG(ERROR) << "No method name in " << nmappings |
| 235 | << "-th mapping"; |
| 236 | return false; |
| 237 | } |
| 238 | m.method_name.assign(method_name_piece.data(), |
| 239 | method_name_piece.size()); |
| 240 | list->push_back(m); |
| 241 | added_sth = true; |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | // If we don't get a valid mapping from the string, issue error. |
| 246 | if (!added_sth) { |
| 247 | LOG(ERROR) << "Invalid mapping: " |
| 248 | << butil::StringPiece(sp.field(), sp.length()); |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | return true; |
| 253 | } |
no test coverage detected