<p>Jeremy LainĂ© has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/10856">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_http_websocket: respond to CLOSE opcode<br><br>This ensures that Asterisk responds properly to frames received from a<br>client with opcode 8 (CLOSE) by:<br><br>- responding with its own CLOSE frame, echoing back the status code<br>- closing the connection<br><br>ASTERISK-28231 #close<br><br>Change-Id: Icb1b60205fc77ee970ddc91d1f545671781344cf<br>---<br>M res/res_http_websocket.c<br>1 file changed, 17 insertions(+), 23 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/56/10856/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/res_http_websocket.c b/res/res_http_websocket.c</span><br><span>index d52e6b3..28e7233 100644</span><br><span>--- a/res/res_http_websocket.c</span><br><span>+++ b/res/res_http_websocket.c</span><br><span>@@ -566,7 +566,7 @@</span><br><span>       *opcode = buf[0] & 0xf;</span><br><span>  *payload_len = buf[1] & 0x7f;</span><br><span>    if (*opcode == AST_WEBSOCKET_OPCODE_TEXT || *opcode == AST_WEBSOCKET_OPCODE_BINARY || *opcode == AST_WEBSOCKET_OPCODE_CONTINUATION ||</span><br><span style="color: hsl(0, 100%, 40%);">-       *opcode == AST_WEBSOCKET_OPCODE_PING || *opcode == AST_WEBSOCKET_OPCODE_PONG) {</span><br><span style="color: hsl(120, 100%, 40%);">+       *opcode == AST_WEBSOCKET_OPCODE_PING || *opcode == AST_WEBSOCKET_OPCODE_PONG  || *opcode == AST_WEBSOCKET_OPCODE_CLOSE) {</span><br><span>                fin = (buf[0] >> 7) & 1;</span><br><span>           mask_present = (buf[1] >> 7) & 1;</span><br><span> </span><br><span>@@ -622,6 +622,22 @@</span><br><span>                       return 0;</span><br><span>            }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+         /* Reply to CLOSE opcode with our own CLOSE and echo back the code */</span><br><span style="color: hsl(120, 100%, 40%);">+         if (*opcode == AST_WEBSOCKET_OPCODE_CLOSE) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  uint16_t code = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                    if (*payload_len >= 2) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           code = ntohs(get_unaligned_uint16(*payload));</span><br><span style="color: hsl(120, 100%, 40%);">+                 }</span><br><span style="color: hsl(120, 100%, 40%);">+                     *payload_len = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+                     ast_websocket_close(session, code);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (session->stream) {</span><br><span style="color: hsl(120, 100%, 40%);">+                             ast_iostream_close(session->stream);</span><br><span style="color: hsl(120, 100%, 40%);">+                               session->stream = NULL;</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>          if (*payload_len) {</span><br><span>                  if (!(new_payload = ast_realloc(session->payload, (session->payload_len + *payload_len)))) {</span><br><span>                           ast_log(LOG_WARNING, "Failed allocation: %p, %zu, %"PRIu64"\n",</span><br><span>@@ -661,28 +677,6 @@</span><br><span>                   *payload = session->payload;</span><br><span>                      session->payload_len = 0;</span><br><span>                 }</span><br><span style="color: hsl(0, 100%, 40%);">-       } else if (*opcode == AST_WEBSOCKET_OPCODE_CLOSE) {</span><br><span style="color: hsl(0, 100%, 40%);">-             session->closing = 1;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-                /* Make the payload available so the user can look at the reason code if they so desire */</span><br><span style="color: hsl(0, 100%, 40%);">-              if (!*payload_len) {</span><br><span style="color: hsl(0, 100%, 40%);">-                    return 0;</span><br><span style="color: hsl(0, 100%, 40%);">-               }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               if (!(new_payload = ast_realloc(session->payload, *payload_len))) {</span><br><span style="color: hsl(0, 100%, 40%);">-                  ast_log(LOG_WARNING, "Failed allocation: %p, %"PRIu64"\n",</span><br><span style="color: hsl(0, 100%, 40%);">-                                  session->payload, *payload_len);</span><br><span style="color: hsl(0, 100%, 40%);">-                     *payload_len = 0;</span><br><span style="color: hsl(0, 100%, 40%);">-                       return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-              }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-               session->payload = new_payload;</span><br><span style="color: hsl(0, 100%, 40%);">-              if (ws_safe_read(session, &buf[frame_size], *payload_len, opcode)) {</span><br><span style="color: hsl(0, 100%, 40%);">-                        return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-              }</span><br><span style="color: hsl(0, 100%, 40%);">-               memcpy(session->payload, &buf[frame_size], *payload_len);</span><br><span style="color: hsl(0, 100%, 40%);">-                *payload = session->payload;</span><br><span style="color: hsl(0, 100%, 40%);">-         frame_size += *payload_len;</span><br><span>  } else {</span><br><span>             ast_log(LOG_WARNING, "WebSocket unknown opcode %u\n", *opcode);</span><br><span>            /* We received an opcode that we don't understand, the RFC states that 1003 is for a type of data that can't be accepted... opcodes</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/10856">change 10856</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/10856"/><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: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Icb1b60205fc77ee970ddc91d1f545671781344cf </div>
<div style="display:none"> Gerrit-Change-Number: 10856 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Jeremy LainĂ© <jeremy.laine@m4x.org> </div>