Remove POSIX backtraces; move ScopedTemporary into its own file.
[dcpomatic.git] / src / lib / internet.cc
index c3f9dce659522099a13b047477494307ecaf3a18..b45eaabf7580ec2c74a0d0559d3b0ed54b377223 100644 (file)
 #include <boost/filesystem.hpp>
 #include <curl/curl.h>
 #include <zip.h>
-#include "util.h"
+#include "scoped_temporary.h"
+#include "compose.hpp"
+#include "safe_stringstream.h"
 
 #include "i18n.h"
 
 using std::string;
-using std::stringstream;
 using std::list;
 using boost::optional;
 using boost::function;
@@ -56,6 +57,8 @@ get_from_zip_url (string url, string file, function<void (boost::filesystem::pat
        curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, get_from_zip_url_data);
        curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
        curl_easy_setopt (curl, CURLOPT_FTP_USE_EPSV, 0);
+       /* Maximum time is 20s */
+       curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
 
        CURLcode const cr = curl_easy_perform (curl);
 
@@ -80,7 +83,7 @@ get_from_zip_url (string url, string file, function<void (boost::filesystem::pat
        ScopedTemporary temp_cert;
        f = temp_cert.open ("wb");
        char buffer[4096];
-       while (1) {
+       while (true) {
                int const N = zip_fread (zip_file, buffer, sizeof (buffer));
                fwrite (buffer, 1, N, f);
                if (N < int (sizeof (buffer))) {
@@ -117,6 +120,8 @@ ftp_ls (string url)
                url += "/";
        }
        curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
+       /* 20s timeout */
+       curl_easy_setopt (curl, CURLOPT_TIMEOUT, 20);
 
        string ls_raw;
        struct curl_slist* commands = 0;
@@ -130,11 +135,10 @@ ftp_ls (string url)
                return list<string> ();
        }
 
-       stringstream s (ls_raw);
-       string line;
+       SafeStringStream s (ls_raw);
        list<string> ls;
        while (s.good ()) {
-               getline (s, line);
+               string const line = s.getline ();
                if (line.length() > 55) {
                        string const file = line.substr (55);
                        if (file != "." && file != "..") {