////////////////////////////////////////////////////////////////////////// Parse a URL to obtain a description for use with metrics when no user provided tag is available. This is used by the remap side of the plugin, while the SNI side uses the FQDN associated with each limiter instance which is obtained from the list of SNIs in the global plugin configuration.
| 83 | // which is obtained from the list of SNIs in the global plugin configuration. |
| 84 | // |
| 85 | std::string |
| 86 | getDescriptionFromUrl(const char *url) |
| 87 | { |
| 88 | TSMBuffer const buf = TSMBufferCreate(); |
| 89 | TSMLoc url_loc = nullptr; |
| 90 | |
| 91 | const int url_len = strlen(url); |
| 92 | std::string description; |
| 93 | |
| 94 | if (TS_SUCCESS == TSUrlCreate(buf, &url_loc) && TS_PARSE_DONE == TSUrlParse(buf, url_loc, &url, url + url_len)) { |
| 95 | int host_len, scheme_len = 0; |
| 96 | const char *s = TSUrlSchemeGet(buf, url_loc, &scheme_len); |
| 97 | const char *h = TSUrlHostGet(buf, url_loc, &host_len); |
| 98 | const int port = TSUrlPortGet(buf, url_loc); |
| 99 | |
| 100 | const std::string hostname = std::string(h, host_len); |
| 101 | const std::string scheme = std::string(s, scheme_len); |
| 102 | |
| 103 | Dbg(dbg_ctl, "scheme = %s, host = %s, port = %d", scheme.c_str(), hostname.c_str(), port); |
| 104 | |
| 105 | description = scheme; |
| 106 | description.append("."); |
| 107 | description.append(hostname); |
| 108 | |
| 109 | // only append the port when it is non-standard |
| 110 | if (!(strncmp(s, TS_URL_SCHEME_HTTP, scheme_len) == 0 && port == 80) && |
| 111 | !(strncmp(s, TS_URL_SCHEME_HTTPS, scheme_len) == 0 && port == 443)) { |
| 112 | description.push_back(':'); |
| 113 | description.append(std::to_string(port)); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (url_loc != nullptr) { |
| 118 | TSHandleMLocRelease(buf, nullptr, url_loc); |
| 119 | } |
| 120 | |
| 121 | TSMBufferDestroy(buf); |
| 122 | |
| 123 | return description; |
| 124 | } |
no test coverage detected