summaryrefslogtreecommitdiff
path: root/src/modules/fetcher/libcurl/LibCurlRewindInputStream.cpp
blob: 1b80ce580f4f2815237335986820c5e884b4512c (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
#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 );

	// otherwise we must use CURL_WRITEFUNC_PAUSE
	assert( data_spooled == n );

	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";
}