Skip to content

Commit 237d2ec

Browse files
committed
New cookie object that allows you to build a cookie.
New cookie_date object that allows you to specify an expire date for cookies. New cookie_time object that allows you to specify an expire time for cookies. cookie_time and cookie_date will be part of cookie_datetime object (not yed uploaded). Refactoring of curl_cookie class.
1 parent 0dc83d0 commit 237d2ec

13 files changed

+536
-70
lines changed

include/cookie.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 - Giuseppe Persico
5+
* File - cookie.h
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
#ifndef CURLCPP_COOKIE_H
27+
#define CURLCPP_COOKIE_H
28+
29+
#include <string>
30+
31+
#include "curl_exception.h"
32+
33+
namespace curl {
34+
/**
35+
* This class allows to specify every single field of an http-style cookie.
36+
*/
37+
class cookie {
38+
public:
39+
cookie() {}
40+
/**
41+
* The constructor allow a fast way to build a cookie.
42+
*/
43+
cookie(const std::string, const std::string, const std::string, const bool = false);
44+
/**
45+
* This constructor overloades the previous one.
46+
*/
47+
cookie(const char *, const char *, const char *, bool = false);
48+
/**
49+
* This method allows to specify the cookie name. It returns a pointer
50+
* to this class to enable cascade calls.
51+
*/
52+
cookie *set_name(const std::string);
53+
/**
54+
* This method overloads the previous one allowing to specify a const char *
55+
* instead of a string.
56+
*/
57+
cookie *set_name(const char *);
58+
/**
59+
* This method allows to specify the cookie path. It returns a pointer
60+
* to this class to enable cascade calls.
61+
*/
62+
cookie *set_path(const std::string) NOEXCEPT;
63+
/**
64+
* This method overloads the previous one allowing to specify a const char *
65+
* instead of a string.
66+
*/
67+
cookie *set_path(const char *) NOEXCEPT;
68+
/**
69+
* This method allows to specify the cookie domain. It returns a pointer
70+
* to this class to enable cascade calls.
71+
*/
72+
cookie *set_domain(const std::string) NOEXCEPT;
73+
/**
74+
* This method overloads the previous one allowing to specify a const char *
75+
* instead of a string.
76+
*/
77+
cookie *set_domain(const char *) NOEXCEPT;
78+
/**
79+
* This method allows to specify the cookie security. It returns a pointer
80+
* to this class to enable cascade calls.
81+
*/
82+
cookie *set_secure(const bool) NOEXCEPT;
83+
/**
84+
* This method overloads the previous one allowing to specify an integer instead
85+
* of a bool.
86+
*/
87+
cookie *set_secure(const unsigned int);
88+
/**
89+
* This method returns the cookie name.
90+
*/
91+
std::string get_name() const NOEXCEPT;
92+
/**
93+
* This method returns the cookie path.
94+
*/
95+
std::string get_path() const NOEXCEPT;
96+
/**
97+
* This method returns the cookie domain.
98+
*/
99+
std::string get_domain() const NOEXCEPT;
100+
/**
101+
* This method returns the cookie security.
102+
*/
103+
bool is_secure() const NOEXCEPT;
104+
/**
105+
* This method allows to get a string representing the entire cookie. Example:
106+
* 'Set-cookie: name=xxx; path=/; domain=/ expires=date'
107+
*/
108+
std::string get_formatted() const NOEXCEPT;
109+
private:
110+
/**
111+
* The cookie name.
112+
*/
113+
std::string name;
114+
/**
115+
* The cookie path.
116+
*/
117+
std::string path;
118+
/**
119+
* The cookie domain.
120+
*/
121+
std::string domain;
122+
/**
123+
* The cookie security.
124+
*/
125+
bool secure;
126+
};
127+
}
128+
129+
#endif //CURLCPP_COOKIE_H

include/cookie_date.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 - Giuseppe Persico
5+
* File - cookie_date.h
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
#ifndef CURLCPP_COOKIE_DATE_H
27+
#define CURLCPP_COOKIE_DATE_H
28+
29+
#include <string>
30+
#include <map>
31+
32+
namespace curl {
33+
// Enumeration to better define months.
34+
enum months { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};
35+
36+
// Leave this alone :)
37+
namespace details {
38+
// Map months numbers with months short names (cookies likes it short XD)
39+
static const std::map<int,std::string> months_names = {
40+
{JANUARY,"Jan"}, {FEBRUARY,"Feb"}, {MARCH,"Mar"}, {APRIL,"Apr"}, {MAY,"May"}, {JUNE,"Jun"},
41+
{JULY,"Jul"},{AUGUST,"Aug"},{SEPTEMBER,"Sep"},{OCTOBER,"Oct"},{NOVEMBER,"Nov"},{DECEMBER,"Dec"}
42+
};
43+
}
44+
45+
/**
46+
* This class provide an easy way to build a date formed by day, month and year
47+
*/
48+
class cookie_date {
49+
public:
50+
/**
51+
* Default constructor. Inizialize the attributes with default values.
52+
*/
53+
cookie_date() : day(1), month("Jan"), year(1970) {}
54+
/**
55+
* Constructor with parameters, which gives a fast way to build a cookie_date object.
56+
*/
57+
cookie_date(const unsigned int, const unsigned int, const unsigned int);
58+
/**
59+
* This method allows to specify a day for the date. If the day is less than zero or
60+
* greater than 31, 1 will be choosen.
61+
*/
62+
cookie_date *set_day(const unsigned int);
63+
/**
64+
* This method allows to specify a month for the date. If the month is not supported,
65+
* January will be choosen.
66+
*/
67+
cookie_date *set_month(const unsigned int);
68+
/**
69+
* This method allows to specify a year for the date. If year is less than 1970, 1970 will
70+
* be choosen.
71+
*/
72+
cookie_date *set_year(const unsigned int);
73+
/**
74+
* This method returns the day number.
75+
*/
76+
unsigned int get_day() const;
77+
/**
78+
* This method returns the month name.
79+
*/
80+
std::string get_month() const;
81+
/**
82+
* This method returns the year number.
83+
*/
84+
unsigned int get_year() const;
85+
private:
86+
/**
87+
* The day for this date.
88+
*/
89+
unsigned int day;
90+
/**
91+
* The month name.
92+
*/
93+
std::string month;
94+
/**
95+
* The month year.
96+
*/
97+
unsigned int year;
98+
};
99+
}
100+
101+
#endif //CURLCPP_COOKIE_DATE_H

include/cookie_datetime.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 - Giuseppe Persico
5+
* File - cookie_datetime.h
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
#ifndef CURLCPP_COOKIE_DATETIME_H
27+
#define CURLCPP_COOKIE_DATETIME_H
28+
29+
namespace curl {
30+
class cookie_datetime {
31+
32+
};
33+
}
34+
35+
#endif //CURLCPP_COOKIE_DATETIME_H

include/cookie_time.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by Giuseppe Persico on 25/03/16.
3+
//
4+
5+
#ifndef CURLCPP_COOKIE_TIME_H
6+
#define CURLCPP_COOKIE_TIME_H
7+
8+
namespace curl {
9+
class cookie_time {
10+
11+
};
12+
}
13+
14+
#endif //CURLCPP_COOKIE_TIME_H

include/curl_cookie.h

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,60 +29,76 @@
2929
#include <string>
3030
#include <vector>
3131
#include <ostream>
32+
3233
#include "curl_easy.h"
34+
#include "cookie.h"
3335

34-
/**
35-
* This class represents a generic cookie handler. It allows a user to get
36-
* and set cookie for a domain, in an easy way and without caring about resources
37-
* allocation and deallocation.
38-
*/
3936
namespace curl {
37+
// Typedef to enhance readability.
4038
using curlcpp_cookies = std::vector<std::string>;
41-
39+
/**
40+
* This class represents a generic cookie handler. It allows a user to get
41+
* and set cookie for a domain, in an easy way and without caring about resources
42+
* allocation and deallocation.
43+
*/
4244
class curl_cookie {
4345
public:
44-
// This constructor allow you to specify a curl_easy object.
46+
/**
47+
* This constructor allow you to specify a curl_easy object.
48+
*/
4549
curl_cookie(curl_easy &easy) : easy(easy) {}
46-
// This method allow you to get all known cookies for a specific domain.
47-
const curlcpp_cookies get() const NOEXCEPT;
4850
/**
4951
* This method allow you to set the cookie file from where to read initial cookies.
5052
* If you pass an empty string or a string containing a non existing file's path,
5153
* the cookie engine will be initialized, but without reading initial cookies.
5254
*/
53-
void set_cookie_file(const std::string) NOEXCEPT;
55+
void set_file(const std::string);
56+
/**
57+
* This method allow you to specify a string that represents a cookie. Such a cookie
58+
* can be either a single line in Netscape / Mozilla format or just regular HTTP-style
59+
* header (Set-Cookie: ...) format. This will also enable the cookie engine. This adds
60+
* that single cookie to the internal cookie store.
61+
*/
62+
void set(const curl::cookie &);
63+
/**
64+
* This method overloads the one previously declared allowing to specify a vector of cookies.
65+
*/
66+
void set(const std::vector<const curl::cookie> &);
5467
/**
5568
* This method allow you to specify a file where libcurl will write every internal
5669
* known-stored cookie when the curl_easy destructor will be called. If no cookies
5770
* are known, no file will be created. Using this option also enables cookies for
5871
* this session, so if you for example follow a location it will make matching cookies
5972
* get sent accordingly.
6073
*/
61-
void set_cookiejar_file(const std::string) NOEXCEPT;
74+
void set_jar_file(const std::string) NOEXCEPT;
6275
/**
63-
* This method allow you to specify a string that represents a cookie. Such a cookie
64-
* can be either a single line in Netscape / Mozilla format or just regular HTTP-style
65-
* header (Set-Cookie: ...) format. This will also enable the cookie engine. This adds
66-
* that single cookie to the internal cookie store.
76+
* This method allow you to get all known cookies for a specific domain.
77+
*/
78+
const curlcpp_cookies get() const NOEXCEPT;
79+
/**
80+
* This method erases all cookies held in memory.
81+
*/
82+
void erase();
83+
/**
84+
* This method writes all the cookies held in memory to the file specifiied with
85+
* set_cookiejar_file method.
6786
*/
68-
void set_cookie_list(const std::string) NOEXCEPT;
69-
void set_cookie_list(const char *) NOEXCEPT;
70-
// This method overloads the one previously declared allowing to specify a vector of cookies.
71-
void set_cookie_list(const std::vector<std::string> &) NOEXCEPT;
72-
// This method overloads the one previously declared allowing to specifiy a stream of cookies.
73-
void set_cookie_list(const std::ostringstream) NOEXCEPT;
74-
// This method erases all cookies held in memory.
75-
void erase() NOEXCEPT;
76-
// This method writes all the cookies held in memory to the file specifiied with set_cookiejar_file method.
77-
void flush() NOEXCEPT;
78-
// This method erases all the session cookies held in memory.
79-
void erase_session() NOEXCEPT;
80-
// This method loads all the cookies from the file specified with set_cookie_file method.
81-
void reload() NOEXCEPT;
87+
void flush();
88+
/**
89+
* This method erases all the session cookies held in memory.
90+
*/
91+
void erase_session();
92+
/**
93+
* This method loads all the cookies from the file specified with set_cookie_file method.
94+
*/
95+
void reload();
8296
private:
83-
// Istance on curl_easy class.
97+
/**
98+
* Istance on curl_easy class.
99+
*/
84100
curl_easy &easy;
85101
};
86102
}
87103

88-
#endif /* curl_cookie_hp */
104+
#endif /* curl_cookie_h */

0 commit comments

Comments
 (0)