1 /*
2  * SessionAsyncDownloadFile.hpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #ifndef SESSION_ASYNC_DOWNLOAD_FILE_HPP
17 #define SESSION_ASYNC_DOWNLOAD_FILE_HPP
18 
19 #include <string>
20 
21 #include <core/http/Util.hpp>
22 #include <core/json/JsonRpc.hpp>
23 
24 namespace rstudio {
25 namespace core {
26    class Error;
27    namespace system {
28       struct ProcessResult;
29    }
30 }
31 namespace session {
32 
33 // use R download.file in an async subprocess (contents of file are in result.stdOut on success)
34 
35 void asyncDownloadFile(const std::string& url,
36                        const boost::function<void(const core::system::ProcessResult&)>& onCompleted);
37 
38 void asyncDownloadFile(const std::string& url,
39                        const core::http::Fields& headers,
40                        const boost::function<void(const core::system::ProcessResult&)>& onCompleted);
41 
42 void asyncDownloadFile(const std::string& url,
43                        const std::string& userAgent,
44                        const core::http::Fields& headers,
45                        const boost::function<void(const core::system::ProcessResult&)>& onCompleted);
46 
47 // wrapper for asyncDownloadFile that parses it's payload as JSON and satisfies a JsonRpcFunctionContinuation
48 // (including checking for and reporting errors on the continuation)
49 
50 typedef boost::function<void(const core::json::Value&, core::json::JsonRpcResponse*)> JsonRpcResponseHandler;
51 
52 void asyncJsonRpcRequest(const std::string& url,
53                          const JsonRpcResponseHandler& handler,
54                          const core::json::JsonRpcFunctionContinuation& cont);
55 
56 void asyncJsonRpcRequest(const std::string& url,
57                          const std::string& userAgent,
58                          const core::http::Fields& headers,
59                          const JsonRpcResponseHandler& handler,
60                          const core::json::JsonRpcFunctionContinuation& cont);
61 
62 bool is404Error(const std::string& stdErr);
63 bool isHostError(const std::string& stdErr);
64 
65 
66 } // namespace session
67 } // namespace rstudio
68 
69 #endif // SESSION_ASYNC_DOWNLOAD_FILE_HPP
70 
71