Re: [Patch] Gallery export + HTTP Auth



Hi,

Maybe a bit a better solution. This time the patch uses a regex and not
substring-crap. The regex is used to determine whether it's an user+pass
containing-url or not and also splits it up.

BTW, this patch is not gallery-only specific, all connections using
http-basic-authentication via FormClient can benefit from it, if the url
was specified the right way (http://username:password example com).

cheers,
Fabian
--- f-spot-0.1.10/src/FormClient.cs.old	2006-03-10 10:24:41.000000000 +0100
+++ f-spot-0.1.10/src/FormClient.cs	2006-03-10 11:01:09.000000000 +0100
@@ -2,6 +2,7 @@
 using System.Net;
 using System.IO;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Web;
@@ -27,6 +28,14 @@
 	private bool multipart = false;
 	private bool first_item;
 
+	private string urlRegex =
+		"(http|https|ftp)://" +  // matches protocol part
+		"(\\w*){1}:(\\w*){1}@" + // matches a user:pass host construct
+		"[-\\w.]+" +             // matches host part
+		"(:\\d+)?" +             // matches port (optional)
+		"(~?/([\\w/_.]*(\\?\\S+)?)?)?[^\"]"; // matches path, and anything else (optional)
+	private Regex urlPattern;
+
 	public bool Buffer = false;
 	public bool SuppressCookiePath = false;
 
@@ -39,6 +48,7 @@
 	{
 		this.Cookies = cookies;
 		this.Items = new ArrayList ();
+		this.urlPattern = new Regex(urlRegex);
 	}
 	
 	public FormClient ()
@@ -198,6 +208,17 @@
 	{
 		this.Progress = progress_item;
 		Request = (HttpWebRequest) WebRequest.Create (uri);
+
+		GroupCollection gc = urlPattern.Match(uri.ToString()).Groups;
+		if (gc.Count > 1) {
+			NetworkCredential cred = new NetworkCredential(gc[2].ToString(), gc[3].ToString());
+			CredentialCache credcache = new CredentialCache();
+			credcache.Add(uri, "Basic", cred);
+			
+			Request.PreAuthenticate = true;
+			Request.Credentials = credcache;
+		}
+
 		CookieCollection cookie_collection = Cookies.GetCookies (uri);
 
 		Request.CookieContainer = new CookieContainer ();


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