File: example07.cpp

package info (click to toggle)
curlpp 0.7.3-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,568 kB
  • ctags: 1,930
  • sloc: sh: 9,617; cpp: 5,608; makefile: 176; ansic: 63
file content (174 lines) | stat: -rw-r--r-- 3,707 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* \file
* Cookies.
* 
*/

#include <string>
#include <sstream>
#include <vector>
#include <iostream>

#include <cstdlib>
#include <ctime>

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>

#define CURLPP_ALLOW_NOT_AVAILABLE

#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>


class YesNo
{
public:
    explicit YesNo(bool yn) : yesno(yn) {}
		std::string operator()() const {
        return yesno ? "Yes" : "No";
    }
		friend std::ostream &operator<<(std::ostream &strm, const YesNo &yn) {
        strm << yn();
        return strm;
    }
private:
    bool yesno;
};

struct MyCookie
{
	std::string name;
	std::string value;
	std::string domain;
	std::string path;
	time_t expires;
	bool tail;
	bool secure;
};

std::ostream &
operator<<(std::ostream &strm, const MyCookie &cook)
{
	strm << "Cookie: '" << cook.name << "' (secure: " << YesNo(cook.secure) << ", tail: "
		<< YesNo(cook.tail) << ") for domain: '" << cook.domain << "', "
		<< "path: '" << cook.path << "'.\n";
	strm << "Value: '" << cook.value << "'.\n";
	strm << "Expires: '" << ctime(&cook.expires) << "'.\n";

	return strm;
}

std::vector<std::string> &
split_cookie_str(const std::string &str, std::vector<std::string> &in)
{
	std::string part;

	std::istringstream strm(str);
	while (getline(strm, part, '\t'))
		in.push_back(part);

	return in;
}

std::vector<std::string>
splitCookieStr(const std::string &str)
{
	std::vector<std::string> split;
	split_cookie_str(str, split);
	return split;
}

std::vector<std::string> &
splitCookieStr(const std::string &str, std::vector<std::string> &in)
{
	return split_cookie_str(str, in);
}

int StrToInt(const std::string &str)
{
	std::istringstream strm(str);
	int i = 0;
	if (!(strm >> i)) {
		throw curlpp::RuntimeError("Unable to convert string '" + str + "' to integer!");
	}
	return i;
}

MyCookie
MakeCookie(const std::string &str_cookie)
{
	std::vector<std::string> vC = splitCookieStr(str_cookie);
	MyCookie cook;

	cook.domain = vC[0];
	cook.tail = vC[1] == "TRUE";
	cook.path = vC[2];
	cook.secure = vC[3] == "TRUE";
	cook.expires = StrToInt(vC[4]);
	cook.name = vC[5];
	cook.value = vC[6];

	return cook;
}

int
main(void)
{
	try
	{
		curlpp::Cleanup myCleanup;
		curlpp::Easy exEasy;
		std::vector<std::string> cookieList;

		// a cookie as in HTTP header
		cookieList.push_back("Set-Cookie: GMAIL_AT=EXPIRED;expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com");

		// a Netscape style cookie with \t
		cookieList.push_back(".google.com\tTRUE\t/\tFALSE\t2147483647\tLSID\tI like you GOOGLE");

		// a Netscape style cookie with tabs in string
		cookieList.push_back(".yahoo.com	TRUE	/	FALSE	0	YAHOO_COOKIE	I like you yahoo, too");

		exEasy.setOpt(new curlpp::options::Url("http://www.google.com"));
		exEasy.setOpt(new curlpp::options::FileTime(true));
		exEasy.setOpt(new curlpp::options::Verbose(true));

		// loop throught the cookies and add one by one
		//
		for (std::vector<std::string>::iterator it = cookieList.begin();
			it != cookieList.end();
			++it)
		{
			exEasy.setOpt(curlpp::options::CookieList(*it));
		}
		exEasy.perform();

		// see what cookies we got
		//
		std::cout << "\nCookies from cookie engine:" << std::endl;
		std::list<std::string> cookies;
		curlpp::infos::CookieList::get(exEasy, cookies);
		int i = 1;
		for (std::list<std::string>::const_iterator it = cookies.begin();
			it != cookies.end();
			++it, i++)
		{
			std::cout << "[" << i << "]: " << MakeCookie(*it) << std::endl;
		}

		exit(EXIT_SUCCESS);
	}

	catch(curlpp::RuntimeError &e)
	{
		std::cerr << e.what() << std::endl;
		exit(EXIT_FAILURE);
	}

	catch(curlpp::LogicError &e)
	{
		std::cout << e.what() << std::endl;
		exit(EXIT_FAILURE);
	}
}