<p>Joshua C. Colp <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/10861">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Sean Bright: Looks good to me, but someone else must approve
  Joshua C. Colp: Looks good to me, but someone else must approve; Approved for Submit
  Kevin Harwell: Looks good to me, approved

</div><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 echoing back the status code in its own<br>CLOSE frame.<br><br>Handling of the CLOSE opcode is moved up with the rest of the opcodes so<br>that unmasking gets applied. The payload is no longer returned to the<br>caller, but neither ARI nor the chan_sip nor pjsip made use of the<br>payload, which is a good thing since it was masked.<br><br>ASTERISK-28231 #close<br><br>Change-Id: Icb1b60205fc77ee970ddc91d1f545671781344cf<br>---<br>M res/res_http_websocket.c<br>1 file changed, 13 insertions(+), 24 deletions(-)<br><br></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 0b75721..a44f601 100644</span><br><span>--- a/res/res_http_websocket.c</span><br><span>+++ b/res/res_http_websocket.c</span><br><span>@@ -99,6 +99,7 @@</span><br><span>      unsigned int close_sent:1;          /*!< Bit to indicate that the session close opcode has been sent and no further data will be sent */</span><br><span>  struct websocket_client *client;    /*!< Client object when connected as a client websocket */</span><br><span>    char session_id[AST_UUID_STR_LEN];  /*!< The identifier for the websocket session */</span><br><span style="color: hsl(120, 100%, 40%);">+       uint16_t close_status_code;         /*!< Status code sent in a CLOSE frame upon shutdown */</span><br><span> };</span><br><span> </span><br><span> /*! \brief Hashing function for protocols */</span><br><span>@@ -173,7 +174,7 @@</span><br><span>       struct ast_websocket *session = obj;</span><br><span> </span><br><span>     if (session->stream) {</span><br><span style="color: hsl(0, 100%, 40%);">-               ast_websocket_close(session, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+              ast_websocket_close(session, session->close_status_code);</span><br><span>                 if (session->stream) {</span><br><span>                    ast_iostream_close(session->stream);</span><br><span>                      session->stream = NULL;</span><br><span>@@ -578,7 +579,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>@@ -637,6 +638,16 @@</span><br><span>                       return 0;</span><br><span>            }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+         /* Save the CLOSE status code which will be sent in our own CLOSE in the destructor */</span><br><span style="color: hsl(120, 100%, 40%);">+                if (*opcode == AST_WEBSOCKET_OPCODE_CLOSE) {</span><br><span style="color: hsl(120, 100%, 40%);">+                  session->closing = 1;</span><br><span style="color: hsl(120, 100%, 40%);">+                      if (*payload_len >= 2) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           session->close_status_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%);">+                     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>@@ -676,28 +687,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/10861">change 10861</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/10861"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-MessageType: merged </div>
<div style="display:none"> Gerrit-Change-Id: Icb1b60205fc77ee970ddc91d1f545671781344cf </div>
<div style="display:none"> Gerrit-Change-Number: 10861 </div>
<div style="display:none"> Gerrit-PatchSet: 3 </div>
<div style="display:none"> Gerrit-Owner: Jeremy LainĂ© <jeremy.laine@m4x.org> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation (1000185) </div>
<div style="display:none"> Gerrit-Reviewer: Jeremy LainĂ© <jeremy.laine@m4x.org> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua C. Colp <jcolp@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Sean Bright <sean.bright@gmail.com> </div>