Raised This Month: $202 Target: $400

[EXTENSION] System2 - Easy to use HTTP(S)/FTP Request API and useful system natives

Post New Thread Reply   
Thread Tools Display Modes
Author Message
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 04:40   [EXTENSION] System2 - Easy to use HTTP(S)/FTP Request API and useful system natives
Reply With Quote #1


System2 allows you to easily make HTTP, HTTPS and FTP requests and also provides some useful system natives, which missing in the Sourcemod API.

Features of System2:
  • Make HTTP(S)/FTP requests with a lot of options:
    • Set the port of the request
    • Download response to a local file (download files)
    • Define the timeout for the request
    • Get progress information (useful when downloading or uploading stuff)
    • Send body data
    • Set custom headers
    • Set user agent and basic authentication
    • Use the HTTP methods: GET, POST, PUT, PATCH, DELETE or HEAD
    • Determine whether to follow redirects or not
    • Upload files to a FTP server
    • List files/folders in a FTP directory
    • Get useful information of the response
      • The status code
      • The body data
      • The headers
      • The HTTP version
      • The last visited URL
      • The total execution time
      • The amount of downloaded / uploaded bytes
      • The average download / upload speed
  • URL encode / decode strings
  • Copy files
  • Extract and Compress files with 7-ZIP
  • Execute system commands and get the output and exit status
  • Retrieve the operating system of the server
  • Calculate the MD5 hash of a string
  • Calculate the MD5 hash of a file
  • Calculate the CRC32 hash of a string
  • Calculate the CRC32 hash of a file

Note 1: To allow the extraction and compression of files, the included data/system2/linux/amd64/7z for 64bit Linux, data/system2/linux/i386/7z for 32bit Linux or data/system2/win/7z.exe file on Windows needs execution rights (744 on linux).
Note 2: You shouldn't need any extra librarys on your machine. But if you get any error just tell me and I'll take a look at it.

Install the extension:
  • Download the extension's ZIP file
  • Extract its content to your server's addons/sourcemod folder
  • Give 7-ZIP execution rights (optional, only if a plugin uses compression):
    • 64-Bit: Give data/system2/linux/amd64/7z execution rights (744)
    • 32-Bit: Give data/system2/linux/i386/7z execution rights (744)

Code examples for Devs:
Or take a look at the system2_http.sp and system2_test.sp examples or system2.inc and request.inc include files

If you miss any other functionality, just ask for it or open an issue on Github and I'll try to implement it.

Configure the request:

Note: When you create requests you have to delete them with the delete statement when you don't need them anymore, until then they can be used more then once.
  • Make a simple HTTP GET request:
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com?action=test");

    // Requests have to be deleted, until then they can be used more then once
    delete httpRequest
  • Make a simple HTTP POST request (By default content type application/x-www-form-urlencoded will be used):
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com");
  • Save output to a file (download a file):
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com/test.txt");
  • Use a custom port:
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com");
  • Set request headers (and use the HTTP PUT method):
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com");
  • Set timeout and follow redirects feature (and use the HTTP DELETE method):
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com");
    httpRequest.FollowRedirects false;
    httpRequest.Timeout 5;
  • Pass any data to the response callback (and use the HTTP HEAD method):
    PHP Code:
    int anyValue 5;

    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com");
    httpRequest.Any anyValue;
  • Use a progress callback:
    PHP Code:
    System2HTTPRequest httpRequest = new System2HTTPRequest(HttpResponseCallback"http://example.com/test.txt");

    void HttpProgressCallback(System2HTTPRequest requestint dlTotalint dlNowint ulTotalint ulNow) {
    PrintToServer("Downloaded %d of %d bytes"dlNowdlTotal);

  • List a directory of a FTP server:
    PHP Code:
    System2FTPRequest ftpRequest = new System2FTPRequest(FtpResponseCallback"ftp://example.com");
    ftpRequest.ListFilenamesOnly true;
  • Download a file from a FTP server:
    PHP Code:
    System2FTPRequest ftpRequest = new System2FTPRequest(FtpResponseCallback"ftp://example.com/test.txt");
  • Upload a file to a FTP server:
    PHP Code:
    System2FTPRequest ftpRequest = new System2FTPRequest(FtpResponseCallback"ftp://example.com/test.txt");
    ftpRequest.AppendToFile true;
    ftpRequest.CreateMissingDirs false;

Use the response:

Note 1: You get the data that you have set in the request with the request parameter in the callback, which is a copy of the original request (and therefore can not be deleted, but reused).
Note 2: The response parameter is only valid in the callback and only if success is true.
  • Get the status code and the time needed for the request:
    PHP Code:
    public void HttpResponseCallback(bool success, const char[] errorSystem2HTTPRequest requestSystem2HTTPResponse responseHTTPRequestMethod method) {
        if (
    success) {
    char lastURL[128];

    int statusCode response.StatusCode;
    float totalTime response.TotalTime;

    PrintToServer("Request to %s finished with status code %d in %.2f seconds"lastURLstatusCodetotalTime);
        } else {
    PrintToServer("Error on request: %s"error);

  • Get the content of the response:
    PHP Code:
    public void HttpResponseCallback(bool success, const char[] errorSystem2HTTPRequest requestSystem2HTTPResponse responseHTTPRequestMethod method) {
        if (
    success) {
    char[] content = new char[response.ContentLength 1];
    response.GetContent(contentresponse.ContentLength 1);

    PrintToServer("Content of the response: %s"content);

  • Retrieve the content of the response line by line:
    PHP Code:
    public void HttpResponseCallback(bool success, const char[] errorSystem2HTTPRequest requestSystem2HTTPResponse responseHTTPRequestMethod method) {
        if (
    success) {
    char content[128];
            for (
    int found 0found response.ContentLength;) {
    found += response.GetContent(contentsizeof(content), found"\n");
    PrintToServer("Line %d: %s"foundcontent);

  • Get headers of the response:
    PHP Code:
    public void HttpResponseCallback(bool success, const char[] errorSystem2HTTPRequest requestSystem2HTTPResponse responseHTTPRequestMethod method) {
        if (
    success) {
    PrintToServer("Found %d headers:"response.Headers);

    char name[128];
    char value[128];

    ArrayList headers response.GetHeaders();
            for (
    int i 0headers.Lengthi++) {
    PrintToServer("\t%s: %s"namevalue);

    // Or just a single header...
    if (response.GetHeader("Content-Type"valuesizeof(value))) {
    PrintToServer("Use content type %s..."value);

  • Get download/upload size/speed:
    PHP Code:
    public void FtpResponseCallback(bool success, const char[] errorSystem2FTPRequest requestSystem2FTPResponse response) {
        if (
    success) {
    char file[PLATFORM_MAX_PATH];

            if (
    strlen(file) > 0) {
    PrintToServer("Uploaded %d bytes with %d bytes / second"response.UploadSizeresponse.UploadSpeed);
            } else {
    PrintToServer("Downloaded %d bytes with %d bytes / second"response.DownloadSizeresponse.DownloadSpeed);

  • Upload the same file to another URL in the response (Reuse the request):
    PHP Code:
    public void FtpResponseCallback(bool success, const char[] errorSystem2FTPRequest requestSystem2FTPResponse response) {
        if (
    success) {
    // Only change the URL and start the request again (other options will retained)

Execute commands:
  • Execute a threaded system command
    PHP Code:
    public void OnPluginStart() {
    System2_ExecuteThreaded(ExecuteCallback"ls /home");

    void ExecuteCallback(bool success, const char[] commandSystem2ExecuteOutput outputany data) {
        if (!
    success || output.ExitStatus != 0) {
    PrintToServer("Couldn't execute commands %s successfully"command);
        } else {
    char outputString[128];
    PrintToServer("Output of the command %s: %s"commandoutputString);

  • Compress a file:
    PHP Code:
    if (!System2_Compress(CompressCallback"test.txt""test.zip")) {
    PrintToServer("7-ZIP was not found or is not executable!");

  • Extract an archive:
    PHP Code:
    if (!System2_Extract(ExtractCallback"test.zip""addons/sourcemod/data")) {
    PrintToServer("7-ZIP was not found or is not executable!");

  • It may be that for some users with 64-bit system the 64-bit 7-ZIP file can't be executed (for example if they have an old libstdc++).
    So it's safer to check this before and then use the 32-bit executable, if this works.
    PHP Code:
    // Boolean which holds the knowledge whether we have to force using the 32-bit executable
    force32Bit false;

    char binDir[PLATFORM_MAX_PATH];
    char binDir32Bit[PLATFORM_MAX_PATH];

    // First check if we can use the 7-ZIP executable which is suitable for the running machine
    if (!System2_Check7ZIP(binDirsizeof(binDir))) {
    // If not: Check if 32-bit 7-ZIP can be used
    if (!System2_Check7ZIP(binDir32Bitsizeof(binDir32Bit), true)) {
    // Print an error if both can't be executed
    if (StrEqual(binDirbinDir32Bit)) {
    LogError("ERROR: 7-ZIP was not found or is not executable at '%s'"binDir);
            } else {
    LogError("ERROR: 7-ZIP was not found or is not executable at '%s' or '%s'"binDirbinDir32Bit);
        } else {
    // 64-bit does not seem to work, but 32-bit do. We have to memorize this.
    force32Bit true;

    // Now use the knowledge whether we have to force 32-bit or not.

Sourcecode on Github: https://github.com/dordnung/System2
Sourcecode of 7-ZIP: https://sourceforge.net/projects/sevenzip/files/7-Zip


- Revert "Disable certificate revocation checks to fix Schannel error"

- Use Debian Strech for compiling as LTS support for jessie ended
- Update OpenSSL to version 1.1.1k and libcurl to 7.76.0
- Fix out-of-memory bug when downloading large files by disable saving of output content when downloading a file
- Add errno message if PosixOpen (command execution) fails
- Disable certificate revocation checks to fix Schannel error

- Fixes crashing on Sourcemod 11 caused by threads
- Updated to libcurl 7.72.0

- Automatically use 32-Bit 7-ZIP when 64-Bit is not executable
- Fix incomplete file when downloading files with FTP
- Added libcurl certificate file for SSL instead of using system's certificate file
- Updated to the latest libcurl version

- Added ability to use a proxy for requests (Pull Request by Kyle)
  See System2Request.SetProxy and System2Request.SetProxyAuthentication methods for more information
- Updated to the latest OpenSSL and libcurl version

- Fixed crash when creating many threads

- Almost all previous natives are now deprecated
- Implemented an enormous HTTP / FTP Request API replacing the old natives
- Made extension much more stable
- Added check if 7-ZIP is executable on Linux
- Output of an execution is now a methodmap replacing the progress approach
- Provide exit status of an execution
- Cleaned up API
- A lot more

- Fixed that the error string on the TransferUpdated callback wasn't empty when there was no error
- Only allowing one FTP connection at the same time, because RFC only allow once
- Added MD5 and CRC32 calculating for strings and files

- Complete code restructure
- A lot of bug fixes
- Added 7z for 32bit and 64bit linux
- Updated openssl and libcurl to newest versions
- Added command to command callback

- Fixed some curl bugs

- Fixed wrong any parameter on callback function

- Added parameter any:data to all callbacks and natives with callbacks

- Fixed a bug that CopyFile doesn't copied all data

- Added native System2_GetPage to get the content of a page
- Now you can retrieve the whole output from a threaded system command (Sorry i had to change the CmdCallback for this!)
- Support for SSL on Linux (Windows already have support)

- Security fixes
- Now every progress update will call the forward

- Added prefix System2_ to natives
- Added DownloadFile
- Added DownloadFTPFile
- Added UploadFTPFile
- Added CopyFile
- Added CompressFile
- Added ExtractArchive

1.5 - Proper usage of threading. Thanks to KyleS

- RunCommand works now non threaded
- Added RunThreadCommand for threaded commands
- Callback for threaded commands gives now: executed command, output and status code
- RunCommand returns status code and gives output
- Command parameter now with arguments
- No need of Metamod -> Only one file for all games!

1.3 - RunCommand is now Threaded and have a callback function!
1.2 - Improved Extension: works now also for left4dead(2)
1.1 - Added GetOS
1.0 - Release
Attached Files
File Type: zip system2.zip (5.60 MB, 312 views)

Last edited by dordnung; 04-16-2021 at 17:10.
dordnung is offline
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 12-23-2010 , 05:55   Re: [EXTENSION] System2
Reply With Quote #2

Full Source Code needs to be provided with extensions.
I've deleted your attachment for now, repost it when you also add the Source.
asherkin is offline
Veteran Member
Join Date: Apr 2010
Old 12-23-2010 , 13:52   Re: [EXTENSION] System2
Reply With Quote #3

Ok added both
dordnung is offline
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 12-23-2010 , 14:10   Re: [EXTENSION] System2
Reply With Quote #4

Zephyrus is offline
Veteran Member
Join Date: Aug 2008
Old 12-24-2010 , 03:23   Re: [EXTENSION] System2
Reply With Quote #5

Why did you define your callbacks as statics? Isn't it a bit overkill?
FaTony is offline
Veteran Member
Join Date: Apr 2010
Old 12-24-2010 , 06:21   Re: [EXTENSION] System2
Reply With Quote #6

do you mean this?

PHP Code:
extern const sp_nativeinfo_t system2_natives[];
cell_t sys_RunCommand(IPluginContext *pContext, const cell_t *params);
cell_t sys_GetGameDir(IPluginContext *pContext, const cell_t *params); 
dordnung is offline
SourceMod Donor
Join Date: Jun 2006
Location: Russia
Old 12-24-2010 , 07:43   Re: [EXTENSION] System2
Reply With Quote #7

I think this extension needs a native for getting OS name like Linux or Windows. Good job btw
For admins: My plugins

For developers: Colors library
exvel is offline
Send a message via ICQ to exvel
Veteran Member
Join Date: Apr 2010
Old 12-24-2010 , 09:05   Re: [EXTENSION] System2
Reply With Quote #8

good idea, i will add this
dordnung is offline
Veteran Member
Join Date: Aug 2009
Old 12-24-2010 , 11:17   Re: [EXTENSION] System2
Reply With Quote #9

Originally Posted by Popoklopsi View Post
good idea, i will add this
PHP Code:
enum Os {
Os_Linux 1,

cell_t Native_GetOS(IPluginContext *pContext, const cell_t *params)
#ifdef __linux__
return Os_Linux;
#elif defined _WIN32
return Os_Windows;
#elif defined __APPLE__
return Os_Mac;

Afronanny is offline
Veteran Member
Join Date: Apr 2010
Old 12-24-2010 , 11:19   Re: [EXTENSION] System2
Reply With Quote #10

Yes i already add this, only have to compile it for linux


Last edited by dordnung; 12-25-2010 at 11:28.
dordnung is offline

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -4. The time now is 03:17.

Powered by vBulletin®
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Theme made by Freecode