blob: 8169b0eee121a5c4082b39b576b5ca497c98c8bd [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- UriParser.cpp -----------------------------------------------------===//
Vince Harron3218c0f2015-01-06 23:38:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Harron3218c0f2015-01-06 23:38:24 +00006//
7//===----------------------------------------------------------------------===//
8
Pavel Labath5f7e5832017-02-10 12:21:22 +00009#include "lldb/Utility/UriParser.h"
Zachary Turner4479ac12017-04-06 18:12:24 +000010
11#include <string>
12
13#include <stdint.h>
14#include <tuple>
Vince Harrone6c5dcf2015-01-15 20:57:01 +000015
16using namespace lldb_private;
Vince Harron3218c0f2015-01-06 23:38:24 +000017
Vince Harron3218c0f2015-01-06 23:38:24 +000018// UriParser::Parse
Zachary Turner245f7fd2016-11-17 01:38:02 +000019bool UriParser::Parse(llvm::StringRef uri, llvm::StringRef &scheme,
20 llvm::StringRef &hostname, int &port,
21 llvm::StringRef &path) {
Pavel Labatheac00c32017-11-02 21:35:26 +000022 llvm::StringRef tmp_scheme, tmp_hostname, tmp_path;
Vince Harron3218c0f2015-01-06 23:38:24 +000023
Zachary Turner245f7fd2016-11-17 01:38:02 +000024 const llvm::StringRef kSchemeSep("://");
Kate Stoneb9c1b512016-09-06 20:57:50 +000025 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 Turner245f7fd2016-11-17 01:38:02 +000031 auto host_pos = pos + kSchemeSep.size();
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 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 Clayton2434d452017-07-24 16:47:04 +000043 if (!host_port.empty() && host_port[0] == '[') {
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 // hostname is enclosed with square brackets.
Emre Kultursay57be22f2020-03-26 10:33:57 +010045 pos = host_port.rfind(']');
Oleksiy Vyalov54971852015-08-20 23:09:34 +000046 if (pos == std::string::npos)
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 return false;
Oleksiy Vyalov54971852015-08-20 23:09:34 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 tmp_hostname = host_port.substr(1, pos - 1);
Zachary Turner245f7fd2016-11-17 01:38:02 +000050 host_port = host_port.drop_front(pos + 1);
51 if (!host_port.empty() && !host_port.consume_front(":"))
52 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 } else {
Zachary Turner245f7fd2016-11-17 01:38:02 +000054 std::tie(tmp_hostname, host_port) = host_port.split(':');
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 }
Oleksiy Vyalov54971852015-08-20 23:09:34 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 // Extract port
Zachary Turner245f7fd2016-11-17 01:38:02 +000058 if (!host_port.empty()) {
59 uint16_t port_value = 0;
60 if (host_port.getAsInteger(0, port_value))
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 return false;
Zachary Turner245f7fd2016-11-17 01:38:02 +000062 port = port_value;
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 } else
64 port = -1;
Oleksiy Vyalov54971852015-08-20 23:09:34 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 scheme = tmp_scheme;
67 hostname = tmp_hostname;
68 path = tmp_path;
69 return true;
Vince Harron3218c0f2015-01-06 23:38:24 +000070}