| 1 |
|
| #include <xs/uri.h> |
| 2 |
|
| #include <xs/export.h> |
| 3 |
|
| #include <unordered_map> |
| 4 |
|
| #include <panda/uri/all.h> |
| 5 |
|
| #include <panda/string_view.h> |
| 6 |
|
| |
| 7 |
|
| using namespace xs; |
| 8 |
|
| using namespace xs::uri; |
| 9 |
|
| using namespace panda::uri; |
| 10 |
|
| using panda::string; |
| 11 |
|
| using panda::string_view; |
| 12 |
|
| |
| 13 |
|
| static std::unordered_map<string, Stash> uri_class_map; |
| 14 |
|
| static Sv::payload_marker_t data_marker; |
| 15 |
|
| |
| 16 |
|
| struct XsUriData { |
| 17 |
136 |
| XsUriData () : query_cache_rev(0) {} |
| 18 |
|
| |
| 19 |
5 |
| void sync_query_hash (const URI* uri) { |
| 20 |
5 |
| Hash hash; |
| 21 |
5 |
▶ | if (query_cache) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (query_cache) { |
0 |
5 |
5 |
|
| 22 |
0 |
▶ | hash = query_cache.value<Hash>(); |
Branch: 1 branch
| Branch | True | False | Total |
| hash = query_cache.value<Hash>(); |
0 |
0 |
0 |
|
| 23 |
0 |
▶ | hash.clear(); |
Branch: 1 branch
| Branch | True | False | Total |
| hash.clear(); |
0 |
0 |
0 |
|
| 24 |
|
| } else { |
| 25 |
5 |
▶ | hash = Hash::create(); |
Branch: 1 branch
| Branch | True | False | Total |
| hash = Hash::create(); |
5 |
0 |
5 |
|
| 26 |
5 |
▶ | query_cache = Ref::create(hash); |
Branch: 1 branch
| Branch | True | False | Total |
| query_cache = Ref::create(hash); |
5 |
0 |
5 |
|
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
5 |
▶ | auto end = uri->query().cend(); |
Branch: 1 branch
| Branch | True | False | Total |
| auto end = uri->query().cend(); |
5 |
0 |
5 |
|
| 30 |
20 |
▶ | for (auto it = uri->query().cbegin(); it != end; ++it) hash.store(it->first, Simple(it->second)); |
Branch: 4 branches
| Branch | True | False | Total |
| for (auto it = uri->query().cbegin(); it != end; ++it) hash.store(it->first, Simple(it->second)); |
5 |
0 |
5 |
| for (auto it = uri->query().cbegin(); it != end; ++it) hash.store(it->first, Simple(it->second)); |
15 |
0 |
15 |
| for (auto it = uri->query().cbegin(); it != end; ++it) hash.store(it->first, Simple(it->second)); |
15 |
0 |
15 |
| for (auto it = uri->query().cbegin(); it != end; ++it) hash.store(it->first, Simple(it->second)); |
15 |
5 |
20 |
|
| 31 |
|
| |
| 32 |
5 |
▶ | query_cache_rev = uri->query().rev; |
Branch: 1 branch
| Branch | True | False | Total |
| query_cache_rev = uri->query().rev; |
5 |
0 |
5 |
|
| 33 |
5 |
| } |
| 34 |
|
| |
| 35 |
6 |
| Ref query_hash (const URI* uri) { |
| 36 |
6 |
▶ | if (!query_cache || query_cache_rev != uri->query().rev) sync_query_hash(uri); |
Branch: 3 branches
| Branch | True | False | Total |
| if (!query_cache || query_cache_rev != uri->query().rev) sync_query_hash(uri); |
1 |
5 |
6 |
| if (!query_cache || query_cache_rev != uri->query().rev) sync_query_hash(uri); |
0 |
1 |
1 |
| if (!query_cache || query_cache_rev != uri->query().rev) sync_query_hash(uri); |
5 |
1 |
6 |
|
| 37 |
6 |
| return query_cache; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| private: |
| 41 |
|
| Ref query_cache; |
| 42 |
|
| uint32_t query_cache_rev; |
| 43 |
|
| }; |
| 44 |
|
| |
| 45 |
118 |
| static void register_perl_scheme (const string& scheme, const string_view& perl_class) { |
| 46 |
118 |
▶ | uri_class_map[scheme] = Stash(perl_class); |
Branch: 2 branches
| Branch | True | False | Total |
| uri_class_map[scheme] = Stash(perl_class); |
118 |
0 |
118 |
| uri_class_map[scheme] = Stash(perl_class); |
118 |
0 |
118 |
|
| 47 |
118 |
| } |
| 48 |
|
| |
| 49 |
19 |
| Stash xs::uri::get_perl_class (const URI* uri) { |
| 50 |
19 |
▶ | auto it = uri_class_map.find(uri->scheme()); |
Branch: 1 branch
| Branch | True | False | Total |
| auto it = uri_class_map.find(uri->scheme()); |
19 |
0 |
19 |
|
| 51 |
19 |
▶ | if (it == uri_class_map.end()) return Stash(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (it == uri_class_map.end()) return Stash(); |
2 |
17 |
19 |
|
| 52 |
17 |
▶ | else return it->second; |
Branch: 1 branch
| Branch | True | False | Total |
| else return it->second; |
17 |
0 |
17 |
|
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
136 |
| void xs::uri::data_attach (Sv& sv) { |
| 56 |
136 |
| void* data = new XsUriData(); |
| 57 |
136 |
▶ | Object(sv).payload_attach(data, &data_marker); |
Branch: 2 branches
| Branch | True | False | Total |
| Object(sv).payload_attach(data, &data_marker); |
136 |
0 |
136 |
| Object(sv).payload_attach(data, &data_marker); |
136 |
0 |
136 |
|
| 58 |
136 |
| } |
| 59 |
|
| |
| 60 |
136 |
| static int data_free (pTHX_ SV*, MAGIC* mg) { |
| 61 |
136 |
| auto data = (XsUriData*)mg->mg_ptr; |
| 62 |
136 |
▶ | delete data; |
Branch: 1 branch
| Branch | True | False | Total |
| delete data; |
136 |
0 |
136 |
|
| 63 |
136 |
| return 0; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
6 |
| static XsUriData* data_get (SV* sv) { |
| 67 |
6 |
▶ | return (XsUriData*)Object(sv).payload(&data_marker).ptr; |
Branch: 1 branch
| Branch | True | False | Total |
| return (XsUriData*)Object(sv).payload(&data_marker).ptr; |
6 |
0 |
6 |
|
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
5 |
| static void add_param (URI* uri, const string& key, const Scalar& val, bool replace = false) { |
| 71 |
5 |
▶ | if (val.is_array_ref()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (val.is_array_ref()) { |
0 |
5 |
5 |
|
| 72 |
0 |
▶ | Array arr = val; |
Branch: 1 branch
| Branch | True | False | Total |
| Array arr = val; |
0 |
0 |
0 |
|
| 73 |
0 |
▶ | if (replace) uri->query().erase(key); |
Branch: 3 branches
| Branch | True | False | Total |
| if (replace) uri->query().erase(key); |
0 |
0 |
0 |
| if (replace) uri->query().erase(key); |
0 |
0 |
0 |
| if (replace) uri->query().erase(key); |
0 |
0 |
0 |
|
| 74 |
0 |
▶ | auto end = arr.end(); |
Branch: 1 branch
| Branch | True | False | Total |
| auto end = arr.end(); |
0 |
0 |
0 |
|
| 75 |
0 |
▶ | for (auto it = arr.begin(); it != end; ++it) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (auto it = arr.begin(); it != end; ++it) { |
0 |
0 |
0 |
| for (auto it = arr.begin(); it != end; ++it) { |
0 |
0 |
0 |
|
| 76 |
0 |
▶ | if (!*it) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!*it) continue; |
0 |
0 |
0 |
| if (!*it) continue; |
0 |
0 |
0 |
|
| 77 |
0 |
▶ | uri->query().emplace(key, xs::in<string>(*it)); |
Branch: 4 branches
| Branch | True | False | Total |
| uri->query().emplace(key, xs::in<string>(*it)); |
0 |
0 |
0 |
| uri->query().emplace(key, xs::in<string>(*it)); |
0 |
0 |
0 |
| uri->query().emplace(key, xs::in<string>(*it)); |
0 |
0 |
0 |
| uri->query().emplace(key, xs::in<string>(*it)); |
0 |
0 |
0 |
|
| 78 |
|
| } |
| 79 |
0 |
| } |
| 80 |
5 |
▶ | else if (replace) uri->param(key, xs::in<string>(val)); |
Branch: 3 branches
| Branch | True | False | Total |
| else if (replace) uri->param(key, xs::in<string>(val)); |
1 |
4 |
5 |
| else if (replace) uri->param(key, xs::in<string>(val)); |
1 |
0 |
1 |
| else if (replace) uri->param(key, xs::in<string>(val)); |
1 |
0 |
1 |
|
| 81 |
4 |
▶ | else uri->query().emplace(key, xs::in<string>(val)); |
Branch: 2 branches
| Branch | True | False | Total |
| else uri->query().emplace(key, xs::in<string>(val)); |
4 |
0 |
4 |
| else uri->query().emplace(key, xs::in<string>(val)); |
4 |
0 |
4 |
|
| 82 |
5 |
| } |
| 83 |
|
| |
| 84 |
2 |
| static void hash2query (Hash& hash, Query* query) { |
| 85 |
2 |
| auto end = hash.end(); |
| 86 |
8 |
▶ | for (auto it = hash.begin(); it != end; ++it) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (auto it = hash.begin(); it != end; ++it) { |
2 |
0 |
2 |
| for (auto it = hash.begin(); it != end; ++it) { |
6 |
2 |
8 |
|
| 87 |
6 |
▶ | string key(it->key()); |
Branch: 2 branches
| Branch | True | False | Total |
| string key(it->key()); |
6 |
0 |
6 |
| string key(it->key()); |
6 |
0 |
6 |
|
| 88 |
6 |
▶ | auto val = it->value(); |
Branch: 1 branch
| Branch | True | False | Total |
| auto val = it->value(); |
6 |
0 |
6 |
|
| 89 |
6 |
▶ | if (val.is_array_ref()) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (val.is_array_ref()) { |
6 |
0 |
6 |
| if (val.is_array_ref()) { |
1 |
5 |
6 |
|
| 90 |
1 |
▶ | Array arr = val; |
Branch: 1 branch
| Branch | True | False | Total |
| Array arr = val; |
1 |
0 |
1 |
|
| 91 |
1 |
▶ | auto end = arr.end(); |
Branch: 1 branch
| Branch | True | False | Total |
| auto end = arr.end(); |
1 |
0 |
1 |
|
| 92 |
4 |
▶ | for (auto it = arr.begin(); it != end; ++it) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (auto it = arr.begin(); it != end; ++it) { |
1 |
0 |
1 |
| for (auto it = arr.begin(); it != end; ++it) { |
3 |
1 |
4 |
|
| 93 |
3 |
▶ | if (!*it) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!*it) continue; |
3 |
0 |
3 |
| if (!*it) continue; |
0 |
3 |
3 |
|
| 94 |
3 |
▶ | query->emplace(key, xs::in<string>(*it)); |
Branch: 3 branches
| Branch | True | False | Total |
| query->emplace(key, xs::in<string>(*it)); |
3 |
0 |
3 |
| query->emplace(key, xs::in<string>(*it)); |
3 |
0 |
3 |
| query->emplace(key, xs::in<string>(*it)); |
3 |
0 |
3 |
|
| 95 |
|
| } |
| 96 |
1 |
| } |
| 97 |
5 |
▶ | else query->emplace(key, xs::in<string>(val)); |
Branch: 2 branches
| Branch | True | False | Total |
| else query->emplace(key, xs::in<string>(val)); |
5 |
0 |
5 |
| else query->emplace(key, xs::in<string>(val)); |
5 |
0 |
5 |
|
| 98 |
6 |
| } |
| 99 |
2 |
| } |
| 100 |
|
| |
| 101 |
3 |
| static void add_query_hash (URI* uri, Hash& hash, bool replace = false) { |
| 102 |
3 |
▶ | if (replace) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (replace) { |
2 |
1 |
3 |
|
| 103 |
2 |
| Query query; |
| 104 |
2 |
▶ | hash2query(hash, &query); |
Branch: 1 branch
| Branch | True | False | Total |
| hash2query(hash, &query); |
2 |
0 |
2 |
|
| 105 |
2 |
▶ | uri->query(query); |
Branch: 1 branch
| Branch | True | False | Total |
| uri->query(query); |
2 |
0 |
2 |
|
| 106 |
2 |
| } |
| 107 |
|
| else { |
| 108 |
1 |
| auto end = hash.end(); |
| 109 |
2 |
▶ | for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
Branch: 6 branches
| Branch | True | False | Total |
| for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
1 |
0 |
1 |
| for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
1 |
0 |
1 |
| for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
1 |
0 |
1 |
| for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
1 |
0 |
1 |
| for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
1 |
0 |
1 |
| for (auto it = hash.begin(); it != end; ++it) add_param(uri, string(it->key()), it->value()); |
1 |
1 |
2 |
|
| 110 |
|
| } |
| 111 |
3 |
| } |
| 112 |
|
| |
| 113 |
15 |
| static void add_query_args (URI* uri, SV** sp, I32 items, bool replace = false) { |
| 114 |
15 |
▶ | if (items == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 1) { |
6 |
9 |
15 |
|
| 115 |
6 |
▶ | if (SvROK(*sp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(*sp)) { |
3 |
3 |
6 |
|
| 116 |
3 |
▶ | Hash hash = *sp; |
Branch: 1 branch
| Branch | True | False | Total |
| Hash hash = *sp; |
3 |
0 |
3 |
|
| 117 |
3 |
▶ | if (hash) add_query_hash(uri, hash, replace); |
Branch: 2 branches
| Branch | True | False | Total |
| if (hash) add_query_hash(uri, hash, replace); |
3 |
0 |
3 |
| if (hash) add_query_hash(uri, hash, replace); |
3 |
0 |
3 |
|
| 118 |
3 |
| } |
| 119 |
3 |
▶ | else if (replace) uri->query(xs::in<string>(*sp)); |
Branch: 3 branches
| Branch | True | False | Total |
| else if (replace) uri->query(xs::in<string>(*sp)); |
0 |
3 |
3 |
| else if (replace) uri->query(xs::in<string>(*sp)); |
0 |
0 |
0 |
| else if (replace) uri->query(xs::in<string>(*sp)); |
0 |
0 |
0 |
|
| 120 |
3 |
▶ | else uri->add_query(xs::in<string>(*sp)); |
Branch: 2 branches
| Branch | True | False | Total |
| else uri->add_query(xs::in<string>(*sp)); |
3 |
0 |
3 |
| else uri->add_query(xs::in<string>(*sp)); |
3 |
0 |
3 |
|
| 121 |
|
| } |
| 122 |
|
| else { |
| 123 |
9 |
| SV** spe = sp + items; |
| 124 |
12 |
▶ | for (; sp < spe; sp += 2) add_param(uri, xs::in<string>(*sp), *(sp+1), replace); |
Branch: 4 branches
| Branch | True | False | Total |
| for (; sp < spe; sp += 2) add_param(uri, xs::in<string>(*sp), *(sp+1), replace); |
3 |
0 |
3 |
| for (; sp < spe; sp += 2) add_param(uri, xs::in<string>(*sp), *(sp+1), replace); |
3 |
0 |
3 |
| for (; sp < spe; sp += 2) add_param(uri, xs::in<string>(*sp), *(sp+1), replace); |
3 |
0 |
3 |
| for (; sp < spe; sp += 2) add_param(uri, xs::in<string>(*sp), *(sp+1), replace); |
3 |
9 |
12 |
|
| 125 |
|
| } |
| 126 |
15 |
| } |
| 127 |
|
| |
| 128 |
|
| MODULE = URI::XS PACKAGE = URI::XS |
| 129 |
|
| PROTOTYPES: DISABLE |
| 130 |
|
| |
| 131 |
|
| BOOT { |
| 132 |
13 |
| data_marker.svt_free = data_free; |
| 133 |
|
| |
| 134 |
13 |
▶ | xs::at_perl_destroy([]{ |
Branch: 2 branches
| Branch | True | False | Total |
| xs::at_perl_destroy([]{ |
13 |
0 |
13 |
| xs::at_perl_destroy([]{ |
13 |
0 |
13 |
|
| 135 |
13 |
| uri_class_map.clear(); |
| 136 |
13 |
| }); |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| URIx uri (string url = string(), int flags = 0) { |
| 140 |
19 |
| RETVAL = URI::create(url, flags); |
| 141 |
19 |
▶ | } |
Branch: 2 branches
| Branch | True | False | Total |
| } |
19 |
0 |
19 |
| } |
19 |
0 |
19 |
|
| 142 |
|
| |
| 143 |
|
| void register_scheme (string scheme, string_view perl_class) { |
| 144 |
1 |
▶ | register_perl_scheme(scheme, perl_class); |
Branch: 1 branch
| Branch | True | False | Total |
| register_perl_scheme(scheme, perl_class); |
1 |
0 |
1 |
|
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| uint64_t bench_parse (string str, int flags = 0) { |
| 148 |
0 |
| RETVAL = 0; |
| 149 |
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 |
|
| 150 |
0 |
▶ | URI u(str, flags); |
Branch: 1 branch
| Branch | True | False | Total |
| URI u(str, flags); |
0 |
0 |
0 |
|
| 151 |
0 |
| RETVAL += u.path().length(); |
| 152 |
0 |
| } |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| void test_parse (string str) { |
| 156 |
0 |
▶ | auto uri = URI(str); |
Branch: 1 branch
| Branch | True | False | Total |
| auto uri = URI(str); |
0 |
0 |
0 |
|
| 157 |
0 |
▶ | printf("scheme=%s\n", uri.scheme().c_str()); |
Branch: 2 branches
| Branch | True | False | Total |
| printf("scheme=%s\n", uri.scheme().c_str()); |
0 |
0 |
0 |
| printf("scheme=%s\n", uri.scheme().c_str()); |
0 |
0 |
0 |
|
| 158 |
0 |
▶ | printf("userinfo=%s\n", uri.user_info().c_str()); |
Branch: 2 branches
| Branch | True | False | Total |
| printf("userinfo=%s\n", uri.user_info().c_str()); |
0 |
0 |
0 |
| printf("userinfo=%s\n", uri.user_info().c_str()); |
0 |
0 |
0 |
|
| 159 |
0 |
▶ | printf("host=%s\n", uri.host().c_str()); |
Branch: 2 branches
| Branch | True | False | Total |
| printf("host=%s\n", uri.host().c_str()); |
0 |
0 |
0 |
| printf("host=%s\n", uri.host().c_str()); |
0 |
0 |
0 |
|
| 160 |
0 |
▶ | printf("port=%d\n", uri.port()); |
Branch: 1 branch
| Branch | True | False | Total |
| printf("port=%d\n", uri.port()); |
0 |
0 |
0 |
|
| 161 |
0 |
▶ | printf("path=%s\n", uri.path().c_str()); |
Branch: 2 branches
| Branch | True | False | Total |
| printf("path=%s\n", uri.path().c_str()); |
0 |
0 |
0 |
| printf("path=%s\n", uri.path().c_str()); |
0 |
0 |
0 |
|
| 162 |
0 |
▶ | printf("query=%s\n", uri.raw_query().c_str()); |
Branch: 3 branches
| Branch | True | False | Total |
| printf("query=%s\n", uri.raw_query().c_str()); |
0 |
0 |
0 |
| printf("query=%s\n", uri.raw_query().c_str()); |
0 |
0 |
0 |
| printf("query=%s\n", uri.raw_query().c_str()); |
0 |
0 |
0 |
|
| 163 |
0 |
▶ | printf("fragment=%s\n", uri.fragment().c_str()); |
Branch: 2 branches
| Branch | True | False | Total |
| printf("fragment=%s\n", uri.fragment().c_str()); |
0 |
0 |
0 |
| printf("fragment=%s\n", uri.fragment().c_str()); |
0 |
0 |
0 |
|
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
|
| void bench_parse_query (string str) { |
| 167 |
0 |
▶ | URI u; |
Branch: 1 branch
| Branch | True | False | Total |
| URI u; |
0 |
0 |
0 |
|
| 168 |
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 |
|
| 169 |
0 |
▶ | u.query_string(str); |
Branch: 1 branch
| Branch | True | False | Total |
| u.query_string(str); |
0 |
0 |
0 |
|
| 170 |
0 |
▶ | u.query(); |
Branch: 1 branch
| Branch | True | False | Total |
| u.query(); |
0 |
0 |
0 |
|
| 171 |
|
| } |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| uint64_t bench_encode_uri_component (string_view str) { |
| 175 |
0 |
| RETVAL = 0; |
| 176 |
0 |
| auto dest = (char*)alloca(str.length() * 3); |
| 177 |
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 |
|
| 178 |
0 |
▶ | encode_uri_component(str, dest); |
Branch: 1 branch
| Branch | True | False | Total |
| encode_uri_component(str, dest); |
0 |
0 |
0 |
|
| 179 |
|
| } |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
|
| uint64_t bench_decode_uri_component (string_view str) { |
| 183 |
0 |
| RETVAL = 0; |
| 184 |
0 |
| auto dest = (char*)alloca(str.length()); |
| 185 |
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 |
|
| 186 |
0 |
▶ | decode_uri_component(str, dest); |
Branch: 1 branch
| Branch | True | False | Total |
| decode_uri_component(str, dest); |
0 |
0 |
0 |
|
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| INCLUDE: encode.xsi |
| 191 |
|
| INCLUDE: URI.xsi |
| 192 |
|
| INCLUDE: schemas.xsi |
| 193 |
|
| INCLUDE: cloning.xsi |