Преглед на файлове

Windows: "Start browser" button should use official API

Replace duplicate implementation of "get port" from main.c and call the
official API function implemented in civetweb.c
bel2125 преди 5 години
родител
ревизия
2c0728fac9
променени са 1 файла, в които са добавени 34 реда и са изтрити 22 реда
  1. 34 22
      src/main.c

+ 34 - 22
src/main.c

@@ -333,32 +333,45 @@ static const char *config_file_top_comment =
     "# save this file and then restart CivetWeb.\n\n";
 
 static const char *
-get_url_to_first_open_port(const char *open_ports)
+get_url_to_first_open_port(const struct mg_context *ctx)
 {
-	static char url[100];
-	int a, b, c, d, port, n;
+	static char url[128];
 
-	if (sscanf(open_ports, "%d.%d.%d.%d:%d%n", &a, &b, &c, &d, &port, &n)
-	    == 5) {
-		snprintf(url,
-		         sizeof(url),
-		         "%s://%d.%d.%d.%d:%d",
-		         open_ports[n] == 's' ? "https" : "http",
-		         a,
-		         b,
-		         c,
-		         d,
-		         port);
-	} else if (sscanf(open_ports, "%d%n", &port, &n) == 1) {
+#define MAX_PORT_COUNT (32)
+
+	struct mg_server_port ports[MAX_PORT_COUNT];
+	int portNum = mg_get_server_ports(ctx, MAX_PORT_COUNT, ports);
+	int i;
+
+	memset(url, 0, sizeof(url));
+
+	/* Prefer IPv4 http, ignore redirects */
+	for (i = 0; i < portNum; i++) {
+		if ((ports[i].protocol == 1) && (ports[i].is_redirect == 0)
+		    && (ports[i].is_ssl == 0)) {
+			snprintf(url, sizeof(url), "http://localhost:%d/", ports[i].port);
+			return url;
+		}
+	}
+	/* Use IPv4 https */
+	for (i = 0; i < portNum; i++) {
+		if ((ports[i].protocol == 1) && (ports[i].is_redirect == 0)
+		    && (ports[i].is_ssl == 1)) {
+			snprintf(url, sizeof(url), "https://localhost:%d/", ports[i].port);
+			return url;
+		}
+	}
+	/* Try IPv6 http, ignore redirects */
+	if (portNum > 0) {
 		snprintf(url,
 		         sizeof(url),
-		         "%s://localhost:%d",
-		         open_ports[n] == 's' ? "https" : "http",
-		         port);
-	} else {
-		snprintf(url, sizeof(url), "%s", "http://localhost:8080");
+		         "%s://localhost:%d/",
+		         (ports[0].is_ssl ? "https" : "http"),
+		         ports[0].port);
 	}
 
+#undef MAX_PORT_COUNT
+
 	return url;
 }
 
@@ -2810,8 +2823,7 @@ WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 		case ID_CONNECT: {
 			/* Get port from server configuration (listening_ports) and build
 			 * URL from port. */
-			const char *port_opts = mg_get_option(g_ctx, "listening_ports");
-			const char *url = get_url_to_first_open_port(port_opts);
+			const char *url = get_url_to_first_open_port(g_ctx);
 
 			/* Open URL with Windows default browser */
 			ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOW);