xref: /haiku/headers/private/netservices/NetworkCookie.h (revision 52c4471a3024d2eb81fe88e2c3982b9f8daa5e56)
1 /*
2  * Copyright 2010 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _B_NETWORK_COOKIE_H_
6 #define _B_NETWORK_COOKIE_H_
7 
8 
9 #include <Archivable.h>
10 #include <DateTime.h>
11 #include <Message.h>
12 #include <String.h>
13 #include <Url.h>
14 
15 
16 namespace BPrivate {
17 
18 namespace Network {
19 
20 
21 class BNetworkCookie : public BArchivable {
22 public:
23 								BNetworkCookie(const char* name,
24 									const char* value, const BUrl& url);
25 								BNetworkCookie(const BString& cookieString,
26 									const BUrl& url);
27 								BNetworkCookie(BMessage* archive);
28 								BNetworkCookie();
29 	virtual						~BNetworkCookie();
30 
31 	// Parse a "SetCookie" string
32 
33 			status_t			ParseCookieString(const BString& string,
34 									const BUrl& url);
35 
36 	// Modify the cookie fields
37 			BNetworkCookie&		SetName(const BString& name);
38 			BNetworkCookie&		SetValue(const BString& value);
39 			status_t			SetDomain(const BString& domain);
40 			status_t			SetPath(const BString& path);
41 			BNetworkCookie&		SetMaxAge(int32 maxAge);
42 			BNetworkCookie&		SetExpirationDate(time_t expireDate);
43 			BNetworkCookie&		SetExpirationDate(BDateTime& expireDate);
44 			BNetworkCookie& 	SetSecure(bool secure);
45 			BNetworkCookie&		SetHttpOnly(bool httpOnly);
46 
47 	// Access the cookie fields
48 			const BString&		Name() const;
49 			const BString&		Value() const;
50 			const BString&		Domain() const;
51 			const BString&		Path() const;
52 			time_t				ExpirationDate() const;
53 			const BString&		ExpirationString() const;
54 			bool				Secure() const;
55 			bool				HttpOnly() const;
56 			const BString&		RawCookie(bool full) const;
57 
58 			bool				IsHostOnly() const;
59 			bool				IsSessionCookie() const;
60 			bool				IsValid() const;
61 			bool				IsValidForUrl(const BUrl& url) const;
62 			bool				IsValidForDomain(const BString& domain) const;
63 			bool				IsValidForPath(const BString& path) const;
64 
65 	// Test if cookie fields are defined
66 			bool				HasName() const;
67 			bool				HasValue() const;
68 			bool				HasDomain() const;
69 			bool				HasPath() const;
70 			bool				HasExpirationDate() const;
71 
72 	// Test if cookie could be deleted
73 			bool				ShouldDeleteAtExit() const;
74 			bool				ShouldDeleteNow() const;
75 
76 	// BArchivable members
77 	virtual	status_t			Archive(BMessage* into,
78 									bool deep = true) const;
79 	static	BArchivable*		Instantiate(BMessage* archive);
80 
81 	// Overloaded operators
82 			bool				operator==(const BNetworkCookie& other);
83 			bool				operator!=(const BNetworkCookie& other);
84 private:
85 			void				_Reset();
86 			int32				_ExtractNameValuePair(const BString& string,
87 									BString& name, BString& value,
88 									int32 index);
89 			int32				_ExtractAttributeValuePair(
90 									const BString& string, BString& name,
91 									BString& value,	int32 index);
92 			BString				_DefaultPathForUrl(const BUrl& url);
93 
94 			bool				_CanBeSetFromUrl(const BUrl& url) const;
95 			bool				_CanBeSetFromDomain(const BString& path) const;
96 			bool				_CanBeSetFromPath(const BString& path) const;
97 
98 private:
99 	mutable	BString				fRawCookie;
100 	mutable	bool				fRawCookieValid;
101 	mutable	BString				fRawFullCookie;
102 	mutable	bool				fRawFullCookieValid;
103 	mutable	BString				fExpirationString;
104 	mutable	bool				fExpirationStringValid;
105 
106 			BString				fName;
107 			BString				fValue;
108 			BString				fDomain;
109 			BString				fPath;
110 			BDateTime			fExpiration;
111 			status_t			fInitStatus;
112 			bool				fSecure;
113 			bool				fHttpOnly;
114 			bool				fHostOnly;
115 			bool				fSessionCookie;
116 };
117 
118 
119 } // namespace Network
120 
121 } // namespace BPrivate
122 
123 #endif // _B_NETWORK_COOKIE_H_
124 
125