' ' ' #################### ' ##### PROLOG ##### ' #################### ' ' A demo using libcurl.dll functions to download ' a URL to a local file. ' Libcurl is a free and easy-to-use client-side ' URL transfer library, supporting ' FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, ' DICT, FILE and LDAP. ' --- ' See CURL: ' http://curl.haxx.se/libcurl/ ' PROGRAM "writefile" VERSION "0.0.2" CONSOLE ' IMPORT "xst" ' Standard library : required by most programs IMPORT "kernel32" ' kernel32.dll IMPORT "libcurl" ' CURL library ' DECLARE FUNCTION Entry () DECLARE CFUNCTION WriteData (bufAddr, elemSize, numElem, fileNumber) ' ' ' ###################### ' ##### Entry () ##### ' ###################### ' ' FUNCTION Entry () ' PRINT "Program Version:", VERSION$(0) curl_global_init ($$CURL_GLOBAL_ALL) ' initialize CURL library including SSL curl_handle = curl_easy_init () ' init the curl session IFZ curl_handle THEN PRINT "curl_easy_init Error" a$ = INLINE$ ("Press ENTER to exit >") RETURN END IF ' PRINT "libcurl version:"; curl_version () ' url$ = "http://www.cacert.org/images/cacert3.png" ' try an image url url$ = "https://www.cacert.org/images/cacert3.png" ' try an image url file$ = MID$(url$, RINCHR(url$, "/") + 1) ' PRINT "File:", file$ cafile$ = "ca-bundle.crt" ua$ = "FileFetch-XBLite/" + VERSION$(0) curl_easy_setopt (curl_handle, $$CURLOPT_URL, &url$) ' set URL to get curl_easy_setopt (curl_handle, $$CURLOPT_USERAGENT, &ua$) ' set UserAgent to identify curl_easy_setopt (curl_handle, $$CURLOPT_NOPROGRESS, 1) ' no progress meter please ' curl_easy_setopt (curl_handle, $$CURLOPT_VERBOSE, 1) ' talk a lot curl_easy_setopt (curl_handle, $$CURLOPT_SSLVERSION, 3) ' set SSL version to v3 curl_easy_setopt (curl_handle, $$CURLOPT_SSL_VERIFYPEER, 0) ' no certificate ca check please curl_easy_setopt (curl_handle, $$CURLOPT_SSL_VERIFYHOST, 0) ' no certificate host check please ' curl_easy_setopt (curl_handle, $$CURLOPT_CAINFO, &cafile$) ' use ca-bundle file for certificate check curl_easy_setopt (curl_handle, $$CURLOPT_WRITEFUNCTION, &WriteData()) ' send all data to this function ofile = OPEN (file$, $$WRNEW) ' open the file to write data IF (ofile < 3) THEN PRINT "File OPEN error" curl_easy_cleanup (curl_handle) a$ = INLINE$ ("Press ENTER to exit >") RETURN ($$TRUE) END IF curl_easy_setopt (curl_handle, $$CURLOPT_WRITEDATA, ofile) ' we want the data to this file handle curl_easy_perform (curl_handle) ' get the file PRINT "Download done." PRINT "File size :"; LOF (ofile); " bytes." CLOSE (ofile) ' close the data file curl_easy_cleanup (curl_handle) ' clean up curl stuff curl_global_cleanup () ' clean up CURL library a$ = INLINE$ ("Press ENTER to exit >") END FUNCTION ' ' ' ########################## ' ##### WriteData () ##### ' ########################## ' CFUNCTION WriteData (bufAddr, elemSize, numElem, fileNumber) ' PRINT bufAddr, elemSize, numElem, fileNumber size = elemSize * numElem data$ = NULL$ (size) ' XstCopyMemory (bufAddr, &data$, size) RtlMoveMemory (&data$, bufAddr, size) ' append file SEEK (fileNumber, LOF (fileNumber)) WRITE [fileNumber], data$ RETURN size END FUNCTION END PROGRAM