summaryrefslogtreecommitdiff
path: root/src/lib/http/url.h
blob: c96641b120786c33031f1c17d6afa95dcf3c85fc (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
// 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/.

#ifndef KEA_URL_H
#define KEA_URL_H

#include <asiolink/io_address.h>
#include <string>

namespace isc {
namespace http {

/// @brief Represents an URL.
///
/// It parses the provided URL and allows for retrieving the parts
/// of it after parsing.
class Url {
public:

    /// @brief Scheme: https or http.
    enum Scheme {
        HTTP,
        HTTPS
    };

    /// @brief Constructor.
    ///
    /// Parses provided URL.
    ///
    /// @param url URL.
    explicit Url(const std::string& url);

    /// @brief compares URLs lexically.
    ///
    /// Both URLs are compared as text.
    ///
    /// @param url URL to be compared with
    /// @return true if the other operator is larger (in lexical sense)
    bool operator<(const Url& url) const;

    /// @brief Checks if the URL is valid.
    ///
    /// @return true if the URL is valid, false otherwise.
    bool isValid() const {
        return (valid_);
    }

    /// @brief Returns parsing error message.
    std::string getErrorMessage() const {
        return (error_message_);
    }

    /// @brief Returns parsed scheme.
    ///
    /// @throw InvalidOperation if URL is invalid.
    Scheme getScheme() const;

    /// @brief Returns hostname.
    ///
    /// @throw InvalidOperation if URL is invalid.
    std::string getHostname() const;

    /// @brief Returns hostname stripped from [ ] characters surrounding
    /// IPv6 address.
    ///
    /// @throw InvalidOperation of URL is invalid.
    std::string getStrippedHostname() const;

    /// @brief Returns port number.
    ///
    /// @return Port number or 0 if URL doesn't contain port number.
    /// @throw InvalidOperation if URL is invalid.
    unsigned getPort() const;

    /// @brief Returns path.
    ///
    /// @throw InvalidOperation if URL is invalid.
    std::string getPath() const;

    /// @brief Returns textual representation of the URL.
    std::string toText() const;

private:

    /// @brief Returns boolean value indicating if the URL is valid.
    void checkValid() const;

    /// @brief Parses URL.
    ///
    /// This method doesn't throw an exception. Call @c isValid to see
    /// if the URL is valid.
    void parse();

    /// @brief Holds specified URL.
    std::string url_;

    /// @brief A flag indicating if the URL is valid.
    bool valid_;

    /// @brief Holds error message after parsing.
    std::string error_message_;

    /// @brief Parsed scheme.
    Scheme scheme_;

    /// @brief Parsed hostname.
    std::string hostname_;

    /// @brief Parsed port number.
    unsigned port_;

    /// @brief Parsed path.
    std::string path_;
};

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

#endif // endif