Skip to content

Commit 89c2c9e

Browse files
committed
Fixed cookie_datetime, cookie_date and cookie_time classes.
1 parent ece7c44 commit 89c2c9e

File tree

5 files changed

+68
-27
lines changed

5 files changed

+68
-27
lines changed

include/cookie.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ namespace curl {
4444
/**
4545
* The constructor allow a fast way to build a cookie.
4646
*/
47-
cookie(const std::string, const cookie_datetime &, const std::string = "/", const std::string = "", const bool = false);
47+
cookie(const std::string, const std::string, const cookie_datetime &, const std::string = "", const std::string = "", const bool = false);
4848
/**
4949
* This constructor overloades the previous one.
5050
*/
51-
cookie(const char *, const cookie_datetime &, const char * = "/", const char * = "", const bool = false);
51+
cookie(const char *, const char *, const cookie_datetime &, const char * = "", const char * = "", const bool = false);
5252
/**
5353
* This method allows to specify the cookie name.
5454
*/
@@ -58,6 +58,14 @@ namespace curl {
5858
* instead of a string.
5959
*/
6060
cookie *set_name(const char *);
61+
/**
62+
* This method allows to specify the cookie value.
63+
*/
64+
cookie *set_value(const std::string);
65+
/**
66+
* This method allows to specify the cookie value,
67+
*/
68+
cookie *set_value(const char *);
6169
/**
6270
* This method allows to specify the cookie path.
6371
*/
@@ -93,6 +101,10 @@ namespace curl {
93101
* This method returns the cookie name.
94102
*/
95103
std::string get_name() const NOEXCEPT;
104+
/**
105+
* This method returns the cookie value.
106+
*/
107+
std::string get_value() const NOEXCEPT;
96108
/**
97109
* This method returns the cookie path.
98110
*/
@@ -119,6 +131,10 @@ namespace curl {
119131
* The cookie name.
120132
*/
121133
std::string name;
134+
/**
135+
* Il valore del cookie.
136+
*/
137+
std::string value;
122138
/**
123139
* The cookie path.
124140
*/

src/cookie.cpp

Lines changed: 27 additions & 18 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 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);
11+
curl::cookie::cookie(const string name, const string value, const cookie_datetime &datetime, const string path, const string domain, const bool secure) {
12+
this->set_name(name)->set_value(value)->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 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);
16+
curl::cookie::cookie(const char *name, const char * value, const cookie_datetime &datetime, const char *path, const char *domain, const bool secure) {
17+
this->set_name(name)->set_value(value)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime);
1818
}
1919

2020
// Implementation of set_name method.
@@ -35,6 +35,22 @@ curl::cookie *curl::cookie::set_name(const char *name) {
3535
return this;
3636
}
3737

38+
// Implementation of set_name method.
39+
curl::cookie *curl::cookie::set_value(const string value) {
40+
this->value = value;
41+
return this;
42+
}
43+
44+
// Implementation of set_value method.
45+
curl::cookie *curl::cookie::set_value(const char *value) {
46+
if (value == nullptr) {
47+
this->value = "";
48+
} else {
49+
this->value = string(value);
50+
}
51+
return this;
52+
}
53+
3854
// Implementation of set_path method.
3955
curl::cookie *curl::cookie::set_path(const string path) NOEXCEPT {
4056
this->path = path.empty() ? "/" : path;
@@ -96,6 +112,11 @@ string curl::cookie::get_name() const NOEXCEPT {
96112
return this->name;
97113
}
98114

115+
// Implmentation of get_value method.
116+
string curl::cookie::get_value() const NOEXCEPT {
117+
return this->value;
118+
}
119+
99120
// Implementation of get_path method.
100121
string curl::cookie::get_path() const NOEXCEPT {
101122
return this->path;
@@ -118,17 +139,5 @@ curl::cookie_datetime curl::cookie::get_datetime() const NOEXCEPT {
118139

119140
// Implementation of get_formatted method.
120141
string curl::cookie::get_formatted() NOEXCEPT {
121-
return "Set-cookie: name="+this->name+"; expires="+this->datetime.get_formatted()+"path="+this->path+"; domain="+this->domain;
122-
}
123-
124-
/*
125-
*
126-
*time_t rawtime;
127-
struct tm * timeinfo;
128-
char buffer [80];
129-
130-
time (&rawtime);
131-
timeinfo = localtime (&rawtime);
132-
133-
strftime (buffer,80,"%d-%b-%Y %X GMT",timeinfo);
134-
*/
142+
return "Set-cookie: "+this->name+"="+this->value+"; expires="+this->datetime.get_formatted()+"path="+this->path+"; domain="+this->domain;
143+
}

src/cookie_datetime.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ curl::cookie_datetime::cookie_datetime(const cookie_time &time, const cookie_dat
1313
// Implementation of set_time method.
1414
curl::cookie_datetime *curl::cookie_datetime::set_time(const cookie_time &time) NOEXCEPT {
1515
this->time = time;
16+
return this;
1617
}
1718

1819
// Implementation of set_date method.
1920
curl::cookie_datetime *curl::cookie_datetime::set_date(const cookie_date &date) NOEXCEPT {
2021
this->date = date;
22+
return this;
2123
}
2224

2325
// Implementation of get_time method.

src/cookie_time.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ curl::cookie_time::cookie_time(const unsigned int hour, const unsigned int minut
1212

1313
// Implementation of set_hour method.
1414
curl::cookie_time *curl::cookie_time::set_hour(const unsigned int hour) NOEXCEPT {
15-
this->hour = (hour < 0 or hour > 23) ? 0 : hour;
15+
this->hour = hour < 0 or hour > 23 ? 0 : hour;
1616
return this;
1717
}
1818

1919
// Implementation of set_minutes method.
2020
curl::cookie_time *curl::cookie_time::set_minutes(const unsigned int minutes) NOEXCEPT {
2121
this->minutes = (minutes < 0 or minutes > 59) ? 0 : minutes;
22+
return this;
2223
}
2324

2425
// Implementation of set_seconds method.
2526
curl::cookie_time *curl::cookie_time::set_seconds(const unsigned int seconds) NOEXCEPT {
2627
this->seconds = (seconds < 0 or seconds > 59) ? 0 : seconds;
28+
return this;
2729
}
2830

2931
// Implementation of get_hour method.

test/cookie.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
#include "curl_exception.h"
55
#include "curl_form.h"
66
#include "curl_cookie.h"
7+
#include "curl_ios.h"
8+
9+
using std::ostringstream;
710

811
using curl::cookie;
912
using curl::curl_header;
1013
using curl::curl_easy;
1114
using curl::curl_easy_exception;
1215
using curl::curl_cookie;
1316
using curl::curlcpp_cookies;
17+
using curl::curl_ios;
1418

1519

1620
/**
@@ -19,24 +23,32 @@ using curl::curlcpp_cookies;
1923
*/
2024

2125
int main() {
26+
// Let's declare a stream
27+
ostringstream str;
28+
// We are going to put the request's output in the previously declared stream
29+
curl_ios<ostringstream> ios(str);
30+
2231
// Easy object to handle the connection, url and verbosity level.
23-
curl_easy easy;
32+
curl_easy easy(ios);
2433
easy.add<CURLOPT_URL>("http://example.com");
25-
easy.add<CURLOPT_VERBOSE>(1L);
2634

2735
// Let's create a cooie
28-
cookie ck("test","/",".example.com",null);
36+
cookie ck;
37+
ck.set_name("nome cookie");
38+
ck.set_value("valore cookie");
39+
ck.set_path("/");
40+
ck.set_domain(".example.com");
2941

3042
// Create a cookie object and add the previously created cookie.
31-
curl_cookie cookie_object(easy);
32-
cookie_object.set(ck);
43+
curl_cookie c_obj(easy);
44+
c_obj.set(ck);
3345

3446
// This rapresents a vector of cookies.
3547
curlcpp_cookies cookies;
3648
try {
3749
easy.perform();
3850
// Retrieve all the cookies for the example.com
39-
cookies = cookie_object.get();
51+
cookies = c_obj.get();
4052
// Delete all the memory helded cookies.
4153
easy.add<CURLOPT_COOKIEFILE>("ALL");
4254
} catch (curl_easy_exception error) {

0 commit comments

Comments
 (0)