Skip to content

Commit 2beaa55

Browse files
committed
* Applied patch from todd for issue #7 (http://code.google.com/p/devdefined-tools/issues/detail?id=7) with minor tweaks. Note: exception handling functionality may be reviewed in the future.
1 parent 095cc76 commit 2beaa55

File tree

4 files changed

+82
-40
lines changed

4 files changed

+82
-40
lines changed

DevDefined.OAuth/Consumer/ConsumerRequest.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
using System.Web;
3232
using System.Xml.Linq;
3333
using DevDefined.OAuth.Framework;
34-
using DevDefined.OAuth.Utility;
3534

3635
namespace DevDefined.OAuth.Consumer
3736
{
@@ -48,6 +47,8 @@ public ConsumerRequest(IOAuthContext context, IOAuthConsumerContext consumerCont
4847
_token = token;
4948
}
5049

50+
#region IConsumerRequest Members
51+
5152
public IOAuthConsumerContext ConsumerContext
5253
{
5354
get { return _consumerContext; }
@@ -66,11 +67,11 @@ public XDocument ToDocument()
6667
public byte[] ToBytes()
6768
{
6869
return FromStream(delegate(Stream stream)
69-
{
70-
var buffer = new byte[stream.Length];
71-
stream.Read(buffer, 0, buffer.Length);
72-
return buffer;
73-
});
70+
{
71+
var buffer = new byte[stream.Length];
72+
stream.Read(buffer, 0, buffer.Length);
73+
return buffer;
74+
});
7475
}
7576

7677
public HttpWebRequest ToWebRequest()
@@ -117,10 +118,10 @@ public RequestDescription GetRequestDescription()
117118
Uri uri = _context.GenerateUri();
118119

119120
var description = new RequestDescription
120-
{
121-
Url = uri,
122-
Method = _context.RequestMethod
123-
};
121+
{
122+
Url = uri,
123+
Method = _context.RequestMethod
124+
};
124125

125126
if ((_context.FormEncodedParameters != null) && (_context.FormEncodedParameters.Count > 0))
126127
{
@@ -141,15 +142,15 @@ public HttpWebResponse ToWebResponse()
141142
try
142143
{
143144
HttpWebRequest request = ToWebRequest();
144-
return (HttpWebResponse)request.GetResponse();
145+
return (HttpWebResponse) request.GetResponse();
145146
}
146147
catch (WebException webEx)
147148
{
148-
OAuthException authException;
149+
Exception wrappedException;
149150

150-
if (WebExceptionHelper.TryWrapException(Context, webEx, out authException))
151+
if (WebExceptionHelper.TryWrapException(Context, webEx, out wrappedException))
151152
{
152-
throw authException;
153+
throw wrappedException;
153154
}
154155

155156
throw;
@@ -196,6 +197,8 @@ public IConsumerRequest SignWithToken(IToken token)
196197
return this;
197198
}
198199

200+
#endregion
201+
199202
public override string ToString()
200203
{
201204
return FromStream(stream => new StreamReader(stream).ReadToEnd());
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1-
using System.Net;
1+
using System;
2+
using System.Net;
23
using DevDefined.OAuth.Framework;
34
using DevDefined.OAuth.Utility;
45

56
namespace DevDefined.OAuth.Consumer
67
{
7-
public static class WebExceptionHelper
8-
{
9-
/// <summary>
10-
/// Will attempt to wrap the exception, returning true if the exception was wrapped, or returning false if it was not (in which case
11-
/// the original exception should be thrown).
12-
/// </summary>
13-
/// <param name="requestContext"></param>
14-
/// <param name="webEx"></param>
15-
/// <param name="authException"></param>
16-
/// <returns><c>true</c>, if the authException should be throw, <c>false</c> if the original web exception should be thrown</returns>
17-
public static bool TryWrapException(IOAuthContext requestContext, WebException webEx, out OAuthException authException)
8+
public static class WebExceptionHelper
189
{
19-
try
20-
{
21-
string content = webEx.Response.ReadToEnd();
22-
if (content.Contains(Parameters.OAuth_Problem))
10+
/// <summary>
11+
/// Will attempt to wrap the exception, returning true if the exception was wrapped, or returning false if it was not (in which case
12+
/// the original exception should be thrown).
13+
/// </summary>
14+
/// <param name="requestContext"></param>
15+
/// <param name="webException"></param>
16+
/// <param name="wrappedException">The wrapped exception (will be set to the webException, if this method returns <c>false</c></param>
17+
/// <returns><c>true</c>, if the authException should be throw, <c>false</c> if the wrapped web exception should be thrown</returns>
18+
public static bool TryWrapException(IOAuthContext requestContext, WebException webException, out Exception wrappedException)
2319
{
24-
var report = new OAuthProblemReport(content);
25-
authException = new OAuthException(report.ProblemAdvice ?? report.Problem, webEx) {Context = requestContext, Report = report};
26-
return true;
20+
try
21+
{
22+
string content = webException.Response.ReadToEnd();
23+
24+
if (content.Contains(Parameters.OAuth_Problem))
25+
{
26+
var report = new OAuthProblemReport(content);
27+
wrappedException = new OAuthException(report.ProblemAdvice ?? report.Problem, webException) { Context = requestContext, Report = report };
28+
}
29+
else
30+
{
31+
wrappedException = new ParsedWebException(content, webException.Message, webException.GetBaseException(), webException.Status, webException.Response);
32+
}
33+
34+
return true;
35+
}
36+
catch
37+
{
38+
wrappedException = webException;
39+
return false;
40+
}
2741
}
28-
}
29-
catch
30-
{
31-
}
32-
authException = new OAuthException();
33-
return false;
3442
}
35-
}
3643
}

DevDefined.OAuth/DevDefined.OAuth.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="Framework\IConsumer.cs" />
8181
<Compile Include="Framework\INonceGenerator.cs" />
8282
<Compile Include="Framework\IOAuthContext.cs" />
83+
<Compile Include="Framework\ParsedWebException.cs" />
8384
<Compile Include="Framework\Signing\IOAuthContextSigner.cs" />
8485
<Compile Include="Framework\IToken.cs" />
8586
<Compile Include="Framework\OAuthException.cs" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Net;
3+
4+
namespace DevDefined.OAuth.Framework
5+
{
6+
/// <summary>
7+
/// Extends WebException. Calls to the underlying Response.GetResponseStream() will fail since the response body has already been read,
8+
/// so the body is included in the property <see cref="ResponseContent" /> instead.
9+
/// </summary>
10+
public class ParsedWebException : WebException
11+
{
12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="T:System.Net.WebException"/> class with the specified error message, nested exception, status, and response.
14+
/// </summary>
15+
/// <param name="content">The resposne body content</param>
16+
/// <param name="message">The text of the error message.</param>
17+
/// <param name="innerException">A nested exception.</param>
18+
/// <param name="status">One of the <see cref="T:System.Net.WebExceptionStatus"/> values.</param>
19+
/// <param name="response">A <see cref="T:System.Net.WebResponse"/> instance that contains the response from the remote host.</param>
20+
public ParsedWebException(string content, string message, Exception innerException, WebExceptionStatus status, WebResponse response)
21+
: base(message, innerException, status, response)
22+
{
23+
ResponseContent = content;
24+
}
25+
26+
/// <summary>
27+
/// The response body content
28+
/// </summary>
29+
public string ResponseContent { get; private set; }
30+
}
31+
}

0 commit comments

Comments
 (0)