copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[mono.git] / mcs / class / System / System.Net / WebConnectionStream.cs
1 //
2 // System.Net.WebConnectionStream
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 // (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.IO;
33 using System.Text;
34 using System.Threading;
35
36 namespace System.Net
37 {
38         class WebConnectionStream : Stream
39         {
40                 static byte [] crlf = new byte [] { 13, 10 };
41                 bool isRead;
42                 WebConnection cnc;
43                 HttpWebRequest request;
44                 byte [] readBuffer;
45                 int readBufferOffset;
46                 int readBufferSize;
47                 int contentLength;
48                 int totalRead;
49                 long totalWritten;
50                 bool nextReadCalled;
51                 int pendingReads;
52                 int pendingWrites;
53                 ManualResetEvent pending;
54                 bool allowBuffering;
55                 bool sendChunked;
56                 MemoryStream writeBuffer;
57                 bool requestWritten;
58                 byte [] headers;
59                 bool disposed;
60                 bool headersSent;
61                 object locker = new object ();
62                 bool initRead;
63                 bool read_eof;
64                 bool complete_request_written;
65                 int read_timeout;
66                 int write_timeout;
67
68                 public WebConnectionStream (WebConnection cnc)
69                 {
70                         isRead = true;
71                         pending = new ManualResetEvent (true);
72                         this.request = cnc.Data.request;
73                         read_timeout = request.ReadWriteTimeout;
74                         write_timeout = read_timeout;
75                         this.cnc = cnc;
76                         string contentType = cnc.Data.Headers ["Transfer-Encoding"];
77                         bool chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
78                         string clength = cnc.Data.Headers ["Content-Length"];
79                         if (!chunkedRead && clength != null && clength != "") {
80                                 try {
81                                         contentLength = Int32.Parse (clength);
82                                         if (contentLength == 0 && !IsNtlmAuth ()) {
83                                                 ReadAll ();
84                                         }
85                                 } catch {
86                                         contentLength = Int32.MaxValue;
87                                 }
88                         } else {
89                                 contentLength = Int32.MaxValue;
90                         }
91                 }
92
93                 public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
94                 {
95                         read_timeout = request.ReadWriteTimeout;
96                         write_timeout = read_timeout;
97                         isRead = false;
98                         this.cnc = cnc;
99                         this.request = request;
100                         allowBuffering = request.InternalAllowBuffering;
101                         sendChunked = request.SendChunked;
102                         if (sendChunked)
103                                 pending = new ManualResetEvent (true);
104                         else if (allowBuffering)
105                                 writeBuffer = new MemoryStream ();
106                 }
107
108                 bool IsNtlmAuth ()
109                 {
110                         bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
111                         string header_name = (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate";
112                         string authHeader = cnc.Data.Headers [header_name];
113                         return (authHeader != null && authHeader.IndexOf ("NTLM") != -1);
114                 }
115
116                 internal void CheckResponseInBuffer ()
117                 {
118                         if (contentLength > 0 && (readBufferSize - readBufferOffset) >= contentLength) {
119                                 if (!IsNtlmAuth ())
120                                         ReadAll ();
121                         }
122                 }
123
124                 internal HttpWebRequest Request {
125                         get { return request; }
126                 }
127
128                 internal WebConnection Connection {
129                         get { return cnc; }
130                 }
131 #if NET_2_0
132                 public override bool CanTimeout {
133                         get { return true; }
134                 }
135 #endif
136
137 #if NET_2_0
138                 public override
139 #endif
140                 int ReadTimeout {
141                         get {
142                                 return read_timeout;
143                         }
144
145                         set {
146                                 if (value < -1)
147                                         throw new ArgumentOutOfRangeException ("value");
148                                 read_timeout = value;
149                         }
150                 }
151
152 #if NET_2_0
153                 public override
154 #endif
155                 int WriteTimeout {
156                         get {
157                                 return write_timeout;
158                         }
159
160                         set {
161                                 if (value < -1)
162                                         throw new ArgumentOutOfRangeException ("value");
163                                 write_timeout = value;
164                         }
165                 }
166
167                 internal bool CompleteRequestWritten {
168                         get { return complete_request_written; }
169                 }
170
171                 internal bool SendChunked {
172                         set { sendChunked = value; }
173                 }
174
175                 internal byte [] ReadBuffer {
176                         set { readBuffer = value; }
177                 }
178
179                 internal int ReadBufferOffset {
180                         set { readBufferOffset = value;}
181                 }
182                 
183                 internal int ReadBufferSize {
184                         set { readBufferSize = value; }
185                 }
186                 
187                 internal byte[] WriteBuffer {
188                         get { return writeBuffer.GetBuffer (); }
189                 }
190
191                 internal int WriteBufferLength {
192                         get { return writeBuffer != null ? (int) writeBuffer.Length : (-1); }
193                 }
194
195                 internal void ForceCompletion ()
196                 {
197                         if (!nextReadCalled) {
198                                 if (contentLength == Int32.MaxValue)
199                                         contentLength = 0;
200                                 nextReadCalled = true;
201                                 cnc.NextRead ();
202                         }
203                 }
204                 
205                 internal void CheckComplete ()
206                 {
207                         bool nrc = nextReadCalled;
208                         if (!nrc && readBufferSize - readBufferOffset == contentLength) {
209                                 nextReadCalled = true;
210                                 cnc.NextRead ();
211                         }
212                 }
213
214                 internal void ReadAll ()
215                 {
216                         if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
217                                 if (isRead && !nextReadCalled) {
218                                         nextReadCalled = true;
219                                         cnc.NextRead ();
220                                 }
221                                 return;
222                         }
223
224                         pending.WaitOne ();
225                         lock (locker) {
226                                 if (totalRead >= contentLength)
227                                         return;
228                                 
229                                 byte [] b = null;
230                                 int diff = readBufferSize - readBufferOffset;
231                                 int new_size;
232
233                                 if (contentLength == Int32.MaxValue) {
234                                         MemoryStream ms = new MemoryStream ();
235                                         byte [] buffer = null;
236                                         if (readBuffer != null && diff > 0) {
237                                                 ms.Write (readBuffer, readBufferOffset, diff);
238                                                 if (readBufferSize >= 8192)
239                                                         buffer = readBuffer;
240                                         }
241
242                                         if (buffer == null)
243                                                 buffer = new byte [8192];
244
245                                         int read;
246                                         while ((read = cnc.Read (request, buffer, 0, buffer.Length)) != 0)
247                                                 ms.Write (buffer, 0, read);
248
249                                         b = ms.GetBuffer ();
250                                         new_size = (int) ms.Length;
251                                         contentLength = new_size;
252                                 } else {
253                                         new_size = contentLength - totalRead;
254                                         b = new byte [new_size];
255                                         if (readBuffer != null && diff > 0) {
256                                                 if (diff > new_size)
257                                                         diff = new_size;
258
259                                                 Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
260                                         }
261                                         
262                                         int remaining = new_size - diff;
263                                         int r = -1;
264                                         while (remaining > 0 && r != 0) {
265                                                 r = cnc.Read (request, b, diff, remaining);
266                                                 remaining -= r;
267                                                 diff += r;
268                                         }
269                                 }
270
271                                 readBuffer = b;
272                                 readBufferOffset = 0;
273                                 readBufferSize = new_size;
274                                 totalRead = 0;
275                                 nextReadCalled = true;
276                         }
277
278                         cnc.NextRead ();
279                 }
280
281                 void WriteCallbackWrapper (IAsyncResult r)
282                 {
283                         WebAsyncResult result = r as WebAsyncResult;
284                         if (result != null && result.AsyncWriteAll)
285                                 return;
286
287                         if (r.AsyncState != null) {
288                                 result = (WebAsyncResult) r.AsyncState;
289                                 result.InnerAsyncResult = r;
290                                 result.DoCallback ();
291                         } else {
292                                 EndWrite (r);
293                         }
294                 }
295
296                 void ReadCallbackWrapper (IAsyncResult r)
297                 {
298                         WebAsyncResult result;
299                         if (r.AsyncState != null) {
300                                 result = (WebAsyncResult) r.AsyncState;
301                                 result.InnerAsyncResult = r;
302                                 result.DoCallback ();
303                         } else {
304                                 EndRead (r);
305                         }
306                 }
307
308                 public override int Read (byte [] buffer, int offset, int size)
309                 {
310                         AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
311                         WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
312                         if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
313                                 nextReadCalled = true;
314                                 cnc.Close (true);
315                                 throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
316                         }
317
318                         return EndRead (res);
319                 }
320
321                 public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
322                                                         AsyncCallback cb, object state)
323                 {
324                         if (!isRead)
325                                 throw new NotSupportedException ("this stream does not allow reading");
326
327                         if (buffer == null)
328                                 throw new ArgumentNullException ("buffer");
329
330                         int length = buffer.Length;
331                         if (offset < 0 || length < offset)
332                                 throw new ArgumentOutOfRangeException ("offset");
333                         if (size < 0 || (length - offset) < size)
334                                 throw new ArgumentOutOfRangeException ("size");
335
336                         lock (locker) {
337                                 pendingReads++;
338                                 pending.Reset ();
339                         }
340
341                         WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
342                         if (totalRead >= contentLength) {
343                                 result.SetCompleted (true, -1);
344                                 result.DoCallback ();
345                                 return result;
346                         }
347                         
348                         int remaining = readBufferSize - readBufferOffset;
349                         if (remaining > 0) {
350                                 int copy = (remaining > size) ? size : remaining;
351                                 Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
352                                 readBufferOffset += copy;
353                                 offset += copy;
354                                 size -= copy;
355                                 totalRead += copy;
356                                 if (size == 0 || totalRead >= contentLength) {
357                                         result.SetCompleted (true, copy);
358                                         result.DoCallback ();
359                                         return result;
360                                 }
361                                 result.NBytes = copy;
362                         }
363
364                         if (cb != null)
365                                 cb = new AsyncCallback (ReadCallbackWrapper);
366
367                         if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
368                                 size = contentLength - totalRead;
369
370                         if (!read_eof) {
371                                 result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
372                         } else {
373                                 result.SetCompleted (true, result.NBytes);
374                                 result.DoCallback ();
375                         }
376                         return result;
377                 }
378
379                 public override int EndRead (IAsyncResult r)
380                 {
381                         WebAsyncResult result = (WebAsyncResult) r;
382                         if (result.EndCalled) {
383                                 int xx = result.NBytes;
384                                 return (xx >= 0) ? xx : 0;
385                         }
386
387                         result.EndCalled = true;
388
389                         if (!result.IsCompleted) {
390                                 int nbytes = -1;
391                                 try {
392                                         nbytes = cnc.EndRead (request, result);
393                                 } catch (Exception exc) {
394                                         lock (locker) {
395                                                 pendingReads--;
396                                                 if (pendingReads == 0)
397                                                         pending.Set ();
398                                         }
399
400                                         nextReadCalled = true;
401                                         cnc.Close (true);
402                                         result.SetCompleted (false, exc);
403                                         result.DoCallback ();
404                                         throw;
405                                 }
406
407                                 if (nbytes < 0) {
408                                         nbytes = 0;
409                                         read_eof = true;
410                                 }
411
412                                 totalRead += nbytes;
413                                 result.SetCompleted (false, nbytes + result.NBytes);
414                                 result.DoCallback ();
415                                 if (nbytes == 0)
416                                         contentLength = totalRead;
417                         }
418
419                         lock (locker) {
420                                 pendingReads--;
421                                 if (pendingReads == 0)
422                                         pending.Set ();
423                         }
424
425                         if (totalRead >= contentLength && !nextReadCalled)
426                                 ReadAll ();
427
428                         int nb = result.NBytes;
429                         return (nb >= 0) ? nb : 0;
430                 }
431
432                 void WriteRequestAsyncCB (IAsyncResult r)
433                 {
434                         WebAsyncResult result = (WebAsyncResult) r.AsyncState;
435                         try {
436                                 cnc.EndWrite2 (request, r);
437                                 result.SetCompleted (false, 0);
438                                 if (!initRead) {
439                                         initRead = true;
440                                         WebConnection.InitRead (cnc);
441                                 }
442                         } catch (Exception e) {
443                                 KillBuffer ();
444                                 nextReadCalled = true;
445                                 cnc.Close (true);
446                                 if (e is System.Net.Sockets.SocketException)
447                                         e = new IOException ("Error writing request", e);
448                                 result.SetCompleted (false, e);
449                         }
450                         complete_request_written = true;
451                         result.DoCallback ();
452                 }
453
454                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
455                                                         AsyncCallback cb, object state)
456                 {
457                         if (request.Aborted)
458                                 throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);
459
460                         if (isRead)
461                                 throw new NotSupportedException ("this stream does not allow writing");
462
463                         if (buffer == null)
464                                 throw new ArgumentNullException ("buffer");
465
466                         int length = buffer.Length;
467                         if (offset < 0 || length < offset)
468                                 throw new ArgumentOutOfRangeException ("offset");
469                         if (size < 0 || (length - offset) < size)
470                                 throw new ArgumentOutOfRangeException ("size");
471
472                         if (sendChunked) {
473                                 lock (locker) {
474                                         pendingWrites++;
475                                         pending.Reset ();
476                                 }
477                         }
478
479                         WebAsyncResult result = new WebAsyncResult (cb, state);
480                         if (!sendChunked)
481                                 CheckWriteOverflow (request.ContentLength, totalWritten, size);
482                         if (allowBuffering && !sendChunked) {
483                                 if (writeBuffer == null)
484                                         writeBuffer = new MemoryStream ();
485                                 writeBuffer.Write (buffer, offset, size);
486                                 totalWritten += size;
487                                 if (request.ContentLength > 0 && totalWritten == request.ContentLength) {
488                                         try {
489                                                 result.AsyncWriteAll = true;
490                                                 result.InnerAsyncResult = WriteRequestAsync (new AsyncCallback (WriteRequestAsyncCB), result);
491                                                 if (result.InnerAsyncResult == null) {
492                                                         if (!result.IsCompleted)
493                                                                 result.SetCompleted (true, 0);
494                                                         result.DoCallback ();
495                                                 }
496                                         } catch (Exception exc) {
497                                                 result.SetCompleted (true, exc);
498                                                 result.DoCallback ();
499                                         }
500                                 } else {
501                                         result.SetCompleted (true, 0);
502                                         result.DoCallback ();
503                                 }
504                                 return result;
505                         }
506
507                         AsyncCallback callback = null;
508                         if (cb != null)
509                                 callback = new AsyncCallback (WriteCallbackWrapper);
510
511                         if (sendChunked) {
512                                 WriteRequest ();
513
514                                 string cSize = String.Format ("{0:X}\r\n", size);
515                                 byte [] head = Encoding.ASCII.GetBytes (cSize);
516                                 int chunkSize = 2 + size + head.Length;
517                                 byte [] newBuffer = new byte [chunkSize];
518                                 Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
519                                 Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
520                                 Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
521
522                                 buffer = newBuffer;
523                                 offset = 0;
524                                 size = chunkSize;
525                         }
526
527                         result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
528                         totalWritten += size;
529                         return result;
530                 }
531
532                 void CheckWriteOverflow (long contentLength, long totalWritten, long size)
533                 {
534                         if (contentLength == -1)
535                                 return;
536
537                         long avail = contentLength - totalWritten;
538                         if (size > avail) {
539                                 KillBuffer ();
540                                 nextReadCalled = true;
541                                 cnc.Close (true);
542                                 throw new ProtocolViolationException (
543                                         "The number of bytes to be written is greater than " +
544                                         "the specified ContentLength.");
545                         }
546                 }
547
548                 public override void EndWrite (IAsyncResult r)
549                 {
550                         if (r == null)
551                                 throw new ArgumentNullException ("r");
552
553                         WebAsyncResult result = r as WebAsyncResult;
554                         if (result == null)
555                                 throw new ArgumentException ("Invalid IAsyncResult");
556
557                         if (result.EndCalled)
558                                 return;
559
560                         result.EndCalled = true;
561                         if (result.AsyncWriteAll) {
562                                 result.WaitUntilComplete ();
563                                 if (result.GotException)
564                                         throw result.Exception;
565                                 return;
566                         }
567
568                         if (allowBuffering && !sendChunked)
569                                 return;
570
571                         if (result.GotException)
572                                 throw result.Exception;
573
574                         try {
575                                 cnc.EndWrite2 (request, result.InnerAsyncResult);
576                                 result.SetCompleted (false, 0);
577                                 result.DoCallback ();
578                         } catch (Exception e) {
579                                 result.SetCompleted (false, e);
580                                 result.DoCallback ();
581                                 throw;
582                         } finally {
583                                 if (sendChunked) {
584                                         lock (locker) {
585                                                 pendingWrites--;
586                                                 if (pendingWrites == 0)
587                                                         pending.Set ();
588                                         }
589                                 }
590                         }
591                 }
592                 
593                 public override void Write (byte [] buffer, int offset, int size)
594                 {
595                         AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
596                         WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
597                         if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
598                                 KillBuffer ();
599                                 nextReadCalled = true;
600                                 cnc.Close (true);
601                                 throw new IOException ("Write timed out.");
602                         }
603
604                         EndWrite (res);
605                 }
606
607                 public override void Flush ()
608                 {
609                 }
610
611                 internal void SetHeaders (byte [] buffer)
612                 {
613                         if (headersSent)
614                                 return;
615
616                         headers = buffer;
617                         long cl = request.ContentLength;
618                         string method = request.Method;
619                         bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
620                                                 method == "TRACE" || method == "DELETE");
621                         if (sendChunked || cl > -1 || no_writestream) {
622                                 WriteHeaders ();
623                                 if (!initRead) {
624                                         initRead = true;
625                                         WebConnection.InitRead (cnc);
626                                 }
627                                 if (!sendChunked && cl == 0)
628                                         requestWritten = true;
629                         }
630                 }
631
632                 internal bool RequestWritten {
633                         get { return requestWritten; }
634                 }
635
636                 IAsyncResult WriteRequestAsync (AsyncCallback cb, object state)
637                 {
638                         requestWritten = true;
639                         byte [] bytes = writeBuffer.GetBuffer ();
640                         int length = (int) writeBuffer.Length;
641                         // Headers already written to the stream
642                         return (length > 0) ? cnc.BeginWrite (request, bytes, 0, length, cb, state) : null;
643                 }
644
645                 void WriteHeaders ()
646                 {
647                         if (headersSent)
648                                 return;
649
650                         headersSent = true;
651                         string err_msg = null;
652                         if (!cnc.Write (request, headers, 0, headers.Length, ref err_msg))
653                                 throw new WebException ("Error writing request: " + err_msg, null, WebExceptionStatus.SendFailure, null);
654                 }
655
656                 internal void WriteRequest ()
657                 {
658                         if (requestWritten)
659                                 return;
660
661                         requestWritten = true;
662                         if (sendChunked)
663                                 return;
664
665                         if (!allowBuffering || writeBuffer == null)
666                                 return;
667
668                         byte [] bytes = writeBuffer.GetBuffer ();
669                         int length = (int) writeBuffer.Length;
670                         if (request.ContentLength != -1 && request.ContentLength < length) {
671                                 nextReadCalled = true;
672                                 cnc.Close (true);
673                                 throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
674                                                         WebExceptionStatus.ServerProtocolViolation, null);
675                         }
676
677                         if (!headersSent) {
678                                 string method = request.Method;
679                                 bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
680                                                         method == "TRACE" || method == "DELETE");
681                                 if (!no_writestream)
682                                         request.InternalContentLength = length;
683                                 request.SendRequestHeaders (true);
684                         }
685                         WriteHeaders ();
686                         if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
687                                 return;
688                                 
689                         IAsyncResult result = null;
690                         if (length > 0)
691                                 result = cnc.BeginWrite (request, bytes, 0, length, null, null);
692                         
693                         if (!initRead) {
694                                 initRead = true;
695                                 WebConnection.InitRead (cnc);
696                         }
697
698                         if (length > 0) 
699                                 complete_request_written = cnc.EndWrite (request, result);
700                         else
701                                 complete_request_written = true;
702                 }
703
704                 internal void InternalClose ()
705                 {
706                         disposed = true;
707                 }
708
709                 public override void Close ()
710                 {
711                         if (sendChunked) {
712                                 if (disposed)
713                                         return;
714                                 disposed = true;
715                                 pending.WaitOne ();
716                                 byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
717                                 string err_msg = null;
718                                 cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
719                                 return;
720                         }
721
722                         if (isRead) {
723                                 if (!nextReadCalled) {
724                                         CheckComplete ();
725                                         // If we have not read all the contents
726                                         if (!nextReadCalled) {
727                                                 nextReadCalled = true;
728                                                 cnc.Close (true);
729                                         }
730                                 }
731                                 return;
732                         } else if (!allowBuffering) {
733                                 complete_request_written = true;
734                                 if (!initRead) {
735                                         initRead = true;
736                                         WebConnection.InitRead (cnc);
737                                 }
738                                 return;
739                         }
740
741                         if (disposed || requestWritten)
742                                 return;
743
744                         long length = request.ContentLength;
745
746                         if (!sendChunked && length != -1 && totalWritten != length) {
747                                 IOException io = new IOException ("Cannot close the stream until all bytes are written");
748                                 nextReadCalled = true;
749                                 cnc.Close (true);
750                                 throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
751                         }
752
753                         WriteRequest ();
754                         disposed = true;
755                 }
756
757                 internal void KillBuffer ()
758                 {
759                         writeBuffer = null;
760                 }
761
762                 public override long Seek (long a, SeekOrigin b)
763                 {
764                         throw new NotSupportedException ();
765                 }
766                 
767                 public override void SetLength (long a)
768                 {
769                         throw new NotSupportedException ();
770                 }
771                 
772                 public override bool CanSeek {
773                         get { return false; }
774                 }
775
776                 public override bool CanRead {
777                         get { return !disposed && isRead; }
778                 }
779
780                 public override bool CanWrite {
781                         get { return !disposed && !isRead; }
782                 }
783
784                 public override long Length {
785                         get { throw new NotSupportedException (); }
786                 }
787
788                 public override long Position {
789                         get { throw new NotSupportedException (); }
790                         set { throw new NotSupportedException (); }
791                 }
792         }
793 }
794