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