| 1 |
|
| #include <xs/export.h> |
| 2 |
|
| #include <xs/typemap.h> |
| 3 |
|
| #include <xs/uri/router.h> |
| 4 |
|
| #include <regex> |
| 5 |
|
| |
| 6 |
|
| using namespace xs; |
| 7 |
|
| using namespace xs::uri; |
| 8 |
|
| using namespace panda; |
| 9 |
|
| using namespace panda::uri; |
| 10 |
|
| using namespace panda::uri::router; |
| 11 |
|
| |
| 12 |
25 |
| static void router_add (SvRouter* r, const Scalar& sv_path, const Scalar& value) { |
| 13 |
25 |
▶ | auto copy = Scalar::create(); |
Branch: 1 branch
| Branch | True | False | Total |
| auto copy = Scalar::create(); |
25 |
0 |
25 |
|
| 14 |
25 |
▶ | SvSetSV_nosteal(copy, value); |
Branch: 2 branches
| Branch | True | False | Total |
| SvSetSV_nosteal(copy, value); |
25 |
0 |
25 |
| SvSetSV_nosteal(copy, value); |
25 |
0 |
25 |
|
| 15 |
25 |
▶ | auto path = xs::in<string>(sv_path); |
Branch: 1 branch
| Branch | True | False | Total |
| auto path = xs::in<string>(sv_path); |
25 |
0 |
25 |
|
| 16 |
25 |
▶ | if (SvRXOK(sv_path)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvRXOK(sv_path)) { |
25 |
0 |
25 |
| if (SvRXOK(sv_path)) { |
4 |
21 |
25 |
|
| 17 |
4 |
▶ | if (path.find("(?^") == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (path.find("(?^") == 0) { |
4 |
0 |
4 |
| if (path.find("(?^") == 0) { |
4 |
0 |
4 |
|
| 18 |
4 |
▶ | static std::regex rere("^\\(\\?\\^[a-z]*:"); |
Branch: 4 branches
| Branch | True | False | Total |
| static std::regex rere("^\\(\\?\\^[a-z]*:"); |
2 |
2 |
4 |
| static std::regex rere("^\\(\\?\\^[a-z]*:"); |
2 |
0 |
2 |
| static std::regex rere("^\\(\\?\\^[a-z]*:"); |
2 |
0 |
2 |
| static std::regex rere("^\\(\\?\\^[a-z]*:"); |
0 |
0 |
0 |
|
| 19 |
4 |
▶ | auto re = regex_replace(std::string(path.data(), path.length()), rere, ""); |
Branch: 2 branches
| Branch | True | False | Total |
| auto re = regex_replace(std::string(path.data(), path.length()), rere, ""); |
4 |
0 |
4 |
| auto re = regex_replace(std::string(path.data(), path.length()), rere, ""); |
4 |
0 |
4 |
|
| 20 |
4 |
▶ | path = string(re.data(), re.length()-1); |
Branch: 2 branches
| Branch | True | False | Total |
| path = string(re.data(), re.length()-1); |
4 |
0 |
4 |
| path = string(re.data(), re.length()-1); |
4 |
0 |
4 |
|
| 21 |
4 |
| } |
| 22 |
4 |
▶ | r->add({Regex(path), copy}); |
Branch: 3 branches
| Branch | True | False | Total |
| r->add({Regex(path), copy}); |
4 |
0 |
4 |
| r->add({Regex(path), copy}); |
4 |
0 |
4 |
| r->add({Regex(path), copy}); |
4 |
0 |
4 |
|
| 23 |
|
| } else { |
| 24 |
21 |
▶ | r->add({path, copy}); |
Branch: 2 branches
| Branch | True | False | Total |
| r->add({path, copy}); |
21 |
0 |
21 |
| r->add({path, copy}); |
21 |
0 |
21 |
|
| 25 |
|
| } |
| 26 |
25 |
| } |
| 27 |
|
| |
| 28 |
|
| MODULE = URI::Router PACKAGE = URI::Router |
| 29 |
|
| PROTOTYPES: DISABLE |
| 30 |
|
| |
| 31 |
|
| BOOT { |
| 32 |
|
| using M = Method; |
| 33 |
6 |
▶ | Stash s(__PACKAGE__); |
Branch: 1 branch
| Branch | True | False | Total |
| Stash s(__PACKAGE__); |
6 |
0 |
6 |
|
| 34 |
60 |
▶ | xs::exp::create_constants(s, { |
Branch: 4 branches
| Branch | True | False | Total |
| xs::exp::create_constants(s, { |
6 |
0 |
6 |
| xs::exp::create_constants(s, { |
6 |
0 |
6 |
| xs::exp::create_constants(s, { |
54 |
6 |
60 |
| xs::exp::create_constants(s, { |
0 |
0 |
0 |
|
| 35 |
|
| {"METHOD_UNSPECIFIED", int(M::Unspecified)}, |
| 36 |
|
| {"METHOD_OPTIONS", int(M::Options) }, |
| 37 |
|
| {"METHOD_GET", int(M::Get) }, |
| 38 |
|
| {"METHOD_HEAD", int(M::Head) }, |
| 39 |
|
| {"METHOD_POST", int(M::Post) }, |
| 40 |
|
| {"METHOD_PUT", int(M::Put) }, |
| 41 |
|
| {"METHOD_DELETE", int(M::Delete) }, |
| 42 |
|
| {"METHOD_TRACE", int(M::Trace) }, |
| 43 |
|
| {"METHOD_CONNECT", int(M::Connect) }, |
| 44 |
|
| }); |
| 45 |
6 |
▶ | xs::exp::autoexport(s); |
Branch: 2 branches
| Branch | True | False | Total |
| xs::exp::autoexport(s); |
6 |
0 |
6 |
| xs::exp::autoexport(s); |
6 |
0 |
6 |
|
| 46 |
6 |
| } |
| 47 |
|
| |
| 48 |
|
| SvRouter* SvRouter::new (...) { |
| 49 |
6 |
| RETVAL = new SvRouter(); |
| 50 |
6 |
▶ | for (I32 i = 1; i < items - 1; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (I32 i = 1; i < items - 1; i += 2) { |
6 |
0 |
6 |
|
| 51 |
6 |
▶ | router_add(RETVAL, ST(i), ST(i+1)); |
Branch: 3 branches
| Branch | True | False | Total |
| router_add(RETVAL, ST(i), ST(i+1)); |
6 |
0 |
6 |
| router_add(RETVAL, ST(i), ST(i+1)); |
6 |
0 |
6 |
| router_add(RETVAL, ST(i), ST(i+1)); |
0 |
0 |
0 |
|
| 52 |
31 |
▶ | } |
Branch: 1 branch
| Branch | True | False | Total |
| } |
25 |
6 |
31 |
|
| 53 |
25 |
▶ | } |
Branch: 3 branches
| Branch | True | False | Total |
| } |
25 |
0 |
25 |
| } |
25 |
0 |
25 |
| } |
25 |
0 |
25 |
|
| 54 |
|
| |
| 55 |
|
| void SvRouter::add (Scalar path, Scalar value) { |
| 56 |
0 |
▶ | router_add(THIS, path, value); |
Branch: 1 branch
| Branch | True | False | Total |
| router_add(THIS, path, value); |
0 |
0 |
0 |
|
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| void SvRouter::route (string_view path, Sv sv_method = Sv()) { |
| 60 |
50 |
▶ | Method method = sv_method ? Method(SvIV(sv_method)) : Method::Get; |
Branch: 2 branches
| Branch | True | False | Total |
| Method method = sv_method ? Method(SvIV(sv_method)) : Method::Get; |
16 |
34 |
50 |
| Method method = sv_method ? Method(SvIV(sv_method)) : Method::Get; |
16 |
0 |
16 |
|
| 61 |
50 |
▶ | auto opt = THIS->route(path, method); |
Branch: 1 branch
| Branch | True | False | Total |
| auto opt = THIS->route(path, method); |
50 |
0 |
50 |
|
| 62 |
50 |
▶ | if (!opt) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opt) XSRETURN_UNDEF; |
15 |
35 |
50 |
|
| 63 |
35 |
| auto& res = *opt; |
| 64 |
|
| |
| 65 |
35 |
▶ | if (GIMME_V != G_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V != G_ARRAY) { |
24 |
11 |
35 |
|
| 66 |
24 |
▶ | XPUSHs(res.value); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs(res.value); |
0 |
24 |
24 |
| XPUSHs(res.value); |
0 |
0 |
0 |
|
| 67 |
24 |
| XSRETURN(1); |
| 68 |
|
| } |
| 69 |
|
| else { |
| 70 |
11 |
| I32 nret = res.captures.size() + 1; |
| 71 |
11 |
▶ | EXTEND(SP, nret); |
Branch: 4 branches
| Branch | True | False | Total |
| EXTEND(SP, nret); |
11 |
0 |
11 |
| EXTEND(SP, nret); |
0 |
11 |
11 |
| EXTEND(SP, nret); |
0 |
11 |
11 |
| EXTEND(SP, nret); |
0 |
0 |
0 |
|
| 72 |
11 |
▶ | XPUSHs(res.value); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs(res.value); |
0 |
11 |
11 |
| XPUSHs(res.value); |
0 |
0 |
0 |
|
| 73 |
26 |
▶ | for (auto& s : res.captures) mXPUSHs(xs::out(s).detach()); |
Branch: 5 branches
| Branch | True | False | Total |
| for (auto& s : res.captures) mXPUSHs(xs::out(s).detach()); |
0 |
15 |
15 |
| for (auto& s : res.captures) mXPUSHs(xs::out(s).detach()); |
0 |
0 |
0 |
| for (auto& s : res.captures) mXPUSHs(xs::out(s).detach()); |
15 |
0 |
15 |
| for (auto& s : res.captures) mXPUSHs(xs::out(s).detach()); |
15 |
0 |
15 |
| for (auto& s : res.captures) mXPUSHs(xs::out(s).detach()); |
15 |
11 |
26 |
|
| 74 |
11 |
| XSRETURN(nret); |
| 75 |
|
| } |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
|
| uint64_t SvRouter::bench (string_view path) { |
| 79 |
0 |
| RETVAL = 0; |
| 80 |
0 |
▶ | for (int i = 0; i < 1000; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i < 1000; ++i) { |
0 |
0 |
0 |
|
| 81 |
0 |
▶ | auto opt = THIS->route(path); |
Branch: 1 branch
| Branch | True | False | Total |
| auto opt = THIS->route(path); |
0 |
0 |
0 |
|
| 82 |
0 |
▶ | if (opt) ++RETVAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (opt) ++RETVAL; |
0 |
0 |
0 |
|
| 83 |
0 |
| } |
| 84 |
|
| } |