@@ -212,6 +212,60 @@ int https_curl_debug(CURL __attribute__((unused)) * handle, curl_infotype type,
212212 return 0 ;
213213}
214214
215+ static const char * http_version_str (const long version ) {
216+ switch (version ) {
217+ case CURL_HTTP_VERSION_1_0 :
218+ return "1.0" ;
219+ case CURL_HTTP_VERSION_1_1 :
220+ return "1.1" ;
221+ case CURL_HTTP_VERSION_2_0 : // fallthrough
222+ case CURL_HTTP_VERSION_2TLS :
223+ return "2" ;
224+ #ifdef CURL_VERSION_HTTP3
225+ case CURL_HTTP_VERSION_3 :
226+ return "3" ;
227+ #endif
228+ default :
229+ FLOG ("Unsupported HTTP version: %d" , version );
230+ }
231+ return "UNKNOWN" ; // unreachable code
232+ }
233+
234+ static void https_set_request_version (https_client_t * client ,
235+ struct https_fetch_ctx * ctx ) {
236+ long http_version_int = CURL_HTTP_VERSION_2TLS ;
237+ switch (client -> opt -> use_http_version ) {
238+ case 1 :
239+ http_version_int = CURL_HTTP_VERSION_1_1 ;
240+ // fallthrough
241+ case 2 :
242+ break ;
243+ case 3 :
244+ #ifdef CURL_VERSION_HTTP3
245+ http_version_int = CURL_HTTP_VERSION_3 ;
246+ #endif
247+ break ;
248+ default :
249+ FLOG_REQ ("Invalid HTTP version: %d" , client -> opt -> use_http_version );
250+ }
251+ DLOG_REQ ("Requesting HTTP/%s" , http_version_str (http_version_int ));
252+
253+ CURLcode easy_code = curl_easy_setopt (ctx -> curl , CURLOPT_HTTP_VERSION , http_version_int );
254+ if (easy_code != CURLE_OK ) {
255+ ELOG_REQ ("Setting HTTP/%s version failed with %d: %s" ,
256+ http_version_str (http_version_int ), easy_code , curl_easy_strerror (easy_code ));
257+
258+ if (client -> opt -> use_http_version == 3 ) {
259+ ELOG ("Try to run application without -q argument!" ); // fallback unknown for current request
260+ client -> opt -> use_http_version = 2 ;
261+ } else if (client -> opt -> use_http_version == 2 ) {
262+ ELOG ("Try to run application with -x argument! Falling back to HTTP/1.1 version." );
263+ client -> opt -> use_http_version = 1 ;
264+ // TODO: consider CURLMOPT_PIPELINING setting??
265+ }
266+ }
267+ }
268+
215269static void https_fetch_ctx_init (https_client_t * client ,
216270 struct https_fetch_ctx * ctx , const char * url ,
217271 const char * data , size_t datalen ,
@@ -228,20 +282,7 @@ static void https_fetch_ctx_init(https_client_t *client,
228282
229283 ASSERT_CURL_EASY_SETOPT (ctx , CURLOPT_RESOLVE , resolv );
230284
231- DLOG_REQ ("Requesting HTTP/1.1: %d" , client -> opt -> use_http_1_1 );
232- CURLcode easy_code = curl_easy_setopt (ctx -> curl , CURLOPT_HTTP_VERSION ,
233- client -> opt -> use_http_1_1 ?
234- CURL_HTTP_VERSION_1_1 :
235- CURL_HTTP_VERSION_2_0 );
236- if (easy_code != CURLE_OK ) {
237- // hopefully errors will be logged once
238- ELOG_REQ ("CURLOPT_HTTP_VERSION error %d: %s" ,
239- easy_code , curl_easy_strerror (easy_code ));
240- if (!client -> opt -> use_http_1_1 ) {
241- ELOG ("Try to run application with -x argument! Forcing HTTP/1.1 version." );
242- client -> opt -> use_http_1_1 = 1 ;
243- }
244- }
285+ https_set_request_version (client , ctx );
245286
246287 if (logging_debug_enabled ()) {
247288 ASSERT_CURL_EASY_SETOPT (ctx , CURLOPT_VERBOSE , 1L );
@@ -301,7 +342,7 @@ static int https_fetch_ctx_process_response(https_client_t *client,
301342 faulty_response = 0 ;
302343 break ;
303344 case CURLE_WRITE_ERROR :
304- WLOG_REQ ("Response content was too large" );
345+ WLOG_REQ ("curl request failed with write error (probably response content was too large) " );
305346 break ;
306347 default :
307348 WLOG_REQ ("curl request failed with %d: %s" , res , curl_easy_strerror (res ));
@@ -322,9 +363,10 @@ static int https_fetch_ctx_process_response(https_client_t *client,
322363 uploaded_bytes > 0 ) {
323364 WLOG_REQ ("Connecting and sending request to resolver was successful, "
324365 "but no response was sent back" );
325- if (client -> opt -> use_http_1_1 ) {
366+ if (client -> opt -> use_http_version == 1 ) {
326367 // for example Unbound DoH servers does not support HTTP/1.x, only HTTP/2
327- WLOG ("Resolver may not support current HTTP/1.1 protocol version" );
368+ WLOG ("Resolver may not support current HTTP/%s protocol version" ,
369+ http_version_str (client -> opt -> use_http_version ));
328370 }
329371 } else {
330372 // in case of HTTP/1.1 this can happen very often depending on DNS query frequency
@@ -404,20 +446,8 @@ static int https_fetch_ctx_process_response(https_client_t *client,
404446 if ((res = curl_easy_getinfo (
405447 ctx -> curl , CURLINFO_HTTP_VERSION , & long_resp )) != CURLE_OK ) {
406448 ELOG_REQ ("CURLINFO_HTTP_VERSION: %s" , curl_easy_strerror (res ));
407- } else {
408- switch (long_resp ) {
409- case CURL_HTTP_VERSION_1_0 :
410- DLOG_REQ ("CURLINFO_HTTP_VERSION: 1.0" );
411- break ;
412- case CURL_HTTP_VERSION_1_1 :
413- DLOG_REQ ("CURLINFO_HTTP_VERSION: 1.1" );
414- break ;
415- case CURL_HTTP_VERSION_2_0 :
416- DLOG_REQ ("CURLINFO_HTTP_VERSION: 2" );
417- break ;
418- default :
419- DLOG_REQ ("CURLINFO_HTTP_VERSION: %d" , long_resp );
420- }
449+ } else if (long_resp != CURL_HTTP_VERSION_NONE ) {
450+ DLOG_REQ ("CURLINFO_HTTP_VERSION: %s" , http_version_str (long_resp ));
421451 }
422452 if ((res = curl_easy_getinfo (
423453 ctx -> curl , CURLINFO_PROTOCOL , & long_resp )) != CURLE_OK ) {
0 commit comments