5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Net.Http.Headers;
31 using System.Threading.Tasks;
34 namespace System.Net.Http
36 public abstract class HttpContent : IDisposable
38 sealed class FixedMemoryStream : MemoryStream
40 readonly long maxSize;
42 public FixedMemoryStream (long maxSize)
45 this.maxSize = maxSize;
48 void CheckOverflow (int count)
50 if (Length + count > maxSize)
51 throw new HttpRequestException (string.Format ("Cannot write more bytes to the buffer than the configured maximum buffer size: {0}", maxSize));
54 public override void WriteByte (byte value)
57 base.WriteByte (value);
60 public override void Write (byte[] buffer, int offset, int count)
62 CheckOverflow (count);
63 base.Write (buffer, offset, count);
67 FixedMemoryStream buffer;
70 HttpContentHeaders headers;
72 public HttpContentHeaders Headers {
74 return headers ?? (headers = new HttpContentHeaders (this));
78 internal long? LoadedBufferLength {
80 return buffer == null ? (long?)null : buffer.Length;
84 // Only used by HttpWebRequest internals which is not async friendly
85 internal void CopyTo (Stream stream)
87 CopyToAsync (stream).Wait ();
90 public Task CopyToAsync (Stream stream)
92 return CopyToAsync (stream, null);
95 public Task CopyToAsync (Stream stream, TransportContext context)
98 throw new ArgumentNullException ("stream");
101 return buffer.CopyToAsync (stream);
103 return SerializeToStreamAsync (stream, context);
106 protected async virtual Task<Stream> CreateContentReadStreamAsync ()
108 await LoadIntoBufferAsync ().ConfigureAwait (false);
112 static FixedMemoryStream CreateFixedMemoryStream (long maxBufferSize)
114 return new FixedMemoryStream (maxBufferSize);
117 public void Dispose ()
122 protected virtual void Dispose (bool disposing)
124 if (disposing && !disposed) {
132 public Task LoadIntoBufferAsync ()
134 return LoadIntoBufferAsync (int.MaxValue);
137 public async Task LoadIntoBufferAsync (long maxBufferSize)
140 throw new ObjectDisposedException (GetType ().ToString ());
145 buffer = CreateFixedMemoryStream (maxBufferSize);
146 await SerializeToStreamAsync (buffer, null).ConfigureAwait (false);
147 buffer.Seek (0, SeekOrigin.Begin);
150 public async Task<Stream> ReadAsStreamAsync ()
153 throw new ObjectDisposedException (GetType ().ToString ());
156 return new MemoryStream (buffer.GetBuffer (), 0, (int)buffer.Length, false);
159 stream = await CreateContentReadStreamAsync ().ConfigureAwait (false);
164 public async Task<byte[]> ReadAsByteArrayAsync ()
166 await LoadIntoBufferAsync ().ConfigureAwait (false);
167 return buffer.ToArray ();
170 public async Task<string> ReadAsStringAsync ()
172 await LoadIntoBufferAsync ().ConfigureAwait (false);
173 if (buffer.Length == 0)
176 var buf = buffer.GetBuffer ();
177 var buf_length = (int) buffer.Length;
178 int preambleLength = 0;
181 if (headers != null && headers.ContentType != null && headers.ContentType.CharSet != null) {
182 encoding = Encoding.GetEncoding (headers.ContentType.CharSet);
183 preambleLength = StartsWith (buf, buf_length, encoding.GetPreamble ());
185 encoding = GetEncodingFromBuffer (buf, buf_length, ref preambleLength) ?? Encoding.UTF8;
188 return encoding.GetString (buf, preambleLength, buf_length - preambleLength);
191 static Encoding GetEncodingFromBuffer (byte[] buffer, int length, ref int preambleLength)
193 var encodings_with_preamble = new [] { Encoding.UTF8, Encoding.UTF32, Encoding.Unicode };
194 foreach (var enc in encodings_with_preamble) {
195 if ((preambleLength = StartsWith (buffer, length, enc.GetPreamble ())) != 0)
202 static int StartsWith (byte[] array, int length, byte[] value)
204 if (length < value.Length)
207 for (int i = 0; i < value.Length; ++i) {
208 if (array [i] != value [i])
215 protected internal abstract Task SerializeToStreamAsync (Stream stream, TransportContext context);
216 protected internal abstract bool TryComputeLength (out long length);