Merge pull request #1593 from kumpera/new_suspend
[mono.git] / mcs / class / System.Net.Http / System.Net.Http / HttpContent.cs
1 //
2 // HttpContent.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System.Net.Http.Headers;
30 using System.IO;
31 using System.Threading.Tasks;
32 using System.Text;
33
34 namespace System.Net.Http
35 {
36         public abstract class HttpContent : IDisposable
37         {
38                 sealed class FixedMemoryStream : MemoryStream
39                 {
40                         readonly long maxSize;
41                         
42                         public FixedMemoryStream (long maxSize)
43                                 : base ()
44                         {
45                                 this.maxSize = maxSize;
46                         }
47                         
48                         void CheckOverflow (int count)
49                         {
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));
52                         }
53                         
54                         public override void WriteByte (byte value)
55                         {
56                                 CheckOverflow (1);
57                                 base.WriteByte (value);
58                         }
59                         
60                         public override void Write (byte[] buffer, int offset, int count)
61                         {
62                                 CheckOverflow (count);
63                                 base.Write (buffer, offset, count);
64                         }
65                 }
66                 
67                 FixedMemoryStream buffer;
68                 Stream stream;
69                 bool disposed;
70                 HttpContentHeaders headers;
71
72                 public HttpContentHeaders Headers {
73                         get {
74                                 return headers ?? (headers = new HttpContentHeaders (this));
75                         }
76                 }
77
78                 internal long? LoadedBufferLength {
79                         get {
80                                 return buffer == null ? (long?)null : buffer.Length;
81                         }
82                 }
83
84                 public Task CopyToAsync (Stream stream)
85                 {
86                         return CopyToAsync (stream, null);
87                 }
88
89                 public Task CopyToAsync (Stream stream, TransportContext context)
90                 {
91                         if (stream == null)
92                                 throw new ArgumentNullException ("stream");
93
94                         if (buffer != null)
95                                 return buffer.CopyToAsync (stream);
96
97                         return SerializeToStreamAsync (stream, context);
98                 }
99
100                 protected async virtual Task<Stream> CreateContentReadStreamAsync ()
101                 {
102                         await LoadIntoBufferAsync ().ConfigureAwait (false);
103                         return buffer;
104                 }
105                 
106                 static FixedMemoryStream CreateFixedMemoryStream (long maxBufferSize)
107                 {
108                         return new FixedMemoryStream (maxBufferSize);
109                 }
110
111                 public void Dispose ()
112                 {
113                         Dispose (true);
114                 }
115                 
116                 protected virtual void Dispose (bool disposing)
117                 {
118                         if (disposing && !disposed) {
119                                 disposed = true;
120
121                                 if (buffer != null)
122                                         buffer.Dispose ();
123                         }
124                 }
125
126                 public Task LoadIntoBufferAsync ()
127                 {
128                         return LoadIntoBufferAsync (int.MaxValue);
129                 }
130
131                 public async Task LoadIntoBufferAsync (long maxBufferSize)
132                 {
133                         if (disposed)
134                                 throw new ObjectDisposedException (GetType ().ToString ());
135
136                         if (buffer != null)
137                                 return;
138
139                         buffer = CreateFixedMemoryStream (maxBufferSize);
140                         await SerializeToStreamAsync (buffer, null).ConfigureAwait (false);
141                         buffer.Seek (0, SeekOrigin.Begin);
142                 }
143                 
144                 public async Task<Stream> ReadAsStreamAsync ()
145                 {
146                         if (disposed)
147                                 throw new ObjectDisposedException (GetType ().ToString ());
148
149                         if (buffer != null)
150                                 return new MemoryStream (buffer.GetBuffer (), 0, (int)buffer.Length, false);
151
152                         if (stream == null)
153                                 stream = await CreateContentReadStreamAsync ().ConfigureAwait (false);
154
155                         return stream;
156                 }
157
158                 public async Task<byte[]> ReadAsByteArrayAsync ()
159                 {
160                         await LoadIntoBufferAsync ().ConfigureAwait (false);
161                         return buffer.ToArray ();
162                 }
163
164                 public async Task<string> ReadAsStringAsync ()
165                 {
166                         await LoadIntoBufferAsync ().ConfigureAwait (false);
167                         if (buffer.Length == 0)
168                                 return string.Empty;
169
170                         var buf = buffer.GetBuffer ();
171                         var buf_length = (int) buffer.Length;
172                         int preambleLength = 0;
173                         Encoding encoding;
174
175                         if (headers != null && headers.ContentType != null && headers.ContentType.CharSet != null) {
176                                 encoding = Encoding.GetEncoding (headers.ContentType.CharSet);
177                                 preambleLength = StartsWith (buf, buf_length, encoding.GetPreamble ());
178                         } else {
179                                 encoding = GetEncodingFromBuffer (buf, buf_length, ref preambleLength) ?? Encoding.UTF8;
180                         }
181
182                         return encoding.GetString (buf, preambleLength, buf_length - preambleLength);
183                 }
184
185                 static Encoding GetEncodingFromBuffer (byte[] buffer, int length, ref int preambleLength)
186                 {
187                         var encodings_with_preamble = new [] { Encoding.UTF8, Encoding.UTF32, Encoding.Unicode };
188                         foreach (var enc in encodings_with_preamble) {
189                                 if ((preambleLength = StartsWith (buffer, length, enc.GetPreamble ())) != 0)
190                                         return enc;
191                         }
192
193                         return null;
194                 }
195
196                 static int StartsWith (byte[] array, int length, byte[] value)
197                 {
198                         if (length < value.Length)
199                                 return 0;
200
201                         for (int i = 0; i < value.Length; ++i) {
202                                 if (array [i] != value [i])
203                                         return 0;
204                         }
205
206                         return value.Length;
207                 }
208
209                 protected internal abstract Task SerializeToStreamAsync (Stream stream, TransportContext context);
210                 protected internal abstract bool TryComputeLength (out long length);
211         }
212 }