[tomboy/mac-0.15.7] [WebSync] Safer use of HttpListener; use from MonoHttp on Windows



commit e71884344a6f2a74cdc3f8dd4472072ecd899b16
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Tue Sep 8 12:06:11 2009 -0700

    [WebSync] Safer use of HttpListener; use from MonoHttp on Windows

 .../WebSyncService/WebSyncPreferencesWidget.cs     |   36 +++++++++++++++++---
 Tomboy/Addins/WebSyncService/WebSyncService.csproj |    2 +-
 2 files changed, 32 insertions(+), 6 deletions(-)
---
diff --git a/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs b/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
index 79b2a78..5d112e7 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
+++ b/Tomboy/Addins/WebSyncService/WebSyncPreferencesWidget.cs
@@ -28,6 +28,11 @@ using System.Web;
 
 using Mono.Unix;
 using Tomboy.WebSync.Api;
+#if !WIN32
+using HL = System.Net;
+#else
+using HL = MonoHttp;
+#endif 
 
 namespace Tomboy.WebSync
 {
@@ -36,7 +41,7 @@ namespace Tomboy.WebSync
 		private Gtk.Entry serverEntry;
 		private Gtk.Button authButton;
 		private Api.OAuth oauth;
-		private MonoHttp.HttpListener listener;
+		private HL.HttpListener listener;
 		
 		private const string callbackHtmlTemplate =
 			@"<html><head><title>{0}</title></head><body><div><h1>{0}</h1>{1}</div></body></html>";
@@ -96,8 +101,10 @@ namespace Tomboy.WebSync
 
 		private void OnAuthButtonClicked (object sender, EventArgs args)
 		{
-			if (listener != null && listener.IsListening)
+			if (listener != null && listener.IsListening) {
+				listener.Stop ();
 				listener.Close ();
+			}
 
 			// TODO: Move this
 			if (Auth == null) {
@@ -122,13 +129,20 @@ namespace Tomboy.WebSync
 			}
 
 			if (!Auth.IsAccessToken) {
-				listener = new MonoHttp.HttpListener ();
+				listener = new HL.HttpListener ();
 				int portToTry = 8000;
 				string callbackUrl = string.Empty;
 				while (!listener.IsListening && portToTry < 9000) {
 					callbackUrl = String.Format ("http://localhost:{0}/tomboy-web-sync/";,
 					                            portToTry);
-					listener.Prefixes.Add (callbackUrl);
+					try {
+						listener.Prefixes.Add (callbackUrl);
+					} catch (Exception e) {
+						Logger.Error ("Exception while trying to add {0} as an HttpListener Prefix",
+							callbackUrl);
+						Logger.Error (e.ToString ());
+						break;
+					}
 					try {
 						listener.Start ();
 						Auth.CallbackUrl = callbackUrl;
@@ -149,6 +163,7 @@ namespace Tomboy.WebSync
 				try {
 					authUrl = Auth.GetAuthorizationUrl ();
 				} catch (Exception e) {
+					listener.Stop ();
 					listener.Close ();
 					Logger.Error ("Failed to get auth URL from " + Server + ". Exception was: " + e.ToString ());
 					// Translators: The web service supporting Tomboy WebSync is not responding as expected
@@ -158,7 +173,15 @@ namespace Tomboy.WebSync
 				}
 
 				IAsyncResult result = listener.BeginGetContext (delegate (IAsyncResult localResult) {
-					var context = listener.EndGetContext (localResult);
+					HL.HttpListenerContext context;
+					try {
+						context = listener.EndGetContext (localResult);
+					} catch (Exception e) {
+						// TODO: Figure out why this error occurs
+						Logger.Error ("Error processing OAuth callback. Could be a sign that you pressed the button to reset the connection. Exception details:");
+						Logger.Error (e.ToString ());
+						return;
+					}
 					// Assuming if we got here user clicked Allow
 					Logger.Debug ("Context request uri query section: " + context.Request.Url.Query);
 					// oauth_verifier is required in OAuth 1.0a, not 1.0
@@ -170,6 +193,7 @@ namespace Tomboy.WebSync
 							throw new ApplicationException ("Unknown error getting access token");
 						Logger.Debug ("Successfully authorized web sync");
 					} catch (Exception e) {
+						listener.Stop ();
 						listener.Close ();
 						Logger.Error ("Failed to authorize web sync, with exception:");
 						Logger.Error (e.ToString ());
@@ -188,6 +212,7 @@ namespace Tomboy.WebSync
 						               Catalog.GetString ("Please return to the Tomboy Preferences window and press Save to start synchronizing."));
 					using (var writer = new System.IO.StreamWriter (context.Response.OutputStream))
 						writer.Write (htmlResponse);
+					listener.Stop ();
 					listener.Close ();
 
 					if (Auth.IsAccessToken) {
@@ -204,6 +229,7 @@ namespace Tomboy.WebSync
 				try {
 					Services.NativeApplication.OpenUrl (authUrl, Screen);
 				} catch (Exception e) {
+					listener.Stop ();
 					listener.Close ();
 					Logger.Error ("Exception opening URL: " + e.Message);
 					// Translators: Sometimes a user's default browser is not set, so we recommend setting it and trying again
diff --git a/Tomboy/Addins/WebSyncService/WebSyncService.csproj b/Tomboy/Addins/WebSyncService/WebSyncService.csproj
index e629a96..be18769 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncService.csproj
+++ b/Tomboy/Addins/WebSyncService/WebSyncService.csproj
@@ -21,7 +21,7 @@
     <DebugType>Full</DebugType>
     <Optimize>false</Optimize>
     <OutputPath>..\..\..\bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DefineConstants>TRACE;DEBUG;WIN32</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <PlatformTarget>x86</PlatformTarget>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]