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

PowerDNS / pdns / 17093529787

20 Aug 2025 08:53AM UTC coverage: 53.36% (-11.3%) from 64.683%
17093529787

Pull #15994

github

web-flow
Merge f7bfd2539 into b46c65b05
Pull Request #15994: REST API: normalize record contents received

26310 of 70564 branches covered (37.29%)

Branch coverage included in aggregate %.

0 of 30 new or added lines in 1 file covered. (0.0%)

24364 existing lines in 215 files now uncovered.

88884 of 145317 relevant lines covered (61.17%)

4017709.01 hits per line

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

0.0
/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

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

UNCOV
25
  bool Router::match(const std::string& route, const URL& requrl, std::map<std::string, TDelim> &params) {
×
UNCOV
26
     size_t rpos = 0;
×
UNCOV
27
     size_t upos = 0;
×
UNCOV
28
     size_t npos = 0;
×
UNCOV
29
     size_t nstart = 0;
×
UNCOV
30
     size_t nend = 0;
×
UNCOV
31
     std::string pname;
×
UNCOV
32
     for(; rpos < route.size() && upos < requrl.path.size(); ) {
×
UNCOV
33
        if (route[rpos] == '<') {
×
UNCOV
34
          nstart = upos;
×
UNCOV
35
          npos = rpos+1;
×
36
          // start of parameter
UNCOV
37
          while(rpos < route.size() && route[rpos] != '>') {
×
UNCOV
38
            rpos++;
×
UNCOV
39
          }
×
UNCOV
40
          pname = std::string(route.begin()+static_cast<long>(npos), route.begin()+static_cast<long>(rpos));
×
41
          // then we also look it on the url
UNCOV
42
          if (pname[0] == '*') {
×
UNCOV
43
            pname = pname.substr(1);
×
44
            // this matches whatever comes after it, basically end of string
UNCOV
45
            nend = requrl.path.size();
×
UNCOV
46
            if (!pname.empty()) {
×
UNCOV
47
              params[pname] = funcptr::tie(nstart,nend);
×
UNCOV
48
            }
×
UNCOV
49
            rpos = route.size();
×
UNCOV
50
            upos = requrl.path.size();
×
UNCOV
51
            break;
×
UNCOV
52
          }
×
53
          // match until url[upos] or next / if pattern is at end
UNCOV
54
          while (upos < requrl.path.size()) {
×
UNCOV
55
            if (route[rpos+1] == '\0' && requrl.path[upos] == '/') {
×
UNCOV
56
              break;
×
UNCOV
57
            }
×
UNCOV
58
            if (requrl.path[upos] == route[rpos+1]) {
×
UNCOV
59
              break;
×
UNCOV
60
            }
×
UNCOV
61
            upos++;
×
UNCOV
62
          }
×
UNCOV
63
          nend = upos;
×
UNCOV
64
          params[pname] = funcptr::tie(nstart, nend);
×
UNCOV
65
          if (upos > 0) {
×
UNCOV
66
            upos--;
×
UNCOV
67
          }
×
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
71
            rpos++;
×
72
            continue;
×
73
          }
×
UNCOV
74
        }
×
UNCOV
75
        else if (route[rpos] != requrl.path[upos]) {
×
UNCOV
76
          break;
×
UNCOV
77
        }
×
78

UNCOV
79
        rpos++; upos++;
×
UNCOV
80
      }
×
UNCOV
81
      return route[rpos] == requrl.path[upos];
×
UNCOV
82
  }
×
83

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

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

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

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

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

UNCOV
120
    req->parameters.clear();
×
121

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

UNCOV
131
    req->routeName = std::move(rname);
×
132

UNCOV
133
    return RouteFound;
×
UNCOV
134
  };
×
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);
×
142
      os << std::get<0>(*i);
×
143
      os.width(50);
×
144
      os << std::get<1>(*i);
×
145
      os.width(ss);
×
146
      os.setf(ff);
×
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
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;
×
157
    std::string mask,method,result;
×
158
    int k1,k2,k3;
×
159

160
    bool found = false;
×
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);
×
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++;
×
190
        continue;
×
191
      }
×
192
    }
×
193
    path << mask.substr(k3);
×
194
    result = path.str();
×
195
    return std::make_pair(method, result);
×
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