Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- UriParser.cpp -----------------------------------------------------===// |
Vince Harron | 3218c0f | 2015-01-06 23:38:24 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Vince Harron | 3218c0f | 2015-01-06 23:38:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Pavel Labath | 5f7e583 | 2017-02-10 12:21:22 +0000 | [diff] [blame] | 9 | #include "lldb/Utility/UriParser.h" |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include <stdint.h> |
| 14 | #include <tuple> |
Vince Harron | e6c5dcf | 2015-01-15 20:57:01 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace lldb_private; |
Vince Harron | 3218c0f | 2015-01-06 23:38:24 +0000 | [diff] [blame] | 17 | |
Vince Harron | 3218c0f | 2015-01-06 23:38:24 +0000 | [diff] [blame] | 18 | // UriParser::Parse |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 19 | bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme, |
| 20 | llvm::StringRef &hostname, int &port, |
| 21 | llvm::StringRef &path) { |
Pavel Labath | eac00c3 | 2017-11-02 21:35:26 +0000 | [diff] [blame] | 22 | llvm::StringRef tmp_scheme, tmp_hostname, tmp_path; |
Vince Harron | 3218c0f | 2015-01-06 23:38:24 +0000 | [diff] [blame] | 23 | |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 24 | const llvm::StringRef kSchemeSep("://"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | auto pos = uri.find(kSchemeSep); |
| 26 | if (pos == std::string::npos) |
| 27 | return false; |
| 28 | |
| 29 | // Extract path. |
| 30 | tmp_scheme = uri.substr(0, pos); |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 31 | auto host_pos = pos + kSchemeSep.size(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | auto path_pos = uri.find('/', host_pos); |
| 33 | if (path_pos != std::string::npos) |
| 34 | tmp_path = uri.substr(path_pos); |
| 35 | else |
| 36 | tmp_path = "/"; |
| 37 | |
| 38 | auto host_port = uri.substr( |
| 39 | host_pos, |
| 40 | ((path_pos != std::string::npos) ? path_pos : uri.size()) - host_pos); |
| 41 | |
| 42 | // Extract hostname |
Greg Clayton | 2434d45 | 2017-07-24 16:47:04 +0000 | [diff] [blame] | 43 | if (!host_port.empty() && host_port[0] == '[') { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | // hostname is enclosed with square brackets. |
Emre Kultursay | 57be22f | 2020-03-26 10:33:57 +0100 | [diff] [blame] | 45 | pos = host_port.rfind(']'); |
Oleksiy Vyalov | 5497185 | 2015-08-20 23:09:34 +0000 | [diff] [blame] | 46 | if (pos == std::string::npos) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | return false; |
Oleksiy Vyalov | 5497185 | 2015-08-20 23:09:34 +0000 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | tmp_hostname = host_port.substr(1, pos - 1); |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 50 | host_port = host_port.drop_front(pos + 1); |
| 51 | if (!host_port.empty() && !host_port.consume_front(":")) |
| 52 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | } else { |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 54 | std::tie(tmp_hostname, host_port) = host_port.split(':'); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | } |
Oleksiy Vyalov | 5497185 | 2015-08-20 23:09:34 +0000 | [diff] [blame] | 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | // Extract port |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 58 | if (!host_port.empty()) { |
| 59 | uint16_t port_value = 0; |
| 60 | if (host_port.getAsInteger(0, port_value)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | return false; |
Zachary Turner | 245f7fd | 2016-11-17 01:38:02 +0000 | [diff] [blame] | 62 | port = port_value; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | } else |
| 64 | port = -1; |
Oleksiy Vyalov | 5497185 | 2015-08-20 23:09:34 +0000 | [diff] [blame] | 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | scheme = tmp_scheme; |
| 67 | hostname = tmp_hostname; |
| 68 | path = tmp_path; |
| 69 | return true; |
Vince Harron | 3218c0f | 2015-01-06 23:38:24 +0000 | [diff] [blame] | 70 | } |