FSBrowserNG icon indicating copy to clipboard operation
FSBrowserNG copied to clipboard

List of available networks showing empty if some SSID has an unprintable character

Open JeffersonRyan opened this issue 5 years ago • 0 comments

If someone at your neighborhood misconfigured his/her router with some funky SSID, the wifiscan() javascript function at config.html is going to crash with the error:

uncaunght syntax error: unexpected token in json

and no network will be listed;

I made a workaround in FSWebServerLib.cpp. look for the handler that starts with:

on("/scan", HTTP_GET, [](AsyncWebServerRequest *request) { 

at the first for loop, add:

		String SSID="";
		for (int j = 0; j < WiFi.SSID(i).length(); ++j)
		  if (int(WiFi.SSID(i)[j]) > 31) 
		  {
			SSID += char(WiFi.SSID(i)[j]);
		  }	

and replace json += ","ssid":"" + WiFi.SSID(i) + """; with: json += ","ssid":"" + SSID + """;

my entire /scan handler:

on("/scan", HTTP_GET, [](AsyncWebServerRequest *request) {
	String json = "[";
	int n = WiFi.scanComplete();
	if (n == WIFI_SCAN_FAILED) {
		WiFi.scanNetworks(true);
	}
	else if (n) {
		for (int i = 0; i < n; ++i) {
			 
		String SSID="";
		for (int j = 0; j < WiFi.SSID(i).length(); ++j)
		  if (int(WiFi.SSID(i)[j]) > 31) 
		  {
			SSID += char(WiFi.SSID(i)[j]);
		  }	

			if (i) json += ",";
			json += "{";
			json += "\"rssi\":" + String(WiFi.RSSI(i));
			json += ",\"ssid\":\"" + SSID + "\"";
			json += ",\"bssid\":\"" + WiFi.BSSIDstr(i) + "\"";
			json += ",\"channel\":" + String(WiFi.channel(i));
			json += ",\"secure\":" + String(WiFi.encryptionType(i));
			json += ",\"hidden\":" + String(WiFi.isHidden(i) ? "true" : "false");
			json += "}";
		}
		WiFi.scanDelete();
		if (WiFi.scanComplete() == WIFI_SCAN_FAILED) {
			WiFi.scanNetworks(true);
		}
	}
	json += "]";
	request->send(200, "text/json", json);
	json = "";
});

Sorry for the formatting. My browser is not enabling the formatting functions of the editor.

Keep in mind that this workaround works by stripping any non-printable character (below ASCII 31) from the SSIDs for displaying so you are probably not going to be able to connect to any "corrected" SSID, because the SSID shown is not the full SSID configured by the AP owner.

JeffersonRyan avatar Dec 21 '19 02:12 JeffersonRyan