@@ -70,6 +70,7 @@ namespace httplib
70
70
typedef std::map<std::string, std::string> Map;
71
71
typedef std::multimap<std::string, std::string> MultiMap;
72
72
typedef std::smatch Match;
73
+ typedef std::function<void (int64_t current, int64_t total)> Progress;
73
74
74
75
struct Request {
75
76
std::string method;
@@ -78,6 +79,7 @@ struct Request {
78
79
std::string body;
79
80
Map params;
80
81
Match matches;
82
+ Progress progress;
81
83
82
84
bool has_header (const char * key) const ;
83
85
std::string get_header_value (const char * key) const ;
@@ -169,7 +171,7 @@ class Client {
169
171
Client (const char * host, int port);
170
172
virtual ~Client ();
171
173
172
- std::shared_ptr<Response> get (const char * path);
174
+ std::shared_ptr<Response> get (const char * path, Progress callback = []( int64_t , int64_t ){} );
173
175
std::shared_ptr<Response> head (const char * path);
174
176
std::shared_ptr<Response> post (const char * path, const std::string& body, const char * content_type);
175
177
std::shared_ptr<Response> post (const char * path, const Map& params);
@@ -511,7 +513,7 @@ inline bool read_headers(Stream& strm, MultiMap& headers)
511
513
}
512
514
513
515
template <typename T>
514
- bool read_content (Stream& strm, T& x, bool allow_no_content_length)
516
+ bool read_content (Stream& strm, T& x, bool allow_no_content_length, Progress progress = []( int64_t , int64_t ){} )
515
517
{
516
518
auto len = get_header_value_int (x.headers , " Content-Length" , 0 );
517
519
if (len) {
@@ -523,6 +525,7 @@ bool read_content(Stream& strm, T& x, bool allow_no_content_length)
523
525
return false ;
524
526
}
525
527
r += r_incr;
528
+ progress (r, len);
526
529
}
527
530
} else if (allow_no_content_length) {
528
531
for (;;) {
@@ -1087,7 +1090,7 @@ inline bool Client::process_request(Stream& strm, const Request& req, Response&
1087
1090
return false ;
1088
1091
}
1089
1092
if (req.method != " HEAD" ) {
1090
- if (!detail::read_content (strm, res, true )) {
1093
+ if (!detail::read_content (strm, res, true , req. progress )) {
1091
1094
return false ;
1092
1095
}
1093
1096
}
@@ -1109,11 +1112,12 @@ inline void Client::add_default_headers(Request& req)
1109
1112
req.set_header (" User-Agent" , " cpp-httplib/0.1" );
1110
1113
}
1111
1114
1112
- inline std::shared_ptr<Response> Client::get (const char * path)
1115
+ inline std::shared_ptr<Response> Client::get (const char * path, Progress callback )
1113
1116
{
1114
1117
Request req;
1115
1118
req.method = " GET" ;
1116
1119
req.path = path;
1120
+ req.progress = callback;
1117
1121
add_default_headers (req);
1118
1122
1119
1123
auto res = std::make_shared<Response>();
0 commit comments