cURL / Mailing Lists / curl-library / Single Mail

curl-library

[PATCH] Use Windows / IE proxy settings (automatic settings aren't handled yet)

From: Cédric Bosdonnat <cedric.bosdonnat_at_free.fr>
Date: Thu, 24 Jan 2013 15:44:44 +0100

---
 lib/Makefile.vc6 |    2 +-
 lib/url.c        |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 78 insertions(+), 3 deletions(-)
diff --git a/lib/Makefile.vc6 b/lib/Makefile.vc6
index a8e7741..5ded250 100644
--- a/lib/Makefile.vc6
+++ b/lib/Makefile.vc6
@@ -116,7 +116,7 @@ LFLAGS     = /nologo /machine:$(MACHINE)
 SSLLIBS    = libeay32.lib ssleay32.lib
 ZLIBLIBSDLL= zdll.lib
 ZLIBLIBS   = zlib.lib
-WINLIBS    = ws2_32.lib wldap32.lib advapi32.lib
+WINLIBS    = ws2_32.lib wldap32.lib advapi32.lib winhttp.lib
 CFLAGS     = $(CFLAGS)
 
 CFGSET     = FALSE
diff --git a/lib/url.c b/lib/url.c
index 80c8a99..cac2575 100644
--- a/lib/url.c
+++ b/lib/url.c
@@ -74,6 +74,10 @@ void idn_free (void *ptr);
 int curl_win32_idn_to_ascii(const char *in, char **out);
 #endif  /* USE_LIBIDN */
 
+#ifdef WIN32
+#include <WinHttp.h>
+#endif
+
 #include "urldata.h"
 #include "netrc.h"
 
@@ -3870,6 +3874,21 @@ static bool check_noproxy(const char* name, const char* no_proxy)
   return FALSE;
 }
 
+#ifdef WIN32
+static char* wstrToCstr( LPWSTR wStr )
+{
+	int bufSize;
+	char* out = NULL;
+	if ( wStr != NULL )
+	{
+		bufSize = WideCharToMultiByte( CP_ACP,  0, wStr, -1, NULL, 0, NULL, NULL );
+		out = ( char* )malloc( bufSize * sizeof(char));
+		WideCharToMultiByte( CP_ACP, 0, wStr, -1, out, bufSize, NULL, NULL );
+	}
+	return out;
+}
+#endif
+
 /****************************************************************
 * Detect what (if any) proxy to use. Remember that this selects a host
 * name and is not limited to HTTP proxies only.
@@ -3878,6 +3897,7 @@ static bool check_noproxy(const char* name, const char* no_proxy)
 static char *detect_proxy(struct connectdata *conn)
 {
   char *proxy = NULL;
+  char *no_proxy=NULL;
 
 #ifndef CURL_DISABLE_HTTP
   /* If proxy was not specified, we check for default proxy environment
@@ -3897,7 +3917,62 @@ static char *detect_proxy(struct connectdata *conn)
    * For compatibility, the all-uppercase versions of these variables are
    * checked if the lowercase versions don't exist.
    */
-  char *no_proxy=NULL;
+#ifdef WIN32
+  WINHTTP_CURRENT_USER_IE_PROXY_CONFIG *ieProxyConfig;
+  ieProxyConfig = (WINHTTP_CURRENT_USER_IE_PROXY_CONFIG*)malloc(sizeof(WINHTTP_CURRENT_USER_IE_PROXY_CONFIG));
+  if(WinHttpGetIEProxyConfigForCurrentUser(ieProxyConfig)) {
+    if(!ieProxyConfig->fAutoDetect) {
+      char *ieProxy;
+      char *ieNoProxy;
+      char* pos;
+
+      ieProxy = wstrToCstr(ieProxyConfig->lpszProxy);
+      ieNoProxy = wstrToCstr(ieProxyConfig->lpszProxyBypass);
+
+      /* Convert the ieNoProxy into a proper no_proxy value */
+      if(NULL != ioNoProxy) {
+        no_proxy = strdup(ieNoProxy);
+        pos = strpbrk(no_proxy, "; ");
+        while (NULL != pos) {
+          no_proxy[pos-no_proxy] = ',';
+          pos = strpbrk(no_proxy, "; ");
+        }
+      }
+
+      if(!check_noproxy(conn->host.name, no_proxy)) {
+        /* Look for the http proxy setting */
+        char* tok;
+
+        if(NULL != ieProxy) {
+          tok = strtok(ieProxy, ";");
+          if(strchr(tok, '=') == NULL) {
+            proxy = strdup(ieProxy);
+          }
+          else {
+            do {
+              if(strncmp(tok, "http=", 5) == 0) {
+                /* We found HTTP proxy value, then use it */
+                proxy = strdup( tok + 5 );
+              }
+              tok = strtok(NULL, ";");
+            }
+            while(NULL != tok);
+          }
+        }
+      }
+
+      free(ieProxy);
+      free(ieNoProxy);
+    }
+    else {
+      /* TODO Handle the Proxy config Auto Detection case */
+    }
+
+    GlobalFree( ieProxyConfig->lpszAutoConfigUrl );
+    GlobalFree( ieProxyConfig->lpszProxy );
+    GlobalFree( ieProxyConfig->lpszProxyBypass );
+  }
+#else /* !WIN32 */
   char proxy_env[128];
 
   no_proxy=curl_getenv("no_proxy");
@@ -3948,9 +4023,9 @@ static char *detect_proxy(struct connectdata *conn)
     }
   } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
        non-proxy */
+#endif /* WIN32 */
   if(no_proxy)
     free(no_proxy);
-
 #else /* !CURL_DISABLE_HTTP */
 
   (void)conn;
-- 
1.7.10.4
--=-lVHc3aFil8uzsC56f/5S
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
--=-lVHc3aFil8uzsC56f/5S--
Received on 2001-09-17