File downloader function with optional SSL support

You can post your code snippets here for others to use and learn from
Post Reply
User avatar
Posts: 376
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO

File downloader function with optional SSL support

Post by jPV »

HTTPS/FTPS URLs aren't supported by Hollywood internally, but only by the hURL plugin. I made a DownloadFile function that can be used to download files to a string or file. The function works without hURL for standard HTTP (non-SSL) URLs, but supports hURL if needed. It loads the plugin only when needed (you don't need an @REQUIRE line in your script), so if you don't need HTTPS URLs, you don't have to provide/load the plugin for nothing. But if you or your program start to require HTTPS support, or has user configurable download URLs, and the plugin isn't available, the function gives a notification to a user to install the plugin instead of just failing. The function should work on most (all?) platforms, and it also checks if the required AmiSSL is installed on Amiga compatible systems.

The function shows requesters only once per session to avoid a flood with many downloads. If you want to change that behaviour, get rid of the global variables. Maybe this could also be expanded to support all functionality of the original DownloadFile() function... but I'll just share it as it is for now.

Code: Select all

Function p_DownloadFile(url$, file$, force)
    /* Synopsis: data$, count = p_DownloadFile(url$[, file$])

       If a URL starts with https or ftps, the file is downloaded directly with hURL,
       but otherwise it's first tried without hURL. If you don't need hURL, the plugin
       isn't loaded to memory at all, and it doesn't have to be available either.
       The function also checks if AmiSSL is available, and notifies if not.
       If a non-secure URL fails, the function tries again with hURL. This solves issues
       when a server has changed from http to https, but the program is using old URLs.
    Local options = {
        ; Add your own settings here:
        ; UserAgent = "MyProgram/" .. #VERSION .. " ( )",

        ; Don't touch to this:
        File = file$
    Local err, data$, count
    If StartsWith(url$, "https", False) Or StartsWith(url$, "ftps", False) Or force
        Local found = HavePlugin("hurl")
        If found
            If Not hurl_loaded
                If ?LoadPlugin("hurl")
                    If Not amissl_asked
                        If SystemRequest("Download File", "AmiSSL v4 is required for HTTPS and FTPS URLs.\nIt can be downloaded from Aminet (", "Download|OK")
                        amissl_asked = True
                    Return("No AmiSSL!", -1)
                    hurl_loaded = True
            options.Adapter = "hurl"
            options.SilentFail = True
            data$, count = DownloadFile(url$, options)
        ElseIf Not hurl_asked
            If SystemRequest("Download File", "The hURL plugin is required for HTTPS and FTPS URLs.", "Download|OK")
            hurl_asked = True
            Return("No hURL!", -1)
        err, data$, count = ?DownloadFile(url$, options)
        If err = #ERR_UNKPROTOCOL
            data$, count = p_DownloadFile(url$, file$, True)
        ElseIf err
            Return(GetErrorName(err), -1)
    Return(data$, count)

Post Reply