<p>Joshua Colp <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/9970">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Sean Bright: Looks good to me, but someone else must approve
Richard Mudgett: Looks good to me, but someone else must approve
George Joseph: Looks good to me, approved
Joshua Colp: Approved for Submit
</div><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;"><span>diff --git a/main/manager.c b/main/manager.c</span><br><span>index b2d3c0e..e5ca571 100644</span><br><span>--- a/main/manager.c</span><br><span>+++ b/main/manager.c</span><br><span>@@ -2803,6 +2803,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) > -1) {</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%);">+/*!</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>@@ -6626,7 +6654,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>@@ -6694,10 +6721,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>@@ -7706,13 +7731,10 @@</span><br><span> struct mansession_session *session = NULL;</span><br><span> uint32_t ident;</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>@@ -7795,17 +7817,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>@@ -7820,11 +7832,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>@@ -7935,8 +7943,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> </span><br><span> time_t time_now = time(NULL);</span><br><span> unsigned long nonce = 0, nc;</span><br><span>@@ -8158,17 +8164,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>@@ -8178,11 +8174,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 = ftell(s.f); /* Calculate approx. size of result */</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/9970">change 9970</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/9970"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Ic45e673fde05bd544be95ad5cdbc69518207c1a1 </div>
<div style="display:none"> Gerrit-Change-Number: 9970 </div>
<div style="display:none"> Gerrit-PatchSet: 8 </div>
<div style="display:none"> Gerrit-Owner: Jaco Kroon <jaco@uls.co.za> </div>
<div style="display:none"> Gerrit-Reviewer: Corey Farrell <git@cfware.com> </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jaco Kroon <jaco@uls.co.za> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Richard Mudgett <rmudgett@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>