xref: /haiku/headers/os/support/Url.h (revision 99158cceddacb52ebe30708c4e6a27b4fb711c8f)
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 
15 class BUrl : public BArchivable {
16 public:
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 
104 private:
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				_IsHostValid() const;
117 			bool				_IsHostIPV6Valid(size_t offset,
118 									int32 length) const;
119 			bool				_IsProtocolValid() const;
120 	static	bool				_IsUnreserved(char c);
121 	static	bool				_IsGenDelim(char c);
122 	static	bool				_IsSubDelim(char c);
123 	static	bool				_IsIPV6Char(char c);
124 	static	bool				_IsUsernameChar(char c);
125 	static	bool				_IsPasswordChar(char c);
126 	static	bool				_IsHostChar(char c);
127 	static	bool				_IsPortChar(char c);
128 
129 			BString				_UrlMimeType() const;
130 
131 private:
132 	mutable	BString				fUrlString;
133 	mutable	BString				fAuthority;
134 	mutable	BString				fUserInfo;
135 
136 			BString				fProtocol;
137 			BString				fUser;
138 			BString				fPassword;
139 			BString				fHost;
140 			int					fPort;
141 			BString				fPath;
142 			BString				fRequest;
143 			BString				fFragment;
144 
145 	mutable	bool				fUrlStringValid : 1;
146 	mutable	bool				fAuthorityValid : 1;
147 	mutable bool				fUserInfoValid : 1;
148 
149 			bool				fHasProtocol : 1;
150 			bool				fHasUserName : 1;
151 			bool				fHasPassword : 1;
152 			bool				fHasHost : 1;
153 			bool				fHasPort : 1;
154 			bool				fHasPath : 1;
155 			bool				fHasRequest : 1;
156 			bool				fHasFragment : 1;
157 };
158 
159 #endif  // _B_URL_H_
160