@@ -82,13 +82,32 @@ static bool protocol_status_ok(Protocol protocol, long protocol_status) {
8282
8383DEFINE_TRIVIAL_CLEANUP_FUNC (CURL * , curl_easy_cleanup );
8484
85+ #define log_error_curle (code , fmt , ...) \
86+ log_error_errno(-EIO, fmt ": %s", ##__VA_ARGS__, curl_easy_strerror(code))
87+
88+ #define CURL_SETOPT_EASY (handle , option , value ) \
89+ ({ \
90+ CURLcode _c; \
91+ _c = curl_easy_setopt(handle, option, (value)); \
92+ if (_c != CURLE_OK) \
93+ return log_error_curle(_c, "Failed to set " #option); \
94+ })
95+
96+ #define CURL_SETOPT_EASY_CANFAIL (handle , option , value ) \
97+ ({ \
98+ CURLcode _c; \
99+ _c = curl_easy_setopt(handle, option, (value)); \
100+ if (_c != CURLE_OK) \
101+ log_error_curle(_c, "Failed to set " #option); \
102+ })
103+
85104static inline const char * get_curl_effective_url (CURL * handle ) {
86105 CURLcode c ;
87106 char * effective_url ;
88107
89108 c = curl_easy_getinfo (handle , CURLINFO_EFFECTIVE_URL , & effective_url );
90109 if (c != CURLE_OK ) {
91- log_error ( "Failed to get CURL effective URL. " );
110+ log_error_curle ( c , "Failed to get CURLINFO_EFFECTIVE_URL " );
92111 return NULL ;
93112 }
94113
@@ -99,10 +118,7 @@ static int configure_curl_easy_handle(CURL *handle, const char *url) {
99118 assert (handle );
100119 assert (url );
101120
102- if (curl_easy_setopt (handle , CURLOPT_URL , url ) != CURLE_OK ) {
103- log_error ("Failed to set CURL URL to: %s" , url );
104- return - EIO ;
105- }
121+ CURL_SETOPT_EASY (handle , CURLOPT_URL , url );
106122
107123 return 0 ;
108124}
@@ -123,55 +139,30 @@ static int make_curl_easy_handle(CURL **ret,
123139 if (!h )
124140 return log_oom ();
125141
126- if (curl_easy_setopt (h , CURLOPT_FOLLOWLOCATION , 1L ) != CURLE_OK ) {
127- log_error ("Failed to turn on location following." );
128- return - EIO ;
129- }
130-
131- if (curl_easy_setopt (h , CURLOPT_PROTOCOLS , arg_protocol == PROTOCOL_FTP ? CURLPROTO_FTP :
132- arg_protocol == PROTOCOL_SFTP ? CURLPROTO_SFTP :
133- CURLPROTO_HTTP | CURLPROTO_HTTPS ) != CURLE_OK ) {
134- log_error ("Failed to limit protocols to HTTP/HTTPS/FTP/SFTP." );
135- return - EIO ;
136- }
142+ CURL_SETOPT_EASY (h , CURLOPT_FOLLOWLOCATION , 1L );
143+ CURL_SETOPT_EASY (h , CURLOPT_PROTOCOLS ,
144+ arg_protocol == PROTOCOL_FTP ? CURLPROTO_FTP :
145+ arg_protocol == PROTOCOL_SFTP ? CURLPROTO_SFTP :
146+ CURLPROTO_HTTP | CURLPROTO_HTTPS );
137147
138148 if (arg_protocol == PROTOCOL_SFTP ) {
139149 /* activate the ssh agent. For this to work you need
140150 to have ssh-agent running (type set | grep SSH_AGENT to check) */
141- if (curl_easy_setopt (h , CURLOPT_SSH_AUTH_TYPES , CURLSSH_AUTH_AGENT ) != CURLE_OK )
142- log_error ("Failed to turn on ssh agent support, ignoring." );
151+ CURL_SETOPT_EASY_CANFAIL (h , CURLOPT_SSH_AUTH_TYPES , CURLSSH_AUTH_AGENT );
143152 }
144153
145154 if (arg_rate_limit_bps > 0 ) {
146- if (curl_easy_setopt (h , CURLOPT_MAX_SEND_SPEED_LARGE , arg_rate_limit_bps ) != CURLE_OK ) {
147- log_error ("Failed to set CURL send speed limit." );
148- return - EIO ;
149- }
150-
151- if (curl_easy_setopt (h , CURLOPT_MAX_RECV_SPEED_LARGE , arg_rate_limit_bps ) != CURLE_OK ) {
152- log_error ("Failed to set CURL receive speed limit." );
153- return - EIO ;
154- }
155+ CURL_SETOPT_EASY (h , CURLOPT_MAX_SEND_SPEED_LARGE , arg_rate_limit_bps );
156+ CURL_SETOPT_EASY (h , CURLOPT_MAX_RECV_SPEED_LARGE , arg_rate_limit_bps );
155157 }
156158
157- if (curl_easy_setopt (h , CURLOPT_WRITEFUNCTION , write_callback ) != CURLE_OK ) {
158- log_error ("Failed to set CURL callback function." );
159- return - EIO ;
160- }
159+ CURL_SETOPT_EASY (h , CURLOPT_WRITEFUNCTION , write_callback );
160+ CURL_SETOPT_EASY (h , CURLOPT_WRITEDATA , write_data );
161161
162- if (curl_easy_setopt (h , CURLOPT_WRITEDATA , write_data ) != CURLE_OK ) {
163- log_error ("Failed to set CURL callback data." );
164- return - EIO ;
165- }
166-
167- if (private ) {
168- if (curl_easy_setopt (h , CURLOPT_PRIVATE , private ) != CURLE_OK ) {
169- log_error ("Failed to set CURL private data." );
170- return - EIO ;
171- }
172- }
162+ if (private )
163+ CURL_SETOPT_EASY (h , CURLOPT_PRIVATE , private );
173164
174- /* (void) curl_easy_setopt (h, CURLOPT_VERBOSE, 1L); */
165+ /* CURL_SETOPT_EASY (h, CURLOPT_VERBOSE, 1L); */
175166
176167 * ret = TAKE_PTR (h );
177168 return 0 ;
0 commit comments