diff options
Diffstat (limited to 'src/DotNetOAuth')
-rw-r--r-- | src/DotNetOAuth/Messaging/Channel.cs | 30 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/DictionaryXmlReader.cs | 17 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/HttpRequestInfo.cs | 5 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessageSerializer.cs | 6 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs | 18 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/MessagingStrings.resx | 6 | ||||
-rw-r--r-- | src/DotNetOAuth/Messaging/Response.cs | 16 | ||||
-rw-r--r-- | src/DotNetOAuth/OAuthChannel.cs | 2 |
8 files changed, 63 insertions, 37 deletions
diff --git a/src/DotNetOAuth/Messaging/Channel.cs b/src/DotNetOAuth/Messaging/Channel.cs index 2ab3636..f674f12 100644 --- a/src/DotNetOAuth/Messaging/Channel.cs +++ b/src/DotNetOAuth/Messaging/Channel.cs @@ -120,8 +120,9 @@ namespace DotNetOAuth.Messaging { this.SendIndirectMessage(directedMessage);
break;
default:
- Debug.Fail("Unrecogized MessageTransport value.");
- throw new ArgumentException();
+ throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
+ MessagingStrings.UnrecognizedEnumValue, "Transport", message.Transport),
+ "message");
}
}
@@ -132,7 +133,12 @@ namespace DotNetOAuth.Messaging { /// <remarks>
/// Requires an HttpContext.Current context.
/// </remarks>
+ /// <exception cref="InvalidOperationException">Thrown when <see cref="HttpContext.Current"/> is null.</exception>
internal IProtocolMessage ReadFromRequest() {
+ if (HttpContext.Current == null) {
+ throw new InvalidOperationException(MessagingStrings.HttpContextRequired);
+ }
+
return this.ReadFromRequest(new HttpRequestInfo(HttpContext.Current.Request));
}
@@ -173,13 +179,14 @@ namespace DotNetOAuth.Messaging { /// Deserializes a dictionary of values into a message.
/// </summary>
/// <param name="fields">The dictionary of values that were read from an HTTP request or response.</param>
- /// <returns>The deserialized message.</returns>
+ /// <returns>The deserialized message, or null if no message could be recognized in the provided data.</returns>
protected virtual IProtocolMessage Receive(Dictionary<string, string> fields) {
- Type messageType = null;
- if (fields != null) {
- messageType = this.MessageTypeProvider.GetRequestMessageType(fields);
+ if (fields == null) {
+ throw new ArgumentNullException("fields");
}
+ Type messageType = this.MessageTypeProvider.GetRequestMessageType(fields);
+
// If there was no data, or we couldn't recognize it as a message, abort.
if (messageType == null) {
return null;
@@ -255,7 +262,7 @@ namespace DotNetOAuth.Messaging { Response response = new Response {
Status = HttpStatusCode.Redirect,
Headers = headers,
- Body = new byte[0],
+ Body = null,
OriginalMessage = message
};
@@ -281,8 +288,7 @@ namespace DotNetOAuth.Messaging { }
WebHeaderCollection headers = new WebHeaderCollection();
- MemoryStream body = new MemoryStream();
- StreamWriter bodyWriter = new StreamWriter(body);
+ StringWriter bodyWriter = new StringWriter();
StringBuilder hiddenFields = new StringBuilder();
foreach (var field in fields) {
hiddenFields.AppendFormat(
@@ -298,7 +304,7 @@ namespace DotNetOAuth.Messaging { Response response = new Response {
Status = HttpStatusCode.OK,
Headers = headers,
- Body = body.ToArray(),
+ Body = bodyWriter.ToString(),
OriginalMessage = message
};
@@ -322,9 +328,7 @@ namespace DotNetOAuth.Messaging { /// <param name="fields">The fields that would be included in a message.</param>
/// <returns>The size (in bytes) of the message payload.</returns>
private static int CalculateSizeOfPayload(IDictionary<string, string> fields) {
- if (fields == null) {
- throw new ArgumentNullException("fields");
- }
+ Debug.Assert(fields != null, "fields == null");
int size = 0;
foreach (var field in fields) {
diff --git a/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs b/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs index 7a5dc21..6ecc41e 100644 --- a/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs +++ b/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs @@ -41,9 +41,8 @@ namespace DotNetOAuth.Messaging { /// <param name="fields">The dictionary to list values from.</param>
/// <returns>The generated <see cref="XmlReader"/>.</returns>
private static XmlReader CreateRoundtripReader(XName rootElement, IDictionary<string, string> fields) {
- if (rootElement == null) {
- throw new ArgumentNullException("rootElement");
- }
+ Debug.Assert(rootElement != null, "rootElement == null");
+ Debug.Assert(fields != null, "fields == null");
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream);
@@ -66,15 +65,9 @@ namespace DotNetOAuth.Messaging { /// <param name="rootElement">The name of the root element to use to surround the dictionary values.</param>
/// <param name="fields">The dictionary with values to serialize.</param>
private static void SerializeDictionaryToXml(XmlWriter writer, XName rootElement, IDictionary<string, string> fields) {
- if (writer == null) {
- throw new ArgumentNullException("writer");
- }
- if (rootElement == null) {
- throw new ArgumentNullException("rootElement");
- }
- if (fields == null) {
- throw new ArgumentNullException("fields");
- }
+ Debug.Assert(writer != null, "writer == null");
+ Debug.Assert(rootElement != null, "rootElement == null");
+ Debug.Assert(fields != null, "fields == null");
writer.WriteStartElement(rootElement.LocalName, rootElement.NamespaceName);
diff --git a/src/DotNetOAuth/Messaging/HttpRequestInfo.cs b/src/DotNetOAuth/Messaging/HttpRequestInfo.cs index 3654367..e4aabb8 100644 --- a/src/DotNetOAuth/Messaging/HttpRequestInfo.cs +++ b/src/DotNetOAuth/Messaging/HttpRequestInfo.cs @@ -10,6 +10,7 @@ namespace DotNetOAuth.Messaging { using System.IO;
using System.Net;
using System.Web;
+ using System.Diagnostics;
/// <summary>
/// A property store of details of an incoming HTTP request.
@@ -122,9 +123,7 @@ namespace DotNetOAuth.Messaging { /// <param name="pairs">The collection a HTTP headers.</param>
/// <returns>A new collection of the given headers.</returns>
private static WebHeaderCollection GetHeaderCollection(NameValueCollection pairs) {
- if (pairs == null) {
- throw new ArgumentNullException("pairs");
- }
+ Debug.Assert(pairs != null, "pairs == null");
WebHeaderCollection headers = new WebHeaderCollection();
foreach (string key in pairs) {
diff --git a/src/DotNetOAuth/Messaging/MessageSerializer.cs b/src/DotNetOAuth/Messaging/MessageSerializer.cs index ac40232..5a39a61 100644 --- a/src/DotNetOAuth/Messaging/MessageSerializer.cs +++ b/src/DotNetOAuth/Messaging/MessageSerializer.cs @@ -12,6 +12,7 @@ namespace DotNetOAuth.Messaging { using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Linq;
+ using System.Diagnostics;
/// <summary>
/// Serializes/deserializes OAuth messages for/from transit.
@@ -46,9 +47,8 @@ namespace DotNetOAuth.Messaging { /// <param name="messageType">The specific <see cref="IProtocolMessage"/>-derived type
/// that will be serialized and deserialized using this class.</param>
private MessageSerializer(Type messageType) {
- if (messageType == null) {
- throw new ArgumentNullException("messageType");
- }
+ Debug.Assert(messageType != null, "messageType == null");
+
if (!typeof(IProtocolMessage).IsAssignableFrom(messageType)) {
throw new ArgumentException(
string.Format(
diff --git a/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs b/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs index cf722bd..0de92f6 100644 --- a/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs +++ b/src/DotNetOAuth/Messaging/MessagingStrings.Designer.cs @@ -97,6 +97,15 @@ namespace DotNetOAuth.Messaging { }
/// <summary>
+ /// Looks up a localized string similar to This method requires a current HttpContext. Alternatively, use an overload of this method that allows you to pass in information without an HttpContext..
+ /// </summary>
+ internal static string HttpContextRequired {
+ get {
+ return ResourceManager.GetString("HttpContextRequired", resourceCulture);
+ }
+ }
+
+ /// <summary>
/// Looks up a localized string similar to Messages that indicate indirect transport must implement the {0} interface..
/// </summary>
internal static string IndirectMessagesMustImplementIDirectedProtocolMessage {
@@ -122,5 +131,14 @@ namespace DotNetOAuth.Messaging { return ResourceManager.GetString("UnexpectedType", resourceCulture);
}
}
+
+ /// <summary>
+ /// Looks up a localized string similar to {0} property has unrecognized value {1}..
+ /// </summary>
+ internal static string UnrecognizedEnumValue {
+ get {
+ return ResourceManager.GetString("UnrecognizedEnumValue", resourceCulture);
+ }
+ }
}
}
diff --git a/src/DotNetOAuth/Messaging/MessagingStrings.resx b/src/DotNetOAuth/Messaging/MessagingStrings.resx index abc913b..2ac776a 100644 --- a/src/DotNetOAuth/Messaging/MessagingStrings.resx +++ b/src/DotNetOAuth/Messaging/MessagingStrings.resx @@ -129,6 +129,9 @@ <data name="ExceptionNotConstructedForTransit" xml:space="preserve">
<value>This exception was not constructed with a root request message that caused it.</value>
</data>
+ <data name="HttpContextRequired" xml:space="preserve">
+ <value>This method requires a current HttpContext. Alternatively, use an overload of this method that allows you to pass in information without an HttpContext.</value>
+ </data>
<data name="IndirectMessagesMustImplementIDirectedProtocolMessage" xml:space="preserve">
<value>Messages that indicate indirect transport must implement the {0} interface.</value>
</data>
@@ -138,4 +141,7 @@ <data name="UnexpectedType" xml:space="preserve">
<value>The type {0} or a derived type was expected, but {1} was given.</value>
</data>
+ <data name="UnrecognizedEnumValue" xml:space="preserve">
+ <value>{0} property has unrecognized value {1}.</value>
+ </data>
</root>
\ No newline at end of file diff --git a/src/DotNetOAuth/Messaging/Response.cs b/src/DotNetOAuth/Messaging/Response.cs index 68950ff..6413e68 100644 --- a/src/DotNetOAuth/Messaging/Response.cs +++ b/src/DotNetOAuth/Messaging/Response.cs @@ -25,6 +25,14 @@ namespace DotNetOAuth.Messaging { /// </remarks>
public class Response {
/// <summary>
+ /// Initializes an instance of the <see cref="Response"/> class.
+ /// </summary>
+ internal Response() {
+ Status = HttpStatusCode.OK;
+ Headers = new WebHeaderCollection();
+ }
+
+ /// <summary>
/// Gets the headers that must be included in the response to the user agent.
/// </summary>
/// <remarks>
@@ -37,7 +45,7 @@ namespace DotNetOAuth.Messaging { /// <summary>
/// Gets the body of the HTTP response.
/// </summary>
- public byte[] Body { get; internal set; }
+ public string Body { get; internal set; }
/// <summary>
/// Gets the HTTP status code to use in the HTTP response.
@@ -62,11 +70,9 @@ namespace DotNetOAuth.Messaging { HttpContext.Current.Response.Clear();
HttpContext.Current.Response.StatusCode = (int)this.Status;
MessagingUtilities.ApplyHeadersToResponse(this.Headers, HttpContext.Current.Response);
- if (this.Body != null && this.Body.Length > 0) {
- HttpContext.Current.Response.OutputStream.Write(this.Body, 0, this.Body.Length);
- HttpContext.Current.Response.OutputStream.Flush();
+ if (this.Body != null) {
+ HttpContext.Current.Response.Output.Write(Body);
}
- HttpContext.Current.Response.OutputStream.Close();
HttpContext.Current.Response.End();
}
}
diff --git a/src/DotNetOAuth/OAuthChannel.cs b/src/DotNetOAuth/OAuthChannel.cs index 4014ecf..aceaa42 100644 --- a/src/DotNetOAuth/OAuthChannel.cs +++ b/src/DotNetOAuth/OAuthChannel.cs @@ -143,7 +143,7 @@ namespace DotNetOAuth { string responseBody = MessagingUtilities.CreateQueryString(fields);
Response encodedResponse = new Response {
- Body = Encoding.UTF8.GetBytes(responseBody),
+ Body = responseBody,
OriginalMessage = response,
Status = System.Net.HttpStatusCode.OK,
Headers = new System.Net.WebHeaderCollection(),
|