This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Introducing XML Input Formatters.
2. Adding DelegatingStream class 3. Unit + Functional tests for formatters.
- Loading branch information
sornaks
committed
Jul 29, 2014
1 parent
43d1253
commit cee73c0
Showing
24 changed files
with
1,395 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
189 changes: 189 additions & 0 deletions
189
src/Microsoft.AspNet.Mvc.ModelBinding/Formatters/DelegatingStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNet.Mvc.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Stream that delegates to an inner stream. | ||
/// This Stream is present so that the inner stream is not closed | ||
/// even when Close() or Dispose() is called. | ||
/// </summary> | ||
public class DelegatingStream : Stream | ||
{ | ||
private readonly Stream _innerStream; | ||
|
||
/// <summary> | ||
/// Initializes a new object of DelegatingStream | ||
/// </summary> | ||
/// <param name="innerStream">The stream on which should not be closed</param> | ||
public DelegatingStream([NotNull] Stream innerStream) | ||
{ | ||
_innerStream = innerStream; | ||
} | ||
|
||
protected Stream InnerStream | ||
{ | ||
get { return _innerStream; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool CanRead | ||
{ | ||
get { return _innerStream.CanRead; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool CanSeek | ||
{ | ||
get { return _innerStream.CanSeek; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool CanWrite | ||
{ | ||
get { return _innerStream.CanWrite; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override long Length | ||
{ | ||
get { return _innerStream.Length; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override long Position | ||
{ | ||
get { return _innerStream.Position; } | ||
set { _innerStream.Position = value; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int ReadTimeout | ||
{ | ||
get { return _innerStream.ReadTimeout; } | ||
set { _innerStream.ReadTimeout = value; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool CanTimeout | ||
{ | ||
get { return _innerStream.CanTimeout; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int WriteTimeout | ||
{ | ||
get { return _innerStream.WriteTimeout; } | ||
set { _innerStream.WriteTimeout = value; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
return _innerStream.Seek(offset, origin); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
return _innerStream.Read(buffer, offset, count); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); | ||
} | ||
#if NET45 | ||
/// <inheritdoc /> | ||
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, | ||
AsyncCallback callback, object state) | ||
{ | ||
return _innerStream.BeginRead(buffer, offset, count, callback, state); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int EndRead(IAsyncResult asyncResult) | ||
{ | ||
return _innerStream.EndRead(asyncResult); | ||
} | ||
#endif | ||
/// <inheritdoc /> | ||
public override int ReadByte() | ||
{ | ||
return _innerStream.ReadByte(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Flush() | ||
{ | ||
_innerStream.Flush(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Task FlushAsync(CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.FlushAsync(cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void SetLength(long value) | ||
{ | ||
_innerStream.SetLength(value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(byte[] buffer, int offset, int count) | ||
{ | ||
_innerStream.Write(buffer, offset, count); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); | ||
} | ||
#if NET45 | ||
/// <inheritdoc /> | ||
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, | ||
AsyncCallback callback, object state) | ||
{ | ||
return _innerStream.BeginWrite(buffer, offset, count, callback, state); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void EndWrite(IAsyncResult asyncResult) | ||
{ | ||
_innerStream.EndWrite(asyncResult); | ||
} | ||
#endif | ||
/// <inheritdoc /> | ||
public override void WriteByte(byte value) | ||
{ | ||
_innerStream.WriteByte(value); | ||
} | ||
#if NET45 | ||
/// <inheritdoc /> | ||
public override void Close() | ||
{ | ||
} | ||
#endif | ||
/// <inheritdoc /> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
// No-op. In CoreCLR this is equivalent to Close. | ||
// Given that we don't own the underlying stream, we never want to do anything interesting here. | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Microsoft.AspNet.Mvc.ModelBinding/Formatters/FormattingUtilities.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Xml; | ||
|
||
namespace Microsoft.AspNet.Mvc.ModelBinding | ||
{ | ||
/// <summary> | ||
/// Contains methods which are used by input formatters. | ||
/// </summary> | ||
public static class FormattingUtilities | ||
{ | ||
public static readonly int DefaultMaxDepth = 32; | ||
|
||
/// <summary> | ||
/// Gets the default Reader Quotas for XmlReader. | ||
/// </summary> | ||
/// <returns>XmlReaderQuotas with default values</returns> | ||
public static XmlDictionaryReaderQuotas GetDefaultXmlReaderQuotas() | ||
{ | ||
#if NET45 | ||
return new XmlDictionaryReaderQuotas() | ||
{ | ||
MaxArrayLength = Int32.MaxValue, | ||
MaxBytesPerRead = Int32.MaxValue, | ||
MaxDepth = DefaultMaxDepth, | ||
MaxNameTableCharCount = Int32.MaxValue, | ||
MaxStringContentLength = Int32.MaxValue | ||
}; | ||
#else | ||
return XmlDictionaryReaderQuotas.Max; | ||
#endif | ||
} | ||
|
||
/// Internal because ContentTypeHeaderValue is internal. | ||
internal static Encoding SelectCharacterEncoding(IList<Encoding> supportedEncodings, | ||
ContentTypeHeaderValue contentType, Type callerType) | ||
{ | ||
if (contentType != null) | ||
{ | ||
// Find encoding based on content type charset parameter | ||
var charset = contentType.CharSet; | ||
if (!string.IsNullOrWhiteSpace(contentType.CharSet)) | ||
{ | ||
for (var i = 0; i < supportedEncodings.Count; i++) | ||
{ | ||
var supportedEncoding = supportedEncodings[i]; | ||
if (string.Equals(charset, supportedEncoding.WebName, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return supportedEncoding; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (supportedEncodings.Count > 0) | ||
{ | ||
return supportedEncodings[0]; | ||
} | ||
|
||
// No supported encoding was found so there is no way for us to start reading. | ||
throw new InvalidOperationException(Resources.FormatMediaTypeFormatterNoEncoding(callerType.FullName)); | ||
} | ||
} | ||
} |
Oops, something went wrong.