cURL
Haxx ad
libcurl

curl's project page on SourceForge.net

Sponsors:
Haxx

cURL > Mailing List > Monthly Index > Single Mail

curlpp mailing list Archives

[cURLpp] curl_easy_getinfo information

From: Peteris Krumins [Newsgroups] <pknewsgroups_at_gmail.com>
Date: Wed, 27 Jul 2005 01:23:59 +0300

Hello!

 I have added functionality to cURLpp for getting information from a
curl handle via
 curl_easy_getinfo.

 I have attached a patch which can be applied to cURLpp-0.5.1 version,
it also includes
 changes mentioned on mailing list about type typos in options.hpp and
it fixes a small exception
 error where std::runtime_error could be thrown istead of
cURLpp::RuntimeError.

 Also I wrote an example06 program showing how to use getInfo functionality.

 I wrote the patch because of simple cookie interface I added to curl
which can be found
 at http://curl.haxx.se/mail/lib-2005-07/0149.html or on my web page
http://www.catonmat.net

 This is patch 1of2.
 Patch 2of2 which I will send shortly includes simple cookie interface
for cURLpp.

Enjoy!
P.Krumins

Index: curlpp/SList.cpp
===================================================================
--- curlpp/SList.cpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/SList.cpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -78,6 +72,19 @@
   return (*this);
 }
 
+cURLpp::SList &
+cURLpp::SList::operator=(cURL::curl_slist &list)
+{
+ cURL::curl_slist *tr = &list;
+ while (tr) {
+ mData.push_back(tr->data);
+ tr = tr->next;
+ }
+ mList = &list;
+ return *this;
+}
+
+
 cURL::curl_slist *
 cURLpp::SList::cslist() const
 {
Index: curlpp/Options.hpp
===================================================================
--- curlpp/Options.hpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/Options.hpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -111,9 +111,9 @@
 #endif
     typedef cURLpp::OptionTrait< bool, cURL::CURLOPT_POST > Post;
     typedef cURLpp::OptionTrait< std::string, cURL::CURLOPT_POSTFIELDS > PostFields;
- typedef cURLpp::OptionTrait< std::string, cURL::CURLOPT_POSTFIELDSIZE > PostFieldSize;
+ typedef cURLpp::OptionTrait< long, cURL::CURLOPT_POSTFIELDSIZE > PostFieldSize;
 #ifdef CURLOPT_POSTFIELDSIZE_LARGE
- typedef cURLpp::OptionTrait< std::string, cURL::CURLOPT_POSTFIELDSIZE_LARGE > PostFieldSizeLarge;
+ typedef cURLpp::OptionTrait< curl_off_t, cURL::CURLOPT_POSTFIELDSIZE_LARGE > PostFieldSizeLarge;
 #endif
     typedef cURLpp::OptionTrait< std::list< cURLpp::FormPart * >, cURL::CURLOPT_HTTPPOST > HttpPost;
     typedef cURLpp::OptionTrait< std::string, cURL::CURLOPT_REFERER > Referer;
Index: curlpp/Easy.cpp
===================================================================
--- curlpp/Easy.cpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/Easy.cpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -22,8 +22,8 @@
  */
 
 #include "Easy.hpp"
+#include "SList.hpp"
 
-
 void
 cURLpp::Easy::perform()
 {
@@ -55,4 +55,36 @@
     OptionList::setOpt(options);
 }
 
+template <class T>
+void
+cURLpp::Easy::getInfo(const cURL::CURLINFO &option, T &val)
+{
+ cURL::CURLcode code;
+
+ code = cURL::curl_easy_getinfo(myCurl.getHandle(), option, &val);
+ if ( code != cURL::CURLE_OK ) {
+ throw cURLpp::LibcurlRuntimeError( myCurl.getErrorBuffer(), code );
+ }
+}
 
+template <>
+void
+cURLpp::Easy::getInfo<cURLpp::SList>(const cURL::CURLINFO &option, cURLpp::SList &val)
+{
+ cURL::CURLcode code;
+ cURL::curl_slist *cookieList;
+
+ code = cURL::curl_easy_getinfo(myCurl.getHandle(), option, &cookieList);
+ if ( code != cURL::CURLE_OK ) {
+ throw cURLpp::LibcurlRuntimeError( myCurl.getErrorBuffer(), code );
+ }
+
+ val = *cookieList;
+}
+
+// explicitly instantiate for char *, long, double
+// these and SList are the only types to be used with getInfo
+//
+template void cURLpp::Easy::getInfo<char *>(const cURL::CURLINFO &option, char * &val);
+template void cURLpp::Easy::getInfo<long>(const cURL::CURLINFO &option, long &val);
+template void cURLpp::Easy::getInfo<double>(const cURL::CURLINFO &option, double &val);
Index: curlpp/SList.hpp
===================================================================
--- curlpp/SList.hpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/SList.hpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -41,6 +41,7 @@
     ~SList();
 
     SList &operator=(const std::list< std::string > &list);
+ SList &operator=(cURL::curl_slist &list);
     operator std::list< std::string > ();
 
     cURL::curl_slist *cslist() const;
Index: curlpp/cURLpp.cpp
===================================================================
--- curlpp/cURLpp.cpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/cURLpp.cpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -19,7 +19,7 @@
    std::string buffer;
    char* p = cURL::curl_escape(url.c_str(), url.size());
    if(!p) {
- throw std::runtime_error( "unable to escape the string" ); //we got an error
+ throw RuntimeError( "unable to escape the string" );
    }
    else {
      buffer = p;
@@ -35,7 +35,7 @@
    char* p = cURL::curl_unescape( url.c_str(), url.size() );
    if ( !p )
    {
- throw RuntimeError( "unable to escape the string" ); //we got an error
+ throw RuntimeError( "unable to unescape the string" );
    }
    else
    {
@@ -52,7 +52,7 @@
    char* p = cURL::curl_getenv( name.c_str() );
    if ( !p )
    {
- throw RuntimeError( "unable to get the environnement string" ); //we got an error
+ throw RuntimeError( "unable to get the environnement string" );
    }
    else
    {
@@ -68,7 +68,7 @@
   char* p = cURL::curl_version();
    if ( !p )
    {
- throw RuntimeError( "unable to get the libcurl version" ); //we got an error
+ throw RuntimeError( "unable to get the libcurl version" );
    }
       
    return std::string( p );
Index: curlpp/Easy.hpp
===================================================================
--- curlpp/Easy.hpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/Easy.hpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -68,6 +68,12 @@
      */
     cURL::CURL *getHandle();
 
+ /**
+ * This function returns internal information from cURL handle.
+ */
+ template <class T>
+ void getInfo(const cURL::CURLINFO &option, T &val);
+
   private:
     cURLpp::CurlHandle myCurl;
   };
Index: curlpp/CurlHandle.cpp
===================================================================
--- curlpp/CurlHandle.cpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/CurlHandle.cpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -61,7 +61,3 @@
 {
   option( cURL::CURLOPT_ERRORBUFFER, (void *)buffer);
 }
-
-
-
-
Index: curlpp/CurlHandle.hpp
===================================================================
--- curlpp/CurlHandle.hpp (.../curlpp-0.5.1) (revision 34)
+++ curlpp/CurlHandle.hpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -60,6 +60,11 @@
      */
     cURL::CURL *getHandle();
 
+ /**
+ * This function returns a pointer to error buffer
+ */
+ char *getErrorBuffer() { return mErrorBuffer; }
+
   private:
     CurlHandle(const CurlHandle &other);
     CurlHandle& operator=(const CurlHandle &other);
Index: examples/example06.cpp
===================================================================
--- examples/example06.cpp (.../curlpp-0.5.1) (revision 0)
+++ examples/example06.cpp (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -0,0 +1,62 @@
+#include <string>
+#include <stdlib.h>
+#include <ctime>
+
+#include <curlpp/cURLpp.hpp>
+#include <curlpp/Options.hpp>
+
+using namespace cURLpp;
+
+int
+main(void)
+{
+ try {
+ Cleanup myCleanup;
+ Easy exEasy;
+
+ exEasy.setOpt(new Options::Url("http://www.google.com"));
+ exEasy.setOpt(new Options::FileTime(true));
+ exEasy.perform();
+
+ // four types that can be used with getInfo()
+ //
+ char *charType;
+ long longType;
+ double doubleType;
+ SList SListType;
+
+ // the 1st parameter of getInfo method is cURL::CURLINFO_* constant
+ // (see http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html for a list of constants)
+ // the 2nd parameter is type mentioned in the same page
+ //
+ exEasy.getInfo(cURL::CURLINFO_EFFECTIVE_URL, charType);
+ std::cout << "---------------\n";
+ std::cout << "Last used url: " << charType << std::endl;
+
+ exEasy.getInfo(cURL::CURLINFO_RESPONSE_CODE, longType);
+ std::cout << "Last respone code: " << longType << std::endl;
+
+ exEasy.getInfo(cURL::CURLINFO_FILETIME, longType);
+ if (longType == -1) {
+ std::cout << "Server did not return remote time" << std::endl;
+ }
+ else {
+ std::cout << "Remote time of retrieved document: " << std::ctime(static_cast<std::time_t *>(&longType))
+ << std::endl;
+ }
+ exEasy.getInfo(cURL::CURLINFO_SPEED_DOWNLOAD, doubleType);
+ std::cout << "Average d/l speed: " << doubleType << " bytes/sec" << std::endl;
+
+ std::exit(EXIT_SUCCESS);
+ }
+ catch(cURLpp::RuntimeError &e)
+ {
+ std::cerr << e.what() << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+ catch(cURLpp::LogicError &e)
+ {
+ std::cout << e.what() << std::endl;
+ std::exit(EXIT_FAILURE);
+ }
+}
\ No newline at end of file
Index: examples/Makefile.am
===================================================================
--- examples/Makefile.am (.../curlpp-0.5.1) (revision 34)
+++ examples/Makefile.am (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -2,7 +2,7 @@
 maintener_programs = example04
 endif
 
-noinst_PROGRAMS = example01 example02 example03 example05 ${maintener_programs}
+noinst_PROGRAMS = example01 example02 example03 example05 example06 ${maintener_programs}
 
 
 
@@ -14,6 +14,7 @@
 
 example05_SOURCES = example05.cpp
 
+example06_SOURCES = example06.cpp
 
 if MAINTENER_CODE
 example04_SOURCES = example04.cpp
Index: examples/README
===================================================================
--- examples/README (.../curlpp-0.5.1) (revision 34)
+++ examples/README (.../branches/curlpp-0.5.1-getInfo) (revision 45)
@@ -3,6 +3,7 @@
  Example 1: This example is made to show you how you can use the Options.
  Example 2: an upload example.
  Example 3: verbose callback example.
+ Example 6: getInfo example.
 
 Examples below are only temporary. You should not rely
 on their particuliar use.

_______________________________________________
cURLpp mailing list
cURLpp_at_rrette.com
http://www.rrette.com/mailman/listinfo/curlpp
Received on 2005-07-27

These mail archives are generated by hypermail.

donate! Page updated November 12, 2010.
web site info

File upload with ASP.NET