<p>Igor Goncharovsky has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/15761">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_pjsip_header_funcs: Add PJSIP_HEADERS() ability to read header by pattern<br><br>PJSIP currently does not provide a function to replace SIP_HEADERS() function to get a list of headers from INVITE request.<br>It may be used to get all X- headers in case the actual set and names of headers unknown.<br><br>ASTERISK-29389<br><br>Change-Id: Ic09d395de71a0021e0d6c5c29e1e19d689079f8b<br>---<br>M res/res_pjsip_header_funcs.c<br>1 file changed, 170 insertions(+), 7 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/61/15761/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_pjsip_header_funcs.c b/res/res_pjsip_header_funcs.c</span><br><span>index 798a1cd..44c8044 100644</span><br><span>--- a/res/res_pjsip_header_funcs.c</span><br><span>+++ b/res/res_pjsip_header_funcs.c</span><br><span>@@ -46,7 +46,10 @@</span><br><span> <parameter name="action" required="true"></span><br><span> <enumlist></span><br><span> <enum name="read"><para>Returns instance <replaceable>number</replaceable></span><br><span style="color: hsl(0, 100%, 40%);">- of header <replaceable>name</replaceable>.</para></enum></span><br><span style="color: hsl(120, 100%, 40%);">+ of header <replaceable>name</replaceable>. A <literal>*</literal></span><br><span style="color: hsl(120, 100%, 40%);">+ may be appended to <replaceable>name</replaceable> to iterate over all </span><br><span style="color: hsl(120, 100%, 40%);">+ headers <emphasis>beginning with</emphasis> <replaceable>name</replaceable>.</span><br><span style="color: hsl(120, 100%, 40%);">+ </para></enum></span><br><span> </span><br><span> <enum name="add"><para>Adds a new header <replaceable>name</replaceable></span><br><span> to this session.</para></enum></span><br><span>@@ -88,6 +91,9 @@</span><br><span> <para>; Set 'via2' to the value of the 2nd 'Via' header.</para></span><br><span> <para>exten => 1,1,Set(via2=${PJSIP_HEADER(read,Via,2)})</para></span><br><span> <para>;</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>; Set 'xhdr' to the value of the 1sx X-header.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>exten => 1,1,Set(xhdr=${PJSIP_HEADER(read,X-*,1)})</para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>;</para></span><br><span> <para>; Add an 'X-Myheader' header with the value of 'myvalue'.</para></span><br><span> <para>exten => 1,1,Set(PJSIP_HEADER(add,X-MyHeader)=myvalue)</para></span><br><span> <para>;</para></span><br><span>@@ -139,6 +145,29 @@</span><br><span> </note></span><br><span> </description></span><br><span> </function></span><br><span style="color: hsl(120, 100%, 40%);">+ <function name="PJSIP_HEADERS" language="en_US"></span><br><span style="color: hsl(120, 100%, 40%);">+ <synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ Gets the list of SIP header names from an INVITE message.</span><br><span style="color: hsl(120, 100%, 40%);">+ </synopsis></span><br><span style="color: hsl(120, 100%, 40%);">+ <syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <parameter name="prefix"></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>If specified, only the headers matching the given prefix are returned.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </parameter></span><br><span style="color: hsl(120, 100%, 40%);">+ </syntax></span><br><span style="color: hsl(120, 100%, 40%);">+ <description></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>Returns a comma-separated list of header names (without values) from the</span><br><span style="color: hsl(120, 100%, 40%);">+ INVITE message. Multiple headers with the same name are included in the list only once.</span><br><span style="color: hsl(120, 100%, 40%);">+ </para></span><br><span style="color: hsl(120, 100%, 40%);">+ <para>For example, <literal>${PJSIP_HEADERS(Co)}</literal> might return</span><br><span style="color: hsl(120, 100%, 40%);">+ <literal>Contact,Content-Length,Content-Type</literal>. As a practical example,</span><br><span style="color: hsl(120, 100%, 40%);">+ you may use <literal>${PJSIP_HEADERS(X-)}</literal> to enumerate optional extended</span><br><span style="color: hsl(120, 100%, 40%);">+ headers.</para></span><br><span style="color: hsl(120, 100%, 40%);">+ </description></span><br><span style="color: hsl(120, 100%, 40%);">+ <see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ <ref type="function">PJSIP_HEADER</ref></span><br><span style="color: hsl(120, 100%, 40%);">+ </see-also></span><br><span style="color: hsl(120, 100%, 40%);">+ </function></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> ***/</span><br><span> </span><br><span> /*! \brief Linked list for accumulating headers */</span><br><span>@@ -240,6 +269,87 @@</span><br><span> return hdr;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Implements PJSIP_HEADERS by searching for the requested header prefix.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Retrieve the header_datastore.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Search for the all matching headers.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Validate the pjsip_hdr found.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Parse pjsip_hdr into a name and copy to the buffer.</span><br><span style="color: hsl(120, 100%, 40%);">+ * Return the value.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int read_headers(void *obj)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct header_data *data = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t len = strlen(data->header_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ pjsip_hdr *hdr = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *pj_hdr_string;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t pj_hdr_string_len;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *p;</span><br><span style="color: hsl(120, 100%, 40%);">+ char *pos;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t plen, wlen = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct hdr_list_entry *le;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct hdr_list *list;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_datastore *, datastore,</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_sip_session_get_datastore(data->channel->session, header_datastore.type),</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!datastore || !datastore->data) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "There was no datastore from which to read headers.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ list = datastore->data;</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_hdr_string = ast_alloca(data->len);</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(list, le, nextptr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pj_strnicmp2(&le->hdr->name, data->header_name, len) == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ /* Found matched header, append to buf */</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr = le->hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+ </span><br><span style="color: hsl(120, 100%, 40%);">+ pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len);</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_hdr_string[pj_hdr_string_len] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ p = strchr(pj_hdr_string, ':');</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!p) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_ERROR,</span><br><span style="color: hsl(120, 100%, 40%);">+ "A malformed header was returned from pjsip_hdr_print_on.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ pj_hdr_string[p - pj_hdr_string] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ p = ast_strip(pj_hdr_string);</span><br><span style="color: hsl(120, 100%, 40%);">+ plen = strlen(p);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (wlen + plen + 1 > data->len) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_ERROR,</span><br><span style="color: hsl(120, 100%, 40%);">+ "Buffer isn't big enough to hold header value. %zu > %zu\n", plen + 1,</span><br><span style="color: hsl(120, 100%, 40%);">+ data->len);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ pos = strstr(data->buf, p);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pos && pos[1] == ',') {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pos == data->buf) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (pos[-1] == ',') {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(data->buf + wlen, p, data->len);</span><br><span style="color: hsl(120, 100%, 40%);">+ wlen += plen;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_copy_string(data->buf + wlen, ",", data->len);</span><br><span style="color: hsl(120, 100%, 40%);">+ wlen++;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!hdr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_debug(1, "There was no header named %s.\n", data->header_name);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ data->buf[wlen-1] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> </span><br><span> /*!</span><br><span> * \internal</span><br><span>@@ -254,11 +364,15 @@</span><br><span> static int read_header(void *obj)</span><br><span> {</span><br><span> struct header_data *data = obj;</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t len = strlen(data->header_name);</span><br><span> pjsip_hdr *hdr = NULL;</span><br><span> char *pj_hdr_string;</span><br><span> size_t pj_hdr_string_len;</span><br><span> char *p;</span><br><span> size_t plen;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct hdr_list_entry *le;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct hdr_list *list;</span><br><span style="color: hsl(120, 100%, 40%);">+ int i = 1;</span><br><span> RAII_VAR(struct ast_datastore *, datastore,</span><br><span> ast_sip_session_get_datastore(data->channel->session, header_datastore.type),</span><br><span> ao2_cleanup);</span><br><span>@@ -268,8 +382,20 @@</span><br><span> return -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- hdr = find_header((struct hdr_list *) datastore->data, data->header_name,</span><br><span style="color: hsl(0, 100%, 40%);">- data->header_number);</span><br><span style="color: hsl(120, 100%, 40%);">+ list = datastore->data;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_LIST_TRAVERSE(list, le, nextptr) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (data->header_name[len - 1] == '*') {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0 && i++ == data->header_number) {</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr = le->hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (pj_stricmp2(&le->hdr->name, data->header_name) == 0 && i++ == data->header_number) {</span><br><span style="color: hsl(120, 100%, 40%);">+ hdr = le->hdr;</span><br><span style="color: hsl(120, 100%, 40%);">+ break;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span> </span><br><span> if (!hdr) {</span><br><span> ast_debug(1, "There was no header named %s.\n", data->header_name);</span><br><span>@@ -435,12 +561,43 @@</span><br><span> }</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Read list of unique SIP headers</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static int func_read_headers(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct header_data header_data;</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_DECLARE_APP_ARGS(args,</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_APP_ARG(header_pattern);</span><br><span style="color: hsl(120, 100%, 40%);">+ );</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_STANDARD_APP_ARGS(args, data);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!chan || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_strlen_zero(args.header_pattern)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(AST_LOG_ERROR, "This function requires a pattern.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ header_data.channel = channel;</span><br><span style="color: hsl(120, 100%, 40%);">+ header_data.header_name = args.header_pattern;</span><br><span style="color: hsl(120, 100%, 40%);">+ header_data.header_value = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ header_data.buf = buf;</span><br><span style="color: hsl(120, 100%, 40%);">+ header_data.len = len;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_sip_push_task_wait_serializer(channel->session->serializer, read_headers, &header_data);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Implements function 'read' callback.</span><br><span> *</span><br><span> * Valid actions are 'read' and 'remove'.</span><br><span> */</span><br><span style="color: hsl(0, 100%, 40%);">-static int func_read_header(struct ast_channel *chan, const char *function, char *data,</span><br><span style="color: hsl(0, 100%, 40%);">- char *buf, size_t len)</span><br><span style="color: hsl(120, 100%, 40%);">+static int func_read_header(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)</span><br><span> {</span><br><span> struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;</span><br><span> struct header_data header_data;</span><br><span>@@ -480,8 +637,7 @@</span><br><span> header_data.len = len;</span><br><span> </span><br><span> if (!strcasecmp(args.action, "read")) {</span><br><span style="color: hsl(0, 100%, 40%);">- return ast_sip_push_task_wait_serializer(channel->session->serializer,</span><br><span style="color: hsl(0, 100%, 40%);">- read_header, &header_data);</span><br><span style="color: hsl(120, 100%, 40%);">+ return ast_sip_push_task_wait_serializer(channel->session->serializer, read_header, &header_data);</span><br><span> } else if (!strcasecmp(args.action, "remove")) {</span><br><span> return ast_sip_push_task_wait_serializer(channel->session->serializer,</span><br><span> remove_header, &header_data);</span><br><span>@@ -561,6 +717,11 @@</span><br><span> .write = func_write_header,</span><br><span> };</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_custom_function pjsip_headers_function = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = "PJSIP_HEADERS",</span><br><span style="color: hsl(120, 100%, 40%);">+ .read = func_read_headers</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*!</span><br><span> * \internal</span><br><span> * \brief Session supplement callback for outgoing INVITE requests</span><br><span>@@ -604,6 +765,7 @@</span><br><span> {</span><br><span> ast_sip_session_register_supplement(&header_funcs_supplement);</span><br><span> ast_custom_function_register(&pjsip_header_function);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_custom_function_register(&pjsip_headers_function);</span><br><span> </span><br><span> return AST_MODULE_LOAD_SUCCESS;</span><br><span> }</span><br><span>@@ -611,6 +773,7 @@</span><br><span> static int unload_module(void)</span><br><span> {</span><br><span> ast_custom_function_unregister(&pjsip_header_function);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_custom_function_unregister(&pjsip_headers_function);</span><br><span> ast_sip_session_unregister_supplement(&header_funcs_supplement);</span><br><span> return 0;</span><br><span> }</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/15761">change 15761</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/15761"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Ic09d395de71a0021e0d6c5c29e1e19d689079f8b </div>
<div style="display:none"> Gerrit-Change-Number: 15761 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Igor Goncharovsky <igor.goncharovsky@gmail.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>