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

PowerDNS / pdns / 9763102220

02 Jul 2024 02:43PM UTC coverage: 66.269% (+1.6%) from 64.704%
9763102220

Pull #14327

github

web-flow
Merge f6cd5346c into 1e12511dd
Pull Request #14327: dnsdist: add support for a callback when a new tickets key is added

25662 of 54094 branches covered (47.44%)

Branch coverage included in aggregate %.

0 of 23 new or added lines in 2 files covered. (0.0%)

2546 existing lines in 61 files now uncovered.

84827 of 112634 relevant lines covered (75.31%)

5261058.81 hits per line

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

58.37
/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) {
960✔
12
    std::string method2 = method;
960✔
13
    bool isopen=false;
960✔
14
    // add into vector
15
    for(std::string::const_iterator i = url.begin(); i != url.end(); i++) {
29,972✔
16
       if (*i == '<' && isopen) throw Error("Invalid URL mask, cannot have < after <");
29,012!
17
       if (*i == '<') isopen = true;
29,012✔
18
       if (*i == '>' && !isopen) throw Error("Invalid URL mask, cannot have > without < first");
29,012!
19
       if (*i == '>') isopen = false;
29,012✔
20
    }
29,012✔
21
    std::transform(method2.begin(), method2.end(), method2.begin(), ::toupper); 
960✔
22
    routes.push_back(funcptr::make_tuple(method2, url, handler, name));
960✔
23
  };
960✔
24

25
  bool Router::match(const std::string& route, const URL& requrl, std::map<std::string, TDelim> &params) {
42,695✔
26
     size_t rpos = 0;
42,695✔
27
     size_t upos = 0;
42,695✔
28
     size_t npos = 0;
42,695✔
29
     size_t nstart = 0;
42,695✔
30
     size_t nend = 0;
42,695✔
31
     std::string pname;
42,695✔
32
     for(; rpos < route.size() && upos < requrl.path.size(); ) {
1,223,549✔
33
        if (route[rpos] == '<') {
1,200,459✔
34
          nstart = upos;
11,189✔
35
          npos = rpos+1;
11,189✔
36
          // start of parameter
37
          while(rpos < route.size() && route[rpos] != '>') {
48,790!
38
            rpos++;
37,601✔
39
          }
37,601✔
40
          pname = std::string(route.begin()+static_cast<long>(npos), route.begin()+static_cast<long>(rpos));
11,189✔
41
          // then we also look it on the url
42
          if (pname[0] == '*') {
11,189✔
43
            pname = pname.substr(1);
1,593✔
44
            // this matches whatever comes after it, basically end of string
45
            nend = requrl.path.size();
1,593✔
46
            if (!pname.empty()) {
1,593!
47
              params[pname] = funcptr::tie(nstart,nend);
1,593✔
48
            }
1,593✔
49
            rpos = route.size();
1,593✔
50
            upos = requrl.path.size();
1,593✔
51
            break;
1,593✔
52
          }
1,593✔
53
          // match until url[upos] or next / if pattern is at end
54
          while (upos < requrl.path.size()) {
214,392✔
55
            if (route[rpos+1] == '\0' && requrl.path[upos] == '/') {
206,424!
56
              break;
×
57
            }
×
58
            if (requrl.path[upos] == route[rpos+1]) {
206,424✔
59
              break;
1,628✔
60
            }
1,628✔
61
            upos++;
204,796✔
62
          }
204,796✔
63
          nend = upos;
9,596✔
64
          params[pname] = funcptr::tie(nstart, nend);
9,596✔
65
          if (upos > 0) {
9,596!
66
            upos--;
9,596✔
67
          }
9,596✔
UNCOV
68
          else {
×
69
            // If upos is zero, do not decrement it and then increment at bottom of loop, this disturbs Coverity.
70
            // Only increment rpos and continue loop
UNCOV
71
            rpos++;
×
UNCOV
72
            continue;
×
UNCOV
73
          }
×
74
        }
9,596✔
75
        else if (route[rpos] != requrl.path[upos]) {
1,189,270✔
76
          break;
18,012✔
77
        }
18,012✔
78

79
        rpos++; upos++;
1,180,854✔
80
      }
1,180,854✔
81
      return route[rpos] == requrl.path[upos];
42,695✔
82
  }
42,695✔
83

84
  RoutingResult Router::route(Request *req, THandlerFunction& handler) {
1,608✔
85
    std::map<std::string, TDelim> params;
1,608✔
86
    bool matched = false;
1,608✔
87
    bool seen = false;
1,608✔
88
    std::string rname;
1,608✔
89

90
    // iterate routes
91
    for (auto& route: routes) {
42,104!
92
      std::string method;
42,104✔
93
      std::string url;
42,104✔
94
      funcptr::tie(method, url, handler, rname) = route;
42,104✔
95

96
      // see if we can't match the url
97
      params.clear();
42,104✔
98
      // simple matcher func
99
      matched = match(url, req->url, params);
42,104✔
100

101
      if (matched && !method.empty() && req->method != method) {
42,104✔
102
         // method did not match, record it though so we can return correct result
103
         matched = false;
2,876✔
104
         seen = true;
2,876✔
105
         continue;
2,876✔
106
      }
2,876✔
107
      if (matched) {
39,228✔
108
        break;
1,608✔
109
      }
1,608✔
110
    }
39,228✔
111

112
    if (!matched) {
1,608!
UNCOV
113
      if (seen) {
×
UNCOV
114
        return RouteNoMethod;
×
UNCOV
115
      }
×
116
      // no route
UNCOV
117
      return RouteNotFound;
×
UNCOV
118
    }
×
119

120
    req->parameters.clear();
1,608✔
121

122
    for (const auto& param: params) {
1,608✔
123
      int nstart = 0;
813✔
124
      int nend = 0;
813✔
125
      funcptr::tie(nstart, nend) = param.second;
813✔
126
      std::string value(req->url.path.begin() + nstart, req->url.path.begin() + nend);
813✔
127
      value = Utility::decodeURL(value);
813✔
128
      req->parameters[param.first] = std::move(value);
813✔
129
    }
813✔
130

131
    req->routeName = std::move(rname);
1,608✔
132

133
    return RouteFound;
1,608✔
134
  };
1,608✔
135

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

155
  std::pair<std::string,std::string> Router::urlFor(const std::string &name, const strstr_map_t& arguments) {
×
156
    std::ostringstream path;
×
UNCOV
157
    std::string mask,method,result;
×
UNCOV
158
    int k1,k2,k3;
×
159

160
    bool found = false;
×
UNCOV
161
    for(TRouteList::iterator i = routes.begin(); !found && i != routes.end(); i++) {
×
162
#if __cplusplus >= 201103L
×
163
      if (std::get<3>(*i) == name) { mask = std::get<1>(*i); method = std::get<0>(*i); found = true; }
×
164
#else
165
      if (i->get<3>() == name) { mask = i->get<1>(); method = i->get<0>(); found = true; }
166
#endif
167
    }
×
168

169
    if (!found)
×
170
      throw Error("Route not found");
×
171

172
    for(k1=0,k3=0;k1<static_cast<int>(mask.size());k1++) {
×
173
      if (mask[k1] == '<') {
×
174
        std::string pname;
×
175
        strstr_map_t::const_iterator pptr;
×
176
        k2=k1;
×
177
        while(k1<static_cast<int>(mask.size()) && mask[k1]!='>') k1++;
×
178
        path << mask.substr(k3,k2-k3);
×
179
        if (mask[k2+1] == '*')
×
180
          pname = std::string(mask.begin() + k2 + 2, mask.begin() + k1);
×
UNCOV
181
        else 
×
182
          pname = std::string(mask.begin() + k2 + 1, mask.begin() + k1);
×
183
        if ((pptr = arguments.find(pname)) != arguments.end()) 
×
184
          path << Utility::encodeURL(pptr->second);
×
185
        k3 = k1+1;
×
186
      }
×
187
      else if (mask[k1] == '*') {
×
188
        // ready 
189
        k3++;
×
UNCOV
190
        continue;
×
UNCOV
191
      }
×
UNCOV
192
    }
×
UNCOV
193
    path << mask.substr(k3);
×
UNCOV
194
    result = path.str();
×
UNCOV
195
    return std::make_pair(method, result);
×
UNCOV
196
  }
×
197
};
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