[PATCH setup 11/11] Use wininet for fetching URLs in direct (non-proxy) case (DO NOT APPLY)

Jon Turney jon.turney@dronecode.org.uk
Mon May 1 12:58:00 GMT 2017


On 29/04/2017 11:53, Jon Turney wrote:
> On 28/04/2017 16:33, Ã
ke Rehnman wrote:
>> On 2017-04-28 14:12, Jon Turney wrote:
>>> From the discussion in [1], I was somewhat surprised to learn
>>> that setup doesn't support https or ftps.
>> For the same exact reason I've just recently patched in curl....
>
> I'd be very interested to see that patch :)

Thanks very much for the patch.

So... I'm in two minds about this.   Your patch seems good, but perhaps 
the problems with using wininet aren't insurmountable, and avoiding 
another dependency would also be good.

Anyhow, I had another go the wininet patch, attached.  I guess you have 
some use case for this, so perhaps you could give it a try and see if it 
works for you?
-------------- next part --------------
From b11932cacaa1743b3dc60c149f268d767e99bf9f Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Thu, 30 Mar 2017 11:48:44 +0100
Subject: [PATCH setup] Use wininet for fetching URLs in direct (non-proxy)
 case

From the discussion in [1], I was somewhat surprised to learn that setup
doesn't support https or ftps.

Switch to using wininet for fetching URLs in the direct (non-proxy) case, as
well. (It's already used in proxy case). This allows https and ftps
protocols to be used.

For the moment, we keep around the existing, hand-built URL fetching as
'Direct (legacy)'.

Arrange for mirrors.lst and setup.ini to be cached by wininet, but not
package archives (as setup maintains it's own cache of those)

Read from wininet in chunks rather than the whole file, so we can report
progress feedback as we download.

Also fix up some bad indentation.

I think the reason we have a handbuilt HTTP client is that back in 2000 or
so, we were concerned about the case where IE5 wasn't installed and so
wininet wasn't available.  But who knows...

[1] https://cygwin.com/ml/cygwin/2017-03/msg00384.html

Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
---
 ConnectionSetting.cc |  5 +++++
 geturl.cc            |  8 ++++----
 net.cc               | 34 +++++++++++++++++-----------------
 netio.cc             | 34 ++++++++++++++++++++++++++++++----
 netio.h              |  4 +++-
 nio-ie5.cc           | 33 ++++++++++++++++++++++++---------
 nio-ie5.h            |  4 ++--
 res.rc               |  2 ++
 resource.h           |  1 +
 9 files changed, 88 insertions(+), 37 deletions(-)

diff --git a/ConnectionSetting.cc b/ConnectionSetting.cc
index 5baf76c..1154d94 100644
--- a/ConnectionSetting.cc
+++ b/ConnectionSetting.cc
@@ -49,6 +49,9 @@ ConnectionSetting::~ConnectionSetting ()
       sprintf(port_str, "%d", NetIO::net_proxy_port);
       UserSettings::instance().set("net-proxy-port", port_str);
       break;
+    case IDC_NET_DIRECT_LEGACY:
+      UserSettings::instance().set("net-method", "Legacy");
+      break;
     default:
 	break;
     }
@@ -63,6 +66,8 @@ ConnectionSetting::typeFromString(const std::string& aType)
     return IDC_NET_IE5;
   if (!casecompare(aType, "Proxy"))
     return IDC_NET_PROXY;
+  if (!casecompare(aType, "Legacy"))
+    return IDC_NET_DIRECT_LEGACY;
 
   /* A sanish default */
   return IDC_NET_IE5;
diff --git a/geturl.cc b/geturl.cc
index 17ad8e9..5abd39a 100644
--- a/geturl.cc
+++ b/geturl.cc
@@ -106,12 +106,12 @@ progress (int bytes)
 }
 
 static void
-getUrlToStream (const string &_url, io_stream *output)
+getUrlToStream (const string &_url, io_stream *output, bool cachable)
 {
   Log (LOG_BABBLE) << "getUrlToStream " << _url << endLog;
   is_local_install = (source == IDC_SOURCE_LOCALDIR);
   init_dialog (_url, 0);
-  NetIO *n = NetIO::open (_url.c_str());
+  NetIO *n = NetIO::open (_url.c_str(), cachable);
   if (!n || !n->ok ())
     {
       delete n;
@@ -153,7 +153,7 @@ get_url_to_membuf (const string &_url, HWND owner)
   try 
     {
       Log (LOG_BABBLE) << "get_url_to_membuf " << _url << endLog;
-      getUrlToStream (_url, membuf);
+      getUrlToStream (_url, membuf, true);
       
       if (membuf->seek (0, IO_SEEK_SET))
     	{
@@ -213,7 +213,7 @@ get_url_to_file (const string &_url,
 
   remove (_filename.c_str());		/* but ignore errors */
 
-  NetIO *n = NetIO::open (_url.c_str());
+  NetIO *n = NetIO::open (_url.c_str(), false);
   if (!n || !n->ok ())
     {
       delete n;
diff --git a/net.cc b/net.cc
index 659cf9b..903f096 100644
--- a/net.cc
+++ b/net.cc
@@ -37,30 +37,31 @@ extern ThreeBarProgressPage Progress;
 
 static StringOption ProxyOption ("", 'p', "proxy", "HTTP/FTP proxy (host:port)", false);
 
-static int rb[] = { IDC_NET_IE5, IDC_NET_DIRECT, IDC_NET_PROXY, 0 };
+static int rb[] = { IDC_NET_IE5, IDC_NET_DIRECT, IDC_NET_PROXY, IDC_NET_DIRECT_LEGACY, 0 };
 static bool doing_loading = false;
 
 void
 NetPage::CheckIfEnableNext ()
 {
-  int e = 0, p = 0, pu = 0;
+  int e = 0, p = 0;
   DWORD ButtonFlags = PSWIZB_BACK;
 
-  if (NetIO::net_method == IDC_NET_IE5)
-    pu = 1;
-  if (NetIO::net_method == IDC_NET_IE5 || NetIO::net_method == IDC_NET_DIRECT)
+  if (NetIO::net_method == IDC_NET_IE5 ||
+      NetIO::net_method == IDC_NET_DIRECT ||
+      NetIO::net_method == IDC_NET_DIRECT_LEGACY)
     e = 1;
   else if (NetIO::net_method == IDC_NET_PROXY)
     {
-      p = pu = 1;
+      p = 1;
       if (NetIO::net_proxy_host && NetIO::net_proxy_port)
-	e = 1;
+        e = 1;
+    }
+
+  if (e)
+    {
+      // There's something in the proxy and port boxes, enable "Next".
+      ButtonFlags |= PSWIZB_NEXT;
     }
-	if (e)
-	{
-		// There's something in the proxy and port boxes, enable "Next".
-		ButtonFlags |= PSWIZB_NEXT;
-	}
 
   GetOwner ()->SetButtons (ButtonFlags);
 
@@ -131,8 +132,8 @@ NetPage::OnInit ()
 
   // Check to see if any radio buttons are selected. If not, select a default.
   if (SendMessage (GetDlgItem (IDC_NET_IE5), BM_GETCHECK, 0, 0) != BST_CHECKED
-      && SendMessage (GetDlgItem (IDC_NET_PROXY), BM_GETCHECK, 0, 0)
-	 != BST_CHECKED)
+      && SendMessage (GetDlgItem (IDC_NET_PROXY), BM_GETCHECK, 0, 0) != BST_CHECKED
+      && SendMessage (GetDlgItem (IDC_NET_DIRECT_LEGACY), BM_GETCHECK, 0, 0) != BST_CHECKED)
     SendMessage (GetDlgItem (IDC_NET_DIRECT), BM_CLICK, 0, 0);
 }
 
@@ -141,9 +142,7 @@ NetPage::OnNext ()
 {
   save_dialog (GetHWND ());
 
-  Log (LOG_PLAIN) << "net: "
-    << ((NetIO::net_method == IDC_NET_IE5) ? "IE5" :
-        (NetIO::net_method == IDC_NET_DIRECT) ? "Direct" : "Proxy") << endLog;
+  Log (LOG_PLAIN) << "net: " << NetIO::net_method_name()  << endLog;
 
   Progress.SetActivateTask (WM_APP_START_SITE_INFO_DOWNLOAD);
   return IDD_INSTATUS;
@@ -170,6 +169,7 @@ NetPage::OnMessageCmd (int id, HWND hwndctl, UINT code)
     case IDC_NET_IE5:
     case IDC_NET_DIRECT:
     case IDC_NET_PROXY:
+    case IDC_NET_DIRECT_LEGACY:
     case IDC_PROXY_HOST:
     case IDC_PROXY_PORT:
       save_dialog (GetHWND());
diff --git a/netio.cc b/netio.cc
index 5ec0b9a..cf634c1 100644
--- a/netio.cc
+++ b/netio.cc
@@ -122,26 +122,32 @@ NetIO::read (char *buf, int nbytes)
 }
 
 NetIO *
-NetIO::open (char const *url)
+NetIO::open (char const *url, bool cachable)
 {
   NetIO *rv = 0;
   enum
-  { http, ftp, file }
+  { http, https, ftp, ftps, file }
   proto;
   if (strncmp (url, "http://", 7) == 0)
     proto = http;
+  else if (strncmp (url, "https://", 8) == 0)
+    proto = https;
   else if (strncmp (url, "ftp://", 6) == 0)
     proto = ftp;
+  else if (strncmp (url, "ftps://", 7) == 0)
+    proto = ftps;
   else
     proto = file;
 
   if (proto == file)
     rv = new NetIO_File (url);
   else if (net_method == IDC_NET_IE5)
-    rv = new NetIO_IE5 (url);
+    rv = new NetIO_IE5 (url, false, cachable);
   else if (net_method == IDC_NET_PROXY)
     rv = new NetIO_HTTP (url);
   else if (net_method == IDC_NET_DIRECT)
+    rv = new NetIO_IE5 (url, true, cachable);
+  else if (net_method == IDC_NET_DIRECT_LEGACY)
     {
       switch (proto)
 	{
@@ -154,10 +160,12 @@ NetIO::open (char const *url)
 	case file:
 	  rv = new NetIO_File (url);
 	  break;
+	default:
+	  mbox (NULL, "Protocol not handled by legacy URL handler", "Cygwin Setup", MB_OK);
 	}
     }
 
-  if (!rv->ok ())
+  if (rv && !rv->ok ())
     {
       delete rv;
       return 0;
@@ -284,3 +292,21 @@ NetIO::get_ftp_auth (HWND owner)
   passwd = &net_ftp_passwd;
   return auth_common (hinstance, IDD_FTP_AUTH, owner);
 }
+
+const char *
+NetIO::net_method_name ()
+{
+  switch (net_method)
+    {
+    case IDC_NET_IE5:
+      return "IE5";
+    case IDC_NET_DIRECT:
+      return "Direct";
+    case IDC_NET_PROXY:
+      return "Proxy";
+    case IDC_NET_DIRECT_LEGACY:
+      return "Direct (legacy)";
+    default:
+      return "Unknown";
+    }
+}
diff --git a/netio.h b/netio.h
index aa06edb..7b7d13f 100644
--- a/netio.h
+++ b/netio.h
@@ -51,7 +51,7 @@ public:
      the given URL.  It uses the network setup state in state.h.  If
      anything fails, either the return values is NULL or the returned
      object is !ok() */
-  static NetIO *open (char const *url);
+  static NetIO *open (char const *url, bool cachable);
 
   /* If !ok() that means the transfer isn't happening. */
   virtual int ok ();
@@ -64,6 +64,8 @@ public:
   static char *net_proxy_host;
   static int net_proxy_port;
 
+  static const char *net_method_name();
+
   /* Helper functions for http/ftp protocols.  Both return nonzero for
      "cancel", zero for "ok".  They set net_proxy_user, etc, in
      state.h */
diff --git a/nio-ie5.cc b/nio-ie5.cc
index 236c459..3375c04 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -27,28 +27,37 @@
 #include "netio.h"
 #include "nio-ie5.h"
 
-static HINTERNET internet = 0;
+static HINTERNET internet_direct = 0;
+static HINTERNET internet_preconfig = 0;
 
-NetIO_IE5::NetIO_IE5 (char const *_url):
+NetIO_IE5::NetIO_IE5 (char const *_url, bool direct, bool cachable):
 NetIO (_url)
 {
   int resend = 0;
+  HINTERNET *internet;
 
-  if (internet == 0)
+  if (direct)
+    internet = &internet_direct;
+  else
+    internet = &internet_preconfig;
+
+  if (*internet == 0)
     {
       InternetAttemptConnect (0);
-      internet = InternetOpen ("Cygwin Setup", INTERNET_OPEN_TYPE_PRECONFIG,
-			       NULL, NULL, 0);
+      *internet = InternetOpen ("Cygwin Setup",
+				direct ? INTERNET_OPEN_TYPE_DIRECT : INTERNET_OPEN_TYPE_PRECONFIG,
+				NULL, NULL, 0);
     }
 
   DWORD flags =
- //    INTERNET_FLAG_DONT_CACHE |
     INTERNET_FLAG_KEEP_CONNECTION |
- //   INTERNET_FLAG_PRAGMA_NOCACHE |
- //   INTERNET_FLAG_RELOAD |
     INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_PASSIVE;
 
-  connection = InternetOpenUrl (internet, url, NULL, 0, flags, 0);
+  if (!cachable) {
+    flags |= INTERNET_FLAG_NO_CACHE_WRITE;
+  }
+
+  connection = InternetOpenUrl (*internet, url, NULL, 0, flags, 0);
 
 try_again:
 
@@ -147,8 +156,14 @@ NetIO_IE5::ok ()
 int
 NetIO_IE5::read (char *buf, int nbytes)
 {
+  /* Read in chunks rather than the whole file at once, so we can do progress
+     reporting */
+  if (nbytes > 4096)
+    nbytes = 4096;
+
   DWORD actual;
   if (InternetReadFile (connection, buf, nbytes, &actual))
     return actual;
+
   return -1;
 }
diff --git a/nio-ie5.h b/nio-ie5.h
index 801cf8a..9a66e2a 100644
--- a/nio-ie5.h
+++ b/nio-ie5.h
@@ -22,8 +22,8 @@ class NetIO_IE5:public NetIO
 {
   HINTERNET connection;
 public:
-    NetIO_IE5 (char const *url);
-   ~NetIO_IE5 ();
+  NetIO_IE5 (char const *url, bool direct, bool cacheable);
+  ~NetIO_IE5 ();
   virtual int ok ();
   virtual int read (char *buf, int nbytes);
   void flush_io ();
diff --git a/res.rc b/res.rc
index aad74ac..8f1ab10 100644
--- a/res.rc
+++ b/res.rc
@@ -163,6 +163,8 @@ BEGIN
                     BS_AUTORADIOBUTTON | WS_TABSTOP,60,70,185,10
     CONTROL         "Use HTTP/FTP &Proxy:",IDC_NET_PROXY,"Button",
                     BS_AUTORADIOBUTTON | WS_TABSTOP,60,85,88,10
+    CONTROL         "&Direct Connection (legacy)",IDC_NET_DIRECT_LEGACY,"Button",
+                    BS_AUTORADIOBUTTON | WS_TABSTOP,60,150,94,10
     EDITTEXT        IDC_PROXY_HOST,120,105,120,12,ES_AUTOHSCROLL | 
                     WS_DISABLED | WS_GROUP
     EDITTEXT        IDC_PROXY_PORT,120,125,30,12,ES_AUTOHSCROLL | 
diff --git a/resource.h b/resource.h
index 68e8023..5bbb668 100644
--- a/resource.h
+++ b/resource.h
@@ -175,3 +175,4 @@
 #define IDC_FILE_INUSE_EDIT               590
 #define IDC_FILE_INUSE_MSG                591
 #define IDC_FILE_INUSE_HELP               592
+#define IDC_NET_DIRECT_LEGACY             593
-- 
2.12.2



More information about the Cygwin-apps mailing list