Browse code

http_async_client: configure curl redirect per query

Federico Cabiddu authored on 30/03/2021 12:39:16
Showing 6 changed files
... ...
@@ -303,6 +303,7 @@ void notification_socket_cb(int fd, short event, void *arg)
303 303
 
304 304
 	memset(&query_params, 0, sizeof(http_m_params_t));
305 305
 	query_params.timeout = aq->query_params.timeout;
306
+	query_params.follow_redirect = aq->query_params.follow_redirect;
306 307
 	query_params.tls_verify_peer = aq->query_params.tls_verify_peer;
307 308
 	query_params.tls_verify_host = aq->query_params.tls_verify_host;
308 309
 	query_params.authmethod = aq->query_params.authmethod;
... ...
@@ -497,6 +498,7 @@ int async_send_query(sip_msg_t *msg, str *query, str *cbname)
497 498
 	aq->query_params.tls_verify_host = ah_params.tls_verify_host;
498 499
 	aq->query_params.suspend_transaction = suspend;
499 500
 	aq->query_params.timeout = ah_params.timeout;
501
+	aq->query_params.follow_redirect = ah_params.follow_redirect;
500 502
 	aq->query_params.tcp_keepalive = ah_params.tcp_keepalive;
501 503
 	aq->query_params.tcp_ka_idle = ah_params.tcp_ka_idle;
502 504
 	aq->query_params.tcp_ka_interval = ah_params.tcp_ka_interval;
... ...
@@ -644,6 +646,7 @@ void set_query_params(struct query_params *p) {
644 646
 	p->tls_verify_peer = tls_verify_peer;
645 647
 	p->suspend_transaction = 1;
646 648
 	p->timeout = http_timeout;
649
+	p->follow_redirect = curl_follow_redirect;
647 650
 	p->method = AH_METH_DEFAULT;
648 651
 	p->authmethod = default_authmethod;
649 652
 	p->tcp_keepalive = tcp_keepalive;
... ...
@@ -91,6 +91,7 @@ struct query_params {
91 91
 	unsigned int tls_verify_host:1;
92 92
 	unsigned int suspend_transaction:1; /* (create and) suspend the current transaction */
93 93
 	unsigned int call_route:1;          /* call script route on reply */
94
+	unsigned int follow_redirect:1; /* follow any Location header in a 3xx response */
94 95
 
95 96
 	unsigned int timeout;
96 97
 	struct header_list headers;
... ...
@@ -482,6 +482,7 @@ xlog("L_INFO", "received reply for query $http_req_id\n");
482 482
 		<listitem><para><emphasis>tcp_keepalive</emphasis>: enable TCP keepalive</para></listitem>
483 483
 		<listitem><para><emphasis>tcp_ka_idle</emphasis>: set TCP keepalive idle time wait</para></listitem>
484 484
 		<listitem><para><emphasis>tcp_ka_interval</emphasis>: set TCP keepalive interval</para></listitem>
485
+		<listitem><para><emphasis>follow_redirect</emphasis>: if defined to a non-zero value, will tell curl to follow HTTP 3xx redirects. Defaults to curl_follow_redirect global parameter.</para></listitem>
485 486
 	</itemizedlist>
486 487
 	<example>
487 488
 		<title><literal>$http_req(key)</literal> variable usage</title>
... ...
@@ -89,6 +89,7 @@ typedef struct hm_params {
89 89
 	str body;
90 90
 	
91 91
 	unsigned int authmethod;
92
+	unsigned int follow_redirect:1;
92 93
 	char* username;
93 94
 	char* password;
94 95
 	int tcp_keepalive;
... ...
@@ -128,7 +128,7 @@ enum http_req_name_t {
128 128
 	E_HRN_TLS_CLIENT_CERT, E_HRN_SUSPEND,
129 129
 	E_HRN_BODY, E_HRN_AUTHMETHOD, E_HRN_USERNAME,
130 130
 	E_HRN_PASSWORD, E_HRN_TCP_KA, E_HRN_TCP_KA_IDLE,
131
-	E_HRN_TCP_KA_INTERVAL
131
+	E_HRN_TCP_KA_INTERVAL, E_HRN_FOLLOW_REDIRECT
132 132
 };
133 133
 
134 134
 enum http_time_name_t {
... ...
@@ -646,6 +646,8 @@ static int ah_parse_req_name(pv_spec_p sp, str *in) {
646 646
 		case 15:
647 647
 			if(strncmp(in->s, "tls_client_cert", 15)==0)
648 648
 				sp->pvp.pvn.u.isname.name.n = E_HRN_TLS_CLIENT_CERT;
649
+			else if(strncmp(in->s, "follow_redirect", 15)==0)
650
+				sp->pvp.pvn.u.isname.name.n = E_HRN_FOLLOW_REDIRECT;
649 651
 			else goto error;
650 652
 			break;
651 653
 		default:
... ...
@@ -875,6 +877,17 @@ static int ah_set_req(struct sip_msg* msg, pv_param_t *param,
875 877
 			ah_params.tcp_ka_interval = tcp_ka_interval;
876 878
 		}
877 879
 		break;
880
+  case E_HRN_FOLLOW_REDIRECT:
881
+		if (tval) {
882
+			if (!(tval->flags & PV_VAL_INT)) {
883
+				LM_ERR("invalid value type for $http_req(follow_redirect)\n");
884
+				return -1;
885
+			}
886
+			ah_params.follow_redirect = tval->ri?1:0;
887
+		} else {
888
+			ah_params.follow_redirect = curl_follow_redirect;
889
+		}
890
+		break;
878 891
 	}
879 892
 
880 893
 	return 1;
... ...
@@ -462,8 +462,8 @@ int new_request(str *query, http_m_params_t *query_params, http_multi_cbe_t cb,
462 462
 		curl_easy_setopt(cell->easy, CURLOPT_VERBOSE, 1L);
463 463
 		curl_easy_setopt(cell->easy, CURLOPT_DEBUGFUNCTION, debug_cb);
464 464
 	}
465
-	if (curl_follow_redirect) {
466
-                curl_easy_setopt(cell->easy, CURLOPT_FOLLOWLOCATION, 1L);
465
+	if (cell->params.follow_redirect) {
466
+		curl_easy_setopt(cell->easy, CURLOPT_FOLLOWLOCATION, 1L);
467 467
 	}
468 468
 	curl_easy_setopt(cell->easy, CURLOPT_ERRORBUFFER, cell->error);
469 469
 	curl_easy_setopt(cell->easy, CURLOPT_PRIVATE, cell);