Url.h revision 2c26ad4b
1/*
2 * Copyright 2010-2016 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _B_URL_H_
6#define _B_URL_H_
7
8
9#include <Archivable.h>
10#include <Message.h>
11#include <Path.h>
12#include <String.h>
13
14
15class BUrl : public BArchivable {
16public:
17								BUrl(const char* url);
18								BUrl(BMessage* archive);
19								BUrl(const BUrl& other);
20								BUrl(const BUrl& base, const BString& relative);
21								BUrl(const BPath& path);
22								BUrl();
23	virtual						~BUrl();
24
25	// URL fields modifiers
26			BUrl&				SetUrlString(const BString& url);
27			BUrl&				SetProtocol(const BString& scheme);
28			BUrl&				SetUserName(const BString& user);
29			BUrl&				SetPassword(const BString& password);
30			void				SetAuthority(const BString& authority);
31			BUrl&				SetHost(const BString& host);
32			BUrl&				SetPort(int port);
33			BUrl&				SetPath(const BString& path);
34			BUrl&				SetRequest(const BString& request);
35			BUrl&				SetFragment(const BString& fragment);
36
37	// URL fields access
38			const BString&		UrlString() const;
39			const BString&		Protocol() const;
40			const BString&		UserName() const;
41			const BString&		Password() const;
42			const BString&		UserInfo() const;
43			const BString&		Host() const;
44			int 				Port() const;
45			const BString&		Authority() const;
46			const BString&		Path() const;
47			const BString&		Request() const;
48			const BString&		Fragment() const;
49
50	// URL fields tests
51			bool 				IsValid() const;
52			bool				HasProtocol() const;
53			bool				HasUserName() const;
54			bool				HasPassword() const;
55			bool				HasUserInfo() const;
56			bool				HasHost() const;
57			bool				HasPort() const;
58			bool				HasAuthority() const;
59			bool				HasPath() const;
60			bool				HasRequest() const;
61			bool				HasFragment() const;
62
63	// Url encoding/decoding of needed fields
64			void				UrlEncode(bool strict = false);
65			void				UrlDecode(bool strict = false);
66
67#ifdef HAIKU_TARGET_PLATFORM_HAIKU
68			status_t			IDNAToAscii();
69			status_t			IDNAToUnicode();
70#endif
71
72	// Url encoding/decoding of strings
73	static	BString				UrlEncode(const BString& url,
74									bool strict = false,
75									bool directory = false);
76	static	BString				UrlDecode(const BString& url,
77									bool strict = false);
78
79#ifdef HAIKU_TARGET_PLATFORM_HAIKU
80	// utility functionality
81			bool				HasPreferredApplication() const;
82			BString				PreferredApplication() const;
83			status_t			OpenWithPreferredApplication(
84									bool onProblemAskUser = true) const;
85#endif
86
87	// BArchivable members
88	virtual	status_t			Archive(BMessage* into,
89									bool deep = true) const;
90	static	BArchivable*		Instantiate(BMessage* archive);
91
92	// URL comparison
93			bool				operator==(BUrl& other) const;
94			bool				operator!=(BUrl& other) const;
95
96	// URL assignment
97			const BUrl&			operator=(const BUrl& other);
98			const BUrl&			operator=(const BString& string);
99			const BUrl&			operator=(const char* string);
100
101	// URL to string conversion
102								operator const char*() const;
103
104private:
105			void				_ResetFields();
106			bool				_ContainsDelimiter(const BString& url);
107			status_t			_ExplodeUrlString(const BString& urlString);
108			BString				_MergePath(const BString& relative) const;
109			void				_SetPathUnsafe(const BString& path);
110
111	static	BString				_DoUrlEncodeChunk(const BString& chunk,
112									bool strict, bool directory = false);
113	static 	BString				_DoUrlDecodeChunk(const BString& chunk,
114									bool strict);
115
116			bool				_IsProtocolValid();
117	static	bool				_IsUnreserved(char c);
118	static	bool				_IsGenDelim(char c);
119	static	bool				_IsSubDelim(char c);
120
121			BString				_UrlMimeType() const;
122
123private:
124	mutable	BString				fUrlString;
125	mutable	BString				fAuthority;
126	mutable	BString				fUserInfo;
127
128			BString				fProtocol;
129			BString				fUser;
130			BString				fPassword;
131			BString				fHost;
132			int					fPort;
133			BString				fPath;
134			BString				fRequest;
135			BString				fFragment;
136
137	mutable	bool				fUrlStringValid : 1;
138	mutable	bool				fAuthorityValid : 1;
139	mutable bool				fUserInfoValid : 1;
140
141			bool				fHasProtocol : 1;
142			bool				fHasUserName : 1;
143			bool				fHasPassword : 1;
144			bool				fHasHost : 1;
145			bool				fHasPort : 1;
146			bool				fHasPath : 1;
147			bool				fHasRequest : 1;
148			bool				fHasFragment : 1;
149};
150
151#endif  // _B_URL_H_
152