diff --git a/wifimgr.py b/wifimgr.py index 650e88d..b0982c9 100644 --- a/wifimgr.py +++ b/wifimgr.py @@ -1,11 +1,12 @@ import network import socket -import ure import time +import ure +import wifimgr_utils -ap_ssid = "WifiManager" -ap_password = "tayfunulu" -ap_authmode = 3 # WPA2 +AP_SSID = "WifiManager" +AP_PASSWORD = "tayfunulu" +AP_AUTHMODE = 3 # WPA2 NETWORK_PROFILES = 'wifi.dat' @@ -14,7 +15,6 @@ server_socket = None - def get_connection(): """return a working WLAN(STA_IF) instance or None""" @@ -22,12 +22,13 @@ def get_connection(): if wlan_sta.isconnected(): return wlan_sta - connected = False + # ESP connecting to WiFi takes time, wait a bit and try again: + time.sleep(3) + if wlan_sta.isconnected(): + return wlan_sta + try: - # ESP connecting to WiFi takes time, wait a bit and try again: - time.sleep(3) - if wlan_sta.isconnected(): - return wlan_sta + connected = False # Read known network profiles from file profiles = read_profiles() @@ -187,11 +188,12 @@ def handle_configure(client, request): return False # version 1.9 compatibility try: - ssid = match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!") - password = match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!") + ssid = wifimgr_utils.unquote(match.group(1).decode("utf-8")) + password = wifimgr_utils.unquote(match.group(2).decode("utf-8")) except Exception: - ssid = match.group(1).replace("%3F", "?").replace("%21", "!") - password = match.group(2).replace("%3F", "?").replace("%21", "!") + # Not both groups of ssid and password captured + send_response(client, "SSID and/or password not provided", status_code=400) + return False if len(ssid) == 0: send_response(client, "SSID must be provided", status_code=400) @@ -266,13 +268,13 @@ def start(port=80): wlan_sta.active(True) wlan_ap.active(True) - wlan_ap.config(essid=ap_ssid, password=ap_password, authmode=ap_authmode) + wlan_ap.config(essid=AP_SSID, password=AP_PASSWORD, authmode=AP_AUTHMODE) server_socket = socket.socket() server_socket.bind(addr) server_socket.listen(1) - print('Connect to WiFi ssid ' + ap_ssid + ', default password: ' + ap_password) + print('Connect to WiFi ssid ' + AP_SSID + ', default password: ' + AP_PASSWORD) print('and access the ESP via your favorite web browser at 192.168.4.1.') print('Listening on:', addr) diff --git a/wifimgr_utils.py b/wifimgr_utils.py new file mode 100644 index 0000000..795909d --- /dev/null +++ b/wifimgr_utils.py @@ -0,0 +1,43 @@ +_hexdig = '0123456789ABCDEFabcdef' +_hextobyte = None + +def unquote(string): + """ + @brief Remove URL encoding from a string, i.e. + unquote('abc%20def') -> 'abc def', see + urllib.parse.unquote_to_bytes() + @param string String that might contain URL/percent encoding + @return string with URL encoding replaced by original characters + """ + global _hextobyte + + # Note: strings are encoded as UTF-8. This is only an issue if it contains + # unescaped non-ASCII characters, which URIs should not. + if not string: + return "" + + if isinstance(string, str): + string = string.encode('utf-8') + + bits = string.split(b'%') + if len(bits) == 1: + return string.decode('utf-8') + + res = [bits[0]] + append = res.append + + # Delay the initialization of the table to not waste memory + # if the function is never called + if _hextobyte is None: + _hextobyte = {(a + b).encode(): bytes([int(a + b, 16)]) + for a in _hexdig for b in _hexdig} + + for item in bits[1:]: + try: + append(_hextobyte[item[:2]]) + append(item[2:]) + except KeyError: + append(b'%') + append(item) + + return (b''.join(res)).decode('utf-8')