Skip to content

Commit

Permalink
Made changes to use 2 Http Clients
Browse files Browse the repository at this point in the history
  • Loading branch information
abnanda1 committed May 3, 2013
1 parent 6d892a3 commit 0402c7b
Show file tree
Hide file tree
Showing 19 changed files with 185 additions and 150 deletions.
3 changes: 3 additions & 0 deletions Microsoft.AspNet.SignalR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.SignalR.Ho
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.SignalR.Client.Samples", "samples\Microsoft.AspNet.SignalR.Client.Samples\Microsoft.AspNet.SignalR.Client.Samples.csproj", "{E0223FDC-0982-4D80-B6C2-BFAA6C6748C5}"
ProjectSection(ProjectDependencies) = postProject
{1EA34A62-E03E-45CF-A9C9-82D2DA0FCD82} = {1EA34A62-E03E-45CF-A9C9-82D2DA0FCD82}
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{08B794D9-FF1D-4536-885A-51892934A1F4}"
ProjectSection(SolutionItems) = preProject
Expand Down
34 changes: 12 additions & 22 deletions src/Microsoft.AspNet.SignalR.Client/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class Connection : IConnection, IDisposable
private JsonSerializer _jsonSerializer = new JsonSerializer();

#if (NET4 || NET45)
private readonly X509CertificateCollection certCollection = new X509CertificateCollection();
private readonly X509CertificateCollection _certCollection = new X509CertificateCollection();
#endif

/// <summary>
Expand Down Expand Up @@ -174,6 +174,16 @@ KeepAliveData IConnection.KeepAliveData
_keepAliveData = value;
}
}

#if NET4 || NET45
X509CertificateCollection IConnection.Certificates
{
get
{
return _certCollection;
}
}
#endif

public TraceLevels TraceLevel { get; set; }

Expand Down Expand Up @@ -580,7 +590,7 @@ public void AddClientCertificate(X509Certificate certificate)
throw new InvalidOperationException(Resources.Error_CertsCanOnlyBeAddedWhenDisconnected);
}

certCollection.Add(certificate);
_certCollection.Add(certificate);
}
}
#endif
Expand Down Expand Up @@ -686,28 +696,8 @@ void IConnection.PrepareRequest(IRequest request)
#else
request.UserAgent = CreateUserAgentString("SignalR.Client");
#endif
#endif
if (Credentials != null)
{
request.Credentials = Credentials;
}

if (CookieContainer != null)
{
request.CookieContainer = CookieContainer;
}

#if !SILVERLIGHT
if (Proxy != null)
{
request.Proxy = Proxy;
}
#endif
request.SetRequestHeaders(Headers);

#if (NET4 || NET45)
request.AddClientCerts(certCollection);
#endif
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Can be called via other clients.")]
Expand Down
13 changes: 11 additions & 2 deletions src/Microsoft.AspNet.SignalR.Client/Http/DefaultHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ namespace Microsoft.AspNet.SignalR.Client.Http
/// </summary>
public class DefaultHttpClient : IHttpClient
{
private IConnection _connection;

public void Initialize(IConnection connection)
{
_connection = connection;
}

/// <summary>
/// Makes an asynchronous http GET request to the specified url.
/// </summary>
/// <param name="url">The url to send the request to.</param>
/// <param name="prepareRequest">A callback that initializes the request with default values.</param>
/// <param name="isLongRunning">Indicates whether the request is long running</param>
/// <returns>A <see cref="T:Task{IResponse}"/>.</returns>
public Task<IResponse> Get(string url, Action<IRequest> prepareRequest)
public Task<IResponse> Get(string url, Action<IRequest> prepareRequest, bool isLongRunning)
{
IRequest req = null;
return HttpHelper.GetAsync(url, request =>
Expand All @@ -34,8 +42,9 @@ public Task<IResponse> Get(string url, Action<IRequest> prepareRequest)
/// <param name="url">The url to send the request to.</param>
/// <param name="prepareRequest">A callback that initializes the request with default values.</param>
/// <param name="postData">form url encoded data.</param>
/// <param name="isLongRunning">Indicates whether the request is long running</param>
/// <returns>A <see cref="T:Task{IResponse}"/>.</returns>
public Task<IResponse> Post(string url, Action<IRequest> prepareRequest, IDictionary<string, string> postData)
public Task<IResponse> Post(string url, Action<IRequest> prepareRequest, IDictionary<string, string> postData, bool isLongRunning)
{
IRequest req = null;
return HttpHelper.PostAsync(url, request =>
Expand Down
12 changes: 10 additions & 2 deletions src/Microsoft.AspNet.SignalR.Client/Http/IHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,30 @@ namespace Microsoft.AspNet.SignalR.Client.Http
/// </summary>
public interface IHttpClient
{
/// <summary>
/// Initializes the Http Clients
/// </summary>
/// <param name="connection">Connection</param>
void Initialize(IConnection connection);

/// <summary>
/// Makes an asynchronous http GET request to the specified url.
/// </summary>
/// <param name="url">The url to send the request to.</param>
/// <param name="prepareRequest">A callback that initializes the request with default values.</param>
/// <param name="isLongRunning">Indicates whether it is a long running request</param>
/// <returns>A <see cref="T:Task{IResponse}"/>.</returns>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get", Justification = "Performs a GET request")]
Task<IResponse> Get(string url, Action<IRequest> prepareRequest);
Task<IResponse> Get(string url, Action<IRequest> prepareRequest, bool isLongRunning);

/// <summary>
/// Makes an asynchronous http POST request to the specified url.
/// </summary>
/// <param name="url">The url to send the request to.</param>
/// <param name="prepareRequest">A callback that initializes the request with default values.</param>
/// <param name="postData">form url encoded data.</param>
/// <param name="isLongRunning">Indicates whether it is a long running request</param>
/// <returns>A <see cref="T:Task{IResponse}"/>.</returns>
Task<IResponse> Post(string url, Action<IRequest> prepareRequest, IDictionary<string, string> postData);
Task<IResponse> Post(string url, Action<IRequest> prepareRequest, IDictionary<string, string> postData, bool isLongRunning);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.AspNet.SignalR.Client.Http
{
public static class IHttpClientExtensions
{
public static Task<IResponse> Post(this IHttpClient client, string url, Action<IRequest> prepareRequest)
public static Task<IResponse> Post(this IHttpClient client, string url, Action<IRequest> prepareRequest, bool isLongRunning)
{
if (client == null)
{
Expand All @@ -24,7 +24,7 @@ public static Task<IResponse> Post(this IHttpClient client, string url, Action<I
throw new ArgumentNullException("prepareRequest");
}

return client.Post(url, prepareRequest, postData: null);
return client.Post(url, prepareRequest, null, isLongRunning);
}
}
}
30 changes: 0 additions & 30 deletions src/Microsoft.AspNet.SignalR.Client/Http/IRequest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.

using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
#if (NET4 || NET45)
using System.Security.Cryptography.X509Certificates;
#endif

namespace Microsoft.AspNet.SignalR.Client.Http
{
Expand All @@ -19,23 +14,6 @@ public interface IRequest
/// </summary>
string UserAgent { get; set; }

/// <summary>
/// The credentials for this request.
/// </summary>
ICredentials Credentials { get; set; }

/// <summary>
/// The cookies for this request.
/// </summary>
CookieContainer CookieContainer { get; set; }

#if !SILVERLIGHT
/// <summary>
/// The proxy information for this request.
/// </summary>
IWebProxy Proxy { get; set; }
#endif

/// <summary>
/// The accept header for this request.
/// </summary>
Expand All @@ -51,13 +29,5 @@ public interface IRequest
/// </summary>
/// <param name="headers">request headers</param>
void SetRequestHeaders(IDictionary<string, string> headers);

#if (NET4 || NET45)
/// <summary>
/// Sets client certificates
/// </summary>
/// <param name="certificates">client certificates</param>
void AddClientCerts(X509CertificateCollection certificates);
#endif
}
}
14 changes: 11 additions & 3 deletions src/Microsoft.AspNet.SignalR.Client/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public interface IConnection
ConnectionState State { get; }
IClientTransport Transport { get; }

#if !SILVERLIGHT
/// <summary>
/// Gets of sets proxy information for the connection.
/// </summary>
IWebProxy Proxy { get; set; }
#endif

#if (NET4 || NET45)
X509CertificateCollection Certificates { get; }
#endif

bool ChangeState(ConnectionState oldState, ConnectionState newState);

IDictionary<string, string> Headers { get; }
Expand All @@ -49,9 +60,6 @@ public interface IConnection
void OnConnectionSlow();
void PrepareRequest(IRequest request);
void UpdateLastKeepAlive();
#if (NET4 || NET45)
void AddClientCertificate(X509Certificate certificate);
#endif
void Trace(TraceLevels level, string format, params object[] args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Task Send(IConnection connection, string data)
{ "data", data }
};

return _httpClient.Post(url, connection.PrepareRequest, postData)
return _httpClient.Post(url, connection.PrepareRequest, postData, false)
.Then(response => response.ReadAsString())
.Then(raw =>
{
Expand Down Expand Up @@ -142,7 +142,7 @@ public void Abort(IConnection connection, TimeSpan timeout)

url += TransportHelper.AppendCustomQueryString(connection, url);

_httpClient.Post(url, connection.PrepareRequest).Catch((ex, state) =>
_httpClient.Post(url, connection.PrepareRequest, false).Catch((ex, state) =>
{
// If there's an error making an http request set the reset event
((HttpBasedTransport)state).CompleteAbort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private void Poll()
// This is called just prior to posting the request to ensure that any in-flight polling request
// is always executed before an OnAfterPoll
OnPolling();
})
}, true)
.ContinueWith(task =>
{
var next = TaskAsyncHelper.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ private void OpenConnection(IConnection connection,

HttpClient.Get(url, req =>
{
_request = req;
_request = req;
_request.Accept = "text/event-stream";
connection.PrepareRequest(_request);
_request.Accept = "text/event-stream";
}).ContinueWith(task =>
}, true).ContinueWith(task =>
{
if (task.IsFaulted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static Task<NegotiationResponse> GetNegotiationResponse(this IHttpClient
#endif
negotiateUrl += AppendCustomQueryString(connection, negotiateUrl);

return httpClient.Get(negotiateUrl, connection.PrepareRequest)
httpClient.Initialize(connection);

return httpClient.Get(negotiateUrl, connection.PrepareRequest, false)
.Then(response => response.ReadAsString())
.Then(raw =>
{
Expand Down
Loading

0 comments on commit 0402c7b

Please sign in to comment.