Skip to content

Commit 75728b1

Browse files
committed
Added week_day to cookie_date expiration
1 parent 89c2c9e commit 75728b1

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

include/cookie_date.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
namespace curl {
3737
// Enumeration to better define months.
3838
enum months { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};
39+
// Enumeration to better define week days.
40+
enum weekdays {MONDAY = 1, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};
3941

4042
// Leave this alone :)
4143
namespace details {
@@ -44,6 +46,11 @@ namespace curl {
4446
{JANUARY,"Jan"}, {FEBRUARY,"Feb"}, {MARCH,"Mar"}, {APRIL,"Apr"}, {MAY,"May"}, {JUNE,"Jun"},
4547
{JULY,"Jul"},{AUGUST,"Aug"},{SEPTEMBER,"Sep"},{OCTOBER,"Oct"},{NOVEMBER,"Nov"},{DECEMBER,"Dec"}
4648
};
49+
// Map week days numbers with days short names (cookies likes it, still, short XD)
50+
static const std::map<int,std::string> weekday_names = {
51+
{MONDAY,"Mon"}, {TUESDAY,"Tue"}, {WEDNESDAY,"Wed"}, {THURSDAY,"Thu"}, {FRIDAY,"Fri"}, {SATURDAY,"Sat"},
52+
{SUNDAY,"Sun"}
53+
};
4754
}
4855

4956
/**
@@ -54,11 +61,16 @@ namespace curl {
5461
/**
5562
* Default constructor. Inizialize the attributes with default values.
5663
*/
57-
cookie_date() : day(1), month("Jan"), year(1970) {}
64+
cookie_date() : week_day("Mon"), day(1), month("Jan"), year(1970) {}
5865
/**
5966
* Constructor with parameters, which gives a fast way to build a cookie_date object.
6067
*/
61-
cookie_date(const unsigned int, const unsigned int, const unsigned int) NOEXCEPT;
68+
cookie_date(const unsigned int, const unsigned int, const unsigned int, const unsigned int) NOEXCEPT;
69+
/**
70+
* This method allows to specify the week dayname for the date. If the day is less or equal
71+
* than zero or greater than 7, 1 will be choosen
72+
*/
73+
cookie_date *set_week_day(const unsigned int) NOEXCEPT;
6274
/**
6375
* This method allows to specify a day for the date. If the day is less than zero or
6476
* greater than 31, 1 will be choosen.
@@ -74,6 +86,10 @@ namespace curl {
7486
* be choosen.
7587
*/
7688
cookie_date *set_year(const unsigned int) NOEXCEPT;
89+
/**
90+
* This method returns the week day name
91+
*/
92+
std::string get_week_day() const NOEXCEPT;
7793
/**
7894
* This method returns the day number.
7995
*/
@@ -99,6 +115,10 @@ namespace curl {
99115
* The month name.
100116
*/
101117
std::string month;
118+
/**
119+
* The week dayname.
120+
*/
121+
std::string week_day;
102122
/**
103123
* The month year.
104124
*/

src/cookie.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,5 @@ curl::cookie_datetime curl::cookie::get_datetime() const NOEXCEPT {
139139

140140
// Implementation of get_formatted method.
141141
string curl::cookie::get_formatted() NOEXCEPT {
142-
return "Set-cookie: "+this->name+"="+this->value+"; expires="+this->datetime.get_formatted()+"path="+this->path+"; domain="+this->domain;
143-
}
142+
return "Set-Cookie: "+this->name+"="+this->value+"; expires="+this->datetime.get_formatted()+"; path="+this->path+"; domain="+this->domain;
143+
}

src/cookie_date.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66
#include "cookie_date.h"
77

88
// Implementation of constructor with parameters.
9-
curl::cookie_date::cookie_date(const unsigned int day, const unsigned int month, const unsigned int year) NOEXCEPT {
10-
this->set_day(day)->set_month(month)->set_year(year);
9+
curl::cookie_date::cookie_date(const unsigned int week_day, const unsigned int day, const unsigned int month, const unsigned int year) NOEXCEPT {
10+
this->set_week_day(week_day)->set_day(day)->set_month(month)->set_year(year);
11+
}
12+
13+
// Implementation of set_week_day method.
14+
curl::cookie_date *curl::cookie_date::set_week_day(const unsigned int week_day) NOEXCEPT {
15+
try {
16+
this->week_day = details::weekday_names.at(week_day);
17+
} catch (const std::out_of_range &exception) {
18+
this->week_day = "Mon";
19+
}
20+
return this;
1121
}
1222

1323
// Implementation of set_day method.
@@ -32,6 +42,10 @@ curl::cookie_date *curl::cookie_date::set_year(const unsigned int year) NOEXCEPT
3242
return this;
3343
}
3444

45+
std::string curl::cookie_date::get_week_day() const NOEXCEPT {
46+
return this->week_day;
47+
}
48+
3549
// Implementation of get_day method.
3650
unsigned int curl::cookie_date::get_day() const NOEXCEPT {
3751
return this->day;
@@ -50,6 +64,6 @@ unsigned int curl::cookie_date::get_year() const NOEXCEPT {
5064
// Implementation of get_formatted method.
5165
std::string curl::cookie_date::get_formatted() NOEXCEPT {
5266
std::ostringstream stream;
53-
stream<<this->get_day()<<"-"<<this->get_month()<<"-"<<this->get_year();
67+
stream<<this->week_day<<", "<<this->day<<"-"<<this->month<<"-"<<this->year;
5468
return stream.str();
5569
}

src/curl_cookie.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ namespace curl {
5454
void curl_cookie::reload() {
5555
this->easy.add<CURLOPT_COOKIELIST>("RELOAD");
5656
}
57-
}
57+
}

test/cookie.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ int main() {
3434

3535
// Let's create a cooie
3636
cookie ck;
37-
ck.set_name("nome cookie");
38-
ck.set_value("valore cookie");
37+
ck.set_name("nomecookie");
38+
ck.set_value("valorecookie");
3939
ck.set_path("/");
4040
ck.set_domain(".example.com");
41+
std::cout<<ck.get_formatted()<<std::endl;
4142

4243
// Create a cookie object and add the previously created cookie.
4344
curl_cookie c_obj(easy);
@@ -54,6 +55,7 @@ int main() {
5455
} catch (curl_easy_exception error) {
5556
error.print_traceback();
5657
}
58+
std::cout<<cookies.size()<<std::endl;
5759
// Print them all!
5860
for (auto cook : cookies) {
5961
std::cout<<cook<<std::endl;

0 commit comments

Comments
 (0)