<p>Jaco Kroon has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10000">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">AMI: be less verbose when adding HTTP headers to AMI/HTTP messages.<br><br>All HTTP/AMI message headers are being sent to the verbose channel.<br>There are multiple places this is happening. Consolidate the loop into<br>a function. Drop the debug/verbose message.<br><br>Convert to using ast_asprintf to perform the length calculation, memory<br>allocation and snprintf all in one step.<br><br>Change-Id: Ic45e673fde05bd544be95ad5cdbc69518207c1a1<br>---<br>M main/manager.c<br>1 file changed, 34 insertions(+), 42 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/00/10000/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/manager.c b/main/manager.c</span><br><span>index a0dcf6c..2aa0989 100644</span><br><span>--- a/main/manager.c</span><br><span>+++ b/main/manager.c</span><br><span>@@ -2804,6 +2804,34 @@</span><br><span> }</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Append additional headers into the message structure from params.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note You likely want to initialize m->hdrcount to 0 before calling this.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void astman_append_headers(struct message *m, const struct ast_variable *params)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ const struct ast_variable *v;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (v = params; v && m->hdrcount < ARRAY_LEN(m->headers); v = v->next) {</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_asprintf((char**)&m->headers[m->hdrcount], "%s: %s", v->name, v->value) < 0)</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ ++m->hdrcount;</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 style="color: hsl(120, 100%, 40%);">+ * \brief Free headers inside message structure, but not the message structure itself.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void astman_free_headers(struct message *m)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ while (m->hdrcount) {</span><br><span style="color: hsl(120, 100%, 40%);">+ --m->hdrcount;</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free((void *) m->headers[m->hdrcount]);</span><br><span style="color: hsl(120, 100%, 40%);">+ m->headers[m->hdrcount] = NULL;</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> * \internal</span><br><span> * \brief Process one "Variable:" header value string.</span><br><span> *</span><br><span>@@ -6658,7 +6686,6 @@</span><br><span> struct message m = { 0 };</span><br><span> char header_buf[sizeof(s->session->inbuf)] = { '\0' };</span><br><span> int res;</span><br><span style="color: hsl(0, 100%, 40%);">- int idx;</span><br><span> int hdr_loss;</span><br><span> time_t now;</span><br><span> </span><br><span>@@ -6726,10 +6753,8 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Free AMI request headers. */</span><br><span style="color: hsl(0, 100%, 40%);">- for (idx = 0; idx < m.hdrcount; ++idx) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free((void *) m.headers[idx]);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_free_headers(&m);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> return res;</span><br><span> }</span><br><span> </span><br><span>@@ -7723,13 +7748,10 @@</span><br><span> uint32_t ident;</span><br><span> int fd;</span><br><span> int blastaway = 0;</span><br><span style="color: hsl(0, 100%, 40%);">- struct ast_variable *v;</span><br><span> struct ast_variable *params = get_params;</span><br><span> char template[] = "/tmp/ast-http-XXXXXX"; /* template for temporary file */</span><br><span> struct ast_str *http_header = NULL, *out = NULL;</span><br><span> struct message m = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int idx;</span><br><span style="color: hsl(0, 100%, 40%);">- size_t hdrlen;</span><br><span> </span><br><span> if (method != AST_HTTP_GET && method != AST_HTTP_HEAD && method != AST_HTTP_POST) {</span><br><span> ast_http_error(ser, 501, "Not Implemented", "Attempt to use unimplemented / unsupported method");</span><br><span>@@ -7812,17 +7834,7 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- for (v = params; v && m.hdrcount < ARRAY_LEN(m.headers); v = v->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- hdrlen = strlen(v->name) + strlen(v->value) + 3;</span><br><span style="color: hsl(0, 100%, 40%);">- m.headers[m.hdrcount] = ast_malloc(hdrlen);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!m.headers[m.hdrcount]) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Allocation failure */</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_debug(1, "HTTP Manager add header %s\n", m.headers[m.hdrcount]);</span><br><span style="color: hsl(0, 100%, 40%);">- ++m.hdrcount;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_append_headers(&m, params);</span><br><span> </span><br><span> if (process_message(&s, &m)) {</span><br><span> if (session->authenticated) {</span><br><span>@@ -7837,11 +7849,7 @@</span><br><span> session->needdestroy = 1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Free request headers. */</span><br><span style="color: hsl(0, 100%, 40%);">- for (idx = 0; idx < m.hdrcount; ++idx) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free((void *) m.headers[idx]);</span><br><span style="color: hsl(0, 100%, 40%);">- m.headers[idx] = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_free_headers(&m);</span><br><span> </span><br><span> ast_str_append(&http_header, 0,</span><br><span> "Content-type: text/%s\r\n"</span><br><span>@@ -7952,8 +7960,6 @@</span><br><span> struct ast_str *http_header = NULL, *out = NULL;</span><br><span> size_t result_size;</span><br><span> struct message m = { 0 };</span><br><span style="color: hsl(0, 100%, 40%);">- unsigned int idx;</span><br><span style="color: hsl(0, 100%, 40%);">- size_t hdrlen;</span><br><span> int fd;</span><br><span> </span><br><span> time_t time_now = time(NULL);</span><br><span>@@ -8176,17 +8182,7 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- for (v = params; v && m.hdrcount < ARRAY_LEN(m.headers); v = v->next) {</span><br><span style="color: hsl(0, 100%, 40%);">- hdrlen = strlen(v->name) + strlen(v->value) + 3;</span><br><span style="color: hsl(0, 100%, 40%);">- m.headers[m.hdrcount] = ast_malloc(hdrlen);</span><br><span style="color: hsl(0, 100%, 40%);">- if (!m.headers[m.hdrcount]) {</span><br><span style="color: hsl(0, 100%, 40%);">- /* Allocation failure */</span><br><span style="color: hsl(0, 100%, 40%);">- continue;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- snprintf((char *) m.headers[m.hdrcount], hdrlen, "%s: %s", v->name, v->value);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_verb(4, "HTTP Manager add header %s\n", m.headers[m.hdrcount]);</span><br><span style="color: hsl(0, 100%, 40%);">- ++m.hdrcount;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_append_headers(&m, params);</span><br><span> </span><br><span> if (process_message(&s, &m)) {</span><br><span> if (u_displayconnects) {</span><br><span>@@ -8196,11 +8192,7 @@</span><br><span> session->needdestroy = 1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- /* Free request headers. */</span><br><span style="color: hsl(0, 100%, 40%);">- for (idx = 0; idx < m.hdrcount; ++idx) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_free((void *) m.headers[idx]);</span><br><span style="color: hsl(0, 100%, 40%);">- m.headers[idx] = NULL;</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_free_headers(&m);</span><br><span> </span><br><span> result_size = lseek(ast_iostream_get_fd(s.stream), 0, SEEK_CUR); /* Calculate approx. size of result */</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10000">change 10000</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/10000"/><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-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ic45e673fde05bd544be95ad5cdbc69518207c1a1 </div>
<div style="display:none"> Gerrit-Change-Number: 10000 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Jaco Kroon <jaco@uls.co.za> </div>