Url.h revision a609673c
1/*
2 * Copyright 2010-2018 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			status_t			IDNAToAscii();
68			status_t			IDNAToUnicode();
69
70	// Url encoding/decoding of strings
71	static	BString				UrlEncode(const BString& url,
72									bool strict = false,
73									bool directory = false);
74	static	BString				UrlDecode(const BString& url,
75									bool strict = false);
76
77	// utility functionality
78			bool				HasPreferredApplication() const;
79			BString				PreferredApplication() const;
80			status_t			OpenWithPreferredApplication(
81									bool onProblemAskUser = true) const;
82
83	// BArchivable members
84	virtual	status_t			Archive(BMessage* into,
85									bool deep = true) const;
86	static	BArchivable*		Instantiate(BMessage* archive);
87
88	// URL comparison
89			bool				operator==(BUrl& other) const;
90			bool				operator!=(BUrl& other) const;
91
92	// URL assignment
93			const BUrl&			operator=(const BUrl& other);
94			const BUrl&			operator=(const BString& string);
95			const BUrl&			operator=(const char* string);
96
97	// URL to string conversion
98								operator const char*() const;
99
100private:
101			void				_ResetFields();
102			bool				_ContainsDelimiter(const BString& url);
103			status_t			_ExplodeUrlString(const BString& urlString,
104									uint32 flags);
105			BString				_MergePath(const BString& relative) const;
106			void				_SetPathUnsafe(const BString& path);
107
108	static	BString				_DoUrlEncodeChunk(const BString& chunk,
109									bool strict, bool directory = false);
110	static 	BString				_DoUrlDecodeChunk(const BString& chunk,
111									bool strict);
112
113			bool				_IsHostValid() const;
114			bool				_IsHostIPV6Valid(size_t offset,
115									int32 length) const;
116			bool				_IsProtocolValid() const;
117	static	bool				_IsUnreserved(char c);
118	static	bool				_IsGenDelim(char c);
119	static	bool				_IsSubDelim(char c);
120	static	bool				_IsIPV6Char(char c);
121	static	bool				_IsUsernameChar(char c);
122	static	bool				_IsPasswordChar(char c);
123	static	bool				_IsHostChar(char c);
124	static	bool				_IsPortChar(char c);
125
126	static	void				_RemoveLastPathComponent(BString& path);
127
128			BString				_UrlMimeType() const;
129
130private:
131	mutable	BString				fUrlString;
132	mutable	BString				fAuthority;
133	mutable	BString				fUserInfo;
134
135			BString				fProtocol;
136			BString				fUser;
137			BString				fPassword;
138			BString				fHost;
139			int					fPort;
140			BString				fPath;
141			BString				fRequest;
142			BString				fFragment;
143
144	mutable	bool				fUrlStringValid : 1;
145	mutable	bool				fAuthorityValid : 1;
146	mutable bool				fUserInfoValid : 1;
147
148			bool				fHasProtocol : 1;
149			bool				fHasUserName : 1;
150			bool				fHasPassword : 1;
151			bool				fHasHost : 1;
152			bool				fHasPort : 1;
153			bool				fHasPath : 1;
154			bool				fHasRequest : 1;
155			bool				fHasFragment : 1;
156};
157
158#endif  // _B_URL_H_
159