Skip to content

Commit 49f9634

Browse files
committed
Fixes and added more overloaded methods to cookie class.
1 parent f2b7a2a commit 49f9634

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

include/cookie.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,17 @@ namespace curl {
9292
* This method overloads the previous one allowing to specify an integer instead
9393
* of a bool.
9494
*/
95-
cookie *set_secure(const unsigned int);
95+
cookie *set_secure(const unsigned int) NOEXCEPT;
96+
/**
97+
* This method overloads the previous one allowing to specify a string to indicate
98+
* whether the cookie is secure (with "secure" keyword) or not (empty string).
99+
*/
100+
cookie *set_secure(const std::string) NOEXCEPT;
101+
/**
102+
* This method overloads the previous one allowing to specify a string to indicate
103+
* whether the cookie is secure (with "secure" keyword) or not (empty string).
104+
*/
105+
cookie *set_secure(const char *) NOEXCEPT;
96106
/**
97107
* This method allows to specify a datetime expiration to this cookie.
98108
*/

include/curl_multi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ namespace curl {
198198
const void *whatever;
199199
const CURLcode code;
200200
};
201+
201202
/**
202203
* Simple default constructor. It is used to give a
203204
* default value to all the attributes and provide a

src/cookie.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,23 @@ curl::cookie *curl::cookie::set_secure(const bool secure) NOEXCEPT {
8989
return this;
9090
}
9191

92+
// Implementation of set_secure overloaded method.
93+
curl::cookie *curl::cookie::set_secure(const string secure) NOEXCEPT {
94+
if (secure == "secure") {
95+
this->secure = 1;
96+
} else {
97+
this->secure = 0;
98+
}
99+
return this;
100+
}
101+
102+
// Implementation of set_secure overloaded method.
103+
curl::cookie *curl::cookie::set_secure(const char *secure) NOEXCEPT {
104+
return this->set_secure(string(secure));
105+
}
106+
92107
// Implementation of set_secure method.
93-
curl::cookie *curl::cookie::set_secure(const unsigned int secure) {
108+
curl::cookie *curl::cookie::set_secure(const unsigned int secure) NOEXCEPT {
94109
if (secure == 0) {
95110
this->secure = false;
96111
} else if (secure == 1) {
@@ -139,5 +154,6 @@ curl::cookie_datetime curl::cookie::get_datetime() const NOEXCEPT {
139154

140155
// Implementation of get_formatted method.
141156
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;
157+
string secure = this->is_secure() == 1 ? "secure" : "";
158+
return "Set-Cookie: "+this->name+"="+this->value+"; expires="+this->datetime.get_formatted()+"; path="+this->path+"; domain="+this->domain+" "+secure;
143159
}

src/cookie_date.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include "cookie_date.h"
77

8+
using std::out_of_range;
9+
using std::ostringstream;
10+
811
// Implementation of constructor with parameters.
912
curl::cookie_date::cookie_date(const unsigned int week_day, const unsigned int day, const unsigned int month, const unsigned int year) NOEXCEPT {
1013
this->set_week_day(week_day)->set_day(day)->set_month(month)->set_year(year);
@@ -14,7 +17,7 @@ curl::cookie_date::cookie_date(const unsigned int week_day, const unsigned int d
1417
curl::cookie_date *curl::cookie_date::set_week_day(const unsigned int week_day) NOEXCEPT {
1518
try {
1619
this->week_day = details::weekday_names.at(week_day);
17-
} catch (const std::out_of_range &exception) {
20+
} catch (const out_of_range &exception) {
1821
this->week_day = "Mon";
1922
}
2023
return this;
@@ -30,7 +33,7 @@ curl::cookie_date *curl::cookie_date::set_day(const unsigned int day) NOEXCEPT {
3033
curl::cookie_date *curl::cookie_date::set_month(const unsigned int month) {
3134
try {
3235
this->month = details::months_names.at(month);
33-
} catch (const std::out_of_range &exception) {
36+
} catch (const out_of_range &exception) {
3437
this->month = "Jan";
3538
}
3639
return this;
@@ -63,7 +66,7 @@ unsigned int curl::cookie_date::get_year() const NOEXCEPT {
6366

6467
// Implementation of get_formatted method.
6568
std::string curl::cookie_date::get_formatted() NOEXCEPT {
66-
std::ostringstream stream;
69+
ostringstream stream;
6770
stream<<this->week_day<<", "<<this->day<<"-"<<this->month<<"-"<<this->year;
6871
return stream.str();
6972
}

src/cookie_time.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "cookie_time.h"
77

8+
using std::ostringstream;
9+
810
// Implementation of constructor with parameters.
911
curl::cookie_time::cookie_time(const unsigned int hour, const unsigned int minutes, const unsigned int seconds) {
1012
this->set_hour(hour)->set_minutes(minutes)->set_seconds(seconds);
@@ -45,7 +47,7 @@ const unsigned int curl::cookie_time::get_seconds() const NOEXCEPT {
4547

4648
// Implementation of get_formatted method.
4749
std::string curl::cookie_time::get_formatted() NOEXCEPT {
48-
std::ostringstream stream;
50+
ostringstream stream;
4951
stream<<this->get_hour()<<":"<<this->get_minutes()<<":"<<this->get_seconds()<<" GMT";
5052
return stream.str();
5153
}

0 commit comments

Comments
 (0)