2 * \file HTTPClient.hxx - simple HTTP client engine for SimHear
5 // Written by James Turner
7 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
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.
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.
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.
24 #ifndef SG_HTTP_CLIENT_HXX
25 #define SG_HTTP_CLIENT_HXX
27 #include <memory> // for std::auto_ptr
28 #include <stdint.h> // for uint_64t
30 #include <simgear/io/HTTPFileRequest.hxx>
31 #include <simgear/io/HTTPMemoryRequest.hxx>
48 void update(int waitTimeout = 0);
50 void makeRequest(const Request_ptr& r);
52 void cancelRequest(const Request_ptr& r, std::string reason = std::string());
55 * Download a resource and save it to a file.
57 * @param url The resource to download
58 * @param filename Path to the target file
59 * @param data Data for POST request
61 FileRequestRef save( const std::string& url,
62 const std::string& filename );
65 * Request a resource and keep it in memory.
67 * @param url The resource to download
69 MemoryRequestRef load(const std::string& url);
71 void setUserAgent(const std::string& ua);
72 void setProxy(const std::string& proxy, int port, const std::string& auth = "");
75 * Specify the maximum permitted simultaneous connections
76 * (default value is 1)
78 void setMaxConnections(unsigned int maxCons);
80 void setMaxHostConnections(unsigned int maxHostConns);
83 * maximum depth to pipeline requests - set to 0 to disable pipelining
85 void setMaxPipelineDepth(unsigned int depth);
87 const std::string& userAgent() const;
89 const std::string& proxyHost() const;
91 const std::string& proxyAuth() const;
94 * predicate, check if at least one connection is active, with at
95 * least one request active or queued.
97 bool hasActiveRequests() const;
100 * crude tracking of bytes-per-second transferred over the socket.
101 * suitable for user feedback and rough profiling, nothing more.
103 unsigned int transferRateBytesPerSec() const;
106 * total bytes downloaded by this HTTP client, for bandwidth usage
109 uint64_t totalBytesDownloaded() const;
111 void debugDumpRequests();
113 void clearAllConnections();
116 static size_t requestWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata);
117 static size_t requestReadCallback(char *ptr, size_t size, size_t nmemb, void *userdata);
118 static size_t requestHeaderCallback(char *buffer, size_t size, size_t nitems, void *userdata);
120 void requestFinished(Connection* con);
122 void receivedBytes(unsigned int count);
124 friend class Connection;
125 friend class Request;
128 std::auto_ptr<ClientPrivate> d;
131 } // of namespace HTTP
133 } // of namespace simgear
135 #endif // of SG_HTTP_CLIENT_HXX