Selaa lähdekoodia

dmq: add content-type header

Charles Chance 12 vuotta sitten
vanhempi
commit
3f6445f462

+ 2 - 2
modules/dmq/bind_dmq.h

@@ -31,9 +31,9 @@
 #include "dmq_funcs.h"
 
 typedef int (*bcast_message_t)(dmq_peer_t* peer, str* body, dmq_node_t* except,
-		dmq_resp_cback_t* resp_cback, int max_forwards);
+		dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type);
 typedef int (*send_message_t)(dmq_peer_t* peer, str* body, dmq_node_t* node,
-		dmq_resp_cback_t* resp_cback, int max_forwards);
+		dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type);
 
 typedef struct dmq_api {
 	register_dmq_peer_t register_dmq_peer;

+ 1 - 1
modules/dmq/dmq.c

@@ -91,7 +91,7 @@ static int parse_server_address(str* uri, struct sip_uri* parsed_uri);
 static cmd_export_t cmds[] = {
 	{"dmq_handle_message",  (cmd_function)dmq_handle_message, 0, handle_dmq_fixup, 0, 
 		REQUEST_ROUTE},
-	{"dmq_send_message", (cmd_function)cfg_dmq_send_message, 3, send_dmq_fixup, 0,
+	{"dmq_send_message", (cmd_function)cfg_dmq_send_message, 4, send_dmq_fixup, 0,
 		ANY_ROUTE},
         {"bind_dmq",        (cmd_function)bind_dmq,       0, 0,              0},
 	{0, 0, 0, 0, 0, 0}

+ 22 - 11
modules/dmq/dmq_funcs.c

@@ -122,7 +122,7 @@ int build_uri_str(str* username, struct sip_uri* uri, str* from)
  * resp_cback - a response callback that gets called when the transaction is complete
  */
 int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except,
-		dmq_resp_cback_t* resp_cback, int max_forwards)
+		dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type)
 {
 	dmq_node_t* node;
 	
@@ -140,7 +140,7 @@ int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except,
 			node = node->next;
 			continue;
 		}
-		if(dmq_send_message(peer, body, node, resp_cback, max_forwards) < 0) {
+		if(dmq_send_message(peer, body, node, resp_cback, max_forwards, content_type) < 0) {
 			LM_ERR("error sending dmq message\n");
 			goto error;
 		}
@@ -162,7 +162,7 @@ error:
  * resp_cback - a response callback that gets called when the transaction is complete
  */
 int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node,
-		dmq_resp_cback_t* resp_cback, int max_forwards)
+		dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type)
 {
 	uac_req_t uac_r;
 	str str_hdr = {0, 0};
@@ -171,14 +171,18 @@ int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node,
 	int result = 0;
 	int len = 0;
 	
-	/* Max-Forwards */
-	str_hdr.len = 20 + CRLF_LEN;
+	if (!content_type) {
+		LM_ERR("content-type is null\n");
+		return -1;
+	}
+	/* add Max-Forwards and Content-Type headers */
+	str_hdr.len = 34 + content_type->len + (CRLF_LEN*2);
 	str_hdr.s = pkg_malloc(str_hdr.len);
 	if(str_hdr.s==NULL) {
 		LM_ERR("no more pkg\n");
 		return -1;
 	}
-	len += sprintf(str_hdr.s, "Max-Forwards: %d%s", max_forwards, CRLF);
+	len += sprintf(str_hdr.s, "Max-Forwards: %d" CRLF "Content-Type: %.*s" CRLF, max_forwards, content_type->len, content_type->s);
 	str_hdr.len = len;
 	
 	cb_param = shm_malloc(sizeof(*cb_param));
@@ -216,11 +220,12 @@ error:
 /**
  * @brief config file function for sending dmq message
  */
-int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body)
+int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body, char* content_type)
 {
 	str peer_str;
 	str to_str;
 	str body_str;
+	str ct_str;
 	
 	if(get_str_fparam(&peer_str, msg, (fparam_t*)peer)<0) {
 		LM_ERR("cannot get peer value\n");
@@ -234,11 +239,17 @@ int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body)
 		LM_ERR("cannot get body value\n");
 		return -1;
 	}
+	if(get_str_fparam(&ct_str, msg, (fparam_t*)content_type)<0) {
+		LM_ERR("cannot get content-type value\n");
+		return -1;
+	}
+
 	
-	LM_INFO("cfg_dmq_send_message: %.*s - %.*s - %.*s\n",
+	LM_INFO("cfg_dmq_send_message: %.*s - %.*s - %.*s - %.*s\n",
 		peer_str.len, peer_str.s,
 		to_str.len, to_str.s,
-		body_str.len, body_str.s);
+		body_str.len, body_str.s,
+		ct_str.len, ct_str.s);
 	
 	dmq_peer_t* destination_peer = find_peer(peer_str);
 	if(!destination_peer) {
@@ -260,7 +271,7 @@ int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to, char* body)
 		goto error;
 	}
 	if(dmq_send_message(destination_peer, &body_str, to_dmq_node,
-				&notification_callback, 1) < 0) {
+				&notification_callback, 1, &ct_str) < 0) {
 		LM_ERR("cannot send dmq message\n");
 		goto error;
 	}
@@ -284,7 +295,7 @@ void ping_servers(unsigned int ticks, void *param) {
 	LM_DBG("ping_servers\n");
 	body = build_notification_body();
 	ret = bcast_dmq_message(dmq_notification_peer, body, notification_node,
-			&notification_callback, 1);
+			&notification_callback, 1, &notification_content_type);
 	pkg_free(body->s);
 	pkg_free(body);
 	if(ret < 0) {

+ 3 - 3
modules/dmq/dmq_funcs.h

@@ -47,12 +47,12 @@ typedef struct dmq_cback_param {
 } dmq_cback_param_t;
 
 int cfg_dmq_send_message(struct sip_msg* msg, char* peer, char* to,
-		char* body);
+		char* body, char* content_type);
 dmq_peer_t* register_dmq_peer(dmq_peer_t* peer);
 int dmq_send_message(dmq_peer_t* peer, str* body, dmq_node_t* node,
-		dmq_resp_cback_t* resp_cback, int max_forwards);
+		dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type);
 int bcast_dmq_message(dmq_peer_t* peer, str* body, dmq_node_t* except,
-		dmq_resp_cback_t* resp_cback, int max_forwards);
+		dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type);
 
 #endif
 

+ 7 - 2
modules/dmq/doc/dmq_admin.xml

@@ -165,7 +165,7 @@ modparam("dmq", "ping_interval", 90)
         </section>
         <section>
                 <title>
-               	<function moreinfo="none">dmq_send_message(peer, node, body)</function>
+               	<function moreinfo="none">dmq_send_message(peer, node, body, content_type)</function>
                 </title>
                 <para>
                 Sends a DMQ message directly from config file.
@@ -187,6 +187,11 @@ modparam("dmq", "ping_interval", 90)
 	                        <emphasis>body</emphasis> - the message body.
 	                        </para>
 	                </listitem>
+                	<listitem>
+                        	<para>
+	                        <emphasis>content_type</emphasis> - the MIME type of the message body.
+	                        </para>
+	                </listitem>
 		</itemizedlist>
                 <para>
                 This function can be used from any route.
@@ -196,7 +201,7 @@ modparam("dmq", "ping_interval", 90)
                 <title><function>dmq_send_message</function> usage</title>
                 <programlisting format="linespecific">
 ...
-	dmq_send_message("peer_name", "sip:10.0.0.21:5060", "Message body...\n");
+	dmq_send_message("peer_name", "sip:10.0.0.21:5060", "Message body...", "text/plain");
 ...
 </programlisting>
                 </example>

+ 2 - 2
modules/dmq/doc/dmq_devel.xml

@@ -60,7 +60,7 @@ typedef struct dmq_api {
         <section>
                 <title>
                 <function moreinfo="none">bcast_message(dmq_peer_t* peer, str* body, dmq_node_t* except,
-                dmq_resp_cback_t* resp_cback, int max_forwards)</function>
+                dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type)</function>
                 </title>
                 <para>
                 Broadcast a DMQ message to all nodes in the DMQ bus excluding self, 
@@ -80,7 +80,7 @@ typedef struct dmq_api {
         <section>
                 <title>
                 <function moreinfo="none">send_message(dmq_peer_t* peer, str* body, dmq_node_t* node,
-                dmq_resp_cback_t* resp_cback, int max_forwards)</function>
+                dmq_resp_cback_t* resp_cback, int max_forwards, str* content_type)</function>
                 </title>
                 <para>
                 Send a DMQ message to a single node.

+ 3 - 3
modules/dmq/notification_peer.c

@@ -26,7 +26,7 @@
 
 #include "notification_peer.h"
 
-static str notification_content_type = str_init("text/plain");
+str notification_content_type = str_init("text/plain");
 dmq_resp_cback_t notification_callback = {&notification_resp_callback_f, 0};
 
 /**
@@ -191,7 +191,7 @@ int dmq_notification_callback(struct sip_msg* msg, peer_reponse_t* resp)
 	if(nodes_recv > 0 && maxforwards > 0) {
 		/* maxforwards is set to 0 so that the message is will not be in a spiral */
 		bcast_dmq_message(dmq_notification_peer, response_body, 0,
-				&notification_callback, maxforwards);
+				&notification_callback, maxforwards, &notification_content_type);
 	}
 	LM_DBG("broadcasted message\n");
 	pkg_free(response_body);
@@ -267,7 +267,7 @@ int request_nodelist(dmq_node_t* node, int forward)
 		return -1;
 	}
 	ret = dmq_send_message(dmq_notification_peer, body, node,
-			&notification_callback, forward);
+			&notification_callback, forward, &notification_content_type);
 	pkg_free(body->s);
 	pkg_free(body);
 	return ret;

+ 2 - 0
modules/dmq/notification_peer.h

@@ -33,6 +33,8 @@
 #include "peer.h"
 #include "dmq_funcs.h"
 
+extern str notification_content_type;
+
 int add_notification_peer();
 int dmq_notification_callback(struct sip_msg* msg, peer_reponse_t* resp);
 int extract_node_list(dmq_node_list_t* update_list, struct sip_msg* msg);