diff --git a/include/cookie.h b/include/cookie.h index 657ec74..f62a3d8 100644 --- a/include/cookie.h +++ b/include/cookie.h @@ -44,15 +44,17 @@ namespace curl { /** * The constructor allow a fast way to build a cookie. */ - cookie(const std::string, const std::string, const cookie_datetime &, const std::string = "", const std::string = "", const bool = false); + cookie(const std::string&, const std::string&, const cookie_datetime &, + const std::string& = "", const std::string& = "", bool = false); /** * This constructor overloades the previous one. */ - cookie(const char *, const char *, const cookie_datetime &, const char * = "", const char * = "", const bool = false); + cookie(const char *, const char *, const cookie_datetime &, const char * = "", + const char * = "", bool = false); /** * This method allows to specify the cookie name. */ - cookie *set_name(const std::string); + cookie *set_name(const std::string&); /** * This method overloads the previous one allowing to specify a const char * * instead of a string. @@ -61,7 +63,7 @@ namespace curl { /** * This method allows to specify the cookie value. */ - cookie *set_value(const std::string); + cookie *set_value(const std::string&); /** * This method allows to specify the cookie value, */ @@ -69,7 +71,7 @@ namespace curl { /** * This method allows to specify the cookie path. */ - cookie *set_path(const std::string) NOEXCEPT; + cookie *set_path(const std::string&) NOEXCEPT; /** * This method overloads the previous one allowing to specify a const char * * instead of a string. @@ -78,7 +80,7 @@ namespace curl { /** * This method allows to specify the cookie domain. */ - cookie *set_domain(const std::string) NOEXCEPT; + cookie *set_domain(const std::string&) NOEXCEPT; /** * This method overloads the previous one allowing to specify a const char * * instead of a string. @@ -87,17 +89,17 @@ namespace curl { /** * This method allows to specify the cookie security. */ - cookie *set_secure(const bool) NOEXCEPT; + cookie *set_secure(bool) NOEXCEPT; /** * This method overloads the previous one allowing to specify an integer instead * of a bool. */ - cookie *set_secure(const unsigned int); + cookie *set_secure(unsigned int); /** * This method overloads the previous one allowing to specify a string to indicate * whether the cookie is secure (with "secure" keyword) or not (empty string). */ - cookie *set_secure(const std::string) NOEXCEPT; + cookie *set_secure(const std::string&) NOEXCEPT; /** * This method overloads the previous one allowing to specify a string to indicate * whether the cookie is secure (with "secure" keyword) or not (empty string). diff --git a/include/cookie_date.h b/include/cookie_date.h index 38b7578..99991f4 100644 --- a/include/cookie_date.h +++ b/include/cookie_date.h @@ -65,27 +65,27 @@ namespace curl { /** * Constructor with parameters, which gives a fast way to build a cookie_date object. */ - cookie_date(const unsigned int, const unsigned int, const unsigned int, const unsigned int) NOEXCEPT; + cookie_date(unsigned int, unsigned int, unsigned int, unsigned int) NOEXCEPT; /** * This method allows to specify the week dayname for the date. If the day is less or equal * than zero or greater than 7, 1 will be choosen */ - cookie_date *set_week_day(const unsigned int) NOEXCEPT; + cookie_date *set_week_day(unsigned int) NOEXCEPT; /** * This method allows to specify a day for the date. If the day is less than zero or * greater than 31, 1 will be choosen. */ - cookie_date *set_day(const unsigned int) NOEXCEPT; + cookie_date *set_day(unsigned int) NOEXCEPT; /** * This method allows to specify a month for the date. If the month is not supported, * January will be choosen. */ - cookie_date *set_month(const unsigned int); + cookie_date *set_month(unsigned int); /** * This method allows to specify a year for the date. If year is less than 1970, 1970 will * be choosen. */ - cookie_date *set_year(const unsigned int) NOEXCEPT; + cookie_date *set_year(unsigned int) NOEXCEPT; /** * This method returns the week day name */ diff --git a/include/cookie_datetime.h b/include/cookie_datetime.h index f9ce39e..813788d 100644 --- a/include/cookie_datetime.h +++ b/include/cookie_datetime.h @@ -38,7 +38,7 @@ namespace curl { /** * Default constructor. */ - cookie_datetime() {} + cookie_datetime() = default; /** * The constructor with parameters allows to specify a time and a date for cookie expiration. */ diff --git a/include/cookie_time.h b/include/cookie_time.h index ee8073f..e5223e1 100644 --- a/include/cookie_time.h +++ b/include/cookie_time.h @@ -45,19 +45,19 @@ namespace curl { /** * The constructor with parameters allows to initialize attributes with custom values. */ - cookie_time(const unsigned int, const unsigned int, const unsigned int); + cookie_time(unsigned int, unsigned int, unsigned int); /** * This method allows to specify the hours. */ - cookie_time *set_hour(const unsigned int) NOEXCEPT; + cookie_time *set_hour(unsigned int) NOEXCEPT; /** * This method allows to specify the minutes. */ - cookie_time *set_minutes(const unsigned int) NOEXCEPT; + cookie_time *set_minutes(unsigned int) NOEXCEPT; /** * This method allows to specify the seconds. */ - cookie_time *set_seconds(const unsigned int) NOEXCEPT; + cookie_time *set_seconds(unsigned int) NOEXCEPT; /** * This method returns the hours. */ diff --git a/include/curl_cookie.h b/include/curl_cookie.h index c6c2112..112f896 100644 --- a/include/curl_cookie.h +++ b/include/curl_cookie.h @@ -46,13 +46,13 @@ namespace curl { /** * This constructor allow you to specify a curl_easy object. */ - curl_cookie(curl_easy &easy) : easy(easy) {} + explicit curl_cookie(curl_easy &easy) : easy(easy) {} /** * This method allow you to set the cookie file from where to read initial cookies. * If you pass an empty string or a string containing a non existing file's path, * the cookie engine will be initialized, but without reading initial cookies. */ - void set_file(const std::string); + void set_file(std::string); /** * This method allow you to specify a string that represents a cookie. Such a cookie * can be either a single line in Netscape / Mozilla format or just regular HTTP-style @@ -64,14 +64,6 @@ namespace curl { * This method overloads the one previously declared allowing to specify a vector of cookies. */ void set(const std::vector &); - /** - * This method allow you to specify a file where libcurl will write every internal - * known-stored cookie when the curl_easy destructor will be called. If no cookies - * are known, no file will be created. Using this option also enables cookies for - * this session, so if you for example follow a location it will make matching cookies - * get sent accordingly. - */ - void set_jar_file(const std::string) NOEXCEPT; /** * This method allow you to get all known cookies for a specific domain. */ diff --git a/include/curl_easy.h b/include/curl_easy.h index 7e67828..251251a 100644 --- a/include/curl_easy.h +++ b/include/curl_easy.h @@ -978,12 +978,12 @@ namespace curl { * This overloaded constructor allows users to specify a flag * used to initialize libcurl environment. */ - explicit curl_easy(const long); + explicit curl_easy(long); /** * This overloaded constructor specifies the environment * initialization flags and an output stream for the libcurl output. */ - template curl_easy(const long, curl_ios &); + template curl_easy(long, curl_ios &); /** * Copy constructor to handle pointer copy. Internally, it uses * a function which duplicates the easy handler. @@ -993,7 +993,7 @@ namespace curl { * Move constructor that moves an easy handler from an istance to * another. */ - curl_easy(curl_easy &&); + curl_easy(curl_easy &&) NOEXCEPT; /** * Assignment operator used to perform assignment between objects * of this class. @@ -1007,7 +1007,7 @@ namespace curl { /** * The destructor will perform cleanup operations. */ - ~curl_easy() NOEXCEPT; + ~curl_easy() NOEXCEPT override; /** * Allows users to specify an option for the current easy handler, * using a curl_pair object. @@ -1018,7 +1018,7 @@ namespace curl { * easy handler. In this way, you can specify any iterable data * structure. */ - template void add(Iterator, const Iterator); + template void add(Iterator, Iterator); /** * Allows users to specify an option for the current easy handler, * specify an option statically and enforce its corresponding type. @@ -1028,7 +1028,7 @@ namespace curl { * Using this function, you can explicitly pause a running connection, * and you can resume a previously paused connection. */ - void pause(const int); + void pause(int); /** * This function converts the given input string to an URL encoded * string and returns a newly allocated string. diff --git a/include/curl_easy_info.h b/include/curl_easy_info.h index e39ac80..916a213 100644 --- a/include/curl_easy_info.h +++ b/include/curl_easy_info.h @@ -36,7 +36,8 @@ namespace curl { */ template class curl_easy_info { public: - curl_easy_info(T pointer) : _pointer(pointer) {} + explicit curl_easy_info(T pointer) : _pointer(pointer) {} + T get() const { return _pointer; } @@ -49,7 +50,8 @@ namespace curl { */ template<> class curl_easy_info { public: - curl_easy_info(char *pointer) : _pointer(pointer) {} + explicit curl_easy_info(char *pointer) : _pointer(pointer) {} + std::string get() const { if (_pointer == nullptr) { return std::string(""); @@ -65,7 +67,8 @@ namespace curl { */ template<> class curl_easy_info { public: - curl_easy_info(struct curl_slist *pointer) : _pointer(pointer) {} + explicit curl_easy_info(struct curl_slist *pointer) : _pointer(pointer) {} + ~curl_easy_info() { if (_pointer != nullptr) { curl_slist_free_all(_pointer); diff --git a/include/curl_exception.h b/include/curl_exception.h index 2bcbded..dc8832c 100644 --- a/include/curl_exception.h +++ b/include/curl_exception.h @@ -54,20 +54,20 @@ namespace curl { /** * This constructor is used to build the error. */ - curl_exception(const std::string&, const std::string&); + curl_exception(const std::string&, const std::string&) NOEXCEPT; /** * The copy constructor allows to copy the object in a thread safe way. */ - curl_exception(const curl_exception &); + curl_exception(const curl_exception &) NOEXCEPT; /** * The assignment operator allows to assign the object to another object in * a thread safe way. */ - curl_exception & operator=(curl_exception &); + curl_exception & operator=(curl_exception const&); /** * The destructor, in this case, doesn't do anything. */ - ~curl_exception() NOEXCEPT; + ~curl_exception() NOEXCEPT override; using std::exception::what; /** * Returns the vector of errors. diff --git a/include/curl_global.h b/include/curl_global.h index fbac0cf..a782433 100644 --- a/include/curl_global.h +++ b/include/curl_global.h @@ -45,7 +45,7 @@ namespace curl { * Overloaded constructor that initializes curl environment * with user specified flag. */ - explicit curl_global(const long); + explicit curl_global(long); /** * Copying disabled to follow RAII idiom. diff --git a/include/curl_header.h b/include/curl_header.h index aa7e53a..dcea591 100644 --- a/include/curl_header.h +++ b/include/curl_header.h @@ -67,12 +67,12 @@ namespace curl { /** * This method allows users to add a header as string. */ - void add(const std::string); + void add(const std::string&); /** * This method allows users to add headers specifying an iterable * data structure containing the headers to add. */ - template void add(Iterator, const Iterator); + template void add(Iterator, Iterator); /** * Simple getter method that returns the pointer to the headers * list. @@ -89,7 +89,7 @@ namespace curl { } // Implementation of copy constructor. - inline curl_header::curl_header(const curl_header &header) : headers(nullptr) { + inline curl_header::curl_header(const curl_header &header) : headers(nullptr), size(0) { *this = header; } diff --git a/include/curl_info.h b/include/curl_info.h index 7976623..8a8322c 100644 --- a/include/curl_info.h +++ b/include/curl_info.h @@ -49,7 +49,7 @@ namespace curl { * Overloaded constructor that allows users to specify the * libcurl version. */ - explicit curl_info(const CURLversion); + explicit curl_info(CURLversion); /** * Returns a string that shows what host information that this * libcurl was built for. @@ -114,7 +114,10 @@ namespace curl { // Implementation of get_ssl_version. inline std::string curl_info::get_ssl_version() const NOEXCEPT { - return std::string(this->version->ssl_version); + if (this->version->ssl_version == nullptr) { + return std::string(""); + } + return std::string(this->version->ssl_version); } // Implementation of get_libz_version. diff --git a/include/curl_interface.h b/include/curl_interface.h index 889df06..7e9067f 100644 --- a/include/curl_interface.h +++ b/include/curl_interface.h @@ -46,7 +46,7 @@ namespace curl { * Overloaded constructor that initializes curl environment * with user specified flag. */ - explicit curl_interface(const long); + explicit curl_interface(long); /** * The virtual destructor will provide an easy and clean * way to deallocate resources, closing curl environment @@ -59,20 +59,15 @@ namespace curl { * This struct is used for initializing curl only once * it is implemented as a singleton pattern */ - struct global_initialisator { - explicit global_initialisator(const long); - ~global_initialisator(); + struct global_initializer { + explicit global_initializer(long); + ~global_initializer(); }; /** - * The singleton instance + * the singleton initialization, constructing a global_initializer. */ - static global_initialisator instance; - - /** - * the singleton initialization, constructing a global_initialisator. - */ - static void init(const long flag); + static void init(long flag); }; // Implementation of constructor. @@ -86,16 +81,15 @@ namespace curl { } // Implementation of the virtual destructor. - template curl_interface::~curl_interface() { - } + template curl_interface::~curl_interface() = default; // Implementation of the static initialization function template void curl_interface::init(const long flag) { - static global_initialisator _instance {flag}; + static global_initializer _instance {flag}; } - // Implementation of the singleton initalizator - template curl_interface::global_initialisator::global_initialisator(const long flag) { + // Implementation of the singleton initializer + template curl_interface::global_initializer::global_initializer(const long flag) { const CURLcode code = curl_global_init(flag); if (code != CURLE_OK) { throw curl_easy_exception(code,__FUNCTION__); @@ -103,7 +97,7 @@ namespace curl { } // Implementation of the singleton destructor - template curl_interface::global_initialisator::~global_initialisator() { + template curl_interface::global_initializer::~global_initializer() { curl_global_cleanup(); } } diff --git a/include/curl_ios.h b/include/curl_ios.h index 2a7fdd6..1b3efcd 100644 --- a/include/curl_ios.h +++ b/include/curl_ios.h @@ -47,7 +47,7 @@ namespace { // Default in-memory read callback. size_t read_memory_callback(void *contents, size_t size, size_t nmemb, void *userp) { const size_t realsize = size * nmemb; - std::istream* const mem = static_cast(userp); + auto* const mem = static_cast(userp); mem->read(static_cast(contents), realsize); return static_cast(mem->gcount()); } @@ -61,14 +61,17 @@ namespace curl { curl_ios(T *stream, curlcpp_callback_type callback_ptr) : _stream(stream) { this->set_callback(callback_ptr); } + // This method allow to specify a custom callback pointer. void set_callback(curlcpp_callback_type callback_ptr) { _callback_ptr = callback_ptr == nullptr ? write_callback : callback_ptr; } + // This method returns the stream pointer. T *get_stream() const { return _stream; } + // This method return the callback function pointer. curlcpp_callback_type get_function() const { return _callback_ptr; @@ -76,6 +79,7 @@ namespace curl { private: // Callback pointer. curlcpp_callback_type _callback_ptr; + // Generic stream pointer. T* _stream; }; @@ -84,23 +88,28 @@ namespace curl { // Template specialization for fstream template<> class curl_ios { public: - // This constructor will initialize the stream with a customized stream and a default in-memory write callback. - curl_ios(std::fstream &fstream) : _callback_ptr(write_callback) { + // This constructor will initialize the stream with a customized stream and a default + // in-memory write callback. + explicit curl_ios(std::fstream &fstream) : _callback_ptr(write_callback) { this->set_stream(fstream); } + // This constructor will initialize the the stream with a custom stream and the callback with a customized one. curl_ios(std::fstream &fstream, curlcpp_callback_type callback_ptr) { this->set_callback(callback_ptr)->set_stream(fstream); } + // This method allow to specify a custom callback pointer. curl_ios *set_callback(curlcpp_callback_type callback_ptr) { _callback_ptr = callback_ptr == nullptr ? write_callback : callback_ptr; return this; } + // This method returns the stream pointer. std::fstream *get_stream() const { return this->_fstream; } + // This method returns the callback for this curl_ios. curlcpp_callback_type get_function() const { return this->_callback_ptr; @@ -117,6 +126,7 @@ namespace curl { private: // Callback pointer. curlcpp_callback_type _callback_ptr; + // Generic stream pointer. std::fstream *_fstream; }; @@ -124,26 +134,36 @@ namespace curl { // Template specialization for ostream class. template<> class curl_ios { public: - // This constructor will initialize the stream with cout and the callback with a default in-memory write callback. + // This constructor will initialize the stream with cout and the callback with a + // default in-memory write callback. curl_ios() : _callback_ptr(write_callback), _io_stream(&std::cout) {} - // This constructor will initialize the stream with a customized stream and a default in-memory write callback. - curl_ios(std::ostream &io_stream) : _callback_ptr(write_callback), _io_stream(&io_stream) {} + + // This constructor will initialize the stream with a customized stream and a + // default in-memory write callback. + explicit curl_ios(std::ostream &io_stream) : + _callback_ptr(write_callback), _io_stream(&io_stream) {} + // This constructor will initialize the stream with cout and a customized write callback. - curl_ios(curlcpp_callback_type callback_ptr) : _io_stream(&std::cout) { + explicit curl_ios(curlcpp_callback_type callback_ptr) : _io_stream(&std::cout) { this->set_callback(callback_ptr); } - // This constructor will initialize the the stream with a custom stream and the callback with a customized one. + + // This constructor will initialize the the stream with a custom stream and the + // callback with a customized one. curl_ios(std::ostream &io_stream, curlcpp_callback_type callback_ptr) : _io_stream(&io_stream) { this->set_callback(callback_ptr); } + // This method allow to specify a custom callback pointer. void set_callback(curlcpp_callback_type callback_ptr) { _callback_ptr = callback_ptr == nullptr ? write_callback : callback_ptr; } + // This method returns the stream pointer. std::ostream *get_stream() const { return this->_io_stream; } + // This method returns the callback for this curl_ios. curlcpp_callback_type get_function() const { return this->_callback_ptr; @@ -151,6 +171,7 @@ namespace curl { private: // The callback pointer. curlcpp_callback_type _callback_ptr; + // A stream pointer. std::ostream *_io_stream; }; @@ -160,20 +181,25 @@ namespace curl { template<> class curl_ios { public: //This constructor allows to specify a custom stringstream stream. - curl_ios(std::stringstream &o_stream) : _callback_ptr(write_callback), _o_stream(&o_stream) {} + explicit curl_ios(std::stringstream &o_stream) : + _callback_ptr(write_callback), _o_stream(&o_stream) {} + //This constructor allows to specify a custom stream and a custom callback pointer. curl_ios(std::stringstream &o_stream, curlcpp_callback_type callback_ptr) { _o_stream = &o_stream; this->set_callback(callback_ptr); } + // This method allows to specify a custom callback pointer. void set_callback(curlcpp_callback_type callback_ptr) { _callback_ptr = callback_ptr == nullptr ? write_callback : callback_ptr; } + // This method returns the stream pointer. std::stringstream *get_stream() const { return this->_o_stream; } + // This method returns the callback pointer. curlcpp_callback_type get_function() const { return this->_callback_ptr; @@ -181,6 +207,7 @@ namespace curl { private: // The callback pointer. curlcpp_callback_type _callback_ptr; + // The ostringstream pointer. std::stringstream *_o_stream; }; @@ -190,20 +217,25 @@ namespace curl { template<> class curl_ios { public: //This constructor allows to specify a custom ostringstream stream. - curl_ios(std::ostringstream &o_stream) : _callback_ptr(write_callback), _o_stream(&o_stream) {} + explicit curl_ios(std::ostringstream &o_stream) : + _callback_ptr(write_callback), _o_stream(&o_stream) {} + //This constructor allows to specify a custom stream and a custom callback pointer. curl_ios(std::ostringstream &o_stream, curlcpp_callback_type callback_ptr) { _o_stream = &o_stream; this->set_callback(callback_ptr); } + // This method allows to specify a custom callback pointer. void set_callback(curlcpp_callback_type callback_ptr) { _callback_ptr = callback_ptr == nullptr ? write_callback : callback_ptr; } + // This method returns the stream pointer. std::ostringstream *get_stream() const { return this->_o_stream; } + // This method returns the callback pointer. curlcpp_callback_type get_function() const { return this->_callback_ptr; @@ -211,6 +243,7 @@ namespace curl { private: // The callback pointer. curlcpp_callback_type _callback_ptr; + // The ostringstream pointer. std::ostringstream *_o_stream; }; @@ -221,26 +254,32 @@ namespace curl { public: // The default constructor will initialize the callback pointer and the stream with default values. curl_ios() : _callback_ptr(read_memory_callback), _istream(&std::cin) {} + //This constructor allows to specify an input stream while the a default callback pointer will be used. - curl_ios(std::istream &istream) : _callback_ptr(read_memory_callback) { + explicit curl_ios(std::istream &istream) : _callback_ptr(read_memory_callback) { _istream = &istream; } + // This overloaded constructor allows to specify a custom callback pointer while the stream will be cin. - curl_ios(curlcpp_callback_type callback_ptr) : _istream(&std::cin) { + explicit curl_ios(curlcpp_callback_type callback_ptr) : _istream(&std::cin) { this->set_callback(callback_ptr); } + // This method allows to specify a custom stream and a custom callback pointer. curl_ios(std::istream &i_stream, curlcpp_callback_type callback_ptr) : _istream(&i_stream) { this->set_callback(callback_ptr); } + // This method allows to specify a custom callback pointer. void set_callback(curlcpp_callback_type callback_ptr) { _callback_ptr = callback_ptr == nullptr ? write_callback : callback_ptr; } + // This method returns the stream pointer. std::istream *get_stream() const { return _istream; } + // This method returns the callback pointer. curlcpp_callback_type get_function() const { return _callback_ptr; @@ -248,6 +287,7 @@ namespace curl { private: // The callback pointer. curlcpp_callback_type _callback_ptr; + // The stream pointer. std::istream *_istream; }; diff --git a/include/curl_multi.h b/include/curl_multi.h index 6f52fd3..579e366 100644 --- a/include/curl_multi.h +++ b/include/curl_multi.h @@ -178,7 +178,7 @@ namespace curl { * The attributes will be initialized with constructors parameters. With * this constructor we provide a fast way to build this kind of object. */ - curl_message(const CURLMsg *); + explicit curl_message(const CURLMsg *); /** * Inline getter method used to return * the message for a single handler. @@ -213,7 +213,7 @@ namespace curl { * to initialize the entire curl environment using custom * options. */ - explicit curl_multi(const long globalOptions); + explicit curl_multi(long globalOptions); /** * Move constructor which moves internal data to another object. */ @@ -226,7 +226,7 @@ namespace curl { * Destructor to deallocate all the resources using * libcurl. */ - ~curl_multi() NOEXCEPT; + ~curl_multi() NOEXCEPT override; /** * This method allows users to add an option to the multi * handler, using an object of curl_pair type. @@ -237,7 +237,7 @@ namespace curl { * easy handler. In this way, you can specify any iterable data * structure. */ - template void add(Iterator, const Iterator); + template void add(Iterator, Iterator); /** * Allows users to specify an option for the current multi handler, @@ -284,7 +284,7 @@ namespace curl { * given an action. Read the libcurl online documentation to learn more * about this function! */ - bool socket_action(const curl_socket_t, const int); + bool socket_action(curl_socket_t, int); /** * This method wraps the libcurl function that extracts file descriptor * information from the multi handler. @@ -295,12 +295,12 @@ namespace curl { * This function polls on all file descriptors used by the curl easy handles * contained in the given multi handle set. */ - void wait(struct curl_waitfd [], const unsigned int, const int, int *); + void wait(struct curl_waitfd [], unsigned int, int, int *); /** * This function creates an association in the multi handle between the given * socket and a private pointer of the application. */ - void assign(const curl_socket_t, void *); + void assign(curl_socket_t, void *); /** * If you are using the libcurl multi interface you should call this method * to figure out how long your application should wait for socket actions diff --git a/include/curl_option.h b/include/curl_option.h index 9a5ab4c..86bfafb 100644 --- a/include/curl_option.h +++ b/include/curl_option.h @@ -23,6 +23,9 @@ * SOFTWARE. */ +#ifndef __curlcpp__curl_option__ +#define __curlcpp__curl_option__ + #include #include "curl_pair.h" @@ -41,3 +44,5 @@ namespace curl { return curl_pair(opt, val); } } // of namespace curl + +#endif /* defined(__curlcpp__curl_option__) */ diff --git a/include/curl_share.h b/include/curl_share.h index c0332b1..c7ae93e 100644 --- a/include/curl_share.h +++ b/include/curl_share.h @@ -102,7 +102,7 @@ namespace curl { * the share handle and initialize the libcurl environment * using customs flags. */ - explicit curl_share(const long); + explicit curl_share(long); /** * Copy constructor to perform the copy of the share handle. */ @@ -116,30 +116,32 @@ namespace curl { * The destructor will free the share handler previously * allocated. */ - ~curl_share() NOEXCEPT; + ~curl_share() NOEXCEPT override; /** * Add method used to add options to the share handle. */ - template void add(const detail::SHOption_type); + template void add(detail::SHOption_type); /** * Allows users to specify a list of options for the current * easy handler. In this way, you can specify any iterable data * structure. */ - template void add(Iterator, const Iterator); + template void add(Iterator, Iterator); + protected: + void initialize_curl_share(); private: CURLSH *curl; }; // Implementation of overloaded constructor. inline curl_share::curl_share(const long flag) : curl_interface(flag) { - curl_share(); + initialize_curl_share(); } // Implementation of copy constructor. inline curl_share::curl_share(const curl_share &share) : curl_interface() { (void)share; // unused - curl_share(); + initialize_curl_share(); } // Implementation of add method diff --git a/include/curl_utility.h b/include/curl_utility.h index f2c7101..ad39690 100644 --- a/include/curl_utility.h +++ b/include/curl_utility.h @@ -1,8 +1,26 @@ -/* - * File: curl_utility.h - * Author: Giuseppe +/** + * The MIT License (MIT) * - * Created on March 26, 2014, 21:17 PM + * Copyright (c) 2014 - Giuseppe Persico + * File - curl_utility.h + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ #ifndef __curlcpp__curl_utility__ diff --git a/src/cookie.cpp b/src/cookie.cpp index e0cfccc..1220ac9 100644 --- a/src/cookie.cpp +++ b/src/cookie.cpp @@ -8,58 +8,62 @@ using std::string; // Implementation of constructor. -curl::cookie::cookie(const string name, const string value, const cookie_datetime &datetime, const string path, const string domain, const bool secure) { - this->set_name(name)->set_value(value)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime); +curl::cookie::cookie(const string& name, const string& value, + const cookie_datetime &datetime, const string& path, const string& domain, const bool secure) { + + set_name(name)->set_value(value)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime); } // Implementation of overloaded constructor. -curl::cookie::cookie(const char *name, const char * value, const cookie_datetime &datetime, const char *path, const char *domain, const bool secure) { - this->set_name(name)->set_value(value)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime); +curl::cookie::cookie(const char *name, const char * value, const cookie_datetime &datetime, + const char *path, const char *domain, const bool secure) { + + set_name(name)->set_value(value)->set_path(path)->set_domain(domain)->set_secure(secure)->set_datetime(datetime); } // Implementation of set_name method. -curl::cookie *curl::cookie::set_name(const string name) { - if (name.empty()) { - throw new curl_easy_exception("Cookie must have a name",__FUNCTION__); +curl::cookie *curl::cookie::set_name(const string& _name) { + if (_name.empty()) { + throw curl_easy_exception("Cookie must have a name",__FUNCTION__); } - this->name = name; + this->name = _name; return this; } // Implementation of set_name overloaded method. -curl::cookie *curl::cookie::set_name(const char *name) { - if (name == nullptr) { - throw new curl_easy_exception("Cookie must have a name",__FUNCTION__); +curl::cookie *curl::cookie::set_name(const char *_name) { + if (_name == nullptr) { + throw curl_easy_exception("Cookie must have a name",__FUNCTION__); } - this->name = string(name); + this->name = string(_name); return this; } // Implementation of set_name method. -curl::cookie *curl::cookie::set_value(const string value) { - this->value = value; +curl::cookie *curl::cookie::set_value(const string& _value) { + this->value = _value; return this; } // Implementation of set_value method. -curl::cookie *curl::cookie::set_value(const char *value) { - if (value == nullptr) { +curl::cookie *curl::cookie::set_value(const char *_value) { + if (_value == nullptr) { this->value = ""; } else { - this->value = string(value); + this->value = string(_value); } return this; } // Implementation of set_path method. -curl::cookie *curl::cookie::set_path(const string path) NOEXCEPT { - this->path = path.empty() ? "/" : path; +curl::cookie *curl::cookie::set_path(const string& _path) NOEXCEPT { + this->path = _path.empty() ? "/" : _path; return this; } // Implementation of set_path overloaded method. -curl::cookie *curl::cookie::set_path(const char *path) NOEXCEPT { - if (path == nullptr) { +curl::cookie *curl::cookie::set_path(const char *_path) NOEXCEPT { + if (_path == nullptr) { this->path = "/"; } else { this->path = string(path); @@ -68,14 +72,14 @@ curl::cookie *curl::cookie::set_path(const char *path) NOEXCEPT { } // Implelementation of set_domain method. -curl::cookie *curl::cookie::set_domain(const string domain) NOEXCEPT { - this->domain = domain; +curl::cookie *curl::cookie::set_domain(const string& _domain) NOEXCEPT { + this->domain = _domain; return this; } // Implementation of set_domain overloaded method. -curl::cookie *curl::cookie::set_domain(const char *domain) NOEXCEPT { - if (domain == nullptr) { +curl::cookie *curl::cookie::set_domain(const char *_domain) NOEXCEPT { + if (_domain == nullptr) { this->domain = ""; } else { this->domain = string(domain); @@ -84,41 +88,46 @@ curl::cookie *curl::cookie::set_domain(const char *domain) NOEXCEPT { } // Implementation of set_secure method. -curl::cookie *curl::cookie::set_secure(const bool secure) NOEXCEPT { - this->secure = secure; +curl::cookie *curl::cookie::set_secure(const bool _secure) NOEXCEPT { + this->secure = _secure; return this; } // Implementation of set_secure overloaded method. -curl::cookie *curl::cookie::set_secure(const string secure) NOEXCEPT { - if (secure == "secure") { - this->secure = 1; +curl::cookie *curl::cookie::set_secure(const string& _secure) NOEXCEPT { + if (_secure == "secure") { + set_secure(true); } else { - this->secure = 0; + set_secure(false); } return this; } // Implementation of set_secure overloaded method. -curl::cookie *curl::cookie::set_secure(const char *secure) NOEXCEPT { - return this->set_secure(string(secure)); +curl::cookie *curl::cookie::set_secure(const char *_secure) NOEXCEPT { + if (_secure == nullptr) { + set_secure(false); + } else { + set_secure(string(_secure)); + } + return this; } // Implementation of set_secure method. -curl::cookie *curl::cookie::set_secure(const unsigned int secure) { - if (secure == 0) { - this->secure = false; - } else if (secure == 1) { - this->secure = true; +curl::cookie *curl::cookie::set_secure(const unsigned int _secure) { + if (_secure == 0) { + set_secure(false); + } else if (_secure == 1) { + set_secure(true); } else { - throw new curl_easy_exception("The security can be 0 (false) or 1 (true)",__FUNCTION__); + throw curl_easy_exception("The security can be 0 (false) or 1 (true)",__FUNCTION__); } return this; } // Implementation of set_datetime method. -curl::cookie *curl::cookie::set_datetime(const cookie_datetime &datetime) NOEXCEPT { - this->datetime = datetime; +curl::cookie *curl::cookie::set_datetime(const cookie_datetime & _datetime) NOEXCEPT { + this->datetime = _datetime; return this; } @@ -154,6 +163,8 @@ curl::cookie_datetime curl::cookie::get_datetime() const NOEXCEPT { // Implementation of get_formatted method. string curl::cookie::get_formatted() NOEXCEPT { - string secure = this->is_secure() == 1 ? "secure" : ""; - return "Set-Cookie: "+this->name+"="+this->value+"; expires="+this->datetime.get_formatted()+"; path="+this->path+"; domain="+this->domain+" "+secure; + string _secure = this->is_secure() == 1 ? "secure" : ""; + + return "Set-Cookie: "+this->name+"="+this->value+"; expires="+this->datetime.get_formatted() + +"; path="+this->path+"; domain="+this->domain+" "+_secure; } diff --git a/src/cookie_time.cpp b/src/cookie_time.cpp index 647a73c..eb2096a 100644 --- a/src/cookie_time.cpp +++ b/src/cookie_time.cpp @@ -8,25 +8,27 @@ using std::ostringstream; // Implementation of constructor with parameters. -curl::cookie_time::cookie_time(const unsigned int hour, const unsigned int minutes, const unsigned int seconds) { +curl::cookie_time::cookie_time(const unsigned int hour, const unsigned int minutes, + const unsigned int seconds) { + this->set_hour(hour)->set_minutes(minutes)->set_seconds(seconds); } // Implementation of set_hour method. -curl::cookie_time *curl::cookie_time::set_hour(const unsigned int hour) NOEXCEPT { - this->hour = hour > 23 ? 0 : hour; +curl::cookie_time *curl::cookie_time::set_hour(unsigned int _hour) NOEXCEPT { + this->hour = _hour > 23 ? 0 : _hour; return this; } // Implementation of set_minutes method. -curl::cookie_time *curl::cookie_time::set_minutes(const unsigned int minutes) NOEXCEPT { - this->minutes = minutes > 59 ? 0 : minutes; +curl::cookie_time *curl::cookie_time::set_minutes(unsigned int _minutes) NOEXCEPT { + this->minutes = _minutes > 59 ? 0 : _minutes; return this; } // Implementation of set_seconds method. -curl::cookie_time *curl::cookie_time::set_seconds(const unsigned int seconds) NOEXCEPT { - this->seconds = seconds > 59 ? 0 : seconds; +curl::cookie_time *curl::cookie_time::set_seconds(unsigned int _seconds) NOEXCEPT { + this->seconds = _seconds > 59 ? 0 : _seconds; return this; } diff --git a/src/curl_easy.cpp b/src/curl_easy.cpp index 37088c3..19cc381 100644 --- a/src/curl_easy.cpp +++ b/src/curl_easy.cpp @@ -41,7 +41,7 @@ curl_easy::curl_easy(const curl_easy &easy) : curl_interface(), curl(nullptr) { } // Implementation of move constructor -curl_easy::curl_easy(curl_easy &&other) : curl_interface(), curl(nullptr) { +curl_easy::curl_easy(curl_easy &&other) NOEXCEPT : curl_interface(), curl(nullptr) { this->curl = other.curl; // Other's pointer is set to nullptr so that destructor doesn't call the // cleanup function. diff --git a/src/curl_exception.cpp b/src/curl_exception.cpp index 472ee9e..72a62c7 100644 --- a/src/curl_exception.cpp +++ b/src/curl_exception.cpp @@ -14,19 +14,19 @@ curlcpp_traceback curl::curl_exception::traceback; std::mutex curl::curl_exception::tracebackLocker; // Constructor implementation. Every call will push into the calls stack the function name and the error occurred. -curl_exception::curl_exception(const std::string &error, const std::string &fun_name) { +curl_exception::curl_exception(const std::string &error, const std::string &fun_name) NOEXCEPT { curl_exception::tracebackLocker.lock(); curl_exception::traceback.insert(curl_exception::traceback.begin(),curlcpp_traceback_object(error,fun_name)); curl_exception::tracebackLocker.unlock(); } // Copy constructor implementation. It makes a copy of the traceback in a thread safe way. -curl_exception::curl_exception(const curl_exception &object) { +curl_exception::curl_exception(const curl_exception &object) NOEXCEPT { curl_exception::traceback = object.get_traceback(); } // Assignment operator implementation. Implement the assignment operation in a thread safe way avoiding self assignment. -curl_exception& curl_exception::operator=(curl_exception &object) { +curl_exception& curl_exception::operator=(curl_exception const &object) { if (&object != this) { curl_exception::traceback = object.get_traceback(); } diff --git a/src/curl_form.cpp b/src/curl_form.cpp index f7d2c3d..686bd4b 100644 --- a/src/curl_form.cpp +++ b/src/curl_form.cpp @@ -54,62 +54,85 @@ void curl_form::add(const curl_pair &form_name, const cur form_name.first(),form_name.second(), form_content.first(),form_content.second(), CURLFORM_END) != 0) { + throw curl_exception("Error while adding the form",__FUNCTION__); } } // Implementation of overloaded add method. -void curl_form::add(const curl_pair &form_name, const curl_pair &form_content, const curl_pair &content_type) { +void curl_form::add(const curl_pair &form_name, + const curl_pair &form_content, + const curl_pair &content_type) { + if (curl_formadd(&this->form_post,&this->last_ptr, form_name.first(),form_name.second(), form_content.first(),form_content.second(), content_type.first(),content_type.second(), CURLFORM_END) != 0) { + throw curl_exception("Error while adding the form",__FUNCTION__); } } -void curl_form::add(const curl_pair &form_name, const curl_pair &form_content, const curl_pair &content_length) { +void curl_form::add(const curl_pair &form_name, + const curl_pair &form_content, + const curl_pair &content_length) { + if (curl_formadd(&this->form_post,&this->last_ptr, form_name.first(),form_name.second(), form_content.first(),form_content.second(), content_length.first(),content_length.second(), CURLFORM_END) != 0) { + throw curl_exception("Error while adding the form",__FUNCTION__); } } // Implementation of add overloaded method. -void curl_form::add(const curl_pair &form_name, const curl_pair &name_length, const curl_pair &form_content) { +void curl_form::add(const curl_pair &form_name, + const curl_pair &name_length, + const curl_pair &form_content) { + if (curl_formadd(&this->form_post,&this->last_ptr, form_name.first(),form_name.second(), form_content.first(),form_content.second(), name_length.first(),name_length.second(), CURLFORM_END) != 0) { + throw curl_exception("Error while adding the form",__FUNCTION__); } } // Implementation of add overloaded method. -void curl_form::add(const curl_pair &form_name, const curl_pair &form_content, const curl_pair &content_length, const curl_pair &content_type) { +void curl_form::add(const curl_pair &form_name, + const curl_pair &form_content, + const curl_pair &content_length, + const curl_pair &content_type) { + if (curl_formadd(&this->form_post,&this->last_ptr, form_name.first(),form_name.second(), form_content.first(),form_content.second(), content_length.first(),content_length.second(), content_type.first(),content_type.second(), CURLFORM_END) != 0) { + throw curl_exception("Error while adding the form",__FUNCTION__); } } // Implementation of add overloaded method for CURLFORM_BUFFERPTR uses -void curl_form::add(const curl_pair &form_name, const curl_pair &form_bufname, const curl_pair &form_content, const curl_pair &content_length) { +void curl_form::add(const curl_pair &form_name, + const curl_pair &form_bufname, + const curl_pair &form_content, + const curl_pair &content_length) { + if (curl_formadd(&this->form_post,&this->last_ptr, form_name.first(),form_name.second(), form_bufname.first(),form_bufname.second(), form_content.first(),form_content.second(), content_length.first(),content_length.second(), CURLFORM_END) != 0) { + throw curl_exception("Error while adding the form",__FUNCTION__); } } @@ -122,18 +145,18 @@ void curl_form::add(const curl_pair &form_name, const vec const size_t size = files.size(); struct curl_forms *new_files; this->is_null(new_files = new struct curl_forms[size]); - if (new_files == nullptr) { - throw bad_alloc(); - } + for (size_t i = 0; i < size; ++i) { new_files[i].option = CURLFORM_FILE; new_files[i].value = files[i].c_str(); } + if (curl_formadd(&this->form_post,&this->last_ptr, form_name.first(),form_name.second(), CURLFORM_ARRAY,new_files, CURLFORM_END) != 0) { delete []new_files; + throw curl_exception("Error while adding the form",__FUNCTION__); } delete []new_files; diff --git a/src/curl_header.cpp b/src/curl_header.cpp index e84bbaa..1a2da62 100644 --- a/src/curl_header.cpp +++ b/src/curl_header.cpp @@ -53,7 +53,7 @@ curl_header::~curl_header() NOEXCEPT { } // Implementation of add overloaded method. -void curl_header::add(const string header) { +void curl_header::add(const string& header) { this->headers = curl_slist_append(this->headers,header.c_str()); if (this->headers == nullptr) { throw curl_exception("Null pointer exception",__FUNCTION__); diff --git a/src/curl_info.cpp b/src/curl_info.cpp index bfc8861..a8e36b9 100644 --- a/src/curl_info.cpp +++ b/src/curl_info.cpp @@ -25,8 +25,8 @@ list curl_info::get_protocols() const NOEXCEPT { list protocols; const char *const *const prot = this->version->protocols; unsigned int i = 0; - while (*(prot+i) != NULL) { - protocols.push_back(string(*(prot+i))); + while (*(prot+i) != nullptr) { + protocols.emplace_back(string(*(prot+i))); i++; } return protocols; diff --git a/src/curl_share.cpp b/src/curl_share.cpp index 4f2a610..3b8e620 100644 --- a/src/curl_share.cpp +++ b/src/curl_share.cpp @@ -9,10 +9,7 @@ using curl::curl_share; // Implementation of default constructor. curl_share::curl_share() : curl_interface() { - this->curl = curl_share_init(); - if (this->curl == nullptr) { - throw curl_share_exception("Null pointer intercepted",__FUNCTION__); - } + initialize_curl_share(); } // Implementation of assignment operator to perform a deep copy. @@ -20,7 +17,7 @@ curl_share &curl_share::operator=(const curl::curl_share &share) { if (this != &share) { return *this; } - curl_share(); + initialize_curl_share(); return *this; } @@ -30,4 +27,11 @@ curl_share::~curl_share() NOEXCEPT { curl_share_cleanup(this->curl); this->curl = nullptr; } +} + +void curl_share::initialize_curl_share() { + this->curl = curl_share_init(); + if (this->curl == nullptr) { + throw curl_share_exception("Null pointer intercepted",__FUNCTION__); + } } \ No newline at end of file