]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPClient.hxx
Expose total bytes to download / remaining
[simgear.git] / simgear / io / HTTPClient.hxx
1 /**
2  * \file HTTPClient.hxx - simple HTTP client engine for SimHear
3  */
4
5 // Written by James Turner
6 //
7 // Copyright (C) 2013  James Turner  <zakalawe@mac.com>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Library General Public
11 // License as published by the Free Software Foundation; either
12 // version 2 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // Library General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22 //
23
24 #ifndef SG_HTTP_CLIENT_HXX
25 #define SG_HTTP_CLIENT_HXX
26
27 #include <memory> // for std::auto_ptr
28 #include <stdint.h> // for uint_64t
29
30 #include <simgear/io/HTTPFileRequest.hxx>
31 #include <simgear/io/HTTPMemoryRequest.hxx>
32
33 namespace simgear
34 {
35
36 namespace HTTP
37 {
38
39 // forward decls
40 class Connection;
41
42 class Client
43 {
44 public:
45     Client();
46     ~Client();
47
48     void update(int waitTimeout = 0);
49
50     void makeRequest(const Request_ptr& r);
51
52     /**
53      * Download a resource and save it to a file.
54      *
55      * @param url       The resource to download
56      * @param filename  Path to the target file
57      * @param data      Data for POST request
58      */
59     FileRequestRef save( const std::string& url,
60                          const std::string& filename );
61
62     /**
63      * Request a resource and keep it in memory.
64      *
65      * @param url   The resource to download
66      */
67     MemoryRequestRef load(const std::string& url);
68
69     void setUserAgent(const std::string& ua);
70     void setProxy(const std::string& proxy, int port, const std::string& auth = "");
71
72     /**
73      * Specify the maximum permitted simultaneous connections
74      * (default value is 1)
75      */
76     void setMaxConnections(unsigned int maxCons);
77
78     void setMaxHostConnections(unsigned int maxHostConns);
79
80     /**
81      * maximum depth to pipeline requests - set to 0 to disable pipelining
82      */
83     void setMaxPipelineDepth(unsigned int depth);
84
85     const std::string& userAgent() const;
86
87     const std::string& proxyHost() const;
88
89     const std::string& proxyAuth() const;
90
91     /**
92      * predicate, check if at least one connection is active, with at
93      * least one request active or queued.
94      */
95     bool hasActiveRequests() const;
96
97     /**
98      * crude tracking of bytes-per-second transferred over the socket.
99      * suitable for user feedback and rough profiling, nothing more.
100      */
101     unsigned int transferRateBytesPerSec() const;
102
103     /**
104      * total bytes downloaded by this HTTP client, for bandwidth usage
105      * monitoring
106      */
107     uint64_t totalBytesDownloaded() const;
108
109     void debugDumpRequests();
110
111     void clearAllConnections();
112 private:
113     // libCurl callbacks
114     static size_t requestWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata);
115     static size_t requestReadCallback(char *ptr, size_t size, size_t nmemb, void *userdata);
116     static size_t requestHeaderCallback(char *buffer, size_t size, size_t nitems, void *userdata);
117
118     void requestFinished(Connection* con);
119
120     void receivedBytes(unsigned int count);
121
122     friend class Connection;
123     friend class Request;
124
125     class ClientPrivate;
126     std::auto_ptr<ClientPrivate> d;
127 };
128
129 } // of namespace HTTP
130
131 } // of namespace simgear
132
133 #endif // of SG_HTTP_CLIENT_HXX