pkglibcurl.pp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. {$mode objfpc}
  2. {$h+}
  3. unit pkglibcurl;
  4. interface
  5. uses Classes,pkgdownload;
  6. Type
  7. TLibCurlDownloader = Class(TBaseDownloader)
  8. Protected
  9. Procedure LibCurlDownload(Const URL : String; Dest : TStream);
  10. Procedure FTPDownload(Const URL : String; Dest : TStream); override;
  11. Procedure HTTPDownload(Const URL : String; Dest : TStream); override;
  12. end;
  13. implementation
  14. uses sysutils,uriparser,libcurl,pkgmessages,pkgglobals,unixtype;
  15. Function DoStreamWrite(Ptr : Pointer; Size : size_t; nmemb: size_t; Data : Pointer) : size_t;cdecl;
  16. begin
  17. Result:=TStream(Data).Write(Ptr^,Size*nmemb);
  18. end;
  19. Procedure TLibCurlDownloader.LibCurlDownload(Const URL : String; Dest : TStream);
  20. Var
  21. HCurl : PCurl;
  22. ErrorBuffer : Array[0..CURL_ERROR_SIZE] of char;
  23. begin
  24. hCurl:= curl_easy_init;
  25. if Assigned(hCurl) then
  26. Try
  27. curl_easy_setopt(hCurl,CURLOPT_ERRORBUFFER, [@ErrorBuffer]);
  28. curl_easy_setopt(hCurl,CURLOPT_URL,[Pchar(URL)]);
  29. curl_easy_setopt(hCurl,CURLOPT_WRITEFUNCTION,[@DoStreamWrite]);
  30. curl_easy_setopt(hCurl,CURLOPT_WRITEDATA,[Pointer(Dest)]);
  31. if Ord(curl_easy_perform(hCurl))<>0 then
  32. Error(SErrDownloadFailed,[StrPas(@ErrorBuffer)])
  33. Finally
  34. curl_easy_cleanup(hCurl);
  35. end
  36. else
  37. Raise Exception.Create('Failed to initialize Curl');
  38. end;
  39. Procedure TLibCurlDownloader.FTPDownload(Const URL : String; Dest : TStream);
  40. begin
  41. LibCurlDownload(URL,Dest);
  42. end;
  43. Procedure TLibCurlDownloader.HTTPDownload(Const URL : String; Dest : TStream);
  44. begin
  45. LibCurlDownload(URL,Dest);
  46. end;
  47. initialization
  48. DownloaderClass:=TLibCurlDownloader;
  49. end.