Skip to content
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
12 changes: 6 additions & 6 deletions websocket-sharp/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ private HttpRequest (string method, string uri, Version version, NameValueCollec

#region Internal Constructors

internal HttpRequest (string method, string uri)
internal HttpRequest (string method, string uri, string userAgent)
: this (method, uri, HttpVersion.Version11, new NameValueCollection ())
{
Headers["User-Agent"] = "websocket-sharp/1.0";
Headers["User-Agent"] = userAgent;
}

#endregion
Expand Down Expand Up @@ -122,20 +122,20 @@ public string RequestUri {

#region Internal Methods

internal static HttpRequest CreateConnectRequest (Uri uri)
internal static HttpRequest CreateConnectRequest (Uri uri, string userAgent)
{
var host = uri.DnsSafeHost;
var port = uri.Port;
var authority = String.Format ("{0}:{1}", host, port);
var req = new HttpRequest ("CONNECT", authority);
var req = new HttpRequest ("CONNECT", authority, userAgent);
req.Headers["Host"] = port == 80 ? host : authority;

return req;
}

internal static HttpRequest CreateWebSocketRequest (Uri uri)
internal static HttpRequest CreateWebSocketRequest(Uri uri, string userAgent)
{
var req = new HttpRequest ("GET", uri.PathAndQuery);
var req = new HttpRequest ("GET", uri.PathAndQuery, userAgent);
var headers = req.Headers;

// Only includes a port number in the Host header value if it's non-default.
Expand Down
19 changes: 17 additions & 2 deletions websocket-sharp/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public class WebSocket : IDisposable
private Stream _stream;
private TcpClient _tcpClient;
private Uri _uri;
private string _userAgent = "websocket-sharp/1.0";
private const string _version = "13";
private TimeSpan _waitTime;

Expand Down Expand Up @@ -595,6 +596,20 @@ public Uri Url {
}
}

/// <summary>
/// Gets or sets the WebSocket User-Agent string as send in the header of the requests.
/// </summary>
public string UserAgent
{
get {
return _userAgent;
}

set {
_userAgent = value;
}
}

/// <summary>
/// Gets or sets the wait time for the response to the Ping or Close.
/// </summary>
Expand Down Expand Up @@ -1168,7 +1183,7 @@ private HttpResponse createHandshakeFailureResponse (HttpStatusCode code)
// As client
private HttpRequest createHandshakeRequest ()
{
var ret = HttpRequest.CreateWebSocketRequest (_uri);
var ret = HttpRequest.CreateWebSocketRequest (_uri, _userAgent);

var headers = ret.Headers;
if (!_origin.IsNullOrEmpty ())
Expand Down Expand Up @@ -1860,7 +1875,7 @@ private bool sendHttpResponse (HttpResponse response)
// As client
private void sendProxyConnectRequest ()
{
var req = HttpRequest.CreateConnectRequest (_uri);
var req = HttpRequest.CreateConnectRequest (_uri, _userAgent);
var res = sendHttpRequest (req, 90000);
if (res.IsProxyAuthenticationRequired) {
var chal = res.Headers["Proxy-Authenticate"];
Expand Down