Skip to content

Commit

Permalink
Merge pull request #24 from Saleslogix/topic-cookies
Browse files Browse the repository at this point in the history
Topic cookies
  • Loading branch information
jbest84 authored May 24, 2021
2 parents 6da9e89 + 20e5bdf commit 0147baa
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 112 deletions.
9 changes: 8 additions & 1 deletion Saleslogix.SData.Client/Framework/IAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ namespace Saleslogix.SData.Client.Framework
public interface IAuthenticator
{
void Authenticate(WebRequest request);
///invoked in response to 401
UnauthorizedAction Unauthorized(WebResponse response);
}

public interface IAuthenticator2 : IAuthenticator
{
///invoked in response to 403
UnauthorizedAction Forbidden(WebResponse response);
}

public enum UnauthorizedAction
{
Throw,
Retry
}
}
}
167 changes: 89 additions & 78 deletions Saleslogix.SData.Client/Framework/SDataRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,22 @@ public virtual SDataResponse GetResponse()
attempts--;
continue;
}
if (ex.Status == WebExceptionStatus.ProtocolError &&
((HttpWebResponse) ex.Response).StatusCode == HttpStatusCode.Unauthorized &&
Authenticator != null && Authenticator.Unauthorized(ex.Response) == UnauthorizedAction.Retry)
if (ex.Status == WebExceptionStatus.ProtocolError && Authenticator != null)
{
continue;
if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Unauthorized)
{
//401
if (Authenticator.Unauthorized(ex.Response) == UnauthorizedAction.Retry) continue;
}
else if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.Forbidden)
{
IAuthenticator2 vAuthenticator2 = Authenticator as IAuthenticator2;
if (vAuthenticator2 != null)
{
//403
if (vAuthenticator2.Forbidden(ex.Response) == UnauthorizedAction.Retry) continue;
}
}
}
throw new SDataException(ex);
}
Expand Down Expand Up @@ -257,87 +268,87 @@ public virtual IAsyncResult BeginGetResponse(AsyncCallback callback, object stat
Action getResponse = null;
Action loop =
() =>
{
MediaType? contentType;
object content;
_request = CreateRequest(uri, out contentType, out content);
if (content != null || Form.Count > 0 || Files.Count > 0)
{
MediaType? contentType;
object content;
_request = CreateRequest(uri, out contentType, out content);
if (content != null || Form.Count > 0 || Files.Count > 0)
{
_request.BeginGetRequestStream(
async =>
_request.BeginGetRequestStream(
async =>
{
try
{
using (var stream = _request.EndGetRequestStream(async))
{
try
{
using (var stream = _request.EndGetRequestStream(async))
{
WriteRequestContent(contentType, content, stream);
}
TraceRequest(_request);
getResponse();
}
catch (Exception ex)
{
result.Failure(ex, async.CompletedSynchronously);
_request = null;
Interlocked.Exchange(ref _state, 0);
}
}, null);
}
else
{
TraceRequest(_request);
getResponse();
}
};
WriteRequestContent(contentType, content, stream);
}
TraceRequest(_request);
getResponse();
}
catch (Exception ex)
{
result.Failure(ex, async.CompletedSynchronously);
_request = null;
Interlocked.Exchange(ref _state, 0);
}
}, null);
}
else
{
TraceRequest(_request);
getResponse();
}
};
getResponse =
() => _request.BeginGetResponse(
async =>
{
try
{
WebResponse response;
try
{
WebResponse response;
try
{
response = _request.EndGetResponse(async);
TraceResponse(response);
}
catch (WebException webEx)
{
response = _request.EndGetResponse(async);
TraceResponse(response);
}
catch (WebException webEx)
{
#if PCL || NETFX_CORE || SILVERLIGHT
if (attempts > 0)
#else
if (webEx.Status == WebExceptionStatus.Timeout && attempts > 0)
if (webEx.Status == WebExceptionStatus.Timeout && attempts > 0)
#endif
{
attempts--;
loop();
return;
}
throw new SDataException(webEx);
}
var httpResponse = response as HttpWebResponse;
var statusCode = httpResponse != null ? httpResponse.StatusCode : 0;
if (statusCode != HttpStatusCode.Found)
{
result.Success(new SDataResponse(response, location), async.CompletedSynchronously);
_request = null;
Interlocked.Exchange(ref _state, 0);
}
else
{
uri = location = response.Headers["Location"];
attempts--;
loop();
return;
}
throw new SDataException(webEx);
}
catch (Exception ex)
var httpResponse = response as HttpWebResponse;
var statusCode = httpResponse != null ? httpResponse.StatusCode : 0;
if (statusCode != HttpStatusCode.Found)
{
result.Failure(ex, async.CompletedSynchronously);
result.Success(new SDataResponse(response, location), async.CompletedSynchronously);
_request = null;
Interlocked.Exchange(ref _state, 0);
}
}, null);
else
{
uri = location = response.Headers["Location"];
loop();
}
}
catch (Exception ex)
{
result.Failure(ex, async.CompletedSynchronously);
_request = null;
Interlocked.Exchange(ref _state, 0);
}
}, null);
try
{
loop();
Expand All @@ -356,7 +367,7 @@ public virtual SDataResponse EndGetResponse(IAsyncResult asyncResult)
Guard.ArgumentIsType<AsyncResult<SDataResponse>>(asyncResult, "asyncResult");
try
{
return ((AsyncResult<SDataResponse>) asyncResult).End();
return ((AsyncResult<SDataResponse>)asyncResult).End();
}
finally
{
Expand All @@ -381,7 +392,7 @@ private WebRequest CreateRequest(string uri, out MediaType? contentType, out obj
methodOverride = true;
contentType = MediaType.Text;
content = uri;
uri = new SDataUri(uri) {Query = null}.ToString();
uri = new SDataUri(uri) { Query = null }.ToString();
}
else
{
Expand Down Expand Up @@ -531,18 +542,18 @@ private void WriteRequestContent(MediaType? contentType, object content, Stream
{
if (contentType != null)
{
var part = new MimePart(requestStream) {ContentType = MediaTypeNames.GetMediaType(contentType.Value)};
var part = new MimePart(requestStream) { ContentType = MediaTypeNames.GetMediaType(contentType.Value) };
multipart.Add(part);
}
foreach (var data in Form)
{
var part = new MimePart(new MemoryStream(Encoding.UTF8.GetBytes(data.Value)))
{
ContentType = MediaTypeNames.TextMediaType,
ContentTransferEncoding = "binary",
ContentDisposition = "inline; name=" + data.Key
};
{
ContentType = MediaTypeNames.TextMediaType,
ContentTransferEncoding = "binary",
ContentDisposition = "inline; name=" + data.Key
};
multipart.Add(part);
}
Expand All @@ -557,11 +568,11 @@ private void WriteRequestContent(MediaType? contentType, object content, Stream
}
var part = new MimePart(file.Stream)
{
ContentType = file.ContentType ?? "application/octet-stream",
ContentTransferEncoding = "binary",
ContentDisposition = contentDisposition
};
{
ContentType = file.ContentType ?? "application/octet-stream",
ContentTransferEncoding = "binary",
ContentDisposition = contentDisposition
};
multipart.Add(part);
}
Expand Down
1 change: 1 addition & 0 deletions Saleslogix.SData.Client/ISDataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ public interface ISDataClient
Task<ISDataResults<T>> ExecuteAsync<T>(SDataParameters parms, CancellationToken cancel = default(CancellationToken));
Task<ISDataResults<IList<T>>> ExecuteBatchAsync<T>(IList<SDataParameters> items, CancellationToken cancel = default(CancellationToken));
#endif
CookieContainer Cookies { get; set; }
}
}
2 changes: 1 addition & 1 deletion Saleslogix.SData.Client/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// by using the '*' as shown below:

[assembly: AssemblyVersion("2.0")]
[assembly: AssemblyFileVersion("2.0.2.1563")]
[assembly: AssemblyFileVersion("2.0.3.1564")]
[assembly: NeutralResourcesLanguage("en-US")]
[assembly: AssemblyInformationalVersion("2.0")]
[assembly: InternalsVisibleTo("Saleslogix.SData.Client.Test, PublicKey=" +
Expand Down
Loading

0 comments on commit 0147baa

Please sign in to comment.