2009-06-21 Gonzalo Paniagua Javier <gonzalo@novell.com>
[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
81                                 try {
82                                         contentLength = Int32.Parse (clength);
83                                         if (contentLength == 0 && !IsNtlmAuth ()) {
84                                                 ReadAll ();
85                                         }
86                                 } catch {
87                                         contentLength = Int32.MaxValue;
88                                 }
89                         } else {
90                                 contentLength = Int32.MaxValue;
91                         }
92                 }
93
94                 public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
95                 {
96                         read_timeout = request.ReadWriteTimeout;
97                         write_timeout = read_timeout;
98                         isRead = false;
99                         this.cnc = cnc;
100                         this.request = request;
101                         allowBuffering = request.InternalAllowBuffering;
102                         sendChunked = request.SendChunked;
103                         if (allowBuffering)
104                                 writeBuffer = new MemoryStream ();
105                         if (sendChunked)
106                                 pending = new ManualResetEvent (true);
107                 }
108
109                 bool IsNtlmAuth ()
110                 {
111                         bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
112                         string header_name = (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate";
113                         string authHeader = cnc.Data.Headers [header_name];
114                         return (authHeader != null && authHeader.IndexOf ("NTLM") != -1);
115                 }
116
117                 internal void CheckResponseInBuffer ()
118                 {
119                         if (contentLength > 0 && (readBufferSize - readBufferOffset) >= contentLength) {
120                                 if (!IsNtlmAuth ())
121                                         ReadAll ();
122                         }
123                 }
124
125                 internal WebConnection Connection {
126                         get { return cnc; }
127                 }
128 #if NET_2_0
129                 public override bool CanTimeout {
130                         get { return true; }
131                 }
132 #endif
133
134 #if NET_2_0
135                 public override
136 #endif
137                 int ReadTimeout {
138                         get {
139                                 return read_timeout;
140                         }
141
142                         set {
143                                 if (value < -1)
144                                         throw new ArgumentOutOfRangeException ("value");
145                                 read_timeout = value;
146                         }
147                 }
148
149 #if NET_2_0
150                 public override
151 #endif
152                 int WriteTimeout {
153                         get {
154                                 return write_timeout;
155                         }
156
157                         set {
158                                 if (value < -1)
159                                         throw new ArgumentOutOfRangeException ("value");
160                                 write_timeout = value;
161                         }
162                 }
163
164                 internal bool CompleteRequestWritten {
165                         get { return complete_request_written; }
166                 }
167
168                 internal bool SendChunked {
169                         set { sendChunked = value; }
170                 }
171
172                 internal byte [] ReadBuffer {
173                         set { readBuffer = value; }
174                 }
175
176                 internal int ReadBufferOffset {
177                         set { readBufferOffset = value;}
178                 }
179                 
180                 internal int ReadBufferSize {
181                         set { readBufferSize = value; }
182                 }
183                 
184                 internal byte[] WriteBuffer {
185                         get { return writeBuffer.GetBuffer (); }
186                 }
187
188                 internal int WriteBufferLength {
189                         get { return writeBuffer != null ? (int) writeBuffer.Length : (-1); }
190                 }
191
192                 internal void ForceCompletion ()
193                 {
194                         if (!nextReadCalled) {
195                                 if (contentLength == Int32.MaxValue)
196                                         contentLength = 0;
197                                 nextReadCalled = true;
198                                 cnc.NextRead ();
199                         }
200                 }
201                 
202                 internal void CheckComplete ()
203                 {
204                         bool nrc = nextReadCalled;
205                         if (!nrc && readBufferSize - readBufferOffset == contentLength) {
206                                 nextReadCalled = true;
207                                 cnc.NextRead ();
208                         }
209                 }
210
211                 internal void ReadAll ()
212                 {
213                         if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
214                                 if (isRead && !nextReadCalled) {
215                                         nextReadCalled = true;
216                                         cnc.NextRead ();
217                                 }
218                                 return;
219                         }
220
221                         pending.WaitOne ();
222                         lock (locker) {
223                                 if (totalRead >= contentLength)
224                                         return;
225                                 
226                                 byte [] b = null;
227                                 int diff = readBufferSize - readBufferOffset;
228                                 int new_size;
229
230                                 if (contentLength == Int32.MaxValue) {
231                                         MemoryStream ms = new MemoryStream ();
232                                         byte [] buffer = null;
233                                         if (readBuffer != null && diff > 0) {
234                                                 ms.Write (readBuffer, readBufferOffset, diff);
235                                                 if (readBufferSize >= 8192)
236                                                         buffer = readBuffer;
237                                         }
238
239                                         if (buffer == null)
240                                                 buffer = new byte [8192];
241
242                                         int read;
243                                         while ((read = cnc.Read (buffer, 0, buffer.Length)) != 0)
244                                                 ms.Write (buffer, 0, read);
245
246                                         b = ms.GetBuffer ();
247                                         new_size = (int) ms.Length;
248                                         contentLength = new_size;
249                                 } else {
250                                         new_size = contentLength - totalRead;
251                                         b = new byte [new_size];
252                                         if (readBuffer != null && diff > 0) {
253                                                 if (diff > new_size)
254                                                         diff = new_size;
255
256                                                 Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
257                                         }
258                                         
259                                         int remaining = new_size - diff;
260                                         int r = -1;
261                                         while (remaining > 0 && r != 0) {
262                                                 r = cnc.Read (b, diff, remaining);
263                                                 remaining -= r;
264                                                 diff += r;
265                                         }
266                                 }
267
268                                 readBuffer = b;
269                                 readBufferOffset = 0;
270                                 readBufferSize = new_size;
271                                 totalRead = 0;
272                                 nextReadCalled = true;
273                         }
274
275                         cnc.NextRead ();
276                 }
277
278                 void WriteCallbackWrapper (IAsyncResult r)
279                 {
280                         WebAsyncResult result;
281                         if (r.AsyncState != null) {
282                                 result = (WebAsyncResult) r.AsyncState;
283                                 result.InnerAsyncResult = r;
284                                 result.DoCallback ();
285                         } else {
286                                 EndWrite (r);
287                         }
288                 }
289
290                 void ReadCallbackWrapper (IAsyncResult r)
291                 {
292                         WebAsyncResult result;
293                         if (r.AsyncState != null) {
294                                 result = (WebAsyncResult) r.AsyncState;
295                                 result.InnerAsyncResult = r;
296                                 result.DoCallback ();
297                         } else {
298                                 EndRead (r);
299                         }
300                 }
301
302                 public override int Read (byte [] buffer, int offset, int size)
303                 {
304                         if (!isRead)
305                                 throw new NotSupportedException ("this stream does not allow reading");
306
307                         if (totalRead >= contentLength)
308                                 return 0;
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 (size < 0 || offset < 0 || length < offset || length - offset < size)
332                                 throw new ArgumentOutOfRangeException ();
333
334                         lock (locker) {
335                                 pendingReads++;
336                                 pending.Reset ();
337                         }
338
339                         WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
340                         if (totalRead >= contentLength) {
341                                 result.SetCompleted (true, -1);
342                                 result.DoCallback ();
343                                 return result;
344                         }
345                         
346                         int remaining = readBufferSize - readBufferOffset;
347                         if (remaining > 0) {
348                                 int copy = (remaining > size) ? size : remaining;
349                                 Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
350                                 readBufferOffset += copy;
351                                 offset += copy;
352                                 size -= copy;
353                                 totalRead += copy;
354                                 if (size == 0 || totalRead >= contentLength) {
355                                         result.SetCompleted (true, copy);
356                                         result.DoCallback ();
357                                         return result;
358                                 }
359                                 result.NBytes = copy;
360                         }
361
362                         if (cb != null)
363                                 cb = new AsyncCallback (ReadCallbackWrapper);
364
365                         if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
366                                 size = contentLength - totalRead;
367
368                         if (!read_eof) {
369                                 result.InnerAsyncResult = cnc.BeginRead (buffer, offset, size, cb, result);
370                         } else {
371                                 result.SetCompleted (true, result.NBytes);
372                                 result.DoCallback ();
373                         }
374                         return result;
375                 }
376
377                 public override int EndRead (IAsyncResult r)
378                 {
379                         WebAsyncResult result = (WebAsyncResult) r;
380                         if (result.EndCalled) {
381                                 int xx = result.NBytes;
382                                 return (xx >= 0) ? xx : 0;
383                         }
384
385                         result.EndCalled = true;
386
387                         if (!result.IsCompleted) {
388                                 int nbytes = -1;
389                                 try {
390                                         nbytes = cnc.EndRead (result);
391                                 } catch (Exception exc) {
392                                         lock (locker) {
393                                                 pendingReads--;
394                                                 if (pendingReads == 0)
395                                                         pending.Set ();
396                                         }
397
398                                         nextReadCalled = true;
399                                         cnc.Close (true);
400                                         result.SetCompleted (false, exc);
401                                         throw;
402                                 }
403
404                                 if (nbytes < 0) {
405                                         nbytes = 0;
406                                         read_eof = true;
407                                 }
408
409                                 totalRead += nbytes;
410                                 result.SetCompleted (false, nbytes + result.NBytes);
411                                 result.DoCallback ();
412                                 if (nbytes == 0)
413                                         contentLength = totalRead;
414                         }
415
416                         lock (locker) {
417                                 pendingReads--;
418                                 if (pendingReads == 0)
419                                         pending.Set ();
420                         }
421
422                         if (totalRead >= contentLength && !nextReadCalled)
423                                 ReadAll ();
424
425                         int nb = result.NBytes;
426                         return (nb >= 0) ? nb : 0;
427                 }
428                 
429                 public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
430                                                         AsyncCallback cb, object state)
431                 {
432                         if (isRead)
433                                 throw new NotSupportedException ("this stream does not allow writing");
434
435                         if (buffer == null)
436                                 throw new ArgumentNullException ("buffer");
437
438                         int length = buffer.Length;
439                         if (size < 0 || offset < 0 || length < offset || length - offset < size)
440                                 throw new ArgumentOutOfRangeException ();
441
442                         if (sendChunked) {
443                                 lock (locker) {
444                                         pendingWrites++;
445                                         pending.Reset ();
446                                 }
447                         }
448
449                         WebAsyncResult result = new WebAsyncResult (cb, state);
450                         if (!sendChunked)
451                                 CheckWriteOverflow (request.ContentLength, totalWritten, size);
452                         if (allowBuffering) {
453                                 writeBuffer.Write (buffer, offset, size);
454                                 totalWritten += size;
455                                 if (!sendChunked) {
456                                         result.SetCompleted (true, 0);
457                                         result.DoCallback ();
458                                         return result;
459                                 }
460                         }
461
462                         AsyncCallback callback = null;
463                         if (cb != null)
464                                 callback = new AsyncCallback (WriteCallbackWrapper);
465
466                         if (sendChunked) {
467                                 WriteRequest ();
468
469                                 string cSize = String.Format ("{0:X}\r\n", size);
470                                 byte [] head = Encoding.ASCII.GetBytes (cSize);
471                                 int chunkSize = 2 + size + head.Length;
472                                 byte [] newBuffer = new byte [chunkSize];
473                                 Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
474                                 Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
475                                 Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);
476
477                                 buffer = newBuffer;
478                                 offset = 0;
479                                 size = chunkSize;
480                         }
481
482                         result.InnerAsyncResult = cnc.BeginWrite (buffer, offset, size, callback, result);
483                         totalWritten += size;
484                         return result;
485                 }
486
487                 static void CheckWriteOverflow (long contentLength, long totalWritten, long size)
488                 {
489                         if (contentLength == -1)
490                                 return;
491
492                         long avail = contentLength - totalWritten;
493                         if (size > avail)
494                                 throw new ProtocolViolationException (
495                                         "The number of bytes to be written is greater than " +
496                                         "the specified ContentLength.");
497                 }
498
499                 public override void EndWrite (IAsyncResult r)
500                 {
501                         if (r == null)
502                                 throw new ArgumentNullException ("r");
503
504                         WebAsyncResult result = r as WebAsyncResult;
505                         if (result == null)
506                                 throw new ArgumentException ("Invalid IAsyncResult");
507
508                         if (result.EndCalled)
509                                 return;
510
511                         result.EndCalled = true;
512
513                         if (allowBuffering && !sendChunked)
514                                 return;
515
516                         if (result.GotException)
517                                 throw result.Exception;
518
519                         try { 
520                                 cnc.EndWrite (result.InnerAsyncResult);
521                                 result.SetCompleted (false, 0);
522                         } catch (Exception e) {
523                                 result.SetCompleted (false, e);
524                         }
525
526                         if (sendChunked) {
527                                 lock (locker) {
528                                         pendingWrites--;
529                                         if (pendingWrites == 0)
530                                                 pending.Set ();
531                                 }
532                         }
533                 }
534                 
535                 public override void Write (byte [] buffer, int offset, int size)
536                 {
537                         if (isRead)
538                                 throw new NotSupportedException ("This stream does not allow writing");
539
540                         AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
541                         WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
542                         if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
543                                 nextReadCalled = true;
544                                 cnc.Close (true);
545                                 throw new IOException ("Write timed out.");
546                         }
547
548                         EndWrite (res);
549                 }
550
551                 public override void Flush ()
552                 {
553                 }
554
555                 internal void SetHeaders (byte [] buffer, int offset, int size)
556                 {
557                         if (headersSent)
558                                 return;
559
560                         if (!allowBuffering || sendChunked) {
561                                 headersSent = true;
562                                 if (!cnc.Connected)
563                                         throw new WebException ("Not connected", null, WebExceptionStatus.SendFailure, null);
564
565                                 
566                                 if (!cnc.Write (buffer, offset, size))
567                                         throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
568
569                                 if (!initRead) {
570                                         initRead = true;
571                                         WebConnection.InitRead (cnc);
572                                 }
573                         } else {
574                                 headers = new byte [size];
575                                 Buffer.BlockCopy (buffer, offset, headers, 0, size);
576                         }
577                 }
578
579                 internal bool RequestWritten {
580                         get { return requestWritten; }
581                 }
582
583                 internal void WriteRequest ()
584                 {
585                         if (requestWritten)
586                                 return;
587
588                         if (sendChunked) {
589                                 request.SendRequestHeaders ();
590                                 requestWritten = true;
591                                 return;
592                         }
593
594                         if (!allowBuffering || writeBuffer == null)
595                                 return;
596
597                         byte [] bytes = writeBuffer.GetBuffer ();
598                         int length = (int) writeBuffer.Length;
599                         if (request.ContentLength != -1 && request.ContentLength < length) {
600                                 throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
601                                                         WebExceptionStatus.ServerProtocolViolation, null);
602                         }
603
604                         request.InternalContentLength = length;
605                         request.SendRequestHeaders ();
606                         requestWritten = true;
607
608                         //
609                         // For small requests, make a copy, it will reduce the traffic, for large
610                         // requests, the NoDelay bit on the socket should take effect (set in WebConnection).
611                         //
612                         if (headers.Length + length < 8192){
613                                 byte[] b = new byte [headers.Length + length];
614
615                                 Buffer.BlockCopy (headers, 0, b, 0, headers.Length);
616                                 Buffer.BlockCopy (bytes, 0, b, headers.Length, length);
617                                 
618                                 if (!cnc.Write (b, 0, b.Length))
619                                         throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
620                                 
621                                 headersSent = true;
622                                 complete_request_written = true;
623                                 
624                                 if (!initRead) {
625                                         initRead = true;
626                                         WebConnection.InitRead (cnc);
627                                 }                               
628                         } else {
629                                 if (!cnc.Write (headers, 0, headers.Length))
630                                         throw new WebException ("Error writing request.", null, WebExceptionStatus.SendFailure, null);
631                                 
632                                 headersSent = true;
633                                 if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
634                                         return;
635                                 
636                                 IAsyncResult result = null;
637                                 if (length > 0)
638                                         result = cnc.BeginWrite (bytes, 0, length, null, null);
639                                 
640                                 if (!initRead) {
641                                         initRead = true;
642                                         WebConnection.InitRead (cnc);
643                                 }
644                                 
645                                 if (length > 0) 
646                                         complete_request_written = cnc.EndWrite (result);
647                                 else
648                                         complete_request_written = true;
649                         }
650                 }
651
652                 internal void InternalClose ()
653                 {
654                         disposed = true;
655                 }
656
657                 internal void ForceCloseConnection ()
658                 {
659                         if (!disposed) {
660                                 disposed = true;
661                                 cnc.Close (true);
662                         }
663                 }
664
665                 public override void Close ()
666                 {
667                         if (sendChunked) {
668                                 pending.WaitOne ();
669                                 byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
670                                 cnc.Write (chunk, 0, chunk.Length);
671                                 return;
672                         }
673
674                         if (isRead) {
675                                 if (!nextReadCalled) {
676                                         CheckComplete ();
677                                         // If we have not read all the contents
678                                         if (!nextReadCalled) {
679                                                 nextReadCalled = true;
680                                                 cnc.Close (true);
681                                         }
682                                 }
683                                 return;
684                         } else if (!allowBuffering) {
685                                 complete_request_written = true;
686                                 if (!initRead) {
687                                         initRead = true;
688                                         WebConnection.InitRead (cnc);
689                                 }
690                                 return;
691                         }
692
693                         if (disposed)
694                                 return;
695
696                         long length = request.ContentLength;
697
698                         // writeBuffer could be null if KillBuffer was already called.
699                         if (writeBuffer != null && length != -1 && length > writeBuffer.Length) {
700                                 IOException io = new IOException ("Cannot close the stream until all bytes are written");
701                                 nextReadCalled = true;
702                                 cnc.Close (true);
703                                 throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
704                         }
705
706                         WriteRequest ();
707                         disposed = true;
708                 }
709
710                 internal void KillBuffer ()
711                 {
712                         writeBuffer = null;
713                 }
714
715                 public override long Seek (long a, SeekOrigin b)
716                 {
717                         throw new NotSupportedException ();
718                 }
719                 
720                 public override void SetLength (long a)
721                 {
722                         throw new NotSupportedException ();
723                 }
724                 
725                 public override bool CanSeek {
726                         get { return false; }
727                 }
728
729                 public override bool CanRead {
730                         get { return isRead; }
731                 }
732
733                 public override bool CanWrite {
734                         get { return !isRead; }
735                 }
736
737                 public override long Length {
738                         get { throw new NotSupportedException (); }
739                 }
740
741                 public override long Position {
742                         get { throw new NotSupportedException (); }
743                         set { throw new NotSupportedException (); }
744                 }
745         }
746 }
747