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

json requests and web requests timeout #11

Open
wants to merge 7 commits 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ release/
*.user
*.cache

/src/GlobalAssemblyInfo.cs
/src/GlobalAssemblyInfo.cs
*.svn
49 changes: 32 additions & 17 deletions src/DevDefined.OAuth/Consumer/ConsumerRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#endregion

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Xml.Linq;
Expand Down Expand Up @@ -102,31 +104,35 @@ public virtual HttpWebRequest ToWebRequest()
request.Proxy = new WebProxy(ProxyServerUri, false);
}

if (description.ContentType == Parameters.HttpFormEncoded)
if (description.Headers.Count > 0)
{
request.ContentType = description.ContentType;
foreach (string key in description.Headers.AllKeys)
{
request.Headers[key] = description.Headers[key];
}
}

using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(description.Body);
}
if (description.ContentType == Parameters.HttpFormEncoded)
{
request.ContentType = description.ContentType;
}
else if (!string.IsNullOrEmpty(description.Body))

// We want to do the alterations last of all but before GetRequestStream() when
// it will be to late for the modifications
if (HttpWebRequestAlterations != null)
{
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(description.Body);
}
HttpWebRequestAlterations.ForEach(action => action(request));
}

if (description.Headers.Count > 0)
if (description.ContentType == Parameters.HttpFormEncoded || !string.IsNullOrEmpty(description.Body))
{
foreach (string key in description.Headers.AllKeys)
using (var writer = new StreamWriter(request.GetRequestStream()))
{
request.Headers[key] = description.Headers[key];
writer.Write(description.Body);
}
}


return request;
}

Expand All @@ -145,7 +151,7 @@ public RequestDescription GetRequestDescription()
}

Uri uri = _context.GenerateUri();

var description = new RequestDescription
{
Url = uri,
Expand All @@ -159,7 +165,14 @@ public RequestDescription GetRequestDescription()
}
else if (!string.IsNullOrEmpty(RequestBody))
{
description.Body = UriUtility.UrlEncode(RequestBody);
if(_consumerContext.EncodeRequestBody)
{
description.Body = UriUtility.UrlEncode(RequestBody);
}
else
{
description.Body = RequestBody;
}
}

if (_consumerContext.UseHeaderForOAuthParameters)
Expand Down Expand Up @@ -234,7 +247,7 @@ public IConsumerRequest SignWithToken(IToken token)
ConsumerContext.SignContextWithToken(_context, token);
return this;
}

public Uri ProxyServerUri { get; set; }

public Action<string> ResponseBodyAction { get; set; }
Expand All @@ -243,6 +256,8 @@ public IConsumerRequest SignWithToken(IToken token)

public string RequestBody { get; set; }

public List<Action<HttpWebRequest>> HttpWebRequestAlterations { get; set; }

private string ResponseBody { get; set; }

public override string ToString()
Expand Down
13 changes: 13 additions & 0 deletions src/DevDefined.OAuth/Consumer/ConsumerRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using DevDefined.OAuth.Framework;
Expand Down Expand Up @@ -122,6 +123,18 @@ public static IConsumerRequest WithHeaders(this IConsumerRequest request, object
return request;
}

public static IConsumerRequest AlterHttpWebRequest(this IConsumerRequest request, Action<HttpWebRequest> alteration)
{
if (request.HttpWebRequestAlterations == null)
{
request.HttpWebRequestAlterations = new List<Action<HttpWebRequest>>();
}

request.HttpWebRequestAlterations.Add(alteration);

return request;
}

public static IConsumerRequest AlterContext(this IConsumerRequest request, Action<IOAuthContext> alteration)
{
alteration(request.Context);
Expand Down
3 changes: 3 additions & 0 deletions src/DevDefined.OAuth/Consumer/IConsumerRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Xml.Linq;
Expand All @@ -23,5 +24,7 @@ public interface IConsumerRequest
Action<string> ResponseBodyAction { get; set; }
string AcceptsType { get; set; }
string RequestBody { get; set; }
List<Action<HttpWebRequest>> HttpWebRequestAlterations { get; set; }

}
}
1 change: 1 addition & 0 deletions src/DevDefined.OAuth/Consumer/IOAuthConsumerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface IOAuthConsumerContext
string UserAgent { get; set; }
AsymmetricAlgorithm Key { get; set; }
bool UseHeaderForOAuthParameters { get; set; }
bool EncodeRequestBody { get; set; }
void SignContext(IOAuthContext context);
void SignContextWithToken(IOAuthContext context, IToken token);
}
Expand Down
3 changes: 2 additions & 1 deletion src/DevDefined.OAuth/Consumer/OAuthConsumerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public INonceGenerator NonceGenerator
public AsymmetricAlgorithm Key { get; set; }
public bool UseHeaderForOAuthParameters { get; set; }
public string UserAgent { get; set; }

public bool EncodeRequestBody { get; set; }

public void SignContext(IOAuthContext context)
{
EnsureStateIsValid();
Expand Down
22 changes: 21 additions & 1 deletion src/DevDefined.OAuth/DevDefined.OAuth.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -54,6 +54,23 @@
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="System" />
Expand Down Expand Up @@ -183,4 +200,7 @@
<Target Name="AfterBuild">
</Target>
-->
<PropertyGroup>
<PostBuildEvent>copy "$(TargetPath)" "$(SolutionDir)\clients\DotNet\ThirdParty\DevDefined\"</PostBuildEvent>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/DevDefined.OAuth/Framework/BoundParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ NameValueCollection Collection
if (_context.UseAuthorizationHeader)
return _context.AuthorizationHeaderParameters;

if (_context.RequestMethod == "GET")
if (_context.RequestMethod == "GET" || _context.UseQueryParametersForOAuth)
return _context.QueryParameters;

return _context.FormEncodedParameters;
Expand Down
1 change: 1 addition & 0 deletions src/DevDefined.OAuth/Framework/IOAuthContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public interface IOAuthContext : IToken
string Verifier { get; set; }

bool UseAuthorizationHeader { get; set; }
bool UseQueryParametersForOAuth { get; set; }

Uri GenerateUri();
string GenerateUrl();
Expand Down
1 change: 1 addition & 0 deletions src/DevDefined.OAuth/Framework/OAuthContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public string Version
}

public bool UseAuthorizationHeader { get; set; }
public bool UseQueryParametersForOAuth { get; set; }

public string Realm
{
Expand Down
15 changes: 14 additions & 1 deletion src/DevDefined.OAuth/Framework/UriUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ static UriUtility()
// see http://stackoverflow.com/questions/846487/how-to-get-uri-escapedatastring-to-comply-with-rfc-3986 for details
static string EscapeUriDataStringRfc3986(string value)
{
var escaped = new StringBuilder(Uri.EscapeDataString(value));
const int maxLengthOfEscapeDataString = 32766;

var escaped = new StringBuilder();

for(int i = 0; i < value.Length; i += maxLengthOfEscapeDataString)
{
int subStringLength = maxLengthOfEscapeDataString;
if (i + subStringLength > value.Length)
{
subStringLength = value.Length - i;
}

escaped.Append(Uri.EscapeDataString(value.Substring(i, subStringLength)));
}

for (int i = 0; i < UriRfc3986CharsToEscape.Length; i++)
{
Expand Down