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 <functional> 28 #include <memory> // for std::unique_ptr 29 #include <stdint.h> // for uint_64t 30 31 #include <simgear/io/HTTPFileRequest.hxx> 32 #include <simgear/io/HTTPMemoryRequest.hxx> 33 34 namespace simgear 35 { 36 37 namespace HTTP 38 { 39 40 // forward decls 41 class Connection; 42 43 class Client 44 { 45 public: 46 Client(); 47 ~Client(); 48 49 void update(int waitTimeout = 0); 50 51 void reset(); 52 53 void makeRequest(const Request_ptr& r); 54 55 void cancelRequest(const Request_ptr& r, std::string reason = std::string()); 56 57 /** 58 * Download a resource and save it to a file. 59 * 60 * @param url The resource to download 61 * @param filename Path to the target file 62 * @param data Data for POST request 63 */ 64 FileRequestRef save( const std::string& url, 65 const std::string& filename ); 66 67 /** 68 * Request a resource and keep it in memory. 69 * 70 * @param url The resource to download 71 */ 72 MemoryRequestRef load(const std::string& url); 73 74 void setUserAgent(const std::string& ua); 75 void setProxy(const std::string& proxy, int port, const std::string& auth = ""); 76 77 /** 78 * Specify the maximum permitted simultaneous connections 79 * (default value is 1) 80 */ 81 void setMaxConnections(unsigned int maxCons); 82 83 void setMaxHostConnections(unsigned int maxHostConns); 84 85 /** 86 * maximum depth to pipeline requests - set to 0 to disable pipelining 87 */ 88 void setMaxPipelineDepth(unsigned int depth); 89 90 const std::string& userAgent() const; 91 92 const std::string& proxyHost() const; 93 94 const std::string& proxyAuth() const; 95 96 /** 97 * predicate, check if at least one connection is active, with at 98 * least one request active or queued. 99 */ 100 bool hasActiveRequests() const; 101 102 /** 103 * crude tracking of bytes-per-second transferred over the socket. 104 * suitable for user feedback and rough profiling, nothing more. 105 */ 106 unsigned int transferRateBytesPerSec() const; 107 108 /** 109 * total bytes downloaded by this HTTP client, for bandwidth usage 110 * monitoring 111 */ 112 uint64_t totalBytesDownloaded() const; 113 114 void debugDumpRequests(); 115 116 void clearAllConnections(); 117 private: 118 // libCurl callbacks 119 static size_t requestWriteCallback(char *ptr, size_t size, size_t nmemb, void *userdata); 120 static size_t requestReadCallback(char *ptr, size_t size, size_t nmemb, void *userdata); 121 static size_t requestHeaderCallback(char *buffer, size_t size, size_t nitems, void *userdata); 122 123 void requestFinished(Connection* con); 124 125 void receivedBytes(unsigned int count); 126 127 friend class Connection; 128 friend class Request; 129 friend class TestApi; 130 131 class ClientPrivate; 132 std::unique_ptr<ClientPrivate> d; 133 }; 134 135 } // of namespace HTTP 136 137 } // of namespace simgear 138 139 #endif // of SG_HTTP_CLIENT_HXX 140