1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_extensions.hxx" 30 31 #if defined WNT 32 #ifdef _MSC_VER 33 #pragma warning(push, 1) /* disable warnings within system headers */ 34 #endif 35 #include <curl/curl.h> 36 #ifdef _MSC_VER 37 #pragma warning(pop) 38 #endif 39 #else 40 #include <curl/curl.h> 41 #endif 42 #include <com/sun/star/beans/PropertyValue.hpp> 43 #include <com/sun/star/container/XNameAccess.hpp> 44 #include <com/sun/star/lang/XMultiServiceFactory.hpp> 45 46 #include "download.hxx" 47 48 namespace beans = com::sun::star::beans ; 49 namespace container = com::sun::star::container ; 50 namespace lang = com::sun::star::lang ; 51 namespace uno = com::sun::star::uno ; 52 53 #define UNISTRING(s) rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(s)) 54 55 56 struct OutData 57 { 58 rtl::Reference< DownloadInteractionHandler >Handler; 59 rtl::OUString File; 60 rtl::OUString DestinationDir; 61 oslFileHandle FileHandle; 62 sal_uInt64 Offset; 63 osl::Condition& StopCondition; 64 CURL *curl; 65 66 OutData(osl::Condition& rCondition) : FileHandle(NULL), Offset(0), StopCondition(rCondition), curl(NULL) {}; 67 }; 68 69 //------------------------------------------------------------------------------ 70 71 static void openFile( OutData& out ) 72 { 73 char * effective_url; 74 curl_easy_getinfo(out.curl, CURLINFO_EFFECTIVE_URL, &effective_url); 75 76 double fDownloadSize; 77 curl_easy_getinfo(out.curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fDownloadSize); 78 79 rtl::OString aURL(effective_url); 80 81 // ensure no trailing '/' 82 sal_Int32 nLen = aURL.getLength(); 83 while( (nLen > 0) && ('/' == aURL[nLen-1]) ) 84 aURL = aURL.copy(0, --nLen); 85 86 // extract file name last '/' 87 sal_Int32 nIndex = aURL.lastIndexOf('/'); 88 if( nIndex > 0 ) 89 { 90 out.File = out.DestinationDir + rtl::OStringToOUString(aURL.copy(nIndex), RTL_TEXTENCODING_UTF8); 91 92 oslFileError rc; 93 94 // Give the user an overwrite warning if the target file exists 95 const sal_Int32 openFlags = osl_File_OpenFlag_Write | osl_File_OpenFlag_Create; 96 do 97 { 98 rc = osl_openFile(out.File.pData, &out.FileHandle, openFlags); 99 100 if( osl_File_E_EXIST == rc && ! out.Handler->downloadTargetExists(out.File) ) 101 { 102 out.StopCondition.set(); 103 break; 104 } 105 106 } while( osl_File_E_EXIST == rc ); 107 108 if( osl_File_E_None == rc ) 109 out.Handler->downloadStarted(out.File, (sal_Int64) fDownloadSize); 110 } 111 } 112 113 //------------------------------------------------------------------------------ 114 115 static inline rtl::OString 116 getStringValue(const uno::Reference< container::XNameAccess >& xNameAccess, const rtl::OUString& aName) 117 { 118 rtl::OString aRet; 119 120 OSL_ASSERT(xNameAccess->hasByName(aName)); 121 uno::Any aValue = xNameAccess->getByName(aName); 122 123 return rtl::OUStringToOString(aValue.get<rtl::OUString>(), RTL_TEXTENCODING_UTF8); 124 } 125 126 //------------------------------------------------------------------------------ 127 128 static inline sal_Int32 129 getInt32Value(const uno::Reference< container::XNameAccess >& xNameAccess, 130 const rtl::OUString& aName, sal_Int32 nDefault=-1) 131 { 132 OSL_ASSERT(xNameAccess->hasByName(aName)); 133 uno::Any aValue = xNameAccess->getByName(aName); 134 135 sal_Int32 n=nDefault; 136 aValue >>= n; 137 return n; 138 } 139 140 //------------------------------------------------------------------------------ 141 142 static size_t 143 write_function( void *ptr, size_t size, size_t nmemb, void *stream ) 144 { 145 OutData *out = reinterpret_cast < OutData * > (stream); 146 147 if( NULL == out->FileHandle ) 148 openFile(*out); 149 150 sal_uInt64 nBytesWritten = 0; 151 152 if( NULL != out->FileHandle ) 153 osl_writeFile(out->FileHandle, ptr, size * nmemb, &nBytesWritten); 154 155 return (size_t) nBytesWritten; 156 } 157 158 //------------------------------------------------------------------------------ 159 160 static int 161 progress_callback( void *clientp, double dltotal, double dlnow, double ultotal, double ulnow ) 162 { 163 (void) ultotal; 164 (void) ulnow; 165 166 OutData *out = reinterpret_cast < OutData * > (clientp); 167 168 OSL_ASSERT( out ); 169 170 if( ! out->StopCondition.check() ) 171 { 172 double fPercent = 0; 173 if ( dltotal + out->Offset ) 174 fPercent = (dlnow + out->Offset) * 100 / (dltotal + out->Offset); 175 if( fPercent < 0 ) 176 fPercent = 0; 177 178 // Do not report progress for redirection replies 179 long nCode; 180 curl_easy_getinfo(out->curl, CURLINFO_RESPONSE_CODE, &nCode); 181 if( (nCode != 302) && (nCode != 303) && (dltotal > 0) ) 182 out->Handler->downloadProgressAt((sal_Int8)fPercent); 183 184 return 0; 185 } 186 187 // If stop condition is set, return non 0 value to abort 188 return -1; 189 } 190 191 //------------------------------------------------------------------------------ 192 193 void 194 Download::getProxyForURL(const rtl::OUString& rURL, rtl::OString& rHost, sal_Int32& rPort) const 195 { 196 if( !m_xContext.is() ) 197 throw uno::RuntimeException( 198 UNISTRING( "Download: empty component context" ), 199 uno::Reference< uno::XInterface >() ); 200 201 uno::Reference< lang::XMultiComponentFactory > xServiceManager(m_xContext->getServiceManager()); 202 203 if( !xServiceManager.is() ) 204 throw uno::RuntimeException( 205 UNISTRING( "Download: unable to obtain service manager from component context" ), 206 uno::Reference< uno::XInterface >() ); 207 208 uno::Reference< lang::XMultiServiceFactory > xConfigProvider( 209 xServiceManager->createInstanceWithContext( UNISTRING( "com.sun.star.configuration.ConfigurationProvider" ), m_xContext ), 210 uno::UNO_QUERY_THROW); 211 212 beans::PropertyValue aProperty; 213 aProperty.Name = UNISTRING( "nodepath" ); 214 aProperty.Value = uno::makeAny( UNISTRING("org.openoffice.Inet/Settings") ); 215 216 uno::Sequence< uno::Any > aArgumentList( 1 ); 217 aArgumentList[0] = uno::makeAny( aProperty ); 218 219 uno::Reference< container::XNameAccess > xNameAccess( 220 xConfigProvider->createInstanceWithArguments( 221 UNISTRING("com.sun.star.configuration.ConfigurationAccess"), aArgumentList ), 222 uno::UNO_QUERY_THROW ); 223 224 OSL_ASSERT(xNameAccess->hasByName(UNISTRING("ooInetProxyType"))); 225 uno::Any aValue = xNameAccess->getByName(UNISTRING("ooInetProxyType")); 226 227 sal_Int32 nProxyType = aValue.get< sal_Int32 >(); 228 if( 0 != nProxyType ) // type 0 means "direct connection to the internet 229 { 230 if( rURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("http:")) ) 231 { 232 rHost = getStringValue(xNameAccess, UNISTRING("ooInetHTTPProxyName")); 233 rPort = getInt32Value(xNameAccess, UNISTRING("ooInetHTTPProxyPort")); 234 } 235 else if( rURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("https:")) ) 236 { 237 rHost = getStringValue(xNameAccess, UNISTRING("ooInetHTTPSProxyName")); 238 rPort = getInt32Value(xNameAccess, UNISTRING("ooInetHTTPSProxyPort")); 239 } 240 else if( rURL.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("ftp:")) ) 241 { 242 rHost = getStringValue(xNameAccess, UNISTRING("ooInetFTPProxyName")); 243 rPort = getInt32Value(xNameAccess, UNISTRING("ooInetFTPProxyPort")); 244 } 245 } 246 } 247 248 //------------------------------------------------------------------------------ 249 250 bool curl_run(const rtl::OUString& rURL, OutData& out, const rtl::OString& aProxyHost, sal_Int32 nProxyPort) 251 { 252 /* Need to investigate further whether it is necessary to call 253 * curl_global_init or not - leave it for now (as the ftp UCB content 254 * provider does as well). 255 */ 256 257 CURL * pCURL = curl_easy_init(); 258 bool ret = false; 259 260 if( NULL != pCURL ) 261 { 262 out.curl = pCURL; 263 264 rtl::OString aURL(rtl::OUStringToOString(rURL, RTL_TEXTENCODING_UTF8)); 265 curl_easy_setopt(pCURL, CURLOPT_URL, aURL.getStr()); 266 267 // abort on http errors 268 curl_easy_setopt(pCURL, CURLOPT_FAILONERROR, 1); 269 270 // enable redirection 271 curl_easy_setopt(pCURL, CURLOPT_FOLLOWLOCATION, 1); 272 273 // write function 274 curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, &out); 275 curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, &write_function); 276 277 // progress handler - Condition::check unfortunatly is not defined const 278 curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 0); 279 curl_easy_setopt(pCURL, CURLOPT_PROGRESSFUNCTION, &progress_callback); 280 curl_easy_setopt(pCURL, CURLOPT_PROGRESSDATA, &out); 281 282 // proxy 283 curl_easy_setopt(pCURL, CURLOPT_PROXY, aProxyHost.getStr()); 284 curl_easy_setopt(pCURL, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); 285 if( -1 != nProxyPort ) 286 curl_easy_setopt(pCURL, CURLOPT_PROXYPORT, nProxyPort); 287 288 if( out.Offset > 0 ) 289 { 290 // curl_off_t offset = nOffset; libcurl seems to be compiled with large 291 // file support (and we not) .. 292 sal_Int64 offset = (sal_Int64) out.Offset; 293 curl_easy_setopt(pCURL, CURLOPT_RESUME_FROM_LARGE, offset); 294 } 295 296 CURLcode cc = curl_easy_perform(pCURL); 297 298 // treat zero byte downloads as errors 299 if( NULL == out.FileHandle ) 300 openFile(out); 301 302 if( CURLE_OK == cc ) 303 { 304 out.Handler->downloadFinished(out.File); 305 ret = true; 306 } 307 308 if ( CURLE_PARTIAL_FILE == cc ) 309 { 310 // this sometimes happens, when a user throws away his user data, but has already 311 // completed the download of an update. 312 double fDownloadSize; 313 curl_easy_getinfo( pCURL, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &fDownloadSize ); 314 if ( -1 == fDownloadSize ) 315 { 316 out.Handler->downloadFinished(out.File); 317 ret = true; 318 } 319 } 320 321 // Avoid target file being removed 322 else if( (CURLE_ABORTED_BY_CALLBACK == cc) || out.StopCondition.check() ) 323 ret = true; 324 325 // Only report errors when not stopped 326 else 327 { 328 rtl::OString aMessage(RTL_CONSTASCII_STRINGPARAM("Unknown error")); 329 330 const char * error_message = curl_easy_strerror(cc); 331 if( NULL != error_message ) 332 aMessage = error_message; 333 334 if ( CURLE_HTTP_RETURNED_ERROR == cc ) 335 { 336 long nError; 337 curl_easy_getinfo( pCURL, CURLINFO_RESPONSE_CODE, &nError ); 338 339 if ( 403 == nError ) 340 aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( " 403: Access denied!" ) ); 341 else if ( 404 == nError ) 342 aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( " 404: File not found!" ) ); 343 else if ( 416 == nError ) 344 { 345 // we got this error probably, because we already downloaded the file 346 out.Handler->downloadFinished(out.File); 347 ret = true; 348 } 349 else 350 { 351 aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( ":error code = " ) ); 352 aMessage += aMessage.valueOf( nError ); 353 aMessage += rtl::OString( RTL_CONSTASCII_STRINGPARAM( " !" ) ); 354 } 355 } 356 if ( !ret ) 357 out.Handler->downloadStalled( rtl::OStringToOUString(aMessage, RTL_TEXTENCODING_UTF8) ); 358 } 359 360 curl_easy_cleanup(pCURL); 361 } 362 363 return ret; 364 } 365 366 //------------------------------------------------------------------------------ 367 368 bool 369 Download::start(const rtl::OUString& rURL, const rtl::OUString& rFile, const rtl::OUString& rDestinationDir) 370 { 371 OSL_ASSERT( m_aHandler.is() ); 372 373 OutData out(m_aCondition); 374 rtl::OUString aFile( rFile ); 375 376 // when rFile is empty, there is no remembered file name. If there is already a file with the 377 // same name ask the user if she wants to resume a download or restart the download 378 if ( !aFile.getLength() ) 379 { 380 // GetFileName() 381 rtl::OUString aURL( rURL ); 382 // ensure no trailing '/' 383 sal_Int32 nLen = aURL.getLength(); 384 while( (nLen > 0) && ('/' == aURL[ nLen-1 ]) ) 385 aURL = aURL.copy( 0, --nLen ); 386 387 // extract file name last '/' 388 sal_Int32 nIndex = aURL.lastIndexOf('/'); 389 aFile = rDestinationDir + aURL.copy( nIndex ); 390 391 // check for existing file 392 oslFileError rc = osl_openFile( aFile.pData, &out.FileHandle, osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ); 393 osl_closeFile(out.FileHandle); 394 out.FileHandle = NULL; 395 396 if( osl_File_E_EXIST == rc ) 397 { 398 if ( m_aHandler->checkDownloadDestination( aURL.copy( nIndex+1 ) ) ) 399 { 400 osl_removeFile( aFile.pData ); 401 aFile = rtl::OUString(); 402 } 403 else 404 m_aHandler->downloadStarted( aFile, 0 ); 405 } 406 else 407 { 408 osl_removeFile( aFile.pData ); 409 aFile = rtl::OUString(); 410 } 411 } 412 413 out.File = aFile; 414 out.DestinationDir = rDestinationDir; 415 out.Handler = m_aHandler; 416 417 if( aFile.getLength() > 0 ) 418 { 419 oslFileError rc = osl_openFile(aFile.pData, &out.FileHandle, osl_File_OpenFlag_Write); 420 421 if( osl_File_E_None == rc ) 422 { 423 // Set file pointer to the end of the file on resume 424 if( osl_File_E_None == osl_setFilePos(out.FileHandle, osl_Pos_End, 0) ) 425 { 426 osl_getFilePos(out.FileHandle, &out.Offset); 427 } 428 } 429 else if( osl_File_E_NOENT == rc ) // file has been deleted meanwhile .. 430 out.File = rtl::OUString(); 431 } 432 433 rtl::OString aProxyHost; 434 sal_Int32 nProxyPort = -1; 435 getProxyForURL(rURL, aProxyHost, nProxyPort); 436 437 bool ret = curl_run(rURL, out, aProxyHost, nProxyPort); 438 439 if( NULL != out.FileHandle ) 440 { 441 osl_syncFile(out.FileHandle); 442 osl_closeFile(out.FileHandle); 443 444 // #i90930# Don't remove already downloaded bits, when curl_run reports an error 445 // because later calls might be successful 446 // if( ! ret ) 447 // osl_removeFile(out.File.pData); 448 } 449 450 m_aCondition.reset(); 451 return ret; 452 } 453 454 //------------------------------------------------------------------------------ 455 456 void 457 Download::stop() 458 { 459 m_aCondition.set(); 460 } 461