summaryrefslogtreecommitdiff
path: root/src/modules/fetcher/libcurl/LibCurlRewindInputStream.cpp
blob: 7d75123f22a4bb73224e8284eecd1e5bf212f804 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "LibCurlRewindInputStream.hpp"
#include "SpoolRewindInputStream.hpp"

#include <curlpp/Options.hpp>
//#include <curlpp/Infos.hpp>
//#include <curlpp/Types.hpp>

using namespace cURLpp;

using namespace std;

class libcurl_buffer : public spool_streambuf
{
	public:
		explicit libcurl_buffer( Easy *curl, size_t bufSize = 256, size_t putBack = 1, size_t spoolBufSize = 8192 );

		size_t write_data( char *ptr, size_t s, size_t n );
		
	protected:
	
		virtual streambuf::int_type readFromSource( );

	private:
		Easy *m_curl;
};

libcurl_buffer::libcurl_buffer( Easy *curl, size_t bufSize, size_t putBack, size_t spoolBufSize )
	: spool_streambuf( bufSize, putBack, spoolBufSize ), m_curl( curl )
{
}

size_t libcurl_buffer::write_data( char *ptr, size_t s, size_t n )
{
	size_t chunk_size = s * n;

	size_t data_spooled = spoolSourceData( ptr, chunk_size );

	return data_spooled;

}

streambuf::int_type libcurl_buffer::readFromSource( )
{
	// done before, we should not get called here!

	return 0;
}

LibCurlRewindInputStream::LibCurlRewindInputStream( const URL &url )
	: SpoolRewindInputStream( url ), m_curl( 0 )
{
	m_curl = new Easy( );

	m_curl->setOpt( Options::Url( url.str( ).c_str( ) ) );
	
	// how to use logger here?
	m_curl->setOpt( Options::Header( true ) );
	m_curl->setOpt( Options::Verbose( true ) );
/*	m_curl->set( DebugFunction( types::DebugFunctionFunctor( &
		request.setOpt(Verbose(true));
		request.setOpt(DebugFunction(curlpp::types::DebugFunctionFunctor(&myWindow, 
			&MyWindow::writeDebug)));
*/
	m_buf = new libcurl_buffer( m_curl );
	rdbuf( m_buf );

	Types::WriteFunctionFunctor functor( static_cast<libcurl_buffer *>( m_buf ), &libcurl_buffer::write_data );
	Options::WriteFunction *wf = new Options::WriteFunction( functor );
	m_curl->setOpt( wf );

	m_curl->perform( );

	// TODO: error handling
}

LibCurlRewindInputStream::~LibCurlRewindInputStream( )
{
	if( m_buf ) delete m_buf;
	if( m_curl ) delete m_curl;
}

string LibCurlRewindInputStream::lastErrMsg( ) const
{
	// TODO: fetch m_curl state and return error
	return "ERROR";
}

#if 0

#include <iostream>
#include <sys/types.h>
#include <cassert>
#include <cstring>
#include <cstdlib>

using namespace std;

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[] ) {
	Easy curl;
	MemoryWriter w;


	cout	<< cURLpp::Infos::ResponseCode::get( curl ) << endl
		<< w.size( ) << " "
		<< url << endl;

	cout	<< w.str( ) << endl;

	return EXIT_SUCCESS;
}
#endif