Author: miguel
Date: 2007-12-27 13:30:23 -0500 (Thu, 27 Dec 2007)
New Revision: 91959

Modified:
   trunk/mcs/class/System/System.Net.Mail/SmtpClient.cs
   trunk/mcs/class/System/System.Net/ChangeLog
   trunk/mcs/class/System/System.Net/HttpListenerContext.cs
   trunk/mcs/class/System/System.Net/WebClient.cs
Log:
2007-12-27  Miguel de Icaza  <[EMAIL PROTECTED]>

        * WebClient.cs: We catch exceptions that happen on the helper
        async threads toa void terminating the application on errors, and
        also raise a variant of the OnXXXXCompleted methods that take an
        Exception argument.

        Note that this differs from the .NET implementation in that
        we actually raise a signal on error, and set the exception, and in
        their case no error is ever raised.

        We now call CompleteAsync in the OnXXXCompleted handlers to ensure
        that the WebClient can be reused immediately by the callback.

        This fixes #350621.

        This also should fix the bug report on IRC where someone
        complained that they could not "chain" invocations to XXXAsync
        methods from the OnXXXXCompleted handlers due to the WebClient
        refusing to work if there was an ongoing operation



Modified: trunk/mcs/class/System/System.Net/ChangeLog
===================================================================
--- trunk/mcs/class/System/System.Net/ChangeLog 2007-12-27 18:28:51 UTC (rev 
91958)
+++ trunk/mcs/class/System/System.Net/ChangeLog 2007-12-27 18:30:23 UTC (rev 
91959)
@@ -1,3 +1,24 @@
+2007-12-27  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * WebClient.cs: We catch exceptions that happen on the helper
+       async threads toa void terminating the application on errors, and
+       also raise a variant of the OnXXXXCompleted methods that take an
+       Exception argument.
+
+       Note that this differs from the .NET implementation in that
+       we actually raise a signal on error, and set the exception, and in
+       their case no error is ever raised.
+
+       We now call CompleteAsync in the OnXXXCompleted handlers to ensure
+       that the WebClient can be reused immediately by the callback.
+
+       This fixes #350621.
+
+       This also should fix the bug report on IRC where someone
+       complained that they could not "chain" invocations to XXXAsync
+       methods from the OnXXXXCompleted handlers due to the WebClient
+       refusing to work if there was an ongoing operation
+
 2007-12-24  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * WebClient.cs: Do not validate method to be null, we do not do

Modified: trunk/mcs/class/System/System.Net/HttpListenerContext.cs
===================================================================
--- trunk/mcs/class/System/System.Net/HttpListenerContext.cs    2007-12-27 
18:28:51 UTC (rev 91958)
+++ trunk/mcs/class/System/System.Net/HttpListenerContext.cs    2007-12-27 
18:30:23 UTC (rev 91959)
@@ -96,7 +96,7 @@
                internal IPrincipal ParseBasicAuthentication (string authData) {
                        try {
                                // Basic AUTH Data is a formatted Base64 String
-                               string domain = null;
+                               //string domain = null;
                                string user = null;
                                string password = null;
                                int pos = -1;
@@ -117,7 +117,7 @@
                                pos = authString.IndexOf ("\\");
        
                                if (pos > 0) {
-                                       domain = authString.Substring (0, pos);
+                                       //domain = authString.Substring (0, 
pos);
                                        user = authString.Substring (pos);
                                } else {
                                        user = authString;

Modified: trunk/mcs/class/System/System.Net/WebClient.cs
===================================================================
--- trunk/mcs/class/System/System.Net/WebClient.cs      2007-12-27 18:28:51 UTC 
(rev 91958)
+++ trunk/mcs/class/System/System.Net/WebClient.cs      2007-12-27 18:30:23 UTC 
(rev 91959)
@@ -30,7 +30,34 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-
+//
+// Notes on CancelAsync and Async methods:
+//
+//    WebClient.CancelAsync is implemented by calling Thread.Interrupt
+//    in our helper thread.   The various async methods have to cancel
+//    any ongoing requests by calling request.Abort () at that point.
+//    In a few places (UploadDataCore, UploadValuesCore,
+//    UploadFileCore) we catch the ThreadInterruptedException and
+//    abort the request there.
+//
+//    Higher level routines (the async callbacks) also need to catch
+//    the exception and raise the OnXXXXCompleted events there with
+//    the "canceled" flag set to true. 
+//
+//    In a few other places where these helper routines are not used
+//    (OpenReadAsync for example) catching the ThreadAbortException
+//    also must abort the request.
+//
+//    The Async methods currently differ in their implementation from
+//    the .NET implementation in that we manually catch any other
+//    exceptions and correctly raise the OnXXXXCompleted passing the
+//    Exception that caused the problem.   The .NET implementation
+//    does not seem to have a mechanism to flag errors that happen
+//    during downloads though.    We do this because we still need to
+//    catch the exception on these helper threads, or we would
+//    otherwise kill the application (on the 2.x profile, uncaught
+//    exceptions in threads terminate the application).
+//
 using System;
 using System.Collections.Specialized;
 using System.ComponentModel;
@@ -1047,12 +1074,14 @@
                                                byte [] data = DownloadDataCore 
((Uri) args [0], args [1]);
                                                OnDownloadDataCompleted (
                                                        new 
DownloadDataCompletedEventArgs (data, null, false, args [1]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                OnDownloadDataCompleted (
                                                        new 
DownloadDataCompletedEventArgs (null, null, true, args [1]));
                                                throw;
-                                       } 
+                                       } catch (Exception e){
+                                               OnDownloadDataCompleted (
+                                                       new 
DownloadDataCompletedEventArgs (null, e, false, args [1]));
+                                       }
                                });
                                object [] cb_args = new object [] {address, 
userToken};
                                async_thread.Start (cb_args);
@@ -1083,12 +1112,13 @@
                                                DownloadFileCore ((Uri) args 
[0], (string) args [1], args [2]);
                                                OnDownloadFileCompleted (
                                                        new 
AsyncCompletedEventArgs (null, false, args [2]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                OnDownloadFileCompleted (
                                                        new 
AsyncCompletedEventArgs (null, true, args [2]));
-                                       } 
-                                       });
+                                       } catch (Exception e){
+                                               OnDownloadFileCompleted (
+                                                       new 
AsyncCompletedEventArgs (e, false, args [2]));
+                                       }});
                                object [] cb_args = new object [] {address, 
fileName, userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1116,12 +1146,13 @@
                                                string data = 
encoding.GetString (DownloadDataCore ((Uri) args [0], args [1]));
                                                OnDownloadStringCompleted (
                                                        new 
DownloadStringCompletedEventArgs (data, null, false, args [1]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                OnDownloadStringCompleted (
                                                        new 
DownloadStringCompletedEventArgs (null, null, true, args [1]));
-                                       } 
-                                       });
+                                       } catch (Exception e){
+                                               OnDownloadStringCompleted (
+                                                       new 
DownloadStringCompletedEventArgs (null, e, false, args [1]));
+                                       }});
                                object [] cb_args = new object [] {address, 
userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1152,14 +1183,14 @@
                                                Stream stream = ProcessResponse 
(response);
                                                OnOpenReadCompleted (
                                                        new 
OpenReadCompletedEventArgs (stream, null, false, args [1]));
-                                               CompleteAsync ();               
                                
                                        } catch (ThreadInterruptedException){
                                                if (request != null)
                                                        request.Abort ();
                                                
                                                OnOpenReadCompleted (new 
OpenReadCompletedEventArgs (null, null, true, args [1]));
-                                       }
-                                       } );
+                                       } catch (Exception e){
+                                               OnOpenReadCompleted (new 
OpenReadCompletedEventArgs (null, e, false, args [1]));
+                                       } });
                                object [] cb_args = new object [] {address, 
userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1194,14 +1225,15 @@
                                                Stream stream = 
request.GetRequestStream ();
                                                OnOpenWriteCompleted (
                                                        new 
OpenWriteCompletedEventArgs (stream, null, false, args [2]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                if (request != null)
                                                        request.Abort ();
                                                OnOpenWriteCompleted (
                                                        new 
OpenWriteCompletedEventArgs (null, null, true, args [2]));
-                                       } 
-                                       });
+                                       } catch (Exception e){
+                                               OnOpenWriteCompleted (
+                                                       new 
OpenWriteCompletedEventArgs (null, e, false, args [2]));
+                                       }});
                                object [] cb_args = new object [] {address, 
method, userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1239,10 +1271,12 @@
                                        
                                                OnUploadDataCompleted (
                                                        new 
UploadDataCompletedEventArgs (data2, null, false, args [3]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                OnUploadDataCompleted (
                                                        new 
UploadDataCompletedEventArgs (null, null, true, args [3]));
+                                       } catch (Exception e){
+                                               OnUploadDataCompleted (
+                                                       new 
UploadDataCompletedEventArgs (null, e, false, args [3]));
                                        }});
                                object [] cb_args = new object [] {address, 
method, data,  userToken};
                                async_thread.Start (cb_args);
@@ -1283,8 +1317,10 @@
                                        } catch (ThreadInterruptedException){
                                                OnUploadFileCompleted (
                                                        new 
UploadFileCompletedEventArgs (null, null, true, args [3]));
-                                       }
-                                       });
+                                       } catch (Exception e){
+                                               OnUploadFileCompleted (
+                                                       new 
UploadFileCompletedEventArgs (null, e, false, args [3]));
+                                       }});
                                object [] cb_args = new object [] {address, 
method, fileName,  userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1320,12 +1356,13 @@
                                                string data2 = UploadString 
((Uri) args [0], (string) args [1], (string) args [2]);
                                                OnUploadStringCompleted (
                                                        new 
UploadStringCompletedEventArgs (data2, null, false, args [3]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                OnUploadStringCompleted (
                                                        new 
UploadStringCompletedEventArgs (null, null, true, args [3]));
-                                       } 
-                                       });
+                                       } catch (Exception e){
+                                               OnUploadStringCompleted (
+                                                       new 
UploadStringCompletedEventArgs (null, e, false, args [3]));
+                                       }});
                                object [] cb_args = new object [] {address, 
method, data, userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1360,12 +1397,13 @@
                                                byte [] data = UploadValuesCore 
((Uri) args [0], (string) args [1], (NameValueCollection) args [2], args [3]);
                                                OnUploadValuesCompleted (
                                                        new 
UploadValuesCompletedEventArgs (data, null, false, args [3]));
-                                               CompleteAsync ();
                                        } catch (ThreadInterruptedException){
                                                OnUploadValuesCompleted (
                                                        new 
UploadValuesCompletedEventArgs (null, null, true, args [3]));
-                                       }
-                                       });
+                                       } catch (Exception e){
+                                               OnUploadValuesCompleted (
+                                                       new 
UploadValuesCompletedEventArgs (null, e, false, args [3]));
+                                       }});
                                object [] cb_args = new object [] {address, 
method, values,  userToken};
                                async_thread.Start (cb_args);
                        }
@@ -1373,12 +1411,14 @@
 
                protected virtual void OnDownloadDataCompleted 
(DownloadDataCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (DownloadDataCompleted != null)
                                DownloadDataCompleted (this, args);
                }
 
                protected virtual void OnDownloadFileCompleted 
(AsyncCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (DownloadFileCompleted != null)
                                DownloadFileCompleted (this, args);
                }
@@ -1391,30 +1431,35 @@
 
                protected virtual void OnDownloadStringCompleted 
(DownloadStringCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (DownloadStringCompleted != null)
                                DownloadStringCompleted (this, args);
                }
 
                protected virtual void OnOpenReadCompleted 
(OpenReadCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (OpenReadCompleted != null)
                                OpenReadCompleted (this, args);
                }
 
                protected virtual void OnOpenWriteCompleted 
(OpenWriteCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (OpenWriteCompleted != null)
                                OpenWriteCompleted (this, args);
                }
 
                protected virtual void OnUploadDataCompleted 
(UploadDataCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (UploadDataCompleted != null)
                                UploadDataCompleted (this, args);
                }
 
                protected virtual void OnUploadFileCompleted 
(UploadFileCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (UploadFileCompleted != null)
                                UploadFileCompleted (this, args);
                }
@@ -1427,12 +1472,14 @@
 
                protected virtual void OnUploadStringCompleted 
(UploadStringCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (UploadStringCompleted != null)
                                UploadStringCompleted (this, args);
                }
 
                protected virtual void OnUploadValuesCompleted 
(UploadValuesCompletedEventArgs args)
                {
+                       CompleteAsync ();
                        if (UploadValuesCompleted != null)
                                UploadValuesCompleted (this, args);
                }

Modified: trunk/mcs/class/System/System.Net.Mail/SmtpClient.cs
===================================================================
--- trunk/mcs/class/System/System.Net.Mail/SmtpClient.cs        2007-12-27 
18:28:51 UTC (rev 91958)
+++ trunk/mcs/class/System/System.Net.Mail/SmtpClient.cs        2007-12-27 
18:30:23 UTC (rev 91959)
@@ -275,7 +275,7 @@
 
                private string EncodeBody (AlternateView av)
                {
-                       Encoding encoding = av.ContentType.CharSet != null ? 
Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
+                       //Encoding encoding = av.ContentType.CharSet != null ? 
Encoding.GetEncoding (av.ContentType.CharSet) : Encoding.UTF8;
 
                        byte [] bytes = new byte [av.ContentStream.Length];
                        av.ContentStream.Read (bytes, 0, bytes.Length);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to