From 18974e3f5124c1468fc9e21b6d2f0710a39bcc29 Mon Sep 17 00:00:00 2001 From: sharp00k Date: Wed, 31 May 2023 17:18:09 +0900 Subject: [PATCH] add_http_response_code_and_body_when_handshake_error --- websocket-sharp/CloseEventArgs.cs | 22 ++++++++++++++++++++++ websocket-sharp/PayloadData.cs | 13 +++++++++++++ websocket-sharp/WebSocket.cs | 9 ++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/websocket-sharp/CloseEventArgs.cs b/websocket-sharp/CloseEventArgs.cs index 50b01ce32..921c0b1c1 100644 --- a/websocket-sharp/CloseEventArgs.cs +++ b/websocket-sharp/CloseEventArgs.cs @@ -100,6 +100,28 @@ public string Reason { } } + /// + /// Gets the http status code for the handshake error + /// + /// + /// + /// A that represents http status code for + /// the handshake error + /// + /// + public int HttpStatusCode => _payloadData.HttpStatusCode; + + /// + /// Gets the http response body for the handshake error + /// + /// + /// + /// A that represents http response body for + /// the handshake error + /// + /// + public string HttpResponseBody => _payloadData.HttpResponseBody; + /// /// Gets a value indicating whether the connection has been closed cleanly. /// diff --git a/websocket-sharp/PayloadData.cs b/websocket-sharp/PayloadData.cs index 6d17bc2a0..fcefc5ab2 100644 --- a/websocket-sharp/PayloadData.cs +++ b/websocket-sharp/PayloadData.cs @@ -95,11 +95,24 @@ internal PayloadData (ushort code, string reason) _data = code.Append (reason); _length = _data.LongLength; } + + internal PayloadData (ushort code, string reason, int httpStatusCode, string httpResponseBody) + { + _data = code.Append (reason); + _length = _data.LongLength; + HttpStatusCode = httpStatusCode; + HttpResponseBody = httpResponseBody; + + } #endregion #region Internal Properties + internal int HttpStatusCode { get; } + + internal string HttpResponseBody { get; } + internal ushort Code { get { return _length >= 2 diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index a9636e6a2..026ac9309 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -797,6 +797,13 @@ private void abort (ushort code, string reason) close (data, false, false); } + private void abort(ushort code, string reason, int httpStatusCode, string httpResponseBody) + { + var data = new PayloadData(code, reason, httpStatusCode, httpResponseBody); + + close(data, false, false); + } + // As server private bool accept () { @@ -1507,7 +1514,7 @@ private bool doHandshake () _log.Error (msg); _log.Debug (res.ToString ()); - abort (1002, "A handshake error has occurred."); + abort (1002, "A handshake error has occurred.", res.StatusCode, res.MessageBody); return false; }