• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 7220508700

15 Dec 2023 09:51AM UTC coverage: 57.697% (+0.01%) from 57.684%
7220508700

Pull #13153

github

web-flow
Merge bfaeecb3e into 10ed86ddf
Pull Request #13153: auth: Move method checking to Router

31976 of 86762 branches covered (0.0%)

Branch coverage included in aggregate %.

514 of 703 new or added lines in 6 files covered. (73.12%)

47 existing lines in 11 files now uncovered.

107598 of 155147 relevant lines covered (69.35%)

4296933.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.29
/ext/yahttp/yahttp/router.cpp
1
/* @file
2
 * @brief Concrete implementation of Router 
3
 */
4
#include "yahttp.hpp"
5
#include "router.hpp"
6

7
namespace YaHTTP {
8
  // router is defined here.
9
  YaHTTP::Router Router::router;
10

11
  void Router::map(const std::string& method, const std::string& url, THandlerFunction handler, const std::string& name) {
328✔
12
    std::string method2 = method;
328✔
13
    bool isopen=false;
328✔
14
    // add into vector
15
    for(std::string::const_iterator i = url.begin(); i != url.end(); i++) {
12,266✔
16
       if (*i == '<' && isopen) throw Error("Invalid URL mask, cannot have < after <");
11,938!
17
       if (*i == '<') isopen = true;
11,938✔
18
       if (*i == '>' && !isopen) throw Error("Invalid URL mask, cannot have > without < first");
11,938!
19
       if (*i == '>') isopen = false;
11,938✔
20
    }
11,938✔
21
    std::transform(method2.begin(), method2.end(), method2.begin(), ::toupper); 
328✔
22
    routes.push_back(funcptr::make_tuple(method2, url, handler, name));
328✔
23
  };
328✔
24

25
  bool Router::match(const std::string& route, const URL& requrl, std::map<std::string, TDelim> &params) {
40,759✔
26
     size_t rpos = 0;
40,759✔
27
     size_t upos = 0;
40,759✔
28
     size_t npos = 0;
40,759✔
29
     size_t nstart = 0;
40,759✔
30
     size_t nend = 0;
40,759✔
31
     std::string pname;
40,759✔
32
     for(; rpos < route.size() && upos < requrl.path.size(); ) {
1,181,917✔
33
        if (route[rpos] == '<') {
1,159,388✔
34
          nstart = upos;
11,033✔
35
          npos = rpos+1;
11,033✔
36
          // start of parameter
37
          while(rpos < route.size() && route[rpos] != '>') {
47,872!
38
            rpos++;
36,839✔
39
          }
36,839✔
40
          pname = std::string(route.begin()+static_cast<long>(npos), route.begin()+static_cast<long>(rpos));
11,033✔
41
          // then we also look it on the url
42
          if (pname[0] == '*') {
11,033✔
43
            pname = pname.substr(1);
1,446✔
44
            // this matches whatever comes after it, basically end of string
45
            nend = requrl.path.size();
1,446✔
46
            if (!pname.empty()) {
1,446!
47
              params[pname] = funcptr::tie(nstart,nend);
1,446✔
48
            }
1,446✔
49
            rpos = route.size();
1,446✔
50
            upos = requrl.path.size();
1,446✔
51
            break;
1,446✔
52
          } else { 
9,599✔
53
            // match until url[upos] or next / if pattern is at end
54
            while (upos < requrl.path.size()) {
214,219✔
55
               if (route[rpos+1] == '\0' && requrl.path[upos] == '/') {
206,260!
NEW
56
                  break;
×
NEW
57
               }
×
58
               if (requrl.path[upos] == route[rpos+1]) {
206,260✔
59
                  break;
1,628✔
60
               }
1,628✔
61
               upos++;
204,632✔
62
            }
204,632✔
63
            nend = upos;
9,587✔
64
            params[pname] = funcptr::tie(nstart, nend);
9,587✔
65
          }
9,587✔
66
          upos--;
9,587✔
67
        }
9,587✔
68
        else if (route[rpos] != requrl.path[upos]) {
1,148,355✔
69
          break;
16,784✔
70
        }
16,784✔
71

72
        rpos++; upos++;
1,141,158✔
73
      }
1,141,158✔
74
      return route[rpos] == requrl.path[upos];
40,759✔
75
  }
40,759✔
76

77
  RoutingResult Router::route(Request *req, THandlerFunction& handler) {
1,458✔
78
    std::map<std::string, TDelim> params;
1,458✔
79
    bool matched = false;
1,458✔
80
    bool seen = false;
1,458✔
81
    std::string rname;
1,458✔
82

83
    // iterate routes
84
    for (auto& route: routes) {
40,267!
85
      std::string method;
40,267✔
86
      std::string url;
40,267✔
87
      funcptr::tie(method, url, handler, rname) = route;
40,267✔
88

89
      // see if we can't match the url
90
      params.clear();
40,267✔
91
      // simple matcher func
92
      matched = match(url, req->url, params);
40,267✔
93

94
      if (matched && !method.empty() && req->method != method) {
40,267✔
95
         // method did not match, record it though so we can return correct result
96
         matched = false;
2,715✔
97
         seen = true;
2,715✔
98
         continue;
2,715✔
99
      }
2,715✔
100
      if (matched) {
37,552✔
101
        break;
1,458✔
102
      }
1,458✔
103
    }
37,552✔
104

105
    if (!matched) {
1,458!
NEW
106
      if (seen) {
×
NEW
107
        return RouteNoMethod;
×
NEW
108
      }
×
109
      // no route
NEW
110
      return RouteNotFound;
×
UNCOV
111
    }
×
112

113
    req->parameters.clear();
1,458✔
114

115
    for (const auto& param: params) {
1,458✔
116
      int nstart = 0;
807✔
117
      int nend = 0;
807✔
118
      funcptr::tie(nstart, nend) = param.second;
807✔
119
      std::string value(req->url.path.begin() + nstart, req->url.path.begin() + nend);
807✔
120
      value = Utility::decodeURL(value);
807✔
121
      req->parameters[param.first] = std::move(value);
807✔
122
    }
807✔
123

124
    req->routeName = std::move(rname);
1,458✔
125

126
    return RouteFound;
1,458✔
127
  };
1,458✔
128

129
  void Router::printRoutes(std::ostream &os) {
×
130
    for(TRouteList::iterator i = routes.begin(); i != routes.end(); i++) {
×
131
#if __cplusplus >= 201103L
×
132
      std::streamsize ss = os.width();
×
133
      std::ios::fmtflags ff = os.setf(std::ios::left);
×
134
      os.width(10);
×
135
      os << std::get<0>(*i);
×
136
      os.width(50);
×
137
      os << std::get<1>(*i);
×
138
      os.width(ss);
×
139
      os.setf(ff);
×
140
      os << "    " << std::get<3>(*i);
×
141
      os << std::endl;
×
142
#else
143
      os << i->get<0>() << "    " << i->get<1>() << "    " << i->get<3>() << std::endl;
144
#endif
145
    } 
×
146
  };
×
147

148
  std::pair<std::string,std::string> Router::urlFor(const std::string &name, const strstr_map_t& arguments) {
×
149
    std::ostringstream path;
×
150
    std::string mask,method,result;
×
151
    int k1,k2,k3;
×
152

153
    bool found = false;
×
154
    for(TRouteList::iterator i = routes.begin(); !found && i != routes.end(); i++) {
×
155
#if __cplusplus >= 201103L
×
156
      if (std::get<3>(*i) == name) { mask = std::get<1>(*i); method = std::get<0>(*i); found = true; }
×
157
#else
158
      if (i->get<3>() == name) { mask = i->get<1>(); method = i->get<0>(); found = true; }
159
#endif
160
    }
×
161

162
    if (!found)
×
163
      throw Error("Route not found");
×
164

165
    for(k1=0,k3=0;k1<static_cast<int>(mask.size());k1++) {
×
166
      if (mask[k1] == '<') {
×
167
        std::string pname;
×
168
        strstr_map_t::const_iterator pptr;
×
169
        k2=k1;
×
170
        while(k1<static_cast<int>(mask.size()) && mask[k1]!='>') k1++;
×
171
        path << mask.substr(k3,k2-k3);
×
172
        if (mask[k2+1] == '*')
×
173
          pname = std::string(mask.begin() + k2 + 2, mask.begin() + k1);
×
174
        else 
×
175
          pname = std::string(mask.begin() + k2 + 1, mask.begin() + k1);
×
176
        if ((pptr = arguments.find(pname)) != arguments.end()) 
×
177
          path << Utility::encodeURL(pptr->second);
×
178
        k3 = k1+1;
×
179
      }
×
180
      else if (mask[k1] == '*') {
×
181
        // ready 
182
        k3++;
×
183
        continue;
×
184
      }
×
185
    }
×
186
    path << mask.substr(k3);
×
187
    result = path.str();
×
188
    return std::make_pair(method, result);
×
189
  }
×
190
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc