LibPkg
download.h
Go to the documentation of this file.
1 // This file is part of LibPkg.
2 //
3 // Copyright 2003-2020 Graham Shaw
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef LIBPKG_DOWNLOAD
18 #define LIBPKG_DOWNLOAD
19 
20 #include <string>
21 #include <fstream>
22 
23 #include "curl/curl.h"
24 
25 // Define the following to add extra logging of the download stage
26 // #define LOG_DOWNLOAD
27 
28 #ifdef LOG_DOWNLOAD
29 #include "libpkg/log.h"
30 #endif
31 
32 namespace pkg {
33 
34 using std::string;
35 
37 class download
38 {
39 public:
41  typedef unsigned long long size_type;
42 
44  static const size_type npos=static_cast<size_type>(-1);
45 
46  // An enumeration for describing the state of the download. */
48  {
56  };
57 
58  struct options
59  {
60  bool use_proxy;
61  string proxy;
62  string do_not_proxy;
63  };
64 
65 private:
67  state_type _state;
68 
70  CURL* _ceasy;
71 
73  CURLcode _result;
74 
75 
77  char* _error_buffer;
78 
80  string _url;
81 
83  std::ofstream _out;
84 
86  size_type _bytes_done;
87 
89  size_type _bytes_total;
90 
91  #ifdef LOG_DOWNLOAD
92 
93  log *_log;
94  #endif
95 
96 public:
102 download(const string& url,const string& pathname, options *opts = nullptr);
103 
105  ~download();
106 
111  { return _state; }
112 
116  CURLcode result() const
117  { return _result; }
118 
122  string message() const
123  { return _error_buffer; }
124 
128  size_type bytes_done()
129  { return _bytes_done; }
130 
134  size_type bytes_total()
135  { return _bytes_total; }
136 
143  size_t write_callback(char* buffer,size_t size,size_t nitems);
144 
150  int progress_callback(double dltotal,double dlnow);
151 
155  void message_callback(CURLMsg* msg);
156 
157 #ifdef LOG_DOWNLOAD
158 
160  int debug_callback(curl_infotype type,char *data, size_t size);
164  void log_to(log *use_log);
165 #endif
166 
167 private:
174  static CURLM* _cmulti;
175 
178  static unsigned int _cmulti_refcount;
179 public:
181  static void poll_all();
182 };
183 
184 }; /* namespace pkg */
185 
186 #endif
string message() const
Get libcurl error message.
Definition: download.h:122
Definition: download.h:58
The state in which the download has been successfully completed.
Definition: download.h:53
string do_not_proxy
Definition: download.h:62
The state in which the download is in progress.
Definition: download.h:50
bool use_proxy
Definition: download.h:60
The namespace used to hold the package management library.
Definition: auto_dir.cc:23
static const size_type npos
A null value for use in place of a byte count.
Definition: download.h:44
size_type bytes_done()
Get number of bytes downloaded.
Definition: download.h:128
~download()
Destroy download action.
Definition: download.cc:135
static void poll_all()
Poll all download operations.
Definition: download.cc:219
int progress_callback(double dltotal, double dlnow)
Handler for CURLOPT_PROGRESSFUNCTION callbacks.
Definition: download.cc:158
unsigned long long size_type
A type for representing byte counts.
Definition: download.h:41
Class to log actions that occur in LibPkg.
Definition: log.h:226
void message_callback(CURLMsg *msg)
Handler for Curl messages.
Definition: download.cc:166
size_t write_callback(char *buffer, size_t size, size_t nitems)
Handler for CURLOPT_WRITEFUNCTION callbacks.
Definition: download.cc:152
download(const string &url, const string &pathname, options *opts=nullptr)
Construct download action.
Definition: download.cc:64
string proxy
Definition: download.h:61
A class for downloading a file from a URL.
Definition: download.h:37
state_type state() const
Get current state of the download.
Definition: download.h:110
state_type
Definition: download.h:47
The state in which the download has failed.
Definition: download.h:55
size_type bytes_total()
Get total number of bytes to download.
Definition: download.h:134
CURLcode result() const
Get libcurl result code.
Definition: download.h:116

Reference Manual LibPkg Version 0.9.0 (4 Sep 2020)