public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/main] Cygwin: cygcheck: split out fetching data from cygwin.com
@ 2023-01-26 18:59 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2023-01-26 18:59 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=28594480df73bbed642b4471445fa106ee387730

commit 28594480df73bbed642b4471445fa106ee387730
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Thu Jan 26 19:58:24 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Thu Jan 26 19:58:30 2023 +0100

    Cygwin: cygcheck: split out fetching data from cygwin.com
    
    In preparation of new functionality, split fetching data
    from cygwin.com out of the package_grep() function.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/utils/mingw/cygcheck.cc | 84 ++++++++++++++++++++++--------------------
 1 file changed, 45 insertions(+), 39 deletions(-)

diff --git a/winsup/utils/mingw/cygcheck.cc b/winsup/utils/mingw/cygcheck.cc
index 69f75927fca7..ce97cfe3a862 100644
--- a/winsup/utils/mingw/cygcheck.cc
+++ b/winsup/utils/mingw/cygcheck.cc
@@ -2008,7 +2008,7 @@ check_keys ()
 static const char safe_chars[] = "$-_.!*'(),";
 
 /* the URL to query.  */
-static const char base_url[] =
+static const char grep_base_url[] =
 	"http://cygwin.com/cgi-bin2/package-grep.cgi?text=1&grep=";
 
 #ifdef __x86_64__
@@ -2018,34 +2018,13 @@ static const char base_url[] =
 #endif
 static const char *ARCH_str = ARCH_STR;
 
-/* Queries Cygwin web site for packages containing files matching a regexp.
-   Return value is 1 if there was a problem, otherwise 0.  */
 static int
-package_grep (char *search)
+fetch_url (const char *url, FILE *outstream)
 {
-  char buf[1024];
-
-  /* construct the actual URL by escaping  */
-  char *url = (char *) alloca (sizeof (base_url) + strlen (ARCH_str)
-			       + strlen (search) * 3);
-  strcpy (url, base_url);
-
-  char *dest;
-  for (dest = &url[sizeof (base_url) - 1]; *search; search++)
-    {
-      if (isalnum (*search)
-	  || memchr (safe_chars, *search, sizeof (safe_chars) - 1))
-	{
-	  *dest++ = *search;
-	}
-      else
-	{
-	  *dest++ = '%';
-	  sprintf (dest, "%02x", (unsigned char) *search);
-	  dest += 2;
-	}
-    }
-  strcpy (dest, ARCH_str);
+  DWORD rc = 0, rc_s = sizeof (DWORD);
+  HINTERNET hi = NULL, hurl = NULL;
+  char buf[4096];
+  DWORD numread;
 
   /* Connect to the net and open the URL.  */
   if (InternetAttemptConnect (0) != ERROR_SUCCESS)
@@ -2055,7 +2034,6 @@ package_grep (char *search)
     }
 
   /* Initialize WinInet and attempt to fetch our URL.  */
-  HINTERNET hi = NULL, hurl = NULL;
   if (!(hi = InternetOpenA ("cygcheck", INTERNET_OPEN_TYPE_PRECONFIG,
 			    NULL, NULL, 0)))
     return display_internet_error ("InternetOpen() failed", NULL);
@@ -2065,34 +2043,62 @@ package_grep (char *search)
 				   "InternetOpenUrl() failed", hi, NULL);
 
   /* Check the HTTP response code.  */
-  DWORD rc = 0, rc_s = sizeof (DWORD);
   if (!HttpQueryInfoA (hurl, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
 		      (void *) &rc, &rc_s, NULL))
     return display_internet_error ("HttpQueryInfo() failed", hurl, hi, NULL);
 
-  if (rc != HTTP_STATUS_OK)
-    {
-      sprintf (buf, "error retrieving results from cygwin.com site, "
-		    "HTTP status code %lu", rc);
-      return display_internet_error (buf, hurl, hi, NULL);
-    }
-
-  /* Fetch result and print to stdout.  */
-  DWORD numread;
+  /* Fetch result and print to outstream.  */
   do
     {
       if (!InternetReadFile (hurl, (void *) buf, sizeof (buf), &numread))
 	return display_internet_error ("InternetReadFile failed", hurl, hi, NULL);
       if (numread)
-	fwrite ((void *) buf, (size_t) numread, 1, stdout);
+	fwrite ((void *) buf, (size_t) numread, 1, outstream);
     }
   while (numread);
 
+  if (rc != HTTP_STATUS_OK)
+    {
+      sprintf (buf, "error retrieving results from cygwin.com site, "
+		    "HTTP status code %lu", rc);
+      return display_internet_error (buf, hurl, hi, NULL);
+    }
+
   InternetCloseHandle (hurl);
   InternetCloseHandle (hi);
   return 0;
 }
 
+/* Queries Cygwin web site for packages containing files matching a regexp.
+   Return value is 1 if there was a problem, otherwise 0.  */
+static int
+package_grep (char *search)
+{
+  /* construct the actual URL by escaping  */
+  char *url = (char *) alloca (sizeof (grep_base_url) + strlen (ARCH_str)
+			       + strlen (search) * 3);
+  strcpy (url, grep_base_url);
+
+  char *dest;
+  for (dest = &url[sizeof (grep_base_url) - 1]; *search; search++)
+    {
+      if (isalnum (*search)
+	  || memchr (safe_chars, *search, sizeof (safe_chars) - 1))
+	{
+	  *dest++ = *search;
+	}
+      else
+	{
+	  *dest++ = '%';
+	  sprintf (dest, "%02x", (unsigned char) *search);
+	  dest += 2;
+	}
+    }
+  strcpy (dest, ARCH_str);
+
+  return fetch_url (url, stdout);
+}
+
 static void __attribute__ ((__noreturn__))
 usage (FILE * stream, int status)
 {

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-26 18:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-26 18:59 [newlib-cygwin/main] Cygwin: cygcheck: split out fetching data from cygwin.com Corinna Vinschen

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).