Skip to content
Open
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
70 changes: 70 additions & 0 deletions websocket-sharp/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class WebSocket : IDisposable
#region Private Fields

private AuthenticationChallenge _authChallenge;
private string _authHeaderValue;
private string _base64Key;
private bool _client;
private Action _closeContext;
Expand Down Expand Up @@ -1683,6 +1684,9 @@ private HttpRequest createHandshakeRequest ()
if (ares != null)
headers["Authorization"] = ares.ToString ();

if (!_authHeaderValue.IsNullOrEmpty ())
headers["Authorization"] = _authHeaderValue;

var hasUserHeader = _userHeaders != null && _userHeaders.Count > 0;

if (hasUserHeader)
Expand Down Expand Up @@ -4550,6 +4554,72 @@ public void SetUserHeader (string name, string value)
}
}

/// <summary>
/// Sets the Authorization request header to a specified string value.
/// </summary>
/// <param name="value">
/// A <see cref="string"/> that specifies the value of the Authorization request header to set.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="value"/> contains an invalid character.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The length of <paramref name="value"/> is greater than 65,535
/// characters.
/// </exception>
/// <exception cref="InvalidOperationException">
/// <para>
/// This method is not available if the interface is not for the client.
/// </para>
/// <para>
/// -or-
/// </para>
/// <para>
/// This method is not available when the current state of the interface
/// is neither New nor Closed.
/// </para>
/// </exception>
public void SetAuthorizationHeader (string value)
{
if (!_client)
{
const string msg = "The interface is not for the client.";

throw new InvalidOperationException (msg);
}

var val = value != null ? value.Trim () : string.Empty;

if (val.Length > 0)
{
if (val.Length > 65535)
{
const string msg = "The length of that value is greater than 65,535 characters.";

throw new ArgumentOutOfRangeException (nameof(value), msg);
}

if (!val.IsText ())
{
const string msg = "That value contains an invalid character.";

throw new ArgumentException (msg, nameof(value));
}
}

lock (_forState)
{
if (!canSet ())
{
const string msg = "The current state of the interface is neither New nor Closed.";

throw new InvalidOperationException (msg);
}

_authHeaderValue = val.Length > 0 ? val : null;
}
}

#endregion

#region Explicit Interface Implementations
Expand Down