Panda3D
Loading...
Searching...
No Matches
urlSpec.h
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file urlSpec.h
10 * @author drose
11 * @date 2002-09-24
12 */
13
14#ifndef URLSPEC_H
15#define URLSPEC_H
16
17#include "pandabase.h"
18#include "pnotify.h"
19
20class Filename;
21
22/**
23 * A container for a URL, e.g. "http://server:port/path".
24 *
25 * The URLSpec object is similar to a Filename in that it contains logic to
26 * identify the various parts of a URL and return (or modify) them separately.
27 */
28class EXPCL_PANDA_DOWNLOADER URLSpec {
29PUBLISHED:
30 URLSpec();
31 INLINE URLSpec(const std::string &url, bool server_name_expected = false);
32 URLSpec(const URLSpec &url, const Filename &path);
33 INLINE void operator = (const std::string &url);
34
35 INLINE bool operator == (const URLSpec &other) const;
36 INLINE bool operator != (const URLSpec &other) const;
37 INLINE bool operator < (const URLSpec &other) const;
38 int compare_to(const URLSpec &other) const;
39 size_t get_hash() const;
40
41 INLINE bool has_scheme() const;
42 INLINE bool has_authority() const;
43 INLINE bool has_username() const;
44 INLINE bool has_server() const;
45 INLINE bool has_port() const;
46 INLINE bool has_path() const;
47 INLINE bool has_query() const;
48
49 std::string get_scheme() const;
50 INLINE std::string get_authority() const;
51 INLINE std::string get_username() const;
52 INLINE std::string get_server() const;
53 INLINE std::string get_port_str() const;
54 uint16_t get_port() const;
55 std::string get_server_and_port() const;
56 bool is_default_port() const;
57 static int get_default_port_for_scheme(const std::string &scheme);
58 std::string get_path() const;
59 INLINE std::string get_query() const;
60 std::string get_path_and_query() const;
61 INLINE bool is_ssl() const;
62
63 INLINE const std::string &get_url() const;
64
65 void set_scheme(const std::string &scheme);
66 void set_authority(const std::string &authority);
67 void set_username(const std::string &username);
68 void set_server(const std::string &server);
69 void set_port(const std::string &port);
70 void set_port(uint16_t port);
71 void set_server_and_port(const std::string &server_and_port);
72 void set_path(const std::string &path);
73 void set_query(const std::string &query);
74
75 void set_url(const std::string &url, bool server_name_expected = false);
76
77 INLINE operator const std::string & () const;
78 INLINE const char *c_str() const;
79 INLINE bool empty() const;
80 INLINE operator bool() const;
81 INLINE size_t length() const;
82 INLINE size_t size() const;
83 INLINE char operator [] (size_t n) const;
84
85 bool input(std::istream &in);
86 void output(std::ostream &out) const;
87
88 static std::string quote(const std::string &source, const std::string &safe = "/");
89 static std::string quote_plus(const std::string &source, const std::string &safe = "/");
90 static std::string unquote(const std::string &source);
91 static std::string unquote_plus(const std::string &source);
92
93 MAKE_PROPERTY(scheme, get_scheme, set_scheme);
94 MAKE_PROPERTY(authority, get_authority, set_authority);
95 MAKE_PROPERTY(username, get_username, set_username);
96 MAKE_PROPERTY(server, get_server, set_server);
97 MAKE_PROPERTY(port, get_port, set_port);
98 MAKE_PROPERTY(server_and_port, get_server_and_port, set_server_and_port);
99 MAKE_PROPERTY(path, get_path, set_path);
100 MAKE_PROPERTY(query, get_query, set_query);
101 MAKE_PROPERTY(ssl, is_ssl);
102
103private:
104 void parse_authority();
105
106 enum Flags {
107 F_has_scheme = 0x0001,
108 F_has_authority = 0x0002,
109 F_has_username = 0x0004,
110 F_has_server = 0x0008,
111 F_has_port = 0x0010,
112 F_has_path = 0x0020,
113 F_has_query = 0x0040,
114 };
115
116 std::string _url;
117 uint16_t _port;
118 int _flags;
119
120 size_t _scheme_end;
121 size_t _username_start;
122 size_t _username_end;
123 size_t _server_start;
124 size_t _server_end;
125 size_t _port_start;
126 size_t _port_end;
127 size_t _path_start;
128 size_t _path_end;
129 size_t _query_start;
130};
131
132INLINE std::istream &operator >> (std::istream &in, URLSpec &url);
133INLINE std::ostream &operator << (std::ostream &out, const URLSpec &url);
134
135#include "urlSpec.I"
136
137#endif
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
A container for a URL, e.g.
Definition urlSpec.h:28
bool has_username() const
Returns true if the URL specifies a username (and/or password), false otherwise.
Definition urlSpec.I:77
bool empty() const
Returns false if the URLSpec is valid (not empty), or true if it is an empty string.
Definition urlSpec.I:209
bool has_path() const
Returns true if the URL includes a path specification (that is, the particular filename on the server...
Definition urlSpec.I:102
static int get_default_port_for_scheme(const std::string &scheme)
Returns the default port number for the indicated scheme, or 0 if there is no known default.
Definition urlSpec.cxx:172
set_port
Replaces the port part of the URL specification.
Definition urlSpec.h:97
set_scheme
Replaces the scheme part of the URL specification.
Definition urlSpec.h:93
set_path
Replaces the path part of the URL specification.
Definition urlSpec.h:99
get_username
Returns the username specified by the URL, if any.
Definition urlSpec.h:95
set_query
Replaces the query part of the URL specification.
Definition urlSpec.h:100
bool has_authority() const
Returns true if the URL specifies an authority (this includes username, server, and/or port),...
Definition urlSpec.I:68
static std::string quote(const std::string &source, const std::string &safe="/")
Returns the source string with all "unsafe" characters quoted, making a string suitable for placing i...
Definition urlSpec.cxx:719
bool has_query() const
Returns true if the URL includes a query specification, false otherwise.
Definition urlSpec.I:110
const std::string & get_url() const
Returns the complete URL specification.
Definition urlSpec.I:184
bool is_default_port() const
Returns true if the port number encoded in this URL is the default port number for the scheme (or if ...
Definition urlSpec.cxx:160
set_server
Replaces the server part of the URL specification.
Definition urlSpec.h:96
get_server
Returns the server name specified by the URL, if any.
Definition urlSpec.h:96
get_query
Returns the query specified by the URL, or empty string if no query is specified.
Definition urlSpec.h:100
get_path
Returns the path specified by the URL, or "/" if no path is specified.
Definition urlSpec.h:99
static std::string unquote_plus(const std::string &source)
Reverses the operation of quote_plus(): converts escaped characters of the form "%xx" to their ascii ...
Definition urlSpec.cxx:837
std::string get_port_str() const
Returns the port specified by the URL as a string, or the empty string if no port is specified.
Definition urlSpec.I:148
get_server_and_port
Returns a string consisting of the server name, followed by a colon, followed by the port number.
Definition urlSpec.h:98
bool has_port() const
Returns true if the URL specifies a port number, false otherwise.
Definition urlSpec.I:93
bool has_scheme() const
Returns true if the URL specifies a scheme (e.g.
Definition urlSpec.I:59
get_scheme
Returns the scheme specified by the URL, or empty string if no scheme is specified.
Definition urlSpec.h:93
static std::string unquote(const std::string &source)
Reverses the operation of quote(): converts escaped characters of the form "%xx" to their ascii equiv...
Definition urlSpec.cxx:801
is_ssl
Returns true if the URL's scheme specifies an SSL-secured protocol such as https, or false otherwise.
Definition urlSpec.h:101
void set_url(const std::string &url, bool server_name_expected=false)
Completely replaces the URL with the indicated string.
Definition urlSpec.cxx:554
get_port
Returns the port number specified by the URL, or the default port if not specified.
Definition urlSpec.h:97
set_authority
Replaces the authority part of the URL specification.
Definition urlSpec.h:94
static std::string quote_plus(const std::string &source, const std::string &safe="/")
Behaves like quote() with the additional behavior of replacing spaces with plus signs.
Definition urlSpec.cxx:758
bool has_server() const
Returns true if the URL specifies a server name, false otherwise.
Definition urlSpec.I:85
set_server_and_port
Replaces the server and port parts of the URL specification simultaneously.
Definition urlSpec.h:98
std::string get_path_and_query() const
Returns the path (or "/" if no path is specified), followed by the query if it is specified.
Definition urlSpec.cxx:228
set_username
Replaces the username part of the URL specification.
Definition urlSpec.h:95
get_authority
Returns the authority specified by the URL (this includes username, server, and/or port),...
Definition urlSpec.h:94
int compare_to(const URLSpec &other) const
Returns a number less than zero if this URLSpec sorts before the other one, greater than zero if it s...
Definition urlSpec.cxx:77
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.