Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: DotNetOpenAuth/DotNetOpenAuth
...
head fork: DotNetOpenAuth/DotNetOpenAuth
  • 3 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
8 src/DotNetOpenAuth.Core/Assumes.cs
@@ -58,6 +58,14 @@ internal static class Assumes {
}
/// <summary>
+ /// Throws an internal error exception.
+ /// </summary>
+ /// <returns>Nothing. This method always throws.</returns>
+ internal static Exception NotReachable() {
+ throw new InternalErrorException();
+ }
+
+ /// <summary>
/// An internal error exception that should never be caught.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1064:ExceptionsShouldBePublic", Justification = "This exception should never be caught.")]
View
1  src/DotNetOpenAuth.Core/DotNetOpenAuth.Core.csproj
@@ -19,6 +19,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Assumes.cs" />
+ <Compile Include="Messaging\Base64WebEncoder.cs" />
<Compile Include="Messaging\Bindings\AsymmetricCryptoKeyStoreWrapper.cs" />
<Compile Include="Messaging\Bindings\CryptoKey.cs" />
<Compile Include="Messaging\Bindings\CryptoKeyCollisionException.cs" />
View
37 src/DotNetOpenAuth.Core/Messaging/Base64WebEncoder.cs
@@ -0,0 +1,37 @@
+//-----------------------------------------------------------------------
+// <copyright file="Base64WebEncoder.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Messaging {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging.Reflection;
+
+ /// <summary>
+ /// A message part encoder that translates between <c>byte[]</c> and base64web encoded strings.
+ /// </summary>
+ internal class Base64WebEncoder : IMessagePartEncoder {
+ /// <summary>
+ /// Encodes the specified value.
+ /// </summary>
+ /// <param name="value">The value. Guaranteed to never be null.</param>
+ /// <returns>The <paramref name="value"/> in string form, ready for message transport.</returns>
+ public string Encode(object value) {
+ return MessagingUtilities.ConvertToBase64WebSafeString((byte[])value);
+ }
+
+ /// <summary>
+ /// Decodes the specified value.
+ /// </summary>
+ /// <param name="value">The string value carried by the transport. Guaranteed to never be null, although it may be empty.</param>
+ /// <returns>The deserialized form of the given string.</returns>
+ /// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception>
+ public object Decode(string value) {
+ return MessagingUtilities.FromBase64WebSafeString(value);
+ }
+ }
+}
View
29 src/DotNetOpenAuth.Core/Messaging/Channel.cs
@@ -38,6 +38,16 @@ public abstract class Channel : IDisposable {
internal static readonly Encoding PostEntityEncoding = new UTF8Encoding(false);
/// <summary>
+ /// A default set of XML dictionary reader quotas that are relatively safe from causing unbounded memory consumption.
+ /// </summary>
+ internal static readonly XmlDictionaryReaderQuotas DefaultUntrustedXmlDictionaryReaderQuotas = new XmlDictionaryReaderQuotas {
+ MaxArrayLength = 1,
+ MaxDepth = 2,
+ MaxBytesPerRead = 8 * 1024,
+ MaxStringContentLength = 16 * 1024,
+ };
+
+ /// <summary>
/// The content-type used on HTTP POST requests where the POST entity is a
/// URL-encoded series of key=value pairs.
/// </summary>
@@ -152,12 +162,7 @@ public abstract class Channel : IDisposable {
this.messageTypeProvider = messageTypeProvider;
this.WebRequestHandler = new StandardWebRequestHandler();
- this.XmlDictionaryReaderQuotas = new XmlDictionaryReaderQuotas {
- MaxArrayLength = 1,
- MaxDepth = 2,
- MaxBytesPerRead = 8 * 1024,
- MaxStringContentLength = 16 * 1024,
- };
+ this.XmlDictionaryReaderQuotas = DefaultUntrustedXmlDictionaryReaderQuotas;
this.outgoingBindingElements = new List<IChannelBindingElement>(ValidateAndPrepareBindingElements(bindingElements));
this.incomingBindingElements = new List<IChannelBindingElement>(this.outgoingBindingElements);
@@ -991,17 +996,7 @@ public TResponse Request<TResponse>(IDirectedProtocolMessage requestMessage)
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No apparent problem. False positive?")]
protected virtual string SerializeAsJson(IMessage message) {
Requires.NotNull(message, "message");
-
- MessageDictionary messageDictionary = this.MessageDescriptions.GetAccessor(message);
- using (var memoryStream = new MemoryStream()) {
- using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(memoryStream, Encoding.UTF8)) {
- MessageSerializer.Serialize(messageDictionary, jsonWriter);
- jsonWriter.Flush();
- }
-
- string json = Encoding.UTF8.GetString(memoryStream.ToArray());
- return json;
- }
+ return MessagingUtilities.SerializeAsJson(message, this.MessageDescriptions);
}
/// <summary>
View
2  src/DotNetOpenAuth.Core/Messaging/MessageSerializer.cs
@@ -74,7 +74,7 @@ internal class MessageSerializer {
}
/// <summary>
- /// Reads the data from a message instance and writes a XML/JSON encoding of it.
+ /// Reads the data from a message instance and writes an XML/JSON encoding of it.
/// </summary>
/// <param name="messageDictionary">The message to be serialized.</param>
/// <param name="writer">The writer to use for the serialized form.</param>
View
126 src/DotNetOpenAuth.Core/Messaging/MessagingUtilities.cs
@@ -16,11 +16,13 @@ namespace DotNetOpenAuth.Messaging {
using System.Linq;
using System.Net;
using System.Net.Mime;
+ using System.Runtime.Serialization.Json;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;
+ using System.Xml;
using DotNetOpenAuth.Messaging.Bindings;
using DotNetOpenAuth.Messaging.Reflection;
@@ -135,6 +137,21 @@ public static class MessagingUtilities {
};
/// <summary>
+ /// The available compression algorithms.
+ /// </summary>
+ internal enum CompressionMethod {
+ /// <summary>
+ /// The Deflate algorithm.
+ /// </summary>
+ Deflate,
+
+ /// <summary>
+ /// The GZip algorithm.
+ /// </summary>
+ Gzip,
+ }
+
+ /// <summary>
/// Transforms an OutgoingWebResponse to an MVC-friendly ActionResult.
/// </summary>
/// <param name="response">The response to send to the user agent.</param>
@@ -838,19 +855,36 @@ public static class MessagingUtilities {
/// Compresses a given buffer.
/// </summary>
/// <param name="buffer">The buffer to compress.</param>
+ /// <param name="method">The compression algorithm to use.</param>
/// <returns>The compressed data.</returns>
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "This Dispose is safe.")]
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No apparent problem. False positive?")]
- internal static byte[] Compress(byte[] buffer) {
+ internal static byte[] Compress(byte[] buffer, CompressionMethod method = CompressionMethod.Deflate) {
Requires.NotNull(buffer, "buffer");
Contract.Ensures(Contract.Result<byte[]>() != null);
using (var ms = new MemoryStream()) {
- using (var compressingStream = new DeflateStream(ms, CompressionMode.Compress, true)) {
+ Stream compressingStream = null;
+ try {
+ switch (method) {
+ case CompressionMethod.Deflate:
+ compressingStream = new DeflateStream(ms, CompressionMode.Compress, true);
+ break;
+ case CompressionMethod.Gzip:
+ compressingStream = new GZipStream(ms, CompressionMode.Compress, true);
+ break;
+ default:
+ Requires.InRange(false, "method");
+ break;
+ }
+
compressingStream.Write(buffer, 0, buffer.Length);
+ return ms.ToArray();
+ } finally {
+ if (compressingStream != null) {
+ compressingStream.Dispose();
+ }
}
-
- return ms.ToArray();
}
}
@@ -858,17 +892,35 @@ public static class MessagingUtilities {
/// Decompresses a given buffer.
/// </summary>
/// <param name="buffer">The buffer to decompress.</param>
+ /// <param name="method">The compression algorithm used.</param>
/// <returns>The decompressed data.</returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No apparent problem. False positive?")]
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "This Dispose is safe.")]
- internal static byte[] Decompress(byte[] buffer) {
+ internal static byte[] Decompress(byte[] buffer, CompressionMethod method = CompressionMethod.Deflate) {
Requires.NotNull(buffer, "buffer");
Contract.Ensures(Contract.Result<byte[]>() != null);
using (var compressedDataStream = new MemoryStream(buffer)) {
using (var decompressedDataStream = new MemoryStream()) {
- using (var decompressingStream = new DeflateStream(compressedDataStream, CompressionMode.Decompress, true)) {
+ Stream decompressingStream = null;
+ try {
+ switch (method) {
+ case CompressionMethod.Deflate:
+ decompressingStream = new DeflateStream(compressedDataStream, CompressionMode.Decompress, true);
+ break;
+ case CompressionMethod.Gzip:
+ decompressingStream = new GZipStream(compressedDataStream, CompressionMode.Decompress, true);
+ break;
+ default:
+ Requires.InRange(false, "method");
+ break;
+ }
+
decompressingStream.CopyTo(decompressedDataStream);
+ } finally {
+ if (decompressingStream != null) {
+ decompressingStream.Dispose();
+ }
}
return decompressedDataStream.ToArray();
@@ -1620,6 +1672,68 @@ public static class MessagingUtilities {
}
/// <summary>
+ /// Serializes the given message as a JSON string.
+ /// </summary>
+ /// <param name="message">The message to serialize.</param>
+ /// <param name="messageDescriptions">The cached message descriptions to use for reflection.</param>
+ /// <returns>A JSON string.</returns>
+ [SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "This Dispose is safe.")]
+ [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No apparent problem. False positive?")]
+ internal static string SerializeAsJson(IMessage message, MessageDescriptionCollection messageDescriptions) {
+ Requires.NotNull(message, "message");
+ Requires.NotNull(messageDescriptions, "messageDescriptions");
+
+ var encoding = Encoding.UTF8;
+ var bytes = SerializeAsJsonBytes(message, messageDescriptions, encoding);
+ string json = encoding.GetString(bytes);
+ return json;
+ }
+
+ /// <summary>
+ /// Serializes the given message as a JSON string.
+ /// </summary>
+ /// <param name="message">The message to serialize.</param>
+ /// <param name="messageDescriptions">The cached message descriptions to use for reflection.</param>
+ /// <param name="encoding">The encoding to use. Defaults to <see cref="Encoding.UTF8"/></param>
+ /// <returns>A JSON string.</returns>
+ [SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "This Dispose is safe.")]
+ [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "No apparent problem. False positive?")]
+ internal static byte[] SerializeAsJsonBytes(IMessage message, MessageDescriptionCollection messageDescriptions, Encoding encoding = null) {
+ Requires.NotNull(message, "message");
+ Requires.NotNull(messageDescriptions, "messageDescriptions");
+
+ encoding = encoding ?? Encoding.UTF8;
+ MessageDictionary messageDictionary = messageDescriptions.GetAccessor(message);
+ using (var memoryStream = new MemoryStream()) {
+ using (var jsonWriter = JsonReaderWriterFactory.CreateJsonWriter(memoryStream, encoding)) {
+ MessageSerializer.Serialize(messageDictionary, jsonWriter);
+ jsonWriter.Flush();
+ }
+
+ return memoryStream.ToArray();
+ }
+ }
+
+ /// <summary>
+ /// Deserializes a JSON object into a message.
+ /// </summary>
+ /// <param name="jsonBytes">The buffer containing the JSON string.</param>
+ /// <param name="receivingMessage">The message to deserialize the object into.</param>
+ /// <param name="messageDescriptions">The cache of message descriptions.</param>
+ /// <param name="encoding">The encoding that the JSON bytes are in.</param>
+ internal static void DeserializeFromJson(byte[] jsonBytes, IMessage receivingMessage, MessageDescriptionCollection messageDescriptions, Encoding encoding = null) {
+ Requires.NotNull(jsonBytes, "jsonBytes");
+ Requires.NotNull(receivingMessage, "receivingMessage");
+ Requires.NotNull(messageDescriptions, "messageDescriptions");
+
+ encoding = encoding ?? Encoding.UTF8;
+ MessageDictionary messageDictionary = messageDescriptions.GetAccessor(receivingMessage);
+ using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(jsonBytes, 0, jsonBytes.Length, encoding, Channel.DefaultUntrustedXmlDictionaryReaderQuotas, null)) {
+ MessageSerializer.Deserialize(messageDictionary, jsonReader);
+ }
+ }
+
+ /// <summary>
/// Prepares what SHOULD be simply a string value for safe injection into Javascript
/// by using appropriate character escaping.
/// </summary>
View
1  src/DotNetOpenAuth.Test/DotNetOpenAuth.Test.csproj
@@ -234,6 +234,7 @@
<Compile Include="Mocks\TestBaseMessage.cs" />
<Compile Include="Mocks\TestDerivedMessage.cs" />
<Compile Include="Mocks\TestDirectResponseMessageWithHttpStatus.cs" />
+ <Compile Include="Mocks\TestMessageWithDate.cs" />
<Compile Include="Mocks\TestReplayProtectedMessage.cs" />
<Compile Include="Mocks\TestDirectedMessage.cs" />
<Compile Include="Mocks\TestBadChannel.cs" />
View
22 src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs
@@ -11,6 +11,7 @@ namespace DotNetOpenAuth.Test.Messaging {
using System.Diagnostics;
using System.IO;
using System.Net;
+ using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using DotNetOpenAuth.Messaging;
@@ -229,6 +230,27 @@ public class MessagingUtilitiesTests : TestBase {
Assert.AreEqual(PlainText, roundTripped);
}
+ [Test]
+ public void SerializeAsJsonTest() {
+ var message = new TestMessageWithDate() {
+ Age = 18,
+ Timestamp = DateTime.Parse("4/28/2012"),
+ Name = "Andrew",
+ };
+ string json = MessagingUtilities.SerializeAsJson(message, this.MessageDescriptions);
+ Assert.That(json, Is.EqualTo("{\"ts\":\"2012-04-28T00:00:00Z\",\"age\":18,\"Name\":\"Andrew\"}"));
+ }
+
+ [Test]
+ public void DeserializeFromJson() {
+ var message = new TestMessageWithDate();
+ string json = "{\"ts\":\"2012-04-28T00:00:00Z\",\"age\":18,\"Name\":\"Andrew\"}";
+ MessagingUtilities.DeserializeFromJson(Encoding.UTF8.GetBytes(json), message, this.MessageDescriptions);
+ Assert.That(message.Age, Is.EqualTo(18));
+ Assert.That(message.Timestamp, Is.EqualTo(DateTime.Parse("4/28/2012")));
+ Assert.That(message.Name, Is.EqualTo("Andrew"));
+ }
+
/// <summary>
/// Verifies that the time-independent string equality check works accurately.
/// </summary>
View
18 src/DotNetOpenAuth.Test/Mocks/TestMessageWithDate.cs
@@ -0,0 +1,18 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestMessageWithDate.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOpenAuth.Messaging;
+
+ internal class TestMessageWithDate : TestBaseMessage {
+ [MessagePart("ts", IsRequired = true)]
+ internal DateTime Timestamp { get; set; }
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.