byte [] readBuffer;
int readBufferOffset;
int readBufferSize;
+ int stream_length; // -1 when CL not present
int contentLength;
int totalRead;
- long totalWritten;
+ internal long totalWritten;
bool nextReadCalled;
int pendingReads;
int pendingWrites;
bool complete_request_written;
int read_timeout;
int write_timeout;
-
- public WebConnectionStream (WebConnection cnc)
- {
+ 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 = cnc.Data.request;
+ this.request = data.request;
read_timeout = request.ReadWriteTimeout;
write_timeout = read_timeout;
this.cnc = cnc;
- string contentType = cnc.Data.Headers ["Transfer-Encoding"];
- bool chunkedRead = (contentType != null && contentType.ToLower ().IndexOf ("chunked") != -1);
- string clength = cnc.Data.Headers ["Content-Length"];
+ 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);
} 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;
bool isProxy = (request.Proxy != null && !request.Proxy.IsBypassed (request.Address));
string header_name = (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate";
string authHeader = cnc.Data.Headers [header_name];
- return (authHeader != null && authHeader.IndexOf ("NTLM") != -1);
+ return (authHeader != null && authHeader.IndexOf ("NTLM", StringComparison.Ordinal) != -1);
}
internal void CheckResponseInBuffer ()
internal WebConnection Connection {
get { return cnc; }
}
-#if NET_2_0
public override bool CanTimeout {
get { return true; }
}
-#endif
-#if NET_2_0
- public override
-#endif
- int ReadTimeout {
+ public override int ReadTimeout {
get {
return read_timeout;
}
}
}
-#if NET_2_0
- public override
-#endif
- int WriteTimeout {
+ public override int WriteTimeout {
get {
return write_timeout;
}
result.InnerAsyncResult = r;
result.DoCallback ();
} else {
- EndWrite (r);
+ try {
+ EndWrite (r);
+ } catch {
+ }
}
}
result.InnerAsyncResult = r;
result.DoCallback ();
} else {
- EndRead (r);
+ try {
+ EndRead (r);
+ } catch {
+ }
}
}
public override int Read (byte [] buffer, int offset, int size)
{
- AsyncCallback cb = new AsyncCallback (ReadCallbackWrapper);
+ AsyncCallback cb = cb_wrapper;
WebAsyncResult res = (WebAsyncResult) BeginRead (buffer, offset, size, cb, null);
if (!res.IsCompleted && !res.WaitUntilComplete (ReadTimeout, false)) {
nextReadCalled = true;
}
if (cb != null)
- cb = new AsyncCallback (ReadCallbackWrapper);
+ cb = cb_wrapper;
if (contentLength != Int32.MaxValue && contentLength - totalRead < size)
size = contentLength - totalRead;
nextReadCalled = true;
cnc.Close (true);
result.SetCompleted (false, exc);
+ result.DoCallback ();
throw;
}
AsyncCallback callback = null;
if (cb != null)
- callback = new AsyncCallback (WriteCallbackWrapper);
+ callback = cb_wrapper;
if (sendChunked) {
WriteRequest ();
size = chunkSize;
}
- result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
+ try {
+ result.InnerAsyncResult = cnc.BeginWrite (request, buffer, offset, size, callback, result);
+ } catch (Exception) {
+ if (!IgnoreIOErrors)
+ throw;
+ result.SetCompleted (true, 0);
+ result.DoCallback ();
+ }
totalWritten += size;
return result;
}
throw result.Exception;
try {
- cnc.EndWrite (request, result.InnerAsyncResult);
+ cnc.EndWrite2 (request, result.InnerAsyncResult);
result.SetCompleted (false, 0);
+ result.DoCallback ();
} catch (Exception e) {
- result.SetCompleted (false, e);
- throw;
+ if (IgnoreIOErrors)
+ result.SetCompleted (false, 0);
+ else
+ result.SetCompleted (false, e);
+ result.DoCallback ();
+ if (!IgnoreIOErrors)
+ throw;
} finally {
if (sendChunked) {
lock (locker) {
public override void Write (byte [] buffer, int offset, int size)
{
- AsyncCallback cb = new AsyncCallback (WriteCallbackWrapper);
+ AsyncCallback cb = cb_wrapper;
WebAsyncResult res = (WebAsyncResult) BeginWrite (buffer, offset, size, cb, null);
if (!res.IsCompleted && !res.WaitUntilComplete (WriteTimeout, false)) {
KillBuffer ();
{
}
- internal void SetHeaders (byte [] buffer, int offset, int size)
+ internal void SetHeadersAsync (byte[] buffer, WebAsyncResult result)
{
if (headersSent)
return;
- headers = new byte [size];
- Buffer.BlockCopy (buffer, offset, headers, 0, size);
+ headers = buffer;
long cl = request.ContentLength;
string method = request.Method;
bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
- method == "TRACE" || method == "DELETE");
- if (sendChunked || cl > -1 || no_writestream) {
- WriteHeaders ();
+ method == "TRACE");
+ bool webdav = (method == "PROPFIND" || method == "PROPPATCH" || method == "MKCOL" ||
+ method == "COPY" || method == "MOVE" || method == "LOCK" ||
+ method == "UNLOCK");
+ if (sendChunked || cl > -1 || no_writestream || webdav) {
+
+ headersSent = true;
+
+ try {
+ result.InnerAsyncResult = cnc.BeginWrite (request, headers, 0, headers.Length, new AsyncCallback(SetHeadersCB), result);
+ if (result.InnerAsyncResult == null) {
+ // when does BeginWrite return null? Is the case when the request is aborted?
+ if (!result.IsCompleted)
+ result.SetCompleted (true, 0);
+ result.DoCallback ();
+ }
+ } catch (Exception exc) {
+ result.SetCompleted (true, exc);
+ result.DoCallback ();
+ }
+ }
+ }
+
+ void SetHeadersCB (IAsyncResult r)
+ {
+ WebAsyncResult result = (WebAsyncResult) r.AsyncState;
+ result.InnerAsyncResult = null;
+ try {
+ cnc.EndWrite2 (request, r);
+ result.SetCompleted (false, 0);
if (!initRead) {
initRead = true;
WebConnection.InitRead (cnc);
}
+ long cl = request.ContentLength;
if (!sendChunked && cl == 0)
- Close ();
+ 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 {
return (length > 0) ? cnc.BeginWrite (request, bytes, 0, length, cb, state) : null;
}
- void WriteHeaders ()
- {
- if (headersSent)
- return;
-
- headersSent = true;
- string err_msg = null;
- if (!cnc.Write (request, headers, 0, headers.Length, ref err_msg))
- throw new WebException ("Error writing request: " + err_msg, null, WebExceptionStatus.SendFailure, null);
- }
-
internal void WriteRequest ()
{
if (requestWritten)
return;
- if (sendChunked) {
- requestWritten = true;
+ requestWritten = true;
+ if (sendChunked)
return;
- }
if (!allowBuffering || writeBuffer == null)
return;
if (!headersSent) {
string method = request.Method;
bool no_writestream = (method == "GET" || method == "CONNECT" || method == "HEAD" ||
- method == "TRACE" || method == "DELETE");
+ method == "TRACE");
if (!no_writestream)
request.InternalContentLength = length;
- request.SendRequestHeaders ();
+
+ byte[] requestHeaders = request.GetRequestHeaders ();
+ WebAsyncResult ar = new WebAsyncResult (null, null);
+ SetHeadersAsync (requestHeaders, ar);
+ ar.AsyncWaitHandle.WaitOne ();
+ if (ar.Exception != null)
+ throw ar.Exception;
}
- WriteHeaders ();
+
if (cnc.Data.StatusCode != 0 && cnc.Data.StatusCode != 100)
return;
throw new WebException ("Request was cancelled.", io, WebExceptionStatus.RequestCanceled);
}
- WriteRequest ();
+ // Commented out the next line to fix xamarin bug #1512
+ //WriteRequest ();
disposed = true;
}
}
public override long Length {
- get { throw new NotSupportedException (); }
+ get {
+ if (!isRead)
+ throw new NotSupportedException ();
+ return stream_length;
+ }
}
public override long Position {