Explorar o código

http_async_client: configure curl redirect per query

Federico Cabiddu %!s(int64=4) %!d(string=hai) anos
pai
achega
91c73d62bc

+ 3 - 0
src/modules/http_async_client/async_http.c

@@ -303,6 +303,7 @@ void notification_socket_cb(int fd, short event, void *arg)
 
 	memset(&query_params, 0, sizeof(http_m_params_t));
 	query_params.timeout = aq->query_params.timeout;
+	query_params.follow_redirect = aq->query_params.follow_redirect;
 	query_params.tls_verify_peer = aq->query_params.tls_verify_peer;
 	query_params.tls_verify_host = aq->query_params.tls_verify_host;
 	query_params.authmethod = aq->query_params.authmethod;
@@ -497,6 +498,7 @@ int async_send_query(sip_msg_t *msg, str *query, str *cbname)
 	aq->query_params.tls_verify_host = ah_params.tls_verify_host;
 	aq->query_params.suspend_transaction = suspend;
 	aq->query_params.timeout = ah_params.timeout;
+	aq->query_params.follow_redirect = ah_params.follow_redirect;
 	aq->query_params.tcp_keepalive = ah_params.tcp_keepalive;
 	aq->query_params.tcp_ka_idle = ah_params.tcp_ka_idle;
 	aq->query_params.tcp_ka_interval = ah_params.tcp_ka_interval;
@@ -644,6 +646,7 @@ void set_query_params(struct query_params *p) {
 	p->tls_verify_peer = tls_verify_peer;
 	p->suspend_transaction = 1;
 	p->timeout = http_timeout;
+	p->follow_redirect = curl_follow_redirect;
 	p->method = AH_METH_DEFAULT;
 	p->authmethod = default_authmethod;
 	p->tcp_keepalive = tcp_keepalive;

+ 1 - 0
src/modules/http_async_client/async_http.h

@@ -91,6 +91,7 @@ struct query_params {
 	unsigned int tls_verify_host:1;
 	unsigned int suspend_transaction:1; /* (create and) suspend the current transaction */
 	unsigned int call_route:1;          /* call script route on reply */
+	unsigned int follow_redirect:1; /* follow any Location header in a 3xx response */
 
 	unsigned int timeout;
 	struct header_list headers;

+ 1 - 0
src/modules/http_async_client/doc/http_async_client_admin.xml

@@ -482,6 +482,7 @@ xlog("L_INFO", "received reply for query $http_req_id\n");
 		<listitem><para><emphasis>tcp_keepalive</emphasis>: enable TCP keepalive</para></listitem>
 		<listitem><para><emphasis>tcp_ka_idle</emphasis>: set TCP keepalive idle time wait</para></listitem>
 		<listitem><para><emphasis>tcp_ka_interval</emphasis>: set TCP keepalive interval</para></listitem>
+		<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>
 	</itemizedlist>
 	<example>
 		<title><literal>$http_req(key)</literal> variable usage</title>

+ 1 - 0
src/modules/http_async_client/hm_hash.h

@@ -89,6 +89,7 @@ typedef struct hm_params {
 	str body;
 	
 	unsigned int authmethod;
+	unsigned int follow_redirect:1;
 	char* username;
 	char* password;
 	int tcp_keepalive;

+ 14 - 1
src/modules/http_async_client/http_async_client_mod.c

@@ -128,7 +128,7 @@ enum http_req_name_t {
 	E_HRN_TLS_CLIENT_CERT, E_HRN_SUSPEND,
 	E_HRN_BODY, E_HRN_AUTHMETHOD, E_HRN_USERNAME,
 	E_HRN_PASSWORD, E_HRN_TCP_KA, E_HRN_TCP_KA_IDLE,
-	E_HRN_TCP_KA_INTERVAL
+	E_HRN_TCP_KA_INTERVAL, E_HRN_FOLLOW_REDIRECT
 };
 
 enum http_time_name_t {
@@ -646,6 +646,8 @@ static int ah_parse_req_name(pv_spec_p sp, str *in) {
 		case 15:
 			if(strncmp(in->s, "tls_client_cert", 15)==0)
 				sp->pvp.pvn.u.isname.name.n = E_HRN_TLS_CLIENT_CERT;
+			else if(strncmp(in->s, "follow_redirect", 15)==0)
+				sp->pvp.pvn.u.isname.name.n = E_HRN_FOLLOW_REDIRECT;
 			else goto error;
 			break;
 		default:
@@ -875,6 +877,17 @@ static int ah_set_req(struct sip_msg* msg, pv_param_t *param,
 			ah_params.tcp_ka_interval = tcp_ka_interval;
 		}
 		break;
+  case E_HRN_FOLLOW_REDIRECT:
+		if (tval) {
+			if (!(tval->flags & PV_VAL_INT)) {
+				LM_ERR("invalid value type for $http_req(follow_redirect)\n");
+				return -1;
+			}
+			ah_params.follow_redirect = tval->ri?1:0;
+		} else {
+			ah_params.follow_redirect = curl_follow_redirect;
+		}
+		break;
 	}
 
 	return 1;

+ 2 - 2
src/modules/http_async_client/http_multi.c

@@ -462,8 +462,8 @@ int new_request(str *query, http_m_params_t *query_params, http_multi_cbe_t cb,
 		curl_easy_setopt(cell->easy, CURLOPT_VERBOSE, 1L);
 		curl_easy_setopt(cell->easy, CURLOPT_DEBUGFUNCTION, debug_cb);
 	}
-	if (curl_follow_redirect) {
-                curl_easy_setopt(cell->easy, CURLOPT_FOLLOWLOCATION, 1L);
+	if (cell->params.follow_redirect) {
+		curl_easy_setopt(cell->easy, CURLOPT_FOLLOWLOCATION, 1L);
 	}
 	curl_easy_setopt(cell->easy, CURLOPT_ERRORBUFFER, cell->error);
 	curl_easy_setopt(cell->easy, CURLOPT_PRIVATE, cell);