medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit ece214b017d50d058d8fa5768794a6a7a0b1f43e
parent 59930a29f1a77dc2674b76651c5cfe47e3925434
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Jan 21 22:37:42 +0200

Add a progress callback to http_get

Diffstat:
http.cc | 6+++++-
http.h | 5++++-
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/http.cc b/http.cc @@ -29,7 +29,7 @@ bool dns_first( const char * host, struct sockaddr_storage * address ) { return true; } -GetResult http_get( const struct sockaddr_storage & address, const char * host, const char * path, std::string * body ) { +GetResult http_get( const struct sockaddr_storage & address, const char * host, const char * path, std::string * body, HttpProgressCallback progress_cb, void * cb_data ) { int sock = socket( address.ss_family, SOCK_STREAM, 0 ); if( sock == -1 ) { return GET_ERROR_SOCKET; @@ -91,6 +91,10 @@ GetResult http_get( const struct sockaddr_storage & address, const char * host, } response.append( buf, checked_cast< size_t >( bytes_read ) ); + + if( progress_cb != NULL ) { + progress_cb( response.size(), cb_data ); + } } // HTTP/1.1 200 OK diff --git a/http.h b/http.h @@ -18,5 +18,8 @@ enum GetResult { GET_ERROR_HTTP_OTHER, }; +#define HTTP_PROGRESS_CALLBACK( f ) void f( size_t bytes_downloaded, void * data ) +typedef HTTP_PROGRESS_CALLBACK( HttpProgressCallback ); + bool dns_first( const char * host, struct sockaddr_storage * address ); -GetResult http_get( const struct sockaddr_storage & address, const char * host, const char * path, std::string * body ); +GetResult http_get( const struct sockaddr_storage & address, const char * host, const char * path, std::string * body, HttpProgressCallback progress_cb = NULL, void * cb_data = NULL );