summaryrefslogtreecommitdiffstats
path: root/src/lib/http/url.cc
blob: fe68f58e6a1cffe11751eca932046b3365e1149b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (C) 2017-2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <exceptions/exceptions.h>
#include <http/url.h>
#include <boost/lexical_cast.hpp>
#include <sstream>

#include <iostream>

namespace isc {
namespace http {

Url::Url(const std::string& url)
    : url_(url), valid_(false), error_message_(), scheme_(Url::HTTPS),
      hostname_(), port_(0), path_() {
    parse();
}

bool
Url::operator<(const Url& url) const {
    return (toText() < url.toText());
}

Url::Scheme
Url::getScheme() const {
    checkValid();
    return (scheme_);
}

std::string
Url::getHostname() const {
    checkValid();
    return (hostname_);
}

std::string
Url::getStrippedHostname() const {
    std::string hostname = getHostname();
    if ((hostname.length() >= 2) && (hostname.at(0) == '[')) {
        return (hostname.substr(1, hostname.length() - 2));
    }

    return (hostname);
}

unsigned
Url::getPort() const {
    checkValid();
    return (port_);
}

std::string
Url::getPath() const {
    checkValid();
    return (path_);
}

std::string
Url::toText() const {
    std::ostringstream s;
    s << (getScheme() == HTTP ? "http" : "https");
    s << "://" << getHostname();

    if (getPort() != 0) {
        s << ":" << getPort();
    }

    s << getPath();

    return (s.str());
}

void
Url::checkValid() const {
    if (!isValid()) {
        isc_throw(InvalidOperation, "invalid URL " << url_ << ": " << error_message_);
    }
}

void
Url::parse() {
    valid_ = false;
    error_message_.clear();
    scheme_ = Url::HTTPS;
    hostname_.clear();
    port_ = 0;
    path_.clear();

    std::ostringstream error;

    // Retrieve scheme
    size_t offset = url_.find(":");
    if ((offset == 0) || (offset == std::string::npos)) {
        error << "url " << url_ << " lacks http or https scheme";
        error_message_ = error.str();
        return;
    }

    // Validate scheme.
    std::string scheme = url_.substr(0, offset);
    if (scheme == "http") {
        scheme_ = Url::HTTP;

    } else if (scheme == "https") {
        scheme_ = Url::HTTPS;

    } else {
        error << "invalid scheme " << scheme << " in " << url_;
        error_message_ = error.str();
        return;
    }

    // Colon and two slashes should follow the scheme
    if (url_.substr(offset, 3) != "://") {
        error << "expected :// after scheme in " << url_;
        error_message_ = error.str();
        return;
    }

    // Move forward to hostname.
    offset += 3;
    if (offset >= url_.length()) {
        error << "hostname missing in " << url_;
        error_message_ = error.str();
        return;
    }

    size_t offset2 = 0;

    // IPv6 address is specified within [ ].
    if (url_.at(offset) == '[') {
        offset2 = url_.find(']', offset);
        if (offset2 == std::string::npos) {
            error << "expected ] after IPv6 address in " << url_;
            error_message_ = error.str();
            return;

        } else if (offset2 == offset + 1) {
            error << "expected IPv6 address within [] in " << url_;
            error_message_ = error.str();
            return;
        }

        // Move one character beyond the ].
        ++offset2;

    } else {
        // There is a normal hostname or IPv4 address. It is terminated
        // by the colon (for port number), a slash (if no port number) or
        // goes up to the end of the URL.
        offset2 = url_.find(":", offset);
        if (offset2 == std::string::npos) {
            offset2 = url_.find("/", offset);
            if (offset2 == std::string::npos) {
                // No port number and no slash.
                offset2 = url_.length();
            }
        }
    }

    // Extract the hostname.
    hostname_ = url_.substr(offset, offset2 - offset);

    // If there is no port number and no path, simply return and mark the
    // URL as valid.
    if (offset2 == url_.length()) {
        valid_ = true;
        return;
    }

    // If there is a port number, we need to read it and convert to
    // numeric value.
    if (url_.at(offset2) == ':') {
        if (offset2 == url_.length() - 1) {
            error << "expected port number after : in " << url_;
            error_message_ = error.str();
            return;
        }
        // Move to the port number.
        ++offset2;

        // Port number may be terminated by a slash or by the end of URL.
        size_t slash_offset = url_.find('/', offset2);
        std::string port_str;
        if (slash_offset == std::string::npos) {
            port_str = url_.substr(offset2);
        } else {
            port_str = url_.substr(offset2, slash_offset - offset2);
        }

        try {
            // Try to convert the port number to numeric value.
            port_ = boost::lexical_cast<unsigned>(port_str);

        } catch (...) {
            error << "invalid port number " << port_str << " in " << url_;
            error_message_ = error.str();
            return;
        }

        // Go to the end of the port section.
        offset2 = slash_offset;
    }

    // If there is anything left in the URL, we consider it a path.
    if (offset2 != std::string::npos) {
        path_ = url_.substr(offset2);
        if (path_.empty()) {
            path_ = "/";
        }
    }

    valid_ = true;
}

} // end of namespace isc::http
} // end of namespace isc