Home

Awesome

libPasCURL

It is delphi and object pascal bindings and wrapper around cURL library. libcurl is the library is using for transferring data specified with URL syntax, supporting HTTP, HTTPS, FTP, FTPS, GOPHER, TFTP, SCP, SFTP, SMB, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3, RTSP and RTMP.

Table of contents

Requirements

Library is tested for

Installation

Get the sources and add the source directory to the project search path. For FPC add the source directory to the fpc.cfg file.

Usage

Clone the repository git clone https://github.com/isemenkov/libpascurl.

Add the unit you want to use to the uses clause.

Examples

  1. RemoteConnectCStyle simple example for use cURL wrapper in C-Style to connect to remote host.
  2. RemoteConnect example how to use curl.http.session.TSession and curl.http.response.TResponse classes to connect to remote host.

Bindings

libpascurl.pas file contains the cURL translated headers to use this library in pascal programs. You can find C API documentation at cURL website.

Usage example

  uses
    libpascurl;

  var
    handle : CURL;
    effectiveUrl, contentType, ip : PChar;
    responseCode, headerSize : Longint;
    contentLength, totalTime : Longword;
    buffer : TStringStream;

  function WriteFunctionCallback (ptr : PChar; size : LongWord;
    nmemb : LongWord; data : Pointer)
  begin
    buffer.WriteString(string(ptr)); 
  end;

  begin
    curl_global_init(CURL_GLOBAL_ALL);
    curl_easy_setopt(handle, CURLOPT_URL, PChar('https://example.dev');
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, @WriteFunctionCallback);
    buffer := TStringStream.Create('');

    if curl_easy_perform = CURLE_OK then
    begin
      New(effectiveUrl);
      New(contentType);
      New(ip);
  
      curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, @effectiveUrl);
      curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, @responseCode);
      curl_easy_getinfo(handle, CURLINFO_HEADER_SIZE, @headerSize);
      curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, @contentType);
      curl_easy_getinfo(handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, @contentLength);
      curl_easy_getinfo(handle, CURLINFO_LOCAL_IP, @ip);
      curl_easy_getinfo(handle, CURLINFO_TOTAL_TIME_T, @totalTime);

      writeln('URL: ':20,                 effectiveUrl);
      writeln('Response code: ':20,       responseCode);
      writeln('Header size, kB: ':20,     FormatFloat('0.00', headerSize / 1024));
      writeln('Content type: ',           contentType);
      writeln('Content length, kB: ':20,  FormatFloat('0.00', contentLength / 1024));
      writeln('IP: ':20,                  ip);
      writeln('Total time, ms: ':20,      totalTime);
      writeln('==== Content ====');
      writeln(buffer.DataString);
    end;

    curl_global_cleanup; 

  end;

Object wrapper

Base functionality

The library contains a set of classes for creating high-level wrappers around the supported protocols. The source/curl/ folder contains base components that implements specific functionality.

ClassDescription
TCURLEasyIt is base class that initialize CURL library and provides error handling functionality.
TSessionIt is parent class for sessions of all supported protocols. It provides a TMemoryBuffer for stored downloading/uploading data.
TResponseIt is parent class for server response data.
TPropertyModuleIt is base class for all sessions and responses additional functionality modules.

Property modules

Session modules
Module classDescription
TModuleDNSClass provide properties to setup libCurl DNS options.
TModuleRequestClass provide properties to setup request properties and callbacks.
TModuleHeaderClass provide properties to setup headers. Can be used only with HTTP-like protocols - HTTP(S), FTP(S), POP3(S), IMAP, SMTP.
TModuleOptionsClass provide properties to setup different libCurl internal options.
TModuleProtocolsClass provide properties to setup libCurl protocol options.
TModuleSocketClass provide properties to socket setup.
TModuleTCPClass provide properties to setup TCP protocol options.
TModuleWriterClass provide properties to setup download callback function.
TModuleReaderClass provide properties to setup upload callback function.
TModuleAuthClass provide properties to setup auth options.
TModuleTLSAuthClass provide properties to setup TLS auth authentication options.
TModuleProxyClass provide properties to setup proxy options.
TModuleSock5Class provide properties to setup sock5 proxy options.
Response modules
Module classDescription
TModuleContentClass provide properties to get content data buffer.
TModuleHeaderClass provide properties to response headers. Can be used only with HTTP-like protocols - HTTP(S), FTP(S), POP3(S), IMAP, SMTP.
TModuleRedirectClass provide information about request redirects.
TModuleSpeedClass provide speed download/upload information.
TModuleTimeoutClass provide timeouts information.
TModuleInfoClass provide session information.

HTTP

THTTPSession and THTTPResponse classes implements wrapper about HTTP(S) protocol. This classes extends the functionality of base classes and provided new one that is specific only to this protocol.

This wrapper used or extends the next main modules:

Session modulesResponse modules
:heavy_check_mark: TModuleDNS:heavy_check_mark: TModuleContent
:heavy_check_mark: TModuleHeader:heavy_check_mark: TModuleHeader
:heavy_check_mark: TModuleOptions:heavy_check_mark: TModuleRedirect
:heavy_check_mark: TModuleProtocols:heavy_check_mark: TModuleSpeed
:heavy_check_mark: TModuleSocket:heavy_check_mark: TModuleTimeout
:heavy_check_mark: TModuleTCP:heavy_check_mark: TModuleInfo
:heavy_check_mark: TModuleWriter
:heavy_check_mark: TModuleReader
:heavy_check_mark: TModuleRequest
:heavy_check_mark: TModuleAuth
:heavy_check_mark: TModuleTLSAuth
:heavy_check_mark: TModuleProxy
:heavy_check_mark: TModuleSock5
Session modules
Module classDescription
TModuleRedirectClass provide properties to setup http(s) redirect options.
TModuleHTTP2Class provide properties to setup HTTP/2 protocol options.
TModuleTimeoutClass provide properties to setup http(s) protocol timeouts options.
Response modules
Module classDescription
TModuleCookieClass provide cookies data.

Usage example

uses 
  curl.http.session, curl.http.response;
  
var
  Session : THTTP.TSession;
  Response : THHTP.TResponse;

begin
  Session.Url := 'https://github.com/isemenkov';
  Session.Redirect.FollowRedirect := True;
  
  Response := Session.Get;
  
  writeln('Url', Response.Request.Url);
  writeln('Response code', Response.Header.ResponseCode);
  writeln('Content-type', Response.Content.ContentType);
  writeln('Content-size', Response.Content.ContentSize);
  writeln('Content', Response.Content.ToString);
end;