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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
|
/* libcmis
* Version: MPL 1.1 / GPLv2+ / LGPLv2+
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Major Contributor(s):
* Copyright (C) 2011 SUSE <cbosdonnat@suse.com>
*
*
* All Rights Reserved.
*
* For minor contributions see the git repository.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPLv2+"), or
* the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
* in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
* instead of those above.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include "curl/curl.h"
#include "internals.hxx"
using namespace std;
namespace mockup
{
extern Configuration* config;
}
/** Code mostly coming from curl
*/
struct curl_slist *curl_slist_append( struct curl_slist * list, const char * data )
{
struct curl_slist* new_item = ( struct curl_slist* ) malloc( sizeof( struct curl_slist ) );
if( new_item )
{
char *dupdata = strdup(data);
if ( dupdata )
{
new_item->next = NULL;
new_item->data = dupdata;
}
else
{
free(new_item);
return NULL;
}
}
else
return NULL;
if ( list )
{
curl_slist* last = list;
while ( last->next )
last = last->next;
last->next = new_item;
return list;
}
/* if this is the first item, then new_item *is* the list */
return new_item;
}
void curl_slist_free_all( struct curl_slist * list )
{
if ( list )
{
struct curl_slist* item = list;
struct curl_slist* next = NULL;
do
{
next = item->next;
if ( item->data )
free( item->data );
free(item);
item = next;
} while ( next );
}
}
void curl_free( void * /*p*/ )
{
/* TODO Implement me */
}
CURLcode curl_global_init( long )
{
return CURLE_OK;
}
CURL *curl_easy_init( void )
{
return new CurlHandle();
}
void curl_easy_cleanup( CURL * curl )
{
CurlHandle* handle = static_cast< CurlHandle* >( curl );
delete( handle );
}
void curl_easy_reset( CURL * curl )
{
CurlHandle* handle = static_cast< CurlHandle * >( curl );
handle->reset( );
}
char *curl_easy_escape( CURL *, const char *string, int length )
{
return strndup( string, length );
}
char *curl_unescape( const char *string, int length )
{
return strndup( string, length );
}
char *curl_easy_unescape( CURL *, const char *string, int length, int * )
{
return curl_unescape( string, length );
}
CURLcode curl_easy_setopt( CURL * curl, CURLoption option, ... )
{
CurlHandle* handle = static_cast< CurlHandle * >( curl );
va_list arg;
va_start( arg, option );
switch ( option )
{
/* TODO Add support for more options */
case CURLOPT_POST:
{
if ( va_arg( arg, long ) )
handle->m_method = string( "POST" );
break;
}
case CURLOPT_UPLOAD:
{
if ( 0 != va_arg( arg, long ) )
handle->m_method = string( "PUT" );
break;
}
case CURLOPT_CUSTOMREQUEST:
handle->m_method = string( va_arg( arg, char* ) );
break;
case CURLOPT_URL:
{
handle->m_url = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_WRITEFUNCTION:
{
handle->m_writeFn = va_arg( arg, write_callback );
break;
}
case CURLOPT_WRITEDATA:
{
handle->m_writeData = va_arg( arg, void* );
break;
}
case CURLOPT_READFUNCTION:
{
handle->m_readFn = va_arg( arg, read_callback );
break;
}
case CURLOPT_READDATA:
{
handle->m_readData = va_arg( arg, void* );
break;
}
case CURLOPT_INFILESIZE:
{
handle->m_readSize = va_arg( arg, long );
break;
}
case CURLOPT_HEADERFUNCTION:
{
handle->m_headersFn = va_arg( arg, headers_callback );
break;
}
case CURLOPT_WRITEHEADER:
{
handle->m_headersData = va_arg( arg, void* );
break;
}
case CURLOPT_USERNAME:
{
handle->m_username = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_PASSWORD:
{
handle->m_password = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_USERPWD:
{
string userpwd( va_arg( arg, char* ) );
size_t pos = userpwd.find( ':' );
if ( pos != string::npos )
{
handle->m_username = userpwd.substr( 0, pos );
handle->m_password = userpwd.substr( pos + 1 );
}
break;
}
case CURLOPT_PROXY:
{
// FIXME curl does some more complex things with port and type
handle->m_proxy = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_NOPROXY:
{
handle->m_noProxy = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_PROXYUSERNAME:
{
handle->m_proxyUser = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_PROXYPASSWORD:
{
handle->m_proxyPass = string( va_arg( arg, char* ) );
break;
}
case CURLOPT_PROXYUSERPWD:
{
string userpwd( va_arg( arg, char* ) );
size_t pos = userpwd.find( ':' );
if ( pos != string::npos )
{
handle->m_proxyUser = userpwd.substr( 0, pos );
handle->m_proxyPass = userpwd.substr( pos + 1 );
}
break;
}
case CURLOPT_SSL_VERIFYHOST:
{
handle->m_verifyHost = va_arg( arg, long ) != 0;
break;
}
case CURLOPT_SSL_VERIFYPEER:
{
handle->m_verifyPeer = va_arg( arg, long ) != 0;
break;
}
case CURLOPT_CERTINFO:
{
handle->m_certInfo = va_arg( arg, long );
break;
}
case CURLOPT_HTTPHEADER:
{
handle->m_headers.clear();
struct curl_slist* headers = va_arg( arg, struct curl_slist* );
while ( headers != NULL )
{
handle->m_headers.push_back( string( headers->data ) );
headers = headers->next;
}
break;
}
default:
{
// We surely don't want to break the test for that.
}
}
va_end( arg );
return CURLE_OK;
}
CURLcode curl_easy_perform( CURL * curl )
{
CurlHandle* handle = static_cast< CurlHandle * >( curl );
/* Fake a bad SSL Certificate? */
if ( !mockup::config->m_badSSLCertificate.empty( ) && handle->m_verifyPeer && handle->m_verifyHost )
{
return CURLE_SSL_CACERT;
}
/* Populate the certificate infos */
if ( handle->m_certInfo && !mockup::config->m_badSSLCertificate.empty( ) )
{
curl_slist * certData = NULL;
string cert( "Cert:" + mockup::config->m_badSSLCertificate );
char* c_cert = strdup( cert.c_str( ) );
certData = curl_slist_append( certData, c_cert );
free( c_cert );
handle->m_certs.num_of_certs = 1;
handle->m_certs.certinfo = ( struct curl_slist** )calloc( ( size_t )1, sizeof( struct curl_slist * ) );
handle->m_certs.certinfo[0] = certData;
}
/* Check the credentials */
if ( mockup::config->hasCredentials( ) &&
( handle->m_username != mockup::config->m_username ||
handle->m_password != mockup::config->m_password ) )
{
// Send HTTP 401
handle->m_httpError = 401;
return CURLE_HTTP_RETURNED_ERROR;
}
// Store the requests for later verifications
stringstream body;
if ( handle->m_readFn && handle->m_readData )
{
size_t bufSize = 2048;
char* buf = new char[bufSize];
size_t read = 0;
do
{
read = handle->m_readFn( buf, 1, bufSize, handle->m_readData );
body.write( buf, read );
} while ( read == bufSize );
delete[] buf;
}
mockup::config->m_requests.push_back( mockup::Request( handle->m_url, handle->m_method, body.str( ), handle->m_headers ) );
return mockup::config->writeResponse( handle );
}
CURLcode curl_easy_getinfo( CURL * curl, CURLINFO info, ... )
{
CurlHandle* handle = static_cast< CurlHandle * >( curl );
va_list arg;
va_start( arg, info );
switch ( info )
{
case CURLINFO_RESPONSE_CODE:
{
long* buf = va_arg( arg, long* );
*buf = handle->m_httpError;
break;
}
case CURLINFO_CERTINFO:
{
struct curl_slist** param = va_arg( arg, struct curl_slist** );
if ( NULL != param )
{
union
{
struct curl_certinfo * to_certinfo;
struct curl_slist * to_slist;
} ptr;
// Fill it
ptr.to_certinfo = &handle->m_certs;
*param = ptr.to_slist;
}
break;
}
default:
{
// We surely don't want to break the test for that.
}
}
va_end( arg );
return CURLE_OK;
}
CurlHandle::CurlHandle( ) :
m_url( ),
m_writeFn( NULL ),
m_writeData( NULL ),
m_readFn( NULL ),
m_readData( NULL ),
m_readSize( 0 ),
m_headersFn( NULL ),
m_headersData( NULL ),
m_username( ),
m_password( ),
m_proxy( ),
m_noProxy( ),
m_proxyUser( ),
m_proxyPass( ),
m_verifyHost( true ),
m_verifyPeer( true ),
m_certInfo( false ),
m_certs( ),
m_httpError( 0 ),
m_method( "GET" ),
m_headers( )
{
}
CurlHandle::CurlHandle( const CurlHandle& copy ) :
m_url( copy.m_url ),
m_writeFn( copy.m_writeFn ),
m_writeData( copy.m_writeData ),
m_readFn( copy.m_readFn ),
m_readData( copy.m_readData ),
m_readSize( copy.m_readSize ),
m_headersFn( copy.m_headersFn ),
m_headersData( copy.m_headersData ),
m_username( copy.m_username ),
m_password( copy.m_password ),
m_proxy( copy.m_proxy ),
m_noProxy( copy.m_noProxy ),
m_proxyUser( copy.m_proxyUser ),
m_proxyPass( copy.m_proxyPass ),
m_verifyHost( copy.m_verifyHost ),
m_verifyPeer( copy.m_verifyPeer ),
m_certInfo( copy.m_certInfo ),
m_certs( copy.m_certs ),
m_httpError( copy.m_httpError ),
m_method( copy.m_method ),
m_headers( copy.m_headers )
{
}
CurlHandle& CurlHandle::operator=( const CurlHandle& copy )
{
if ( this != © )
{
m_url = copy.m_url;
m_writeFn = copy.m_writeFn;
m_writeData = copy.m_writeData;
m_readFn = copy.m_readFn;
m_readData = copy.m_readData;
m_readSize = copy.m_readSize;
m_headersFn = copy.m_headersFn;
m_headersData = copy.m_headersData;
m_username = copy.m_username;
m_password = copy.m_password;
m_proxy = copy.m_proxy;
m_noProxy = copy.m_noProxy;
m_proxyUser = copy.m_proxyUser;
m_proxyPass = copy.m_proxyPass;
m_verifyHost = copy.m_verifyHost;
m_verifyPeer = copy.m_verifyPeer;
m_certInfo = copy.m_certInfo;
m_certs = copy.m_certs;
m_httpError = copy.m_httpError;
m_method = copy.m_method;
m_headers = copy.m_headers;
}
return *this;
}
CurlHandle::~CurlHandle( )
{
reset();
}
void CurlHandle::reset( )
{
m_url = string( );
m_writeFn = NULL;
m_writeData = NULL;
m_readFn = NULL;
m_readData = NULL;
m_readSize = 0;
m_username = string( );
m_password = string( );
m_proxy = string( );
m_noProxy = string( );
m_proxyUser = string( );
m_proxyPass = string( );
m_verifyHost = true;
m_verifyPeer = true;
m_certInfo = false;
for ( int i = 0; i < m_certs.num_of_certs; ++i )
{
curl_slist_free_all( m_certs.certinfo[i] );
m_certs.certinfo[i] = NULL;
}
free( m_certs.certinfo );
m_certs.certinfo = NULL;
m_certs.num_of_certs = 0;
m_method = "GET";
m_headers.clear( );
}
|