std::size_t Receive( void* buffer, size_t size, size_t nmemb,void *userp)
{
int segsize = size * nmemb;
DWORD dw;
HANDLE hFile = ::CreateFile(dwFilename, GENERIC_WRITE
, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ( INVALID_HANDLE_VALUE == hFile ) {
*(int *)userp = 1;
printf("Error\n");
return 0;
} else {
SetFilePointer(hFile,0,0,FILE_END);
WriteFile(hFile,buffer,segsize, &dw,0);
CloseHandle(hFile);
if (dw!=segsize) {
*(int *)userp = 2;
printf("Error\n");
return 0;
}
}
userp=0;
return segsize;
}
static int progress_callback(void *ptr, curl_off_t TotalDownloadSize,
curl_off_t finishedDownloadSize, curl_off_t TotalToUpload, curl_off_t NowUploaded) {
curl_off_t processed = (finishedDownloadSize > NowUploaded) ? finishedDownloadSize : NowUploaded;
curl_off_t total = (TotalDownloadSize > TotalToUpload) ? TotalDownloadSize : TotalToUpload;
if(total == 0 )return 0;
int left = processed*100/total;
printf("%ld/%ld(%d.%02d%%)\n",(long)processed,(long)total, left,(processed*100-left*total)*100/total);
return 0;
}
void testcurl(){
// CURL *curl_easy_init();
// char* szLibPath = "libcurl";
Impl *m_pimpl;// = new Impl;
m_pimpl = new Impl;
char error[CURL_ERROR_SIZE] = {} ;
long http_code = 0;
int wr_error = 0;
m_pimpl->curl = _curl_easy_init();
_curl_easy_reset( m_pimpl->curl ) ;
_curl_easy_setopt( m_pimpl->curl, CURLOPT_SSL_VERIFYPEER, 0L ) ;
_curl_easy_setopt( m_pimpl->curl, CURLOPT_SSL_VERIFYHOST, 0L ) ;
//_curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADERFUNCTION, &CurlAgent::HeaderCallback ) ;
//_curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADERDATA, this ) ;
_curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADER, 0L ) ;
_curl_easy_setopt(m_pimpl->curl, CURLOPT_CUSTOMREQUEST, "GET" );
_curl_easy_setopt(m_pimpl->curl, CURLOPT_HTTPGET, 1L);
setFile(L"Everything-1.4.1.1015.x86-Setup.exe");
_curl_easy_setopt(m_pimpl->curl, CURLOPT_ERRORBUFFER, error ) ;
_curl_easy_setopt(m_pimpl->curl, CURLOPT_URL, "https://www.voidtools.com/Everything-1.4.1.1015.x86-Setup.exe");
_curl_easy_setopt(m_pimpl->curl, CURLOPT_WRITEFUNCTION, &Receive ) ;
_curl_easy_setopt(m_pimpl->curl, CURLOPT_WRITEDATA, (void *)&wr_error);
//setting the progress callback function
_curl_easy_setopt(m_pimpl->curl, CURLOPT_NOPROGRESS, 0L);
_curl_easy_setopt(m_pimpl->curl, CURLOPT_XFERINFOFUNCTION , progress_callback);
//curl_easy_setopt(curl, CURLOPT_XFERINFODATA, this);
_curl_easy_setopt(m_pimpl->curl, CURLOPT_FOLLOWLOCATION, 1L);
_curl_easy_setopt(m_pimpl->curl, CURLOPT_FILETIME, 1L);
CURLcode curl_code = _curl_easy_perform(m_pimpl->curl);
curl_off_t filetime;
int cres = _curl_easy_getinfo(m_pimpl->curl, CURLINFO_FILETIME_T , &filetime);
if(cres == CURLE_OK){
if(filetime >= 0 && filetime < CURL_OFF_T_C(910670515199)) {
HANDLE hFile = CreateFileW(dwFilename, FILE_WRITE_ATTRIBUTES,
(FILE_SHARE_READ | FILE_SHARE_WRITE |
FILE_SHARE_DELETE),
NULL, OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE) {
FILETIME ft;
curl_off_t converted = ((curl_off_t)filetime * 10000000) +
CURL_OFF_T_C(116444736000000000);
ft.dwLowDateTime = (DWORD)(converted & 0xFFFFFFFF);
ft.dwHighDateTime = (DWORD)(converted >> 32);
SetFileTime(hFile,&ft,&ft,&ft);
CloseHandle(hFile);
}
}
}
_curl_easy_getinfo(m_pimpl->curl, CURLINFO_RESPONSE_CODE, &http_code);
//_curl_getinfo(m_pimpl->curl,);
printf("%d",http_code); _curl_easy_cleanup(m_pimpl->curl);
}
|
Comments