Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_http_response_code_and_body_when_handshake_error #730

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions websocket-sharp/CloseEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public string Reason {
}
}

/// <summary>
/// Gets the http status code for the handshake error
/// </summary>
/// <value>
/// <para>
/// A <see cref="int"/> that represents http status code for
/// the handshake error
/// </para>
/// </value>
public int HttpStatusCode => _payloadData.HttpStatusCode;

/// <summary>
/// Gets the http response body for the handshake error
/// </summary>
/// <value>
/// <para>
/// A <see cref="int"/> that represents http response body for
/// the handshake error
/// </para>
/// </value>
public string HttpResponseBody => _payloadData.HttpResponseBody;

/// <summary>
/// Gets a value indicating whether the connection has been closed cleanly.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions websocket-sharp/PayloadData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion websocket-sharp/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
{
Expand Down Expand Up @@ -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;
}
Expand Down