blob: 29647bbb421d5d7020bad5dbc860c73103ccddeb [file] [log] [blame]
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_CORE_FACE_URI_H
8#define NFD_CORE_FACE_URI_H
9
10#include "common.hpp"
11
12namespace nfd {
13
Junxiao Shi61e3cc52014-03-03 20:40:28 -070014/// represents a URI in Face Management protocol
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080015class FaceUri
16{
17public:
18 class Error : public std::runtime_error
19 {
20 public:
21 Error(const std::string& what) : std::runtime_error(what) {}
22 };
23
24 FaceUri();
Junxiao Shi61e3cc52014-03-03 20:40:28 -070025
26 /** \brief construct by parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080027 *
Junxiao Shi61e3cc52014-03-03 20:40:28 -070028 * \param uri scheme://domain[:port]/path
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080029 * \throw FaceUri::Error if URI cannot be parsed
30 */
31 explicit
32 FaceUri(const std::string& uri);
33
Junxiao Shi61e3cc52014-03-03 20:40:28 -070034 // This overload is needed so that calls with string literal won't be
35 // resolved to boost::asio::local::stream_protocol::endpoint overload.
36 explicit
37 FaceUri(const char* uri);
38
39 explicit
40 FaceUri(const boost::asio::ip::tcp::endpoint& endpoint);
41
42 explicit
43 FaceUri(const boost::asio::ip::udp::endpoint& endpoint);
44
45 explicit
46 FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint);
47
48 /// exception-safe parsing
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080049 bool
50 parse(const std::string& uri);
51
Junxiao Shi61e3cc52014-03-03 20:40:28 -070052 /// get scheme (protocol)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080053 const std::string&
54 getScheme() const;
55
Junxiao Shi61e3cc52014-03-03 20:40:28 -070056 /// get host (domain)
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080057 const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -070058 getHost() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080059
Junxiao Shi61e3cc52014-03-03 20:40:28 -070060 /// get port
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080061 const std::string&
62 getPort() const;
63
Junxiao Shi61e3cc52014-03-03 20:40:28 -070064 /// get path
65 const std::string&
66 getPath() const;
67
68 /// write as a string
69 std::string
70 toString() const;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080071
72private:
73 std::string m_scheme;
Junxiao Shi61e3cc52014-03-03 20:40:28 -070074 std::string m_host;
75 /// whether to add [] around host when writing string
76 bool m_isV6;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080077 std::string m_port;
Junxiao Shi61e3cc52014-03-03 20:40:28 -070078 std::string m_path;
79
80 friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri);
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080081};
82
83inline
84FaceUri::FaceUri()
85{
86}
87
88inline const std::string&
89FaceUri::getScheme() const
90{
91 return m_scheme;
92}
93
94inline const std::string&
Junxiao Shi61e3cc52014-03-03 20:40:28 -070095FaceUri::getHost() const
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080096{
Junxiao Shi61e3cc52014-03-03 20:40:28 -070097 return m_host;
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -080098}
99
100inline const std::string&
101FaceUri::getPort() const
102{
103 return m_port;
104}
105
Junxiao Shi61e3cc52014-03-03 20:40:28 -0700106inline const std::string&
107FaceUri::getPath() const
108{
109 return m_path;
110}
111
112inline std::string
113FaceUri::toString() const
114{
115 std::ostringstream os;
116 os << *this;
117 return os.str();
118}
119
120inline std::ostream&
121operator<<(std::ostream& os, const FaceUri& uri)
122{
123 os << uri.m_scheme << "://";
124 if (uri.m_isV6) {
125 os << "[" << uri.m_host << "]";
126 }
127 else {
128 os << uri.m_host;
129 }
130 if (!uri.m_port.empty()) {
131 os << ":" << uri.m_port;
132 }
133 os << uri.m_path;
134 return os;
135}
136
Alexander Afanasyev7b9347b2014-02-28 08:37:56 -0800137} // namespace nfd
138
139#endif // NFD_CORE_FACE_URI_H