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 | #include "face-uri.hpp" |
| 8 | #include "core/logger.hpp" |
| 9 | #include <boost/regex.hpp> |
| 10 | |
| 11 | NFD_LOG_INIT("FaceUri"); |
| 12 | |
| 13 | namespace nfd { |
| 14 | |
| 15 | FaceUri::FaceUri(const std::string& uri) |
| 16 | { |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 17 | if (!parse(uri)) { |
| 18 | throw Error("Malformed URI: " + uri); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | FaceUri::FaceUri(const char* uri) |
| 23 | { |
| 24 | if (!parse(uri)) { |
| 25 | throw Error("Malformed URI: " + std::string(uri)); |
| 26 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | bool |
| 30 | FaceUri::parse(const std::string& uri) |
| 31 | { |
| 32 | m_scheme.clear(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 33 | m_host.clear(); |
| 34 | m_isV6 = false; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 35 | m_port.clear(); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 36 | m_path.clear(); |
| 37 | |
| 38 | boost::regex protocolExp("(\\w+\\d?)://([^/]*)(\\/[^?]*)?"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 39 | boost::smatch protocolMatch; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 40 | if (!boost::regex_match(uri, protocolMatch, protocolExp)) { |
| 41 | return false; |
| 42 | } |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 43 | m_scheme = protocolMatch[1]; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 44 | m_path = protocolMatch[3]; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 45 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 46 | const std::string& authority = protocolMatch[2]; |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 47 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 48 | boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [host]:port |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 49 | boost::regex v4Exp("^((\\d+\\.){3}\\d+)(:(\\d+))?$"); |
Alexander Afanasyev | a39b90b | 2014-03-05 15:31:00 +0000 | [diff] [blame] | 50 | boost::regex hostExp("^(([^:]*))(:(\\d+))?$"); // host:port |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 51 | |
| 52 | boost::smatch match; |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 53 | m_isV6 = boost::regex_match(authority, match, v6Exp); |
| 54 | if (m_isV6 || |
| 55 | boost::regex_match(authority, match, v4Exp) || |
| 56 | boost::regex_match(authority, match, hostExp)) { |
| 57 | m_host = match[1]; |
| 58 | m_port = match[4]; |
| 59 | } |
| 60 | else { |
| 61 | if (m_path.empty()) { |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | NFD_LOG_DEBUG("URI [" << uri << "] parsed into: " << |
| 67 | m_scheme << ", " << m_host << ", " << m_port << ", " << m_path); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 71 | FaceUri::FaceUri(const boost::asio::ip::tcp::endpoint& endpoint) |
| 72 | { |
| 73 | m_isV6 = endpoint.address().is_v6(); |
| 74 | m_scheme = m_isV6 ? "tcp6" : "tcp4"; |
| 75 | m_host = endpoint.address().to_string(); |
| 76 | m_port = boost::lexical_cast<std::string>(endpoint.port()); |
| 77 | } |
| 78 | |
| 79 | FaceUri::FaceUri(const boost::asio::ip::udp::endpoint& endpoint) |
| 80 | { |
| 81 | m_isV6 = endpoint.address().is_v6(); |
| 82 | m_scheme = m_isV6 ? "udp6" : "udp4"; |
| 83 | m_host = endpoint.address().to_string(); |
| 84 | m_port = boost::lexical_cast<std::string>(endpoint.port()); |
| 85 | } |
| 86 | |
| 87 | FaceUri::FaceUri(const boost::asio::local::stream_protocol::endpoint& endpoint) |
| 88 | : m_isV6(false) |
| 89 | { |
| 90 | m_scheme = "unix"; |
| 91 | m_path = endpoint.path(); |
| 92 | } |
| 93 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 94 | } // namespace nfd |