Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | namespace nfd { |
| 13 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 14 | /// represents a URI in Face Management protocol |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 15 | class FaceUri |
| 16 | { |
| 17 | public: |
| 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 Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 25 | |
| 26 | /** \brief construct by parsing |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 27 | * |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 28 | * \param uri scheme://domain[:port]/path |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 29 | * \throw FaceUri::Error if URI cannot be parsed |
| 30 | */ |
| 31 | explicit |
| 32 | FaceUri(const std::string& uri); |
| 33 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 34 | // 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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 49 | bool |
| 50 | parse(const std::string& uri); |
| 51 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 52 | /// get scheme (protocol) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 53 | const std::string& |
| 54 | getScheme() const; |
| 55 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 56 | /// get host (domain) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 57 | const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 58 | getHost() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 59 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 60 | /// get port |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 61 | const std::string& |
| 62 | getPort() const; |
| 63 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | /// get path |
| 65 | const std::string& |
| 66 | getPath() const; |
| 67 | |
| 68 | /// write as a string |
| 69 | std::string |
| 70 | toString() const; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | std::string m_scheme; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 74 | std::string m_host; |
| 75 | /// whether to add [] around host when writing string |
| 76 | bool m_isV6; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 77 | std::string m_port; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 78 | std::string m_path; |
| 79 | |
| 80 | friend std::ostream& operator<<(std::ostream& os, const FaceUri& uri); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | inline |
| 84 | FaceUri::FaceUri() |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | inline const std::string& |
| 89 | FaceUri::getScheme() const |
| 90 | { |
| 91 | return m_scheme; |
| 92 | } |
| 93 | |
| 94 | inline const std::string& |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 95 | FaceUri::getHost() const |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 96 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 97 | return m_host; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | inline const std::string& |
| 101 | FaceUri::getPort() const |
| 102 | { |
| 103 | return m_port; |
| 104 | } |
| 105 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 106 | inline const std::string& |
| 107 | FaceUri::getPath() const |
| 108 | { |
| 109 | return m_path; |
| 110 | } |
| 111 | |
| 112 | inline std::string |
| 113 | FaceUri::toString() const |
| 114 | { |
| 115 | std::ostringstream os; |
| 116 | os << *this; |
| 117 | return os.str(); |
| 118 | } |
| 119 | |
| 120 | inline std::ostream& |
| 121 | operator<<(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 Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 137 | } // namespace nfd |
| 138 | |
| 139 | #endif // NFD_CORE_FACE_URI_H |