blob: e32665ebdd4055de4ce6f04791dcc5656afae361 [file] [log] [blame]
tylerliu01d63ca2020-10-06 16:29:23 -07001//
2// Created by Tyler on 10/6/20.
3//
4
5#include "assignment-param.hpp"
6
7namespace ndn {
8namespace ndncert {
9
10_LOG_INIT(ndncert.assignment.param);
tylerliuafb27a02020-10-06 17:20:06 -070011
tylerliu01d63ca2020-10-06 16:29:23 -070012NDNCERT_REGISTER_FUNCFACTORY(AssignmentParam, "param");
13
14AssignmentParam::AssignmentParam()
15 : NameAssignmentFuncFactory("param")
16{
17}
18
19NameAssignmentFunc
20AssignmentParam::getFunction(const std::string &factoryParam) {
21 std::list<std::string> paramList;
22 size_t index = 0, startIndex = 0;
tylerliu3f63c022020-10-07 11:08:29 -070023 while ((index = factoryParam.find("/", startIndex)) != std::string::npos) {
24 auto component = factoryParam.substr(startIndex, index - startIndex);
25 if (!component.empty()) {
26 paramList.push_back(component);
27 }
tylerliu01d63ca2020-10-06 16:29:23 -070028 startIndex = index + 1;
29 }
30 if (startIndex != factoryParam.size()) {
31 paramList.push_back(factoryParam.substr(startIndex));
32 }
tylerliu42510132020-10-07 15:48:02 -070033 return [paramList](const std::vector<std::tuple<std::string, std::string>> params){
34 if (params.size() > paramList.size() * 8) { // might be attack
35 BOOST_THROW_EXCEPTION(std::runtime_error("Too many extra parameters given"));
36 }
37 std::map<std::string, std::string> paramMap;
38 for (const auto& param : params) {
39 paramMap[std::get<0>(param)] = std::get<1>(param);
40 if (std::get<1>(param).size() == 0) { // empty parameter!
41 return std::vector<PartialName>();
42 }
43 }
tylerliu01d63ca2020-10-06 16:29:23 -070044
tylerliu42510132020-10-07 15:48:02 -070045 //construct name
46 PartialName name;
47 for (const auto& field : paramList) {
48 auto it = paramMap.find(field);
49 if (it == paramMap.end()) {
50 return std::vector<PartialName>();
51 } else {
52 name.append(it->second);
53 }
54 }
55 std::vector<PartialName> nameList;
56 nameList.push_back(name);
57 return nameList;
58 };
tylerliu01d63ca2020-10-06 16:29:23 -070059}
60
61}
62}