blob: a30bb7baf8c638584958ee62559fcdeb3b7ab6b2 [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#include "face-uri.hpp"
8#include "core/logger.hpp"
9#include <boost/regex.hpp>
10
11NFD_LOG_INIT("FaceUri");
12
13namespace nfd {
14
15FaceUri::FaceUri(const std::string& uri)
16{
17 if (!parse(uri))
18 {
19 throw Error("Malformed URI: " + uri);
20 }
21}
22
23bool
24FaceUri::parse(const std::string& uri)
25{
26 m_scheme.clear();
27 m_domain.clear();
28 m_port.clear();
29
30 boost::regex protocolExp("(\\w+\\d?)://(.+)");
31 boost::smatch protocolMatch;
32 if (!boost::regex_match(uri, protocolMatch, protocolExp))
33 {
34 return false;
35 }
36 m_scheme = protocolMatch[1];
37
38 const std::string& remote = protocolMatch[2];
39
40 boost::regex v6Exp("^\\[(([a-fA-F0-9:]+))\\](:(\\d+))?$"); // [stuff]:port
41 boost::regex v4Exp("^((\\d+\\.){3}\\d+)(:(\\d+))?$");
42 boost::regex hostExp("^(([^:]+))(:(\\d+))?$"); // stuff:port
43
44 boost::smatch match;
45 if (boost::regex_match(remote, match, v6Exp) ||
46 boost::regex_match(remote, match, v4Exp) ||
47 boost::regex_match(remote, match, hostExp))
48 {
49 m_domain = match[1];
50 m_port = match[4];
51 }
52 else
53 {
54 return false;
55 }
56
57 NFD_LOG_DEBUG("URI [" << uri << "] parsed into: "
58 << m_scheme << ", " << m_domain << ", " << m_port);
59 return true;
60}
61
62} // namespace nfd