@@ -583,6 +583,37 @@ static SQLRETURN dbc_curl_init(esodbc_dbc_st *dbc)
583583 INFOH (dbc , "no username provided: auth disabled." );
584584 }
585585
586+ /* proxy parameters */
587+ if (dbc -> proxy_url .cnt ) {
588+ dbc -> curl_err = curl_easy_setopt (curl , CURLOPT_PROXY ,
589+ dbc -> proxy_url .str );
590+ if (dbc -> curl_err != CURLE_OK ) {
591+ ERRH (dbc , "libcurl: failed to set the proxy URL." );
592+ goto err ;
593+ }
594+ if (dbc -> proxy_uid .cnt ) {
595+ dbc -> curl_err = curl_easy_setopt (curl , CURLOPT_PROXYUSERNAME ,
596+ dbc -> proxy_uid .str );
597+ if (dbc -> curl_err != CURLE_OK ) {
598+ ERRH (dbc , "libcurl: failed to set the proxy username." );
599+ goto err ;
600+ }
601+ if (dbc -> proxy_pwd .cnt ) {
602+ dbc -> curl_err = curl_easy_setopt (curl , CURLOPT_PROXYPASSWORD ,
603+ dbc -> proxy_pwd .str );
604+ if (dbc -> curl_err != CURLE_OK ) {
605+ ERRH (dbc , "libcurl: failed to set the proxy password." );
606+ goto err ;
607+ }
608+ }
609+ }
610+ } else {
611+ dbc -> curl_err = curl_easy_setopt (curl , CURLOPT_PROXY , "" );
612+ if (dbc -> curl_err != CURLE_OK ) {
613+ WARNH (dbc , "libcurl: failed to generally disable proxying." );
614+ }
615+ }
616+
586617 /* set the write call-back for answers */
587618 dbc -> curl_err = curl_easy_setopt (curl , CURLOPT_WRITEFUNCTION ,
588619 write_callback );
@@ -1130,7 +1161,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
11301161 const static wstr_st http_prefix = WSTR_INIT ("http://" );
11311162 const static wstr_st https_prefix = WSTR_INIT ("https://" );
11321163 wstr_st prefix ;
1133- int cnt , ipv6 ;
1164+ int cnt , ipv6 , n ;
11341165 SQLBIGINT secure , timeout , max_body_size , max_fetch_size , varchar_limit ;
11351166 SQLWCHAR buff_url [ESODBC_MAX_URL_LEN ];
11361167 wstr_st url = (wstr_st ) {
@@ -1191,7 +1222,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
11911222
11921223 if (secure ) {
11931224 if (! wstr_to_utf8 (& attrs -> ca_path , & dbc -> ca_path )) {
1194- ERRNH (dbc , "failed to convert CA path `" LWPDL "` to UTF8." ,
1225+ ERRH (dbc , "failed to convert CA path `" LWPDL "` to UTF8." ,
11951226 LWSTR (& attrs -> ca_path ));
11961227 SET_HDIAG (dbc , SQL_STATE_HY000 , "reading the CA file path "
11971228 "failed" , 0 );
@@ -1222,7 +1253,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
12221253 url .cnt = (size_t )cnt ;
12231254 }
12241255 if (! wstr_to_utf8 (& url , & dbc -> close_url )) {
1225- ERRNH (dbc , "failed to convert URL `" LWPDL "` to UTF8." , LWSTR (& url ));
1256+ ERRH (dbc , "failed to convert URL `" LWPDL "` to UTF8." , LWSTR (& url ));
12261257 SET_HDIAG (dbc , SQL_STATE_HY000 , "server SQL URL's UTF8 conversion "
12271258 "failed" , 0 );
12281259 goto err ;
@@ -1233,7 +1264,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
12331264 * dup'ed since libcurl needs the 0 terminator */
12341265 url .cnt -= sizeof (ELASTIC_SQL_CLOSE_SUBPATH ) - /*\0*/ 1 ;
12351266 if (! wstr_to_utf8 (& url , & dbc -> url )) {
1236- ERRNH (dbc , "failed to convert URL `" LWPDL "` to UTF8." , LWSTR (& url ));
1267+ ERRH (dbc , "failed to convert URL `" LWPDL "` to UTF8." , LWSTR (& url ));
12371268 SET_HDIAG (dbc , SQL_STATE_HY000 , "server SQL URL's UTF8 conversion "
12381269 "failed" , 0 );
12391270 goto err ;
@@ -1258,7 +1289,7 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
12581289 url .cnt = (size_t )cnt ;
12591290 }
12601291 if (! wstr_to_utf8 (& url , & dbc -> root_url )) {
1261- ERRNH (dbc , "failed to convert URL `" LWPDL "` to UTF8." , LWSTR (& url ));
1292+ ERRH (dbc , "failed to convert URL `" LWPDL "` to UTF8." , LWSTR (& url ));
12621293 SET_HDIAG (dbc , SQL_STATE_HY000 , "server root URL's UTF8 conversion "
12631294 "failed" , 0 );
12641295 goto err ;
@@ -1312,6 +1343,74 @@ SQLRETURN config_dbc(esodbc_dbc_st *dbc, esodbc_dsn_attrs_st *attrs)
13121343 dbc -> timeout = (SQLUINTEGER )timeout ;
13131344 INFOH (dbc , "timeout: %lu." , dbc -> timeout );
13141345
1346+ /*
1347+ * proxy settings
1348+ */
1349+ if (wstr2bool (& attrs -> proxy_enabled )) {
1350+ ipv6 = wcsnstr (attrs -> proxy_host .str , attrs -> proxy_host .cnt , L':' ) !=
1351+ NULL ;
1352+ cnt = swprintf (url .str , sizeof (buff_url )/sizeof (* buff_url ),
1353+ L"" WPFWP_LDESC "://" WPFCP_DESC WPFWP_LDESC WPFCP_DESC ,
1354+ LWSTR (& attrs -> proxy_type ),
1355+ ipv6 ? "[" : "" , LWSTR (& attrs -> proxy_host ), ipv6 ? "]" : "" );
1356+ if (cnt > 0 && attrs -> proxy_port .cnt ) {
1357+ n = swprintf (url .str + cnt ,
1358+ sizeof (buff_url )/sizeof (* buff_url ) - cnt ,
1359+ L":" WPFWP_LDESC , LWSTR (& attrs -> proxy_port ));
1360+ } else {
1361+ n = 0 ;
1362+ }
1363+ if (cnt <= 0 || n < 0 ) {
1364+ ERRNH (dbc , "failed to print proxy URL out of type: `" LWPDL "`, "
1365+ "host: `" LWPDL "` and port: `" LWPDL "`." ,
1366+ LWSTR (& attrs -> proxy_type ), LWSTR (& attrs -> proxy_host ),
1367+ LWSTR (& attrs -> proxy_port ));
1368+ SET_HDIAG (dbc , SQL_STATE_HY000 , "printing proxy URL failed" , 0 );
1369+ goto err ;
1370+ } else {
1371+ url .cnt = cnt + n ;
1372+ }
1373+ if (! wstr_to_utf8 (& url , & dbc -> proxy_url )) {
1374+ ERRH (dbc , "failed to convert URL `" LWPDL "` to UTF8." ,
1375+ LWSTR (& url ));
1376+ SET_HDIAG (dbc , SQL_STATE_HY000 , "proxy URL's UTF8 conversion "
1377+ "failed" , 0 );
1378+ goto err ;
1379+ }
1380+ INFOH (dbc , "proxy URL: `%s`." , dbc -> proxy_url .str );
1381+
1382+ if (wstr2bool (& attrs -> proxy_auth_enabled )) {
1383+ if (attrs -> proxy_auth_uid .cnt ) {
1384+ if (! wstr_to_utf8 (& attrs -> proxy_auth_uid , & dbc -> proxy_uid )) {
1385+ ERRH (dbc , "failed to convert proxy user ID `" LWPDL "` to"
1386+ " UTF8." , LWSTR (& attrs -> proxy_auth_uid ));
1387+ SET_HDIAG (dbc , SQL_STATE_HY000 , "proxy UID's UTF8 "
1388+ "conversion failed" , 0 );
1389+ goto err ;
1390+ }
1391+ INFOH (dbc , "proxy UID: `%s`." , dbc -> proxy_uid .str );
1392+
1393+ if (attrs -> proxy_auth_pwd .cnt ) {
1394+ if (! wstr_to_utf8 (& attrs -> proxy_auth_pwd ,
1395+ & dbc -> proxy_pwd )) {
1396+ ERRH (dbc , "failed to convert proxy password [%zu] `%s`"
1397+ " to UTF8" , attrs -> proxy_auth_pwd .cnt ,
1398+ ESODBC_PWD_VAL_SUBST );
1399+ SET_HDIAG (dbc , SQL_STATE_HY000 , "proxy password's "
1400+ "UTF8 conversion failed" , 0 );
1401+ goto err ;
1402+ }
1403+ /* indicates the presence of a non-empty password */
1404+ INFOH (dbc , "proxy PWD: " ESODBC_PWD_VAL_SUBST "." );
1405+ }
1406+ }
1407+ } else {
1408+ INFOH (dbc , "proxy authentication disabled." );
1409+ }
1410+ } else {
1411+ INFOH (dbc , "proxy disabled." );
1412+ }
1413+
13151414 /*
13161415 * set max body size
13171416 */
@@ -1513,6 +1612,21 @@ void cleanup_dbc(esodbc_dbc_st *dbc)
15131612 } else {
15141613 assert (dbc -> pwd .cnt == 0 );
15151614 }
1615+ if (dbc -> proxy_url .str ) {
1616+ free (dbc -> proxy_url .str );
1617+ dbc -> proxy_url .str = NULL ;
1618+ dbc -> proxy_url .cnt = 0 ;
1619+ }
1620+ if (dbc -> proxy_uid .str ) {
1621+ free (dbc -> proxy_uid .str );
1622+ dbc -> proxy_uid .str = NULL ;
1623+ dbc -> proxy_uid .cnt = 0 ;
1624+ }
1625+ if (dbc -> proxy_pwd .str ) {
1626+ free (dbc -> proxy_pwd .str );
1627+ dbc -> proxy_pwd .str = NULL ;
1628+ dbc -> proxy_pwd .cnt = 0 ;
1629+ }
15161630 if (dbc -> fetch .str ) {
15171631 free (dbc -> fetch .str );
15181632 dbc -> fetch .str = NULL ;
0 commit comments