Skip to content

Commit ece7c44

Browse files
committed
Added cookie_datetime object to easly specify cookies expiration time&date
1 parent 089712b commit ece7c44

File tree

11 files changed

+287
-47
lines changed

11 files changed

+287
-47
lines changed

include/cookie.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,28 @@
2929
#include <string>
3030

3131
#include "curl_exception.h"
32+
#include "cookie_datetime.h"
3233

3334
namespace curl {
3435
/**
3536
* This class allows to specify every single field of an http-style cookie.
3637
*/
3738
class cookie {
3839
public:
40+
/*
41+
* Default constructor.
42+
*/
3943
cookie() {}
4044
/**
4145
* The constructor allow a fast way to build a cookie.
4246
*/
43-
cookie(const std::string, const std::string, const std::string, const bool = false);
47+
cookie(const std::string, const cookie_datetime &, const std::string = "/", const std::string = "", const bool = false);
4448
/**
4549
* This constructor overloades the previous one.
4650
*/
47-
cookie(const char *, const char *, const char *, bool = false);
51+
cookie(const char *, const cookie_datetime &, const char * = "/", const char * = "", const bool = false);
4852
/**
49-
* This method allows to specify the cookie name. It returns a pointer
50-
* to this class to enable cascade calls.
53+
* This method allows to specify the cookie name.
5154
*/
5255
cookie *set_name(const std::string);
5356
/**
@@ -56,8 +59,7 @@ namespace curl {
5659
*/
5760
cookie *set_name(const char *);
5861
/**
59-
* This method allows to specify the cookie path. It returns a pointer
60-
* to this class to enable cascade calls.
62+
* This method allows to specify the cookie path.
6163
*/
6264
cookie *set_path(const std::string) NOEXCEPT;
6365
/**
@@ -66,8 +68,7 @@ namespace curl {
6668
*/
6769
cookie *set_path(const char *) NOEXCEPT;
6870
/**
69-
* This method allows to specify the cookie domain. It returns a pointer
70-
* to this class to enable cascade calls.
71+
* This method allows to specify the cookie domain.
7172
*/
7273
cookie *set_domain(const std::string) NOEXCEPT;
7374
/**
@@ -76,15 +77,18 @@ namespace curl {
7677
*/
7778
cookie *set_domain(const char *) NOEXCEPT;
7879
/**
79-
* This method allows to specify the cookie security. It returns a pointer
80-
* to this class to enable cascade calls.
80+
* This method allows to specify the cookie security.
8181
*/
8282
cookie *set_secure(const bool) NOEXCEPT;
8383
/**
8484
* This method overloads the previous one allowing to specify an integer instead
8585
* of a bool.
8686
*/
8787
cookie *set_secure(const unsigned int);
88+
/**
89+
* This method allows to specify a datetime expiration to this cookie.
90+
*/
91+
cookie *set_datetime(const cookie_datetime &) NOEXCEPT;
8892
/**
8993
* This method returns the cookie name.
9094
*/
@@ -97,6 +101,10 @@ namespace curl {
97101
* This method returns the cookie domain.
98102
*/
99103
std::string get_domain() const NOEXCEPT;
104+
/**
105+
* This method returns the datetime expire object for this cookie.
106+
*/
107+
cookie_datetime get_datetime() const NOEXCEPT;
100108
/**
101109
* This method returns the cookie security.
102110
*/
@@ -105,7 +113,7 @@ namespace curl {
105113
* This method allows to get a string representing the entire cookie. Example:
106114
* 'Set-cookie: name=xxx; path=/; domain=/ expires=date'
107115
*/
108-
std::string get_formatted() const NOEXCEPT;
116+
std::string get_formatted() NOEXCEPT;
109117
private:
110118
/**
111119
* The cookie name.
@@ -119,6 +127,10 @@ namespace curl {
119127
* The cookie domain.
120128
*/
121129
std::string domain;
130+
/**
131+
* The cookie expire date and time.
132+
*/
133+
cookie_datetime datetime;
122134
/**
123135
* The cookie security.
124136
*/

include/cookie_date.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
#include <string>
3030
#include <map>
31+
#include <iostream>
32+
#include <sstream>
33+
34+
#include "curl_config.h"
3135

3236
namespace curl {
3337
// Enumeration to better define months.
@@ -54,12 +58,12 @@ namespace curl {
5458
/**
5559
* Constructor with parameters, which gives a fast way to build a cookie_date object.
5660
*/
57-
cookie_date(const unsigned int, const unsigned int, const unsigned int);
61+
cookie_date(const unsigned int, const unsigned int, const unsigned int) NOEXCEPT;
5862
/**
5963
* This method allows to specify a day for the date. If the day is less than zero or
6064
* greater than 31, 1 will be choosen.
6165
*/
62-
cookie_date *set_day(const unsigned int);
66+
cookie_date *set_day(const unsigned int) NOEXCEPT;
6367
/**
6468
* This method allows to specify a month for the date. If the month is not supported,
6569
* January will be choosen.
@@ -69,19 +73,23 @@ namespace curl {
6973
* This method allows to specify a year for the date. If year is less than 1970, 1970 will
7074
* be choosen.
7175
*/
72-
cookie_date *set_year(const unsigned int);
76+
cookie_date *set_year(const unsigned int) NOEXCEPT;
7377
/**
7478
* This method returns the day number.
7579
*/
76-
unsigned int get_day() const;
80+
unsigned int get_day() const NOEXCEPT;
7781
/**
7882
* This method returns the month name.
7983
*/
80-
std::string get_month() const;
84+
std::string get_month() const NOEXCEPT;
8185
/**
8286
* This method returns the year number.
8387
*/
84-
unsigned int get_year() const;
88+
unsigned int get_year() const NOEXCEPT;
89+
/**
90+
* This method returns the date formatted as day-month-year
91+
*/
92+
std::string get_formatted() NOEXCEPT;
8593
private:
8694
/**
8795
* The day for this date.

include/cookie_datetime.h

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,52 @@
2626
#ifndef CURLCPP_COOKIE_DATETIME_H
2727
#define CURLCPP_COOKIE_DATETIME_H
2828

29+
#include "cookie_date.h"
30+
#include "cookie_time.h"
31+
2932
namespace curl {
33+
/**
34+
* This class provide an easy way to specify the date and the time of cooking expiration.
35+
*/
3036
class cookie_datetime {
31-
37+
public:
38+
/**
39+
* Default constructor.
40+
*/
41+
cookie_datetime() {}
42+
/**
43+
* The constructor with parameters allows to specify a time and a date for cookie expiration.
44+
*/
45+
cookie_datetime(const cookie_time &, const cookie_date &) NOEXCEPT;
46+
/**
47+
* This method allows to set the expiration time.
48+
*/
49+
cookie_datetime *set_time(const cookie_time &) NOEXCEPT;
50+
/**
51+
* This method allows to set the expiration date.
52+
*/
53+
cookie_datetime *set_date(const cookie_date &) NOEXCEPT;
54+
/**
55+
* This method returns the time object.
56+
*/
57+
const cookie_time get_time() const NOEXCEPT;
58+
/**
59+
* This method returns the date object.
60+
*/
61+
const cookie_date get_date() const NOEXCEPT;
62+
/**
63+
* This method returns the cookie_datetime as a string.
64+
*/
65+
std::string get_formatted() NOEXCEPT;
66+
private:
67+
/**
68+
* Time object.
69+
*/
70+
cookie_time time;
71+
/**
72+
* Date object.
73+
*/
74+
cookie_date date;
3275
};
3376
}
3477

include/cookie_time.h

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,93 @@
1-
//
2-
// Created by Giuseppe Persico on 25/03/16.
3-
//
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+
*/
425

526
#ifndef CURLCPP_COOKIE_TIME_H
627
#define CURLCPP_COOKIE_TIME_H
728

29+
#include "curl_config.h"
30+
31+
#include <string>
32+
#include <iostream>
33+
#include <sstream>
34+
835
namespace curl {
36+
/**
37+
* This class provides a fast way to build a time object formed by hour, minutes and seconds.
38+
*/
939
class cookie_time {
10-
40+
public:
41+
/**
42+
* The default constructor will initialize every attribute with zero.
43+
*/
44+
cookie_time() : hour(0), minutes(0), seconds(0) {};
45+
/**
46+
* The constructor with parameters allows to initialize attributes with custom values.
47+
*/
48+
cookie_time(const unsigned int, const unsigned int, const unsigned int);
49+
/**
50+
* This method allows to specify the hours.
51+
*/
52+
cookie_time *set_hour(const unsigned int) NOEXCEPT;
53+
/**
54+
* This method allows to specify the minutes.
55+
*/
56+
cookie_time *set_minutes(const unsigned int) NOEXCEPT;
57+
/**
58+
* This method allows to specify the seconds.
59+
*/
60+
cookie_time *set_seconds(const unsigned int) NOEXCEPT;
61+
/**
62+
* This method returns the hours.
63+
*/
64+
const unsigned int get_hour() const NOEXCEPT;
65+
/**
66+
* This method returns the minutes.
67+
*/
68+
const unsigned int get_minutes() const NOEXCEPT;
69+
/**
70+
* This method returns the seconds.
71+
*/
72+
const unsigned int get_seconds() const NOEXCEPT;
73+
/**
74+
* This method returns the time formatted as h:m:s
75+
*/
76+
std::string get_formatted() NOEXCEPT;
77+
private:
78+
/**
79+
* The hours.
80+
*/
81+
unsigned int hour;
82+
/**
83+
* The minutes.
84+
*/
85+
unsigned int minutes;
86+
/**
87+
* The seconds.
88+
*/
89+
unsigned int seconds;
1190
};
1291
}
1392

14-
#endif //CURLCPP_COOKIE_TIME_H
93+
#endif //CURLCPP_COOKIE_TIME_H

include/curl_cookie.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ namespace curl {
5959
* header (Set-Cookie: ...) format. This will also enable the cookie engine. This adds
6060
* that single cookie to the internal cookie store.
6161
*/
62-
void set(const curl::cookie &);
62+
void set(curl::cookie &);
6363
/**
6464
* This method overloads the one previously declared allowing to specify a vector of cookies.
6565
*/
66-
void set(const std::vector<const curl::cookie> &);
66+
void set(const std::vector<curl::cookie> &);
6767
/**
6868
* This method allow you to specify a file where libcurl will write every internal
6969
* known-stored cookie when the curl_easy destructor will be called. If no cookies

src/cookie.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
using std::string;
99

1010
// Implementation of constructor.
11-
curl::cookie::cookie(const string name, const string path, const string domain, const bool secure) {
12-
this->set_name(name)->set_path(path)->set_domain(domain)->set_secure(secure);
11+
curl::cookie::cookie(const string name, const cookie_datetime &datetime, const string path, const string domain, const bool secure) {
12+
this->set_name(name)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime);
1313
}
1414

1515
// Implementation of overloaded constructor.
16-
curl::cookie::cookie(const char *name, const char *path, const char *domain, const bool secure) {
17-
this->set_name(name)->set_path(path)->set_domain(domain)->set_secure(secure);
16+
curl::cookie::cookie(const char *name, const cookie_datetime &datetime, const char *path, const char *domain, const bool secure) {
17+
this->set_name(name)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime);
1818
}
1919

2020
// Implementation of set_name method.
@@ -73,7 +73,7 @@ curl::cookie *curl::cookie::set_secure(const bool secure) NOEXCEPT {
7373
return this;
7474
}
7575

76-
//
76+
// Implementation of set_secure method.
7777
curl::cookie *curl::cookie::set_secure(const unsigned int secure) {
7878
if (secure == 0) {
7979
this->secure = false;
@@ -85,6 +85,12 @@ curl::cookie *curl::cookie::set_secure(const unsigned int secure) {
8585
return this;
8686
}
8787

88+
// Implementation of set_datetime method.
89+
curl::cookie *curl::cookie::set_datetime(const cookie_datetime &datetime) NOEXCEPT {
90+
this->datetime = datetime;
91+
return this;
92+
}
93+
8894
// Implementation of get_name method.
8995
string curl::cookie::get_name() const NOEXCEPT {
9096
return this->name;
@@ -105,9 +111,14 @@ bool curl::cookie::is_secure() const NOEXCEPT {
105111
return this->secure;
106112
}
107113

114+
// Implementation of get_datetime method.
115+
curl::cookie_datetime curl::cookie::get_datetime() const NOEXCEPT {
116+
return this->datetime;
117+
}
118+
108119
// Implementation of get_formatted method.
109-
string curl::cookie::get_formatted() const NOEXCEPT {
110-
return "Set-cookie: name="+this->name+"; path="+this->path+"; domain="+this->domain;
120+
string curl::cookie::get_formatted() NOEXCEPT {
121+
return "Set-cookie: name="+this->name+"; expires="+this->datetime.get_formatted()+"path="+this->path+"; domain="+this->domain;
111122
}
112123

113124
/*

0 commit comments

Comments
 (0)