Skip to content

Commit 101c9bd

Browse files
committed
Defined CURLINFO_* options, fixed bugs, removed useless compare in cookie_datetime.
1 parent 49f9634 commit 101c9bd

File tree

4 files changed

+155
-84
lines changed

4 files changed

+155
-84
lines changed

include/curl_easy.h

Lines changed: 129 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,88 +37,173 @@
3737
#include "curl_form.h"
3838
#include "curl_easy_info.h"
3939

40-
4140
#define CURLCPP_DEFINE_OPTION(opt, value_type)\
4241
template <> struct option_t<opt> {\
4342
using type = value_type;\
4443
}
4544

45+
#define CURLCPP_DEFINE_INFO(info,value_type)\
46+
template <> struct info_t<info> {\
47+
using type = value_type;\
48+
}
49+
4650
namespace curl {
51+
// Definition of CURLINFO_* constants)
52+
namespace detail_info {
53+
template <CURLINFO> struct info_t;
54+
template <CURLINFO info> using Info_type = typename info_t<info>::type;
55+
// Last URL used
56+
CURLCPP_DEFINE_INFO(CURLINFO_EFFECTIVE_URL,char *);
57+
// Response code
58+
CURLCPP_DEFINE_INFO(CURLINFO_RESPONSE_CODE,long);
59+
// Last proxy connect response code
60+
CURLCPP_DEFINE_INFO(CURLINFO_HTTP_CONNECTCODE,long);
61+
// Remote time of the retrieved document.
62+
CURLCPP_DEFINE_INFO(CURLINFO_FILETIME,long);
63+
// Total time of previous transfer.
64+
CURLCPP_DEFINE_INFO(CURLINFO_TOTAL_TIME,double);
65+
// Time from start until name resolving completed.
66+
CURLCPP_DEFINE_INFO(CURLINFO_NAMELOOKUP_TIME,double);
67+
// Time from start until remote hosst proxy completed.
68+
CURLCPP_DEFINE_INFO(CURLINFO_CONNECT_TIME,double);
69+
// Time fron start until SSL/SSH handshake completed.
70+
CURLCPP_DEFINE_INFO(CURLINFO_APPCONNECT_TIME,double);
71+
// Time from start until just before the transfter begins.
72+
CURLCPP_DEFINE_INFO(CURLINFO_PRETRANSFER_TIME,double);
73+
// Time from start until just when the first byte is received.
74+
CURLCPP_DEFINE_INFO(CURLINFO_STARTTRANSFER_TIME,double);
75+
// Time taken for all redirect steps before the final transfer.
76+
CURLCPP_DEFINE_INFO(CURLINFO_REDIRECT_TIME,double);
77+
// Total number of redirects that were followed.
78+
CURLCPP_DEFINE_INFO(CURLINFO_REDIRECT_COUNT,long);
79+
// URL a redirect would take you to, had you enabled redirects.
80+
CURLCPP_DEFINE_INFO(CURLINFO_REDIRECT_URL,char *);
81+
// Number of bytes uploaded.
82+
CURLCPP_DEFINE_INFO(CURLINFO_SIZE_UPLOAD,double);
83+
// Number of bytes downloaded.
84+
CURLCPP_DEFINE_INFO(CURLINFO_SIZE_DOWNLOAD,double);
85+
// Average download speed.
86+
CURLCPP_DEFINE_INFO(CURLINFO_SPEED_DOWNLOAD,double);
87+
// Average upload speed.
88+
CURLCPP_DEFINE_INFO(CURLINFO_SPEED_UPLOAD,double);
89+
// Number of bytes of all headers received.
90+
CURLCPP_DEFINE_INFO(CURLINFO_HEADER_SIZE,long);
91+
// Number of bytes sent in the issued HTTP requests.
92+
CURLCPP_DEFINE_INFO(CURLINFO_REQUEST_SIZE,long);
93+
// Certificate verification result.
94+
CURLCPP_DEFINE_INFO(CURLINFO_SSL_VERIFYRESULT,long);
95+
// A list of OpenSSL crypto engines.
96+
CURLCPP_DEFINE_INFO(CURLINFO_SSL_ENGINES,struct curl_slist *);
97+
// Content length from the Content-Length header.
98+
CURLCPP_DEFINE_INFO(CURLINFO_CONTENT_LENGTH_DOWNLOAD,long);
99+
// Upload size.
100+
CURLCPP_DEFINE_INFO(CURLINFO_CONTENT_LENGTH_UPLOAD,long);
101+
// Content type from the Content-Type header.
102+
CURLCPP_DEFINE_INFO(CURLINFO_CONTENT_TYPE,char *);
103+
// User's private data pointer.
104+
CURLCPP_DEFINE_INFO(CURLINFO_PRIVATE,char *);
105+
// Avaiable HTTP authentication methods.
106+
CURLCPP_DEFINE_INFO(CURLINFO_HTTPAUTH_AVAIL,long);
107+
// Avaiable HTTP proxy authentication methods.
108+
CURLCPP_DEFINE_INFO(CURLINFO_PROXYAUTH_AVAIL,long);
109+
// The errno from the las failure to connect.
110+
CURLCPP_DEFINE_INFO(CURLINFO_OS_ERRNO,long);
111+
// Number of new succesful connections used for previous trasnfer.
112+
CURLCPP_DEFINE_INFO(CURLINFO_NUM_CONNECTS,long);
113+
// IP Address of the last connection.
114+
CURLCPP_DEFINE_INFO(CURLINFO_PRIMARY_IP,char *);
115+
// Port of the last connection.
116+
CURLCPP_DEFINE_INFO(CURLINFO_PRIMARY_PORT,long);
117+
// Local-end IP address of last connection.
118+
CURLCPP_DEFINE_INFO(CURLINFO_LOCAL_IP,char *);
119+
// Local-end port of last connection.
120+
CURLCPP_DEFINE_INFO(CURLINFO_LOCAL_PORT,long);
121+
// List of all known cookies.
122+
CURLCPP_DEFINE_INFO(CURLINFO_COOKIELIST,struct curl_slist *);
123+
// Last socket used.
124+
CURLCPP_DEFINE_INFO(CURLINFO_LASTSOCKET,long);
125+
// This option is avaiable in libcurl 7.45 or greater.
126+
#if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM >= 0x072D00
127+
// The session's active socket.
128+
CURLCPP_DEFINE_INFO(CURLINFO_ACTIVE_SOCKET,curl_socket_t *);
129+
#endif
130+
// The entry path after logging in to an FTP server.
131+
CURLCPP_DEFINE_INFO(CURLINFO_FTP_ENTRY_PATH,char *);
132+
// Certificate chain
133+
CURLCPP_DEFINE_INFO(CURLINFO_CERTINFO,struct curl_certinfo *);
134+
// This costant is avaiable with libcurl < 7.48
135+
#if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM < 0x073000
136+
// TSL session info that can be used for further processing.
137+
CURLCPP_DEFINE_INFO(CURLINFO_TLS_SESSION,struct curl_tlssessioninfo *);
138+
#else
139+
// TSL session info that can be used for further processing.
140+
CURLCPP_DEFINE_INFO(CURLINFO_TSL_SSL_PTR,struct curl_tlssessioninfo *);
141+
#endif
142+
// Whether or not a time conditional was met.
143+
CURLCPP_DEFINE_INFO(CURLINFO_CONDITION_UNMET,long);
144+
// RTSP session ID.
145+
CURLCPP_DEFINE_INFO(CURLINFO_RTSP_SESSION_ID,char *);
146+
// RTSP CSeq that will next be used.
147+
CURLCPP_DEFINE_INFO(CURLINFO_RTSP_CLIENT_CSEQ,long);
148+
// RTPS CSeq that will next be expected.
149+
CURLCPP_DEFINE_INFO(CURLINFO_RTSP_SERVER_CSEQ,long);
150+
// RTSP CSeq last received.
151+
CURLCPP_DEFINE_INFO(CURLINFO_RTSP_CSEQ_RECV,long);
152+
}
153+
47154
// Detail namespace
48155
namespace detail {
49156
template <CURLoption> struct option_t;
50157
template <CURLoption opt> using Option_type = typename option_t<opt>::type;
51-
52158
CURLCPP_DEFINE_OPTION(CURLOPT_WRITEDATA, void*);
53-
54159
/* The full URL to get/put */
55160
CURLCPP_DEFINE_OPTION(CURLOPT_URL, const char*);
56-
57161
/* Port number to connect to, if other than default. */
58162
CURLCPP_DEFINE_OPTION(CURLOPT_PORT, long);
59-
60163
/* Name of proxy to use. */
61164
CURLCPP_DEFINE_OPTION(CURLOPT_PROXY, const char*);
62-
63165
/* "user:password;options" to use when fetching. */
64166
CURLCPP_DEFINE_OPTION(CURLOPT_USERPWD, const char*);
65-
66167
/* "user:password" to use with proxy. */
67168
CURLCPP_DEFINE_OPTION(CURLOPT_PROXYUSERPWD, const char*);
68-
69169
/* Range to get, specified as an ASCII string. */
70170
CURLCPP_DEFINE_OPTION(CURLOPT_RANGE, const char*);
71-
72-
/* not used */
73-
74171
/* Specified file stream to upload from (use as input): */
75172
CURLCPP_DEFINE_OPTION(CURLOPT_READDATA, void*);
76-
77173
/* Buffer to receive error messages in, must be at least CURL_ERROR_SIZE
78-
* bytes big. If this is not used, error messages go to stderr instead: */
174+
* bytes big. If this is not used, error messages go to stderr instead: */
79175
CURLCPP_DEFINE_OPTION(CURLOPT_ERRORBUFFER, char*);
80-
81176
/* Function that will be called to store the output (instead of fwrite). The
82177
* parameters will use fwrite() syntax, make sure to follow them. */
83178
CURLCPP_DEFINE_OPTION(CURLOPT_WRITEFUNCTION, size_t(*)(void *ptr, size_t size, size_t nmemb, void *userdata));
84-
85179
/* Function that will be called to read the input (instead of fread). The
86180
* parameters will use fread() syntax, make sure to follow them. */
87181
CURLCPP_DEFINE_OPTION(CURLOPT_READFUNCTION, size_t(*)(void *buffer, size_t size, size_t nitems, void *instream));
88-
89182
/* Time-out the read operation after this amount of seconds */
90183
CURLCPP_DEFINE_OPTION(CURLOPT_TIMEOUT, long);
91-
92184
/* If the CURLOPT_INFILE is used, this can be used to inform libcurl about
93-
* how large the file being sent really is. That allows better error
94-
* checking and better verifies that the upload was successful. -1 means
95-
* unknown size.
96-
*
97-
* For large file support, there is also a _LARGE version of the key
98-
* which takes an off_t type, allowing platforms with larger off_t
99-
* sizes to handle larger files. See below for INFILESIZE_LARGE.
100-
*/
185+
* how large the file being sent really is. That allows better error
186+
* checking and better verifies that the upload was successful. -1 means
187+
* unknown size.
188+
*
189+
* For large file support, there is also a _LARGE version of the key
190+
* which takes an off_t type, allowing platforms with larger off_t
191+
* sizes to handle larger files. See below for INFILESIZE_LARGE. */
101192
CURLCPP_DEFINE_OPTION(CURLOPT_INFILESIZE, long);
102-
103193
/* POST static input fields. */
104194
CURLCPP_DEFINE_OPTION(CURLOPT_POSTFIELDS, const char*);
105-
106195
/* Set the referrer page (needed by some CGIs) */
107196
CURLCPP_DEFINE_OPTION(CURLOPT_REFERER, const char*);
108-
109197
/* Set the FTP PORT string (interface name, named or numerical IP address)
110198
Use i.e '-' to use default address. */
111199
CURLCPP_DEFINE_OPTION(CURLOPT_FTPPORT, const char*);
112-
113200
/* Set the User-Agent string (examined by some CGIs) */
114201
CURLCPP_DEFINE_OPTION(CURLOPT_USERAGENT, const char*);
115-
116202
/* If the download receives less than "low speed limit" bytes/second
117-
* during "low speed time" seconds, the operations is aborted.
118-
* You could i.e if you have a pretty high speed connection, abort if
119-
* it is less than 2000 bytes/sec during 20 seconds.
120-
*/
121-
203+
* during "low speed time" seconds, the operations is aborted.
204+
* You could i.e if you have a pretty high speed connection, abort if
205+
* it is less than 2000 bytes/sec during 20 seconds.
206+
*/
122207
/* Set the "low speed limit" */
123208
CURLCPP_DEFINE_OPTION(CURLOPT_LOW_SPEED_LIMIT, long);
124209

@@ -960,7 +1045,7 @@ namespace curl {
9601045
/**
9611046
* This method allows to get information about the current curl session.
9621047
*/
963-
template<typename T> curl::curl_easy_info<T> get_info(const CURLINFO);
1048+
template<CURLINFO Info> const curl_easy_info<detail_info::Info_type<Info>> get_info();
9641049
/**
9651050
* Simple getter method used to return the easy handle.
9661051
*/
@@ -1013,11 +1098,14 @@ namespace curl {
10131098
}
10141099

10151100
// Implementation of get_info method.
1016-
template<typename T> curl::curl_easy_info<T> curl_easy::get_info(const CURLINFO info) {
1017-
T *pointer;
1018-
const CURLcode code = curl_easy_getinfo(this->curl,info,&pointer);
1019-
curl::curl_easy_info<T> inf(code,pointer);
1020-
return inf;
1101+
template<CURLINFO Info> const curl_easy_info<detail_info::Info_type<Info>> curl_easy::get_info() {
1102+
detail_info::Info_type<Info> val;
1103+
const CURLcode code = curl_easy_getinfo(this->curl,Info,&val);
1104+
if (code != CURLE_OK) {
1105+
throw curl_easy_exception(code,__FUNCTION__);
1106+
}
1107+
curl_easy_info<detail_info::Info_type<Info>> easy_info(val);
1108+
return easy_info;
10211109
}
10221110

10231111
// Implementation of get_curl method.

include/curl_easy_info.h

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,65 +26,49 @@
2626
#ifndef curl_easy_info_h
2727
#define curl_easy_info_h
2828

29-
#include <curl/curl.h>
3029
#include <vector>
3130
#include <string>
3231
#include <utility>
3332

3433
namespace curl {
34+
/**
35+
* This object contains a pair which contains curl easy get_info result.
36+
*/
3537
template<typename T> class curl_easy_info {
3638
public:
37-
curl_easy_info(const CURLcode code, T *pointer) : _code(code), _pointer(pointer) {}
38-
std::pair<const CURLcode,T> get() const {
39-
return std::make_pair(_code,*_pointer);
39+
curl_easy_info(T pointer) : _pointer(pointer) {}
40+
T get() const {
41+
return _pointer;
4042
}
4143
private:
42-
const CURLcode _code;
43-
T *_pointer;
44+
T _pointer;
4445
};
4546

46-
template<> class curl_easy_info<char> {
47+
/**
48+
* Template specialization for char *
49+
*/
50+
template<> class curl_easy_info<char *> {
4751
public:
48-
curl_easy_info(const CURLcode code, char *pointer) : _code(code), _pointer(pointer) {}
49-
std::pair<const CURLcode,std::string> get() const {
50-
return std::make_pair(_code,std::string(_pointer));
52+
curl_easy_info(char *pointer) : _pointer(pointer) {}
53+
std::string get() const {
54+
return std::string(_pointer);
5155
}
5256
private:
53-
const CURLcode _code;
5457
char *_pointer;
5558
};
56-
57-
template<> class curl_easy_info<long> {
58-
public:
59-
curl_easy_info(const CURLcode code, long *pointer) : _code(code), _pointer(pointer) {}
60-
std::pair<const CURLcode,long> get() const {
61-
return std::make_pair(_code,*_pointer);
62-
}
63-
private:
64-
const CURLcode _code;
65-
long *_pointer;
66-
};
67-
68-
template<> class curl_easy_info<double> {
69-
public:
70-
curl_easy_info(const CURLcode code, double *pointer) : _code(code), _pointer(pointer) {}
71-
std::pair<const CURLcode,double> get() const {
72-
return std::make_pair(_code,*_pointer);
73-
}
74-
private:
75-
const CURLcode _code;
76-
double *_pointer;
77-
};
7859

79-
template<> class curl_easy_info<struct curl_slist> {
60+
/**
61+
* Template specialization for struct curl_slist *.
62+
*/
63+
template<> class curl_easy_info<struct curl_slist *> {
8064
public:
81-
curl_easy_info(const CURLcode code, struct curl_slist *pointer) : _code(code), _pointer(pointer) {}
65+
curl_easy_info(struct curl_slist *pointer) : _pointer(pointer) {}
8266
~curl_easy_info() {
8367
if (_pointer != nullptr) {
8468
curl_slist_free_all(_pointer);
8569
}
8670
}
87-
std::pair<const CURLcode,std::vector<std::string>> get() {
71+
std::vector<std::string> get() {
8872
struct curl_slist *backup = _pointer;
8973
std::vector<std::string> infos;
9074
while (backup != nullptr) {
@@ -94,10 +78,9 @@ namespace curl {
9478
}
9579
backup = backup->next;
9680
}
97-
return std::make_pair(_code,infos);
81+
return infos;
9882
}
9983
private:
100-
const CURLcode _code;
10184
struct curl_slist *_pointer;
10285
};
10386
}

src/cookie_time.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ curl::cookie_time::cookie_time(const unsigned int hour, const unsigned int minut
1414

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

2121
// Implementation of set_minutes method.
2222
curl::cookie_time *curl::cookie_time::set_minutes(const unsigned int minutes) NOEXCEPT {
23-
this->minutes = (minutes < 0 or minutes > 59) ? 0 : minutes;
23+
this->minutes = minutes > 59 ? 0 : minutes;
2424
return this;
2525
}
2626

2727
// Implementation of set_seconds method.
2828
curl::cookie_time *curl::cookie_time::set_seconds(const unsigned int seconds) NOEXCEPT {
29-
this->seconds = (seconds < 0 or seconds > 59) ? 0 : seconds;
29+
this->seconds = seconds > 59 ? 0 : seconds;
3030
return this;
3131
}
3232

src/curl_cookie.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ using std::ostringstream;
1414
namespace curl {
1515
// Implementation of the get method.
1616
const curlcpp_cookies curl_cookie::get() const NOEXCEPT {
17-
auto info = this->easy.get_info<struct curl_slist>(CURLINFO_COOKIELIST);
18-
return info.get().second;
17+
auto info = this->easy.get_info<CURLINFO_COOKIELIST>();
18+
return info.get();
1919
}
2020

2121
// Implementation of set_cookie_file method.

0 commit comments

Comments
 (0)