Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1989 runge 1
/*
2
 *
3
 *  Copyright (C) 2012  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
 
22
#include "Url.h"
23
 
24
#include <boost/lexical_cast.hpp>
25
 
26
#include "common/common.h"
27
#include "common/exception.h"
28
#include "common/log.h"
29
 
30
namespace atom {
31
namespace net {
32
 
33
struct Protocol
34
{
35
  std::string         name_;
36
  ApplicationProtocol protocol_;
37
  unsigned int        default_port_;
38
};
39
 
40
static const Protocol protocols_[] = {
41
  { "http",     APPLICATION_PROTOCOL_HTTP,    80 },
42
  { "ftp",      APPLICATION_PROTOCOL_FTP,     21 }
43
};
44
 
45
static const std::string log_module_ = "net::url";
46
 
47
 
48
Url::Url()
49
{
50
  this->protocol_ = APPLICATION_PROTOCOL_UNKNOWN;
51
  this->port_     = 0;
52
  this->hostname_ = "";
53
  this->path_     = "";
54
  this->raw_      = "";
55
}
56
 
57
Url::Url(std::string url)
58
{
59
  this->SetUrl(url);
60
}
61
 
62
std::string Url::GetRaw()
63
{
64
  return this->raw_;
65
}
66
 
67
void Url::SetUrl(std::string url)
68
{
69
  std::string::size_type position = 0;
70
 
71
  /* Set raw */
72
  this->raw_ = url;
73
 
74
  // http://www.google.com:80/index.html
75
  log::Debug(log_module_, "Parsing url: \"%s\".", url.data());
76
 
77
 
78
  /* Initialize member variables */
79
  this->protocol_ = APPLICATION_PROTOCOL_UNKNOWN;
80
  this->port_     = 0;
81
  this->hostname_ = "";
82
  this->path_     = "";
83
 
84
 
85
  /* Parse protocol */
86
  position = url.find(":");
87
 
88
  if (std::string::npos == position)
89
  {
90
    throw exception::parsing_failed();
91
  }
92
 
93
  std::string protocol_string = url.substr(0, position);
94
 
95
  log::Debug(log_module_, "Parsed protocol string: \"%s\".", protocol_string.data());
96
 
97
  for (unsigned int n = 0; n < ELEMENTS_OF(protocols_); n++)
98
  {
99
    if (protocol_string == protocols_[n].name_)
100
    {
101
      this->protocol_ = protocols_[n].protocol_;
102
      this->port_     = protocols_[n].default_port_;
103
      break;
104
    }
105
  }
106
 
107
  url.erase(0, position + 3);
108
 
109
 
110
  /* Parse host */
111
  position = url.find_first_of(":/");
112
 
113
  if (std::string::npos == position)
114
  {
115
    position = url.length();
116
  }
117
 
118
  this->hostname_ = url.substr(0, position);
119
 
120
  log::Debug(log_module_, "Parsed hostname string: \"%s\".", this->hostname_.data());
121
 
122
  url.erase(0, position);
123
 
124
 
125
  if (url.length() > 0)
126
  {
127
    /* Parse port */
128
    if (':' == url[0])
129
    {
130
      position = url.find_first_of("/");
131
 
132
      if (std::string::npos == position)
133
      {
134
        position = url.length();
135
      }
136
 
137
      this->port_ = boost::lexical_cast<unsigned int>(url.substr(1, position - 1).data());
138
 
139
      log::Debug(log_module_, "Parsed port: %u.", this->port_);
140
 
141
      url.erase(0, position);
142
    }
143
  }
144
 
145
 
146
  /* Parse path */
147
  if (url.length() > 0)
148
  {
149
    this->path_ = url;
150
 
151
    log::Debug(log_module_, "Parsed path string: \"%s\".", this->path_.data());
152
  }
153
 
154
}
155
 
156
ApplicationProtocol Url::GetProtocol()
157
{
158
  return this->protocol_;
159
}
160
 
161
unsigned int Url::GetPort()
162
{
163
  return this->port_;
164
}
165
 
166
std::string Url::GetHostname()
167
{
168
  return this->hostname_;
169
}
170
 
171
std::string Url::GetPath()
172
{
173
  return this->path_;
174
}
175
 
176
 
177
}; // namespace net
178
}; // namespace atom