Apache Mesos
url.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __MESOS_ZOOKEEPER_URL_HPP__
18 #define __MESOS_ZOOKEEPER_URL_HPP__
19 
20 #include <string>
21 
23 
24 #include <stout/error.hpp>
25 #include <stout/option.hpp>
26 #include <stout/strings.hpp>
27 #include <stout/try.hpp>
28 
29 namespace zookeeper {
30 
31 // Describes a ZooKeeper URL of the form:
32 //
33 // zk://username:password@servers/path
34 //
35 // Where username:password is for the 'digest' scheme (see ZooKeeper
36 // documentation regarding "access controls using ACLs") and servers
37 // is of the form:
38 //
39 // host1:port1,host2:port2,host3:port3
40 //
41 // Note that in the future we may want to support authentication
42 // mechanisms other than 'digest' and have a URL of the following
43 // form.
44 //
45 // zk://scheme:credentials@servers/path
46 class URL
47 {
48 public:
49  static Try<URL> parse(const std::string& url);
50 
51  static const char* scheme()
52  {
53  return "zk://";
54  }
55 
57  const std::string servers;
58  const std::string path;
59 
60 private:
61  URL(const std::string& _servers,
62  const std::string& _path)
63  : servers(_servers),
64  path(_path) {}
65 
66  URL(const std::string& credentials,
67  const std::string& _servers,
68  const std::string& _path)
69  : authentication(Authentication("digest", credentials)),
70  servers(_servers),
71  path(_path) {}
72 };
73 
74 
75 inline Try<URL> URL::parse(const std::string& url)
76 {
77  std::string s = strings::trim(url);
78 
79  if (!strings::startsWith(s, URL::scheme())) {
80  return Error("Expecting 'zk://' at the beginning of the URL");
81  }
82  s = s.substr(5);
83 
84  // Look for the trailing '/' (if any), that's where the path starts.
85  std::string path;
86  do {
87  size_t index = s.find_last_of('/');
88 
89  if (index == std::string::npos) {
90  break;
91  } else {
92  path = s.substr(index) + path;
93  s = s.substr(0, index);
94  }
95  } while (true);
96 
97  if (path == "") {
98  path = "/";
99  }
100 
101  // Look for the trailing '@' (if any), that's where servers starts.
102  size_t index = s.find_last_of('@');
103 
104  if (index != std::string::npos) {
105  return URL(s.substr(0, index), s.substr(index + 1), path);
106  } else {
107  return URL(s, path);
108  }
109 }
110 
111 inline std::ostream& operator<<(std::ostream& stream, const URL& url)
112 {
113  stream << URL::scheme();
114  if (url.authentication.isSome()) {
115  stream << url.authentication.get() << "@";
116  }
117  return stream << url.servers << url.path;
118 }
119 
120 } // namespace zookeeper {
121 
122 #endif // __MESOS_ZOOKEEPER_URL_HPP__
const std::string servers
Definition: url.hpp:57
const std::string path
Definition: url.hpp:58
std::ostream & operator<<(std::ostream &stream, const Authentication &authentication)
Definition: authentication.hpp:61
Definition: errorbase.hpp:36
Definition: option.hpp:29
Definition: check.hpp:33
Definition: authentication.hpp:33
Definition: authentication.hpp:35
static Try< URL > parse(const std::string &url)
Definition: url.hpp:75
Definition: url.hpp:46
bool startsWith(const std::string &s, const std::string &prefix)
Definition: strings.hpp:381
static const char * scheme()
Definition: url.hpp:51
std::string trim(const std::string &from, Mode mode=ANY, const std::string &chars=WHITESPACE)
Definition: strings.hpp:67
const Option< Authentication > authentication
Definition: url.hpp:56