| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/url.cc |
| Date: | 2025-11-09 02:35:23 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 46 | 48 | 95.8% |
| Branches: | 47 | 73 | 64.4% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "url.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | |||
| 9 | #include "util/string.h" | ||
| 10 | |||
| 11 | const int Url::kDefaultPort = 80; | ||
| 12 | const char *Url::kDefaultProtocol = "http"; | ||
| 13 | |||
| 14 | 324 | Url *Url::Parse(const std::string &url, const std::string &default_protocol, | |
| 15 | int default_port) { | ||
| 16 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 306 times.
|
324 | if (url.empty()) { |
| 17 | 18 | return NULL; | |
| 18 | } | ||
| 19 | |||
| 20 | 306 | size_t cursor = 0; | |
| 21 | |||
| 22 | // Is there a protocol prefix? | ||
| 23 |
1/2✓ Branch 1 taken 306 times.
✗ Branch 2 not taken.
|
306 | std::string protocol = default_protocol; |
| 24 | 306 | const size_t sep_pos = url.find("://"); | |
| 25 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 180 times.
|
306 | if (sep_pos != std::string::npos) { |
| 26 |
1/2✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
|
126 | protocol = url.substr(0, sep_pos); |
| 27 | 126 | cursor = sep_pos + 3; | |
| 28 | } | ||
| 29 | |||
| 30 | 306 | std::string host; | |
| 31 | 306 | std::string path; | |
| 32 | 306 | uint64_t port = default_port; | |
| 33 | |||
| 34 | // Try to find another ":", preceding a port number | ||
| 35 | 306 | const size_t col_pos = url.find(":", cursor); | |
| 36 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 180 times.
|
306 | if (col_pos != std::string::npos) { |
| 37 | // Port number was given | ||
| 38 |
1/2✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
|
126 | host = url.substr(cursor, col_pos - cursor); |
| 39 | 126 | cursor = col_pos + 1; | |
| 40 | |||
| 41 | 126 | const size_t slash_pos = url.find("/", cursor); | |
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
|
126 | if (slash_pos == 0) { |
| 43 | ✗ | return NULL; | |
| 44 | } | ||
| 45 | |||
| 46 | // Check that part between ":" and "/" or the end of the string is an | ||
| 47 | // unsigned number | ||
| 48 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 36 times.
|
126 | const size_t port_end = slash_pos == std::string::npos ? std::string::npos |
| 49 | : slash_pos - cursor; | ||
| 50 |
4/7✓ Branch 1 taken 126 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 126 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 90 times.
✓ Branch 8 taken 36 times.
|
126 | if (!String2Uint64Parse(url.substr(cursor, port_end), &port)) { |
| 51 | 90 | return NULL; | |
| 52 | } | ||
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 18 times.
|
36 | if (slash_pos != std::string::npos) { |
| 55 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | path = url.substr(slash_pos); |
| 56 | } | ||
| 57 | } else { | ||
| 58 | // No port number was given | ||
| 59 | 180 | const size_t slash_pos = url.find("/", cursor); | |
| 60 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 108 times.
|
180 | if (slash_pos != std::string::npos) { |
| 61 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | host = url.substr(cursor, slash_pos - cursor); |
| 62 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | path = url.substr(slash_pos); |
| 63 | } else { | ||
| 64 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | host = url.substr(cursor); |
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | // At the moment, host name validation is very basic | ||
| 69 |
3/4✓ Branch 1 taken 216 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 144 times.
|
216 | if (!ValidateHost(host)) { |
| 70 | 72 | return NULL; | |
| 71 | } | ||
| 72 | |||
| 73 |
2/4✓ Branch 1 taken 144 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
|
144 | return new Url(protocol, host, path, port); |
| 74 | 306 | } | |
| 75 | |||
| 76 | 216 | bool Url::ValidateHost(const std::string &host) { | |
| 77 |
2/2✓ Branch 1 taken 54 times.
✓ Branch 2 taken 162 times.
|
216 | if (host.empty()) { |
| 78 | 54 | return false; | |
| 79 | } | ||
| 80 | |||
| 81 | // Host must not be just a number | ||
| 82 | uint64_t test; | ||
| 83 |
3/4✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 144 times.
|
162 | if (String2Uint64Parse(host, &test)) { |
| 84 | 18 | return false; | |
| 85 | } | ||
| 86 | |||
| 87 | 144 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 | ✗ | Url::Url() : protocol_(), host_(), path_(), port_(), address_() { } | |
| 91 | |||
| 92 | 144 | Url::Url(const std::string &protocol, const std::string &host, | |
| 93 | 144 | const std::string &path, int port) | |
| 94 |
2/4✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 144 times.
✗ Branch 6 not taken.
|
144 | : protocol_(protocol), host_(host), path_(path), port_(port), address_() { |
| 95 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 90 times.
|
144 | if (port_ != kDefaultPort) { |
| 96 |
6/12✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 54 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 54 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 54 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 54 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 54 times.
✗ Branch 17 not taken.
|
54 | address_ = protocol + "://" + host + ":" + StringifyInt(port_) + path; |
| 97 | } else { | ||
| 98 |
3/6✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 90 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 90 times.
✗ Branch 8 not taken.
|
90 | address_ = protocol + "://" + host + path; |
| 99 | } | ||
| 100 | 144 | } | |
| 101 | 144 | Url::~Url() { } | |
| 102 |