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