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