Logging improvements to allow prettier displays in the server GUI.
[dcpomatic.git] / src / lib / internet.cc
index c3f9dce659522099a13b047477494307ecaf3a18..7bc818717a44737f2719960a8bc8832aff4eec82 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #include <boost/function.hpp>
 #include <boost/optional.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/algorithm/string.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;
+using boost::algorithm::trim;
 
 static size_t
 get_from_zip_url_data (void* buffer, size_t size, size_t nmemb, void* stream)
@@ -50,12 +53,14 @@ get_from_zip_url (string url, string file, function<void (boost::filesystem::pat
        /* Download the ZIP file to temp_zip */
        CURL* curl = curl_easy_init ();
        curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
-       
+
        ScopedTemporary temp_zip;
        FILE* f = temp_zip.open ("wb");
        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);
 
@@ -66,21 +71,21 @@ get_from_zip_url (string url, string file, function<void (boost::filesystem::pat
        }
 
        /* Open the ZIP file and read `file' out of it */
-       
+
        struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
        if (!zip) {
                return optional<string> (_("Could not open downloaded ZIP file"));
        }
-       
+
        struct zip_file* zip_file = zip_fopen (zip, file.c_str(), 0);
        if (!zip_file) {
                return optional<string> (_("Unexpected ZIP file contents"));
        }
-       
+
        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))) {
@@ -88,7 +93,7 @@ get_from_zip_url (string url, string file, function<void (boost::filesystem::pat
                }
        }
        temp_cert.close ();
-       
+
        load (temp_cert.file ());
        return optional<string> ();
 }
@@ -117,6 +122,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 +137,11 @@ 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 line = s.getline ();
+               trim (line);
                if (line.length() > 55) {
                        string const file = line.substr (55);
                        if (file != "." && file != "..") {