blob: 748da14135a2d862900fc7ce0e1a7485f11a7599 (
plain)
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
|
#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: test1 <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;
}
|