public inbox for cygwin-apps@cygwin.com
 help / color / mirror / Atom feed
From: Jon Turney <jon.turney@dronecode.org.uk>
To: cygwin-apps@cygwin.com
Cc: Jon Turney <jon.turney@dronecode.org.uk>
Subject: [PATCH setup 12/16] Spit out GetNetAuth from NetIO
Date: Fri,  8 Mar 2024 18:34:31 +0000	[thread overview]
Message-ID: <20240308183440.4263-13-jon.turney@dronecode.org.uk> (raw)
In-Reply-To: <20240308183440.4263-1-jon.turney@dronecode.org.uk>

There's still all kinds of janky stuff here: The network proxy
configuration fetched by ConnectionSetting is stored into static members
of the NetIO class, rather than held there and accessed.

Again, define a virtual class as the interface through which user
interaction takes place, and implement that for GUI and CLI clients.

While we're at it, we arrange for the GUI dialogs for network auth to be
properly parented.
---
 GetNetAuth.h         |  30 ++++++++++
 Makefile.am          |   2 +
 cli/CliGetNetAuth.cc |  45 ++++++++++++++
 cli/CliGetNetAuth.h  |  32 ++++++++++
 gui/GuiGetNetAuth.cc | 138 +++++++++++++++++++++++++++++++++++++++++++
 gui/GuiGetNetAuth.h  |  38 ++++++++++++
 net.cc               |   5 ++
 netio.cc             | 125 +--------------------------------------
 netio.h              |  19 ++----
 nio-ie5.cc           |   4 +-
 10 files changed, 298 insertions(+), 140 deletions(-)
 create mode 100644 GetNetAuth.h
 create mode 100644 cli/CliGetNetAuth.cc
 create mode 100644 cli/CliGetNetAuth.h
 create mode 100644 gui/GuiGetNetAuth.cc
 create mode 100644 gui/GuiGetNetAuth.h

diff --git a/GetNetAuth.h b/GetNetAuth.h
new file mode 100644
index 0000000..0a26e85
--- /dev/null
+++ b/GetNetAuth.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2000, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ * Written by DJ Delorie <dj@cygnus.com>
+ *
+ */
+
+#ifndef SETUP_GETNETAUTH_H
+#define SETUP_GETNETAUTH_H
+
+class GetNetAuth
+{
+public:
+  /* Helper functions for http/ftp protocols.  Both return nonzero for
+     "cancel", zero for "ok".  They set net_proxy_user, etc, in
+     state.h */
+  virtual int get_auth () = 0;
+  virtual int get_proxy_auth () = 0;
+  virtual int get_ftp_auth () = 0;
+};
+
+#endif /* SETUP_GETNETAUTH_H */
diff --git a/Makefile.am b/Makefile.am
index de066b7..f257e3a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -182,6 +182,8 @@ endif
 	gpg-packet.cc \
 	gpg-packet.h \
 	gui/GuiParseFeedback.cc \
+	gui/GuiGetNetAuth.cc \
+	gui/GuiGetNetAuth.h \
 	gui/GuiGetUrlFeedback.cc \
 	gui/GuiFeedback.h \
 	ini.cc \
diff --git a/cli/CliGetNetAuth.cc b/cli/CliGetNetAuth.cc
new file mode 100644
index 0000000..a1fde3b
--- /dev/null
+++ b/cli/CliGetNetAuth.cc
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2000, 2001, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ */
+
+/* Query user for auth information required */
+
+#include "netio.h"
+#include "CliGetNetAuth.h"
+
+#include "LogFile.h"
+
+static int
+auth_common(const char *mode)
+{
+  Log (LOG_PLAIN) << mode << " not implemented" << endLog;
+  Logger ().exit (1);
+  return 1;
+}
+
+int
+CliGetNetAuth::get_auth ()
+{
+  return auth_common("get_auth");
+}
+
+int
+CliGetNetAuth::get_proxy_auth ()
+{
+  return auth_common("get_proxy_auth");
+}
+
+int
+CliGetNetAuth::get_ftp_auth ()
+{
+  return auth_common("get_ftp_auth");
+}
diff --git a/cli/CliGetNetAuth.h b/cli/CliGetNetAuth.h
new file mode 100644
index 0000000..7ff4520
--- /dev/null
+++ b/cli/CliGetNetAuth.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2000, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ * Written by DJ Delorie <dj@cygnus.com>
+ *
+ */
+
+#ifndef SETUP_CLI_GETNETAUTH_H
+#define SETUP_CLI_GETNETAUTH_H
+
+#include "GetNetAuth.h"
+
+class CliGetNetAuth : public GetNetAuth
+{
+public:
+  /* Helper functions for http/ftp protocols.  Both return nonzero for
+     "cancel", zero for "ok".  They set net_proxy_user, etc, in
+     state.h */
+  int get_auth ();
+  int get_proxy_auth ();
+  int get_ftp_auth ();
+};
+
+#endif /* SETUP_CLI_GETNETAUTH_H */
diff --git a/gui/GuiGetNetAuth.cc b/gui/GuiGetNetAuth.cc
new file mode 100644
index 0000000..a6d4917
--- /dev/null
+++ b/gui/GuiGetNetAuth.cc
@@ -0,0 +1,138 @@
+/*
+ * Copyright (c) 2000, 2001, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ */
+
+/* Query user for auth information required */
+
+#include "netio.h"
+#include "GuiGetNetAuth.h"
+
+#include "LogFile.h"
+
+#include "resource.h"
+#include "dialog.h"
+
+static char **user, **passwd;
+static int loading = 0;
+
+static void
+check_if_enable_ok (HWND h)
+{
+  int e = 0;
+  if (*user)
+    e = 1;
+  EnableWindow (GetDlgItem (h, IDOK), e);
+}
+
+static void
+load_dialog (HWND h)
+{
+  loading = 1;
+  eset (h, IDC_NET_USER, *user);
+  eset (h, IDC_NET_PASSWD, *passwd);
+  check_if_enable_ok (h);
+  loading = 0;
+}
+
+static void
+save_dialog (HWND h)
+{
+  *user = eget (h, IDC_NET_USER, *user);
+  *passwd = eget (h, IDC_NET_PASSWD, *passwd);
+  if (! *passwd) {
+    *passwd = new char[1];
+    (*passwd)[0] = '\0';
+  }
+}
+
+static BOOL
+auth_cmd (HWND h, int id, HWND hwndctl, UINT code)
+{
+  switch (id)
+    {
+
+    case IDC_NET_USER:
+    case IDC_NET_PASSWD:
+      if (code == EN_CHANGE && !loading)
+	{
+	  save_dialog (h);
+	  check_if_enable_ok (h);
+	}
+      break;
+
+    case IDOK:
+      save_dialog (h);
+      EndDialog (h, 0);
+      break;
+
+    case IDCANCEL:
+      EndDialog (h, 1);
+      Logger ().exit (1);
+      break;
+    }
+  return 0;
+}
+
+static INT_PTR CALLBACK
+auth_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
+{
+  switch (message)
+    {
+    case WM_INITDIALOG:
+      load_dialog (h);
+      return FALSE;
+    case WM_COMMAND:
+      auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
+      return 0;
+    }
+  return FALSE;
+}
+
+static int
+auth_common (int id, HWND owner)
+{
+  return DialogBox (NULL, MAKEINTRESOURCE (id), owner, auth_proc);
+}
+
+int
+GuiGetNetAuth::get_auth ()
+{
+  user = &NetIO::net_user;
+  passwd = &NetIO::net_passwd;
+  return auth_common (IDD_NET_AUTH, owner);
+}
+
+int
+GuiGetNetAuth::get_proxy_auth ()
+{
+  user = &NetIO::net_proxy_user;
+  passwd = &NetIO::net_proxy_passwd;
+  return auth_common (IDD_PROXY_AUTH, owner);
+}
+
+int
+GuiGetNetAuth::get_ftp_auth ()
+{
+  if (NetIO::net_ftp_user)
+    {
+      delete[] NetIO::net_ftp_user;
+      NetIO::net_ftp_user = NULL;
+    }
+  if (NetIO::net_ftp_passwd)
+    {
+      delete[] NetIO::net_ftp_passwd;
+      NetIO::net_ftp_passwd = NULL;
+    }
+  user = &NetIO::net_ftp_user;
+  passwd = &NetIO::net_ftp_passwd;
+  return auth_common (IDD_FTP_AUTH, owner);
+}
diff --git a/gui/GuiGetNetAuth.h b/gui/GuiGetNetAuth.h
new file mode 100644
index 0000000..21758f2
--- /dev/null
+++ b/gui/GuiGetNetAuth.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2000, Red Hat, Inc.
+ *
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ *
+ *     A copy of the GNU General Public License can be found at
+ *     http://www.gnu.org/
+ *
+ * Written by DJ Delorie <dj@cygnus.com>
+ *
+ */
+
+#ifndef SETUP_GUI_GETNETAUTH_H
+#define SETUP_GUI_GETNETAUTH_H
+
+#include "win32.h"
+#include "GetNetAuth.h"
+
+class GuiGetNetAuth : public GetNetAuth
+{
+public:
+  GuiGetNetAuth (HWND owner_) : owner(owner_) { };
+
+  /* Helper functions for http/ftp protocols.  Both return nonzero for
+     "cancel", zero for "ok".  They set net_proxy_user, etc, in
+     state.h */
+  int get_auth ();
+  int get_proxy_auth ();
+  int get_ftp_auth ();
+
+private:
+  HWND owner;
+};
+
+#endif /* SETUP_GUI_GETNETAUTH_H */
diff --git a/net.cc b/net.cc
index f460efc..ee84e64 100644
--- a/net.cc
+++ b/net.cc
@@ -33,6 +33,8 @@
 #include "propsheet.h"
 #include "threebar.h"
 #include "ConnectionSetting.h"
+#include "gui/GuiGetNetAuth.h"
+
 extern ThreeBarProgressPage Progress;
 
 static StringOption ProxyOption ("", 'p', "proxy", IDS_HELPTEXT_PROXY, false);
@@ -113,6 +115,9 @@ NetPage::OnInit ()
   if (!NetIO::net_method)
     NetIO::net_method = IDC_NET_PRECONFIG;
 
+  // install the interactive network auth getter
+  NetIO::auth_getter = new GuiGetNetAuth(GetHWND());
+
   if (proxyString.size ())
   {
     unsigned int pos = proxyString.find_last_of (':');
diff --git a/netio.cc b/netio.cc
index d6bfc24..7d32f9f 100644
--- a/netio.cc
+++ b/netio.cc
@@ -21,17 +21,10 @@
 
 #include "LogFile.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include <shlwapi.h>
 
 #include "resource.h"
-#include "state.h"
-#include "msg.h"
 #include "nio-ie5.h"
-#include "dialog.h"
 
 int NetIO::net_method;
 char *NetIO::net_proxy_host;
@@ -43,6 +36,7 @@ char *NetIO::net_proxy_user;
 char *NetIO::net_proxy_passwd;
 char *NetIO::net_ftp_user;
 char *NetIO::net_ftp_passwd;
+GetNetAuth *NetIO::auth_getter;
 
 int
 NetIO::ok ()
@@ -107,123 +101,6 @@ NetIO::open (char const *url, bool cachable)
   return rv;
 }
 
-
-static char **user, **passwd;
-static int loading = 0;
-
-static void
-check_if_enable_ok (HWND h)
-{
-  int e = 0;
-  if (*user)
-    e = 1;
-  EnableWindow (GetDlgItem (h, IDOK), e);
-}
-
-static void
-load_dialog (HWND h)
-{
-  loading = 1;
-  eset (h, IDC_NET_USER, *user);
-  eset (h, IDC_NET_PASSWD, *passwd);
-  check_if_enable_ok (h);
-  loading = 0;
-}
-
-static void
-save_dialog (HWND h)
-{
-  *user = eget (h, IDC_NET_USER, *user);
-  *passwd = eget (h, IDC_NET_PASSWD, *passwd);
-  if (! *passwd) {
-    *passwd = new char[1];
-    (*passwd)[0] = '\0';
-  }
-}
-
-static BOOL
-auth_cmd (HWND h, int id, HWND hwndctl, UINT code)
-{
-  switch (id)
-    {
-
-    case IDC_NET_USER:
-    case IDC_NET_PASSWD:
-      if (code == EN_CHANGE && !loading)
-	{
-	  save_dialog (h);
-	  check_if_enable_ok (h);
-	}
-      break;
-
-    case IDOK:
-      save_dialog (h);
-      EndDialog (h, 0);
-      break;
-
-    case IDCANCEL:
-      EndDialog (h, 1);
-      Logger ().exit (1);
-      break;
-    }
-  return 0;
-}
-
-static INT_PTR CALLBACK
-auth_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
-{
-  switch (message)
-    {
-    case WM_INITDIALOG:
-      load_dialog (h);
-      return FALSE;
-    case WM_COMMAND:
-      auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
-      return 0;
-    }
-  return FALSE;
-}
-
-static int
-auth_common (int id, HWND owner)
-{
-  return DialogBox (NULL, MAKEINTRESOURCE (id), owner, auth_proc);
-}
-
-int
-NetIO::get_auth (HWND owner)
-{
-  user = &net_user;
-  passwd = &net_passwd;
-  return auth_common (IDD_NET_AUTH, owner);
-}
-
-int
-NetIO::get_proxy_auth (HWND owner)
-{
-  user = &net_proxy_user;
-  passwd = &net_proxy_passwd;
-  return auth_common (IDD_PROXY_AUTH, owner);
-}
-
-int
-NetIO::get_ftp_auth (HWND owner)
-{
-  if (net_ftp_user)
-    {
-      delete[] net_ftp_user;
-      net_ftp_user = NULL;
-    }
-  if (net_ftp_passwd)
-    {
-      delete[] net_ftp_passwd;
-      net_ftp_passwd = NULL;
-    }
-  user = &net_ftp_user;
-  passwd = &net_ftp_passwd;
-  return auth_common (IDD_FTP_AUTH, owner);
-}
-
 const char *
 NetIO::net_method_name ()
 {
diff --git a/netio.h b/netio.h
index f2ecfdd..98d435b 100644
--- a/netio.h
+++ b/netio.h
@@ -16,23 +16,22 @@
 #ifndef SETUP_NETIO_H
 #define SETUP_NETIO_H
 
-#include "win32.h"
+#include "GetNetAuth.h"
 
 /* This is the parent class for all the access methods known to setup
    (i.e. ways to download files from the internet or other sources */
 
 class NetIO
 {
-protected:
+public:
   static char *net_user;
   static char *net_passwd;
   static char *net_proxy_user;
   static char *net_proxy_passwd;
   static char *net_ftp_user;
   static char *net_ftp_passwd;
+  static GetNetAuth *auth_getter;
 
-
-public:
   /* if nonzero, this is the estimated total file size */
   int file_size;
 
@@ -40,9 +39,8 @@ public:
   virtual ~ NetIO () {};
 
   /* The user calls this function to create a suitable accessor for
-     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() */
+     the given URL. If anything fails, either the return values is NULL
+     or the returned object is !ok() */
   static NetIO *open (char const *url, bool cachable);
 
   /* If !ok() that means the transfer isn't happening. */
@@ -57,13 +55,6 @@ public:
   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 */
-  int get_auth (HWND owner);
-  int get_proxy_auth (HWND owner);
-  int get_ftp_auth (HWND owner);
 };
 
 #endif /* SETUP_NETIO_H */
diff --git a/nio-ie5.cc b/nio-ie5.cc
index 2117e33..2c7e85a 100644
--- a/nio-ie5.cc
+++ b/nio-ie5.cc
@@ -274,14 +274,14 @@ try_again:
 	  if (type == 401)	/* authorization required */
 	    {
 	      flush_io ();
-	      get_auth (NULL);
+	      auth_getter->get_auth ();
 	      resend = 1;
 	      goto try_again;
 	    }
 	  else if (type == 407)	/* proxy authorization required */
 	    {
 	      flush_io ();
-	      get_proxy_auth (NULL);
+	      auth_getter->get_proxy_auth ();
 	      resend = 1;
 	      goto try_again;
 	    }
-- 
2.43.0


  parent reply	other threads:[~2024-03-08 18:36 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-08 18:34 [PATCH setup 00/16] Groundwork for a GUI-less installation tool Jon Turney
2024-03-08 18:34 ` [PATCH setup 01/16] Drop forward declaration of non-existent class IniState Jon Turney
2024-03-08 18:34 ` [PATCH setup 02/16] Move setup_exts[] to the only place it's used Jon Turney
2024-03-08 18:34 ` [PATCH setup 03/16] Split GuiParseFeedback out from ini fetcher Jon Turney
2024-03-08 18:34 ` [PATCH setup 04/16] Split out site into SiteSettings and SitePage Jon Turney
2024-03-08 18:34 ` [PATCH setup 05/16] Don't call Antivirus::AtExit() directly from Logger::exit() Jon Turney
2024-03-08 18:34 ` [PATCH setup 06/16] Simplify invocation of UserSettings::open_settings() Jon Turney
2024-03-08 18:34 ` [PATCH setup 07/16] Split out URL fetching progress reporting Jon Turney
2024-03-08 18:34 ` [PATCH setup 08/16] Instantiate found_ini_list in ini.cc Jon Turney
2024-03-08 18:34 ` [PATCH setup 09/16] Move is_64bit to state Jon Turney
2024-03-08 18:34 ` [PATCH setup 10/16] Move setup.ini pathame components to ini.cc Jon Turney
2024-03-08 18:34 ` [PATCH setup 11/16] Drop hinstance global Jon Turney
2024-03-08 18:34 ` Jon Turney [this message]
2024-03-08 18:34 ` [PATCH setup 13/16] Split out hash checking progress reporting Jon Turney
2024-03-08 18:34 ` [PATCH setup 14/16] Push check_for_cached into package_source Jon Turney
2024-03-08 18:34 ` [PATCH setup 15/16] Put various shared subcomponents into a convenience library Jon Turney
2024-03-08 18:34 ` [PATCH setup 16/16] Add beginnings of a command line installation tool Jon Turney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240308183440.4263-13-jon.turney@dronecode.org.uk \
    --to=jon.turney@dronecode.org.uk \
    --cc=cygwin-apps@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).