Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 866 lines (739 sloc) 22.285 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
//
// System.Net.WebConnectionStream
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2003 Ximian, Inc (http://www.ximian.com)
// (C) 2004 Novell, Inc (http://www.novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.IO;
using System.Text;
using System.Threading;

namespace System.Net
{
class WebConnectionStream : Stream
{
static byte [] crlf = new byte [] { 13, 10 };
bool isRead;
WebConnection cnc;
HttpWebRequest request;
byte [] readBuffer;
int readBufferOffset;
int readBufferSize;
int stream_length; // -1 when CL not present
int contentLength;
int totalRead;
internal long totalWritten;
bool nextReadCalled;
int pendingReads;
int pendingWrites;
ManualResetEvent pending;
bool allowBuffering;
bool sendChunked;
MemoryStream writeBuffer;
bool requestWritten;
byte [] headers;
bool disposed;
bool headersSent;
object locker = new object ();
bool initRead;
bool read_eof;
bool complete_request_written;
int read_timeout;
int write_timeout;
AsyncCallback cb_wrapper; // Calls to ReadCallbackWrapper or WriteCallbacWrapper
internal bool IgnoreIOErrors;

public WebConnectionStream (WebConnection cnc, WebConnectionData data)
{
if (data == null)
throw new InvalidOperationException ("data was not initialized");
if (data.Headers == null)
throw new InvalidOperationException ("data.Headers was not initialized");
if (data.request == null)
throw new InvalidOperationException ("data.request was not initialized");
isRead = true;
cb_wrapper = new AsyncCallback (ReadCallbackWrapper);
pending = new ManualResetEvent (true);
this.request = data.request;
read_timeout = request.ReadWriteTimeout;
write_timeout = read_timeout;
this.cnc = cnc;
string contentType = data.Headers ["Transfer-Encoding"];
bool chunkedRead = (contentType != null && contentType.IndexOf ("chunked", StringComparison.OrdinalIgnoreCase) != -1);
string clength = data.Headers ["Content-Length"];
if (!chunkedRead && clength != null && clength != "") {
try {
contentLength = Int32.Parse (clength);
if (contentLength == 0 && !IsNtlmAuth ()) {
ReadAll ();
}
} catch {
contentLength = Int32.MaxValue;
}
} else {
contentLength = Int32.MaxValue;
}

// Negative numbers?
if (!Int32.TryParse (clength, out stream_length))
stream_length = -1;
}

public WebConnectionStream (WebConnection cnc, HttpWebRequest request)
{
read_timeout = request.ReadWriteTimeout;
write_timeout = read_timeout;
isRead = false;
cb_wrapper = new AsyncCallback (WriteCallbackWrapper);
this.cnc = cnc;
this.request = request;
allowBuffering = request.InternalAllowBuffering;
sendChunked = request.SendChunked;
if (sendChunked)
pending = new ManualResetEvent (true);
else if (allowBuffering)
writeBuffer = new MemoryStream ();
}

bool CheckAuthHeader (string headerName)
{
var authHeader = cnc.Data.Headers [headerName];
return (authHeader != null && authHeader.IndexOf ("NTLM", StringComparison.Ordinal) != -1);
}

bool IsNtlmAuth ()
{
bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
if (isProxy && CheckAuthHeader ("Proxy-Authenticate"))
return true;
return CheckAuthHeader ("WWW-Authenticate");
}

internal void CheckResponseInBuffer ()
{
if (contentLength > 0 && (readBufferSize - readBufferOffset) >= contentLength) {
if (!IsNtlmAuth ())
ReadAll ();
}
}

internal HttpWebRequest Request {
get { return request; }
}

internal WebConnection Connection {
get { return cnc; }
}
public override bool CanTimeout {
get { return true; }
}

public override int ReadTimeout {
get {
return read_timeout;
}

set {
if (value < -1)
throw new ArgumentOutOfRangeException ("value");
read_timeout = value;
}
}

public override int WriteTimeout {
get {
return write_timeout;
}

set {
if (value < -1)
throw new ArgumentOutOfRangeException ("value");
write_timeout = value;
}
}

internal bool CompleteRequestWritten {
get { return complete_request_written; }
}

internal bool SendChunked {
set { sendChunked = value; }
}

internal byte [] ReadBuffer {
set { readBuffer = value; }
}

internal int ReadBufferOffset {
set { readBufferOffset = value; }
}

internal int ReadBufferSize {
set { readBufferSize = value; }
}

internal byte[] WriteBuffer {
get { return writeBuffer.GetBuffer (); }
}

internal int WriteBufferLength {
get { return writeBuffer != null ? (int) writeBuffer.Length : (-1); }
}

internal void ForceCompletion ()
{
if (!nextReadCalled) {
if (contentLength == Int32.MaxValue)
contentLength = 0;
nextReadCalled = true;
cnc.NextRead ();
}
}

internal void CheckComplete ()
{
bool nrc = nextReadCalled;
if (!nrc && readBufferSize - readBufferOffset == contentLength) {
nextReadCalled = true;
cnc.NextRead ();
}
}

internal void ReadAll ()
{
if (!isRead || read_eof || totalRead >= contentLength || nextReadCalled) {
if (isRead && !nextReadCalled) {
nextReadCalled = true;
cnc.NextRead ();
}
return;
}

pending.WaitOne ();
lock (locker) {
if (totalRead >= contentLength)
return;

byte [] b = null;
int diff = readBufferSize - readBufferOffset;
int new_size;

if (contentLength == Int32.MaxValue) {
MemoryStream ms = new MemoryStream ();
byte [] buffer = null;
if (readBuffer != null && diff > 0) {
ms.Write (readBuffer, readBufferOffset, diff);
if (readBufferSize >= 8192)
buffer = readBuffer;
}

if (buffer == null)
buffer = new byte [8192];

int read;
while ((read = cnc.Read (request, buffer, 0, buffer.Length)) != 0)
ms.Write (buffer, 0, read);

b = ms.GetBuffer ();
new_size = (int) ms.Length;
contentLength = new_size;
} else {
new_size = contentLength - totalRead;
b = new byte [new_size];
if (readBuffer != null && diff > 0) {
if (diff > new_size)
diff = new_size;

Buffer.BlockCopy (readBuffer, readBufferOffset, b, 0, diff);
}

int remaining = new_size - diff;
int r = -1;
while (remaining > 0 && r != 0) {
r = cnc.Read (request, b, diff, remaining);
remaining -= r;
diff += r;
}
}

readBuffer = b;
readBufferOffset = 0;
readBufferSize = new_size;
totalRead = 0;
nextReadCalled = true;
}

cnc.NextRead ();
}

void WriteCallbackWrapper (IAsyncResult r)
{
WebAsyncResult result = r as WebAsyncResult;
if (result != null && result.AsyncWriteAll)
return;

if (r.AsyncState != null) {
result = (WebAsyncResult) r.AsyncState;
result.InnerAsyncResult = r;
result.DoCallback ();
} else {
try {
EndWrite (r);
} catch {
}
}
}

void ReadCallbackWrapper (IAsyncResult r)
{
WebAsyncResult result;
if (r.AsyncState != null) {
result = (WebAsyncResult) r.AsyncState;
result.InnerAsyncResult = r;
result.DoCallback ();
} else {
try {
EndRead (r);
} catch {
}
}
}

public override int Read (byte [] buffer, int offset, int size)
{
AsyncCallback cb = cb_wrapper;
WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
nextReadCalled = true;
cnc.Close (true);
throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
}

return EndRead (res);
}

public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
AsyncCallback cb, object state)
{
if (!isRead)
throw new NotSupportedException ("this stream does not allow reading");

if (buffer == null)
throw new ArgumentNullException ("buffer");

int length = buffer.Length;
if (offset < 0 || length < offset)
throw new ArgumentOutOfRangeException ("offset");
if (size < 0 || (length - offset) < size)
throw new ArgumentOutOfRangeException ("size");

lock (locker) {
pendingReads++;
pending.Reset ();
}

WebAsyncResult result = new WebAsyncResult (cb, state, buffer, offset, size);
if (totalRead >= contentLength) {
result.SetCompleted (true, -1);
result.DoCallback ();
return result;
}

int remaining = readBufferSize - readBufferOffset;
if (remaining > 0) {
int copy = (remaining > size) ? size : remaining;
Buffer.BlockCopy (readBuffer, readBufferOffset, buffer, offset, copy);
readBufferOffset += copy;
offset += copy;
size -= copy;
totalRead += copy;
if (size == 0 || totalRead >= contentLength) {
result.SetCompleted (true, copy);
result.DoCallback ();
return result;
}
result.NBytes = copy;
}

if (cb != null)
cb = cb_wrapper;

if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
size = contentLength - totalRead;

if (!read_eof) {
result.InnerAsyncResult = cnc.BeginRead (request, buffer, offset, size, cb, result);
} else {
result.SetCompleted (true, result.NBytes);
result.DoCallback ();
}
return result;
}

public override int EndRead (IAsyncResult r)
{
WebAsyncResult result = (WebAsyncResult) r;
if (result.EndCalled) {
int xx = result.NBytes;
return (xx >= 0) ? xx : 0;
}

result.EndCalled = true;

if (!result.IsCompleted) {
int nbytes = -1;
try {
nbytes = cnc.EndRead (request, result);
} catch (Exception exc) {
lock (locker) {
pendingReads--;
if (pendingReads == 0)
pending.Set ();
}

nextReadCalled = true;
cnc.Close (true);
result.SetCompleted (false, exc);
result.DoCallback ();
throw;
}

if (nbytes < 0) {
nbytes = 0;
read_eof = true;
}

totalRead += nbytes;
result.SetCompleted (false, nbytes + result.NBytes);
result.DoCallback ();
if (nbytes == 0)
contentLength = totalRead;
}

lock (locker) {
pendingReads--;
if (pendingReads == 0)
pending.Set ();
}

if (totalRead >= contentLength && !nextReadCalled)
ReadAll ();

int nb = result.NBytes;
return (nb >= 0) ? nb : 0;
}

void WriteAsyncCB (IAsyncResult r)
{
WebAsyncResult result = (WebAsyncResult) r.AsyncState;
result.InnerAsyncResult = null;

try {
cnc.EndWrite (request, true, r);
result.SetCompleted (false, 0);
if (!initRead) {
initRead = true;
WebConnection.InitRead (cnc);
}
} catch (Exception e) {
KillBuffer ();
nextReadCalled = true;
cnc.Close (true);
if (e is System.Net.Sockets.SocketException)
e = new IOException ("Error writing request", e);
result.SetCompleted (false, e);
}

if (allowBuffering && !sendChunked && request.ContentLength > 0 && totalWritten == request.ContentLength)
complete_request_written = true;

result.DoCallback ();
}

public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
AsyncCallback cb, object state)
{
if (request.Aborted)
throw new WebException ("The request was canceled.", null, WebExceptionStatus.RequestCanceled);

if (isRead)
throw new NotSupportedException ("this stream does not allow writing");

if (buffer == null)
throw new ArgumentNullException ("buffer");

int length = buffer.Length;
if (offset < 0 || length < offset)
throw new ArgumentOutOfRangeException ("offset");
if (size < 0 || (length - offset) < size)
throw new ArgumentOutOfRangeException ("size");

if (sendChunked) {
lock (locker) {
pendingWrites++;
pending.Reset ();
}
}

WebAsyncResult result = new WebAsyncResult (cb, state);
AsyncCallback callback = new AsyncCallback (WriteAsyncCB);

if (sendChunked) {
requestWritten = true;

string cSize = String.Format ("{0:X}\r\n", size);
byte[] head = Encoding.ASCII.GetBytes (cSize);
int chunkSize = 2 + size + head.Length;
byte[] newBuffer = new byte [chunkSize];
Buffer.BlockCopy (head, 0, newBuffer, 0, head.Length);
Buffer.BlockCopy (buffer, offset, newBuffer, head.Length, size);
Buffer.BlockCopy (crlf, 0, newBuffer, head.Length + size, crlf.Length);

if (allowBuffering) {
if (writeBuffer == null)
writeBuffer = new MemoryStream ();
writeBuffer.Write (buffer, offset, size);
totalWritten += size;
}

buffer = newBuffer;
offset = 0;
size = chunkSize;
} else {
CheckWriteOverflow (request.ContentLength, totalWritten, size);

if (allowBuffering) {
if (writeBuffer == null)
writeBuffer = new MemoryStream ();
writeBuffer.Write (buffer, offset, size);
totalWritten += size;

if (request.ContentLength <= 0 || totalWritten < request.ContentLength) {
result.SetCompleted (true, 0);
result.DoCallback ();
return result;
}

result.AsyncWriteAll = true;
requestWritten = true;
buffer = writeBuffer.GetBuffer ();
offset = 0;
size = (int)totalWritten;
}
}

try {
result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
if (result.InnerAsyncResult == null) {
if (!result.IsCompleted)
result.SetCompleted (true, 0);
result.DoCallback ();
}
} catch (Exception) {
if (!IgnoreIOErrors)
throw;
result.SetCompleted (true, 0);
result.DoCallback ();
}
totalWritten += size;
return result;
}

void CheckWriteOverflow (long contentLength, long totalWritten, long size)
{
if (contentLength == -1)
return;

long avail = contentLength - totalWritten;
if (size > avail) {
KillBuffer ();
nextReadCalled = true;
cnc.Close (true);
throw new ProtocolViolationException (
"The number of bytes to be written is greater than " +
"the specified ContentLength.");
}
}

public override void EndWrite (IAsyncResult r)
{
if (r == null)
throw new ArgumentNullException ("r");

WebAsyncResult result = r as WebAsyncResult;
if (result == null)
throw new ArgumentException ("Invalid IAsyncResult");

if (result.EndCalled)
return;

result.EndCalled = true;
if (result.AsyncWriteAll) {
result.WaitUntilComplete ();
if (result.GotException)
throw result.Exception;
return;
}

if (allowBuffering && !sendChunked)
return;

if (result.GotException)
throw result.Exception;

if (sendChunked) {
lock (locker) {
pendingWrites--;
if (pendingWrites == 0)
pending.Set ();
}
}
}

public override void Write (byte [] buffer, int offset, int size)
{
AsyncCallback cb = cb_wrapper;
WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
KillBuffer ();
nextReadCalled = true;
cnc.Close (true);
throw new IOException ("Write timed out.");
}

EndWrite (res);
}

public override void Flush ()
{
}

internal WebAsyncResult SetHeadersAsync (bool setInternalLength, AsyncCallback callback, object state)
{
if (headersSent)
return null;

string method = request.Method;
bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
method == "TRACE");
bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
method == "COPY" || method == "MOVE" || method == "LOCK" ||
method == "UNLOCK");

if (setInternalLength && !no_writestream && writeBuffer != null)
request.InternalContentLength = writeBuffer.Length;

if (sendChunked || request.ContentLength > -1 || no_writestream || webdav) {
headersSent = true;
headers = request.GetRequestHeaders ();

var result = new WebAsyncResult (callback, state);
result.InnerAsyncResult = cnc.BeginWrite (request, headers, 0, headers.Length, new AsyncCallback (SetHeadersCB), result);
if (result.InnerAsyncResult != null)
return result;
}

return null;
}

void SetHeadersCB (IAsyncResult r)
{
WebAsyncResult result = (WebAsyncResult) r.AsyncState;
result.InnerAsyncResult = null;
try {
cnc.EndWrite (request, true, r);
result.SetCompleted (false, 0);
if (!initRead) {
initRead = true;
WebConnection.InitRead (cnc);
}
long cl = request.ContentLength;
if (!sendChunked && cl == 0)
requestWritten = true;
} catch (WebException e) {
result.SetCompleted (false, e);
} catch (Exception e) {
result.SetCompleted (false, new WebException ("Error writing headers", e, WebExceptionStatus.SendFailure));
}
result.DoCallback ();
}

internal bool RequestWritten {
get { return requestWritten; }
}

internal WebAsyncResult WriteRequestAsync (AsyncCallback callback, object state)
{
if (requestWritten)
return null;

requestWritten = true;
if (sendChunked)
return null;

if (!allowBuffering || writeBuffer == null)
return null;

byte[] bytes = writeBuffer.GetBuffer ();
int length = (int)writeBuffer.Length;
if (request.ContentLength != -1 && request.ContentLength < length) {
nextReadCalled = true;
cnc.Close (true);
throw new WebException ("Specified Content-Length is less than the number of bytes to write", null,
WebExceptionStatus.ServerProtocolViolation, null);
}

var result = new WebAsyncResult (callback, state);
result.InnerAsyncResult = SetHeadersAsync (true, WriteRequestAsyncCB, result);
if (result.InnerAsyncResult == null)
WriteRequestAsyncCB (result);
return result;
}

void WriteRequestAsyncCB (IAsyncResult ar)
{
var result = (WebAsyncResult)ar;
var innerResult = (WebAsyncResult)result.InnerAsyncResult;
result.InnerAsyncResult = null;

if (innerResult != null && innerResult.GotException) {
result.SetCompleted (false, innerResult.Exception);
result.DoCallback ();
return;
}

if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100) {
result.SetCompleted (false, 0);
result.DoCallback ();
return;
}

byte[] bytes = writeBuffer.GetBuffer ();
int length = (int)writeBuffer.Length;

if (length > 0)
result.InnerAsyncResult = cnc.BeginWrite (request, bytes, 0, length, WriteRequestAsyncCB2, result);

if (!initRead) {
initRead = true;
WebConnection.InitRead (cnc);
}

if (length == 0) {
result.SetCompleted (false, 0);
result.DoCallback ();
complete_request_written = true;
}
}

void WriteRequestAsyncCB2 (IAsyncResult ar)
{
var result = (WebAsyncResult)ar.AsyncState;
var innerResult = result.InnerAsyncResult;
result.InnerAsyncResult = null;

try {
complete_request_written = cnc.EndWrite (request, false, innerResult);
result.SetCompleted (false, 0);
} catch (Exception exc) {
result.SetCompleted (false, exc);
} finally {
result.DoCallback ();
}
}

internal void InternalClose ()
{
disposed = true;
}

public override void Close ()
{
if (sendChunked) {
if (disposed)
return;
disposed = true;
pending.WaitOne ();
byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
string err_msg = null;
cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);
return;
}

if (isRead) {
if (!nextReadCalled) {
CheckComplete ();
// If we have not read all the contents
if (!nextReadCalled) {
nextReadCalled = true;
cnc.Close (true);
}
}
return;
} else if (!allowBuffering) {
complete_request_written = true;
if (!initRead) {
initRead = true;
WebConnection.InitRead (cnc);
}
return;
}

if (disposed || requestWritten)
return;

long length = request.ContentLength;

if (!sendChunked && length != -1 && totalWritten != length) {
IOException io = new IOException ("Cannot close the stream until all bytes are written");
nextReadCalled = true;
cnc.Close (true);
throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
}

// Commented out the next line to fix xamarin bug #1512
//WriteRequest ();
disposed = true;
}

internal void KillBuffer ()
{
writeBuffer = null;
}

public override long Seek (long a, SeekOrigin b)
{
throw new NotSupportedException ();
}

public override void SetLength (long a)
{
throw new NotSupportedException ();
}

public override bool CanSeek {
get { return false; }
}

public override bool CanRead {
get { return !disposed && isRead; }
}

public override bool CanWrite {
get { return !disposed && !isRead; }
}

public override long Length {
get {
if (!isRead)
throw new NotSupportedException ();
return stream_length;
}
}

public override long Position {
get { throw new NotSupportedException (); }
set { throw new NotSupportedException (); }
}
}
}

Something went wrong with that request. Please try again.