summaryrefslogtreecommitdiff
path: root/tests/libcurlpp/test2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/libcurlpp/test2.cpp')
-rw-r--r--tests/libcurlpp/test2.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/libcurlpp/test2.cpp b/tests/libcurlpp/test2.cpp
new file mode 100644
index 0000000..983f1bd
--- /dev/null
+++ b/tests/libcurlpp/test2.cpp
@@ -0,0 +1,85 @@
+#include <curlpp/cURLpp.hpp>
+#include <curlpp/Easy.hpp>
+#include <curlpp/Options.hpp>
+#include <curlpp/Infos.hpp>
+#include <curlpp/Types.hpp>
+
+#include <iostream>
+#include <sys/types.h>
+#include <cassert>
+#include <cstring>
+#include <cstdlib>
+
+using namespace std;
+using namespace cURLpp;
+
+class MemoryWriter {
+ static const size_t initial_size = 100;
+
+ protected:
+ char *c;
+ size_t c_size;
+
+ public:
+ MemoryWriter( ) {
+ c = (char *)malloc( initial_size );
+ c_size = 0;
+ }
+
+ ~MemoryWriter( ) {
+ if( c != NULL )
+ free( c );
+ }
+
+ size_t f( char *ptr, size_t s, size_t n ) {
+ size_t part_size = s * n;
+
+ c = (char *)realloc( c, c_size + part_size + 1 );
+
+ assert( c != NULL );
+
+ memcpy( &c[c_size], ptr, part_size );
+ c_size += part_size;
+ c[c_size] = '\0';
+
+ return part_size;
+ }
+
+ char *str( ) {
+ return c;
+ }
+
+ size_t size( ) {
+ return c_size;
+ }
+};
+
+int main( int argc, char *argv[] ) {
+ char *url;
+ Easy curl;
+ MemoryWriter w;
+
+ if( argc != 2 ) {
+ cerr << "usage: test2 <url>" << endl;
+ return EXIT_FAILURE;
+ }
+ url = argv[1];
+
+ curl.setOpt( Options::Url( url ) );
+ //curl.setOpt( Options::Header( true ) );
+ //curl.setOpt( Options::Verbose( true ) );
+
+ Types::WriteFunctionFunctor functor( &w, &MemoryWriter::f );
+ Options::WriteFunction *wf = new Options::WriteFunction( functor );
+ curl.setOpt( wf );
+
+ curl.perform( );
+
+ cout << cURLpp::Infos::ResponseCode::get( curl ) << endl
+ << w.size( ) << " "
+ << url << endl;
+
+ cout << w.str( ) << endl;
+
+ return EXIT_SUCCESS;
+}