Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for long and unconventional SSIDs and passwords #15

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

TilCreator
Copy link
Contributor

This makes sure that all data is received and correctly decoded.

@TilCreator TilCreator changed the title Add support for long and strange SSIDs and passwords Add support for long and unconventional SSIDs and passwords May 29, 2018
wifimgr.py Outdated
s = r[i]
try:
r[i] = chr(int(s[:2], 16)) + s[2:]
except Exception:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not add new code with except: or except Exception:, rather catch the more specific exceptions.

wifimgr.py Outdated
@@ -15,6 +15,17 @@
server_socket = None


def _unquote(s):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, isn't there something in urllib (or elsewhere) for that already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In cPython it's possible to do that with urllib, but Micropython doesn't have urllib and apparently nothing similar.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, in that case one could consider copying quote/unquote from the cpython stdlib - just to avoid issues in new code and maybe be able to use it from some lib later in case micropython adds that lib or there is some external lib on pypi (use same interface).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just tried that and I don't think it would be a good idea, because the function depends on about 70 lines of code, it needs more resources and had to be modified to run on Micropython. What I think would be better to just rename the function to the urllib counterpart ( unquote_plus docs ).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, if you can support the same interface and semantics, using the same name makes sense.

wifimgr.py Outdated
password = match.group(2).replace("%3F", "?").replace("%21", "!")

ssid = _unquote(match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!"))
password = _unquote(match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you keep the replace for the %XX stuff here, isn't that something your new _unquote call should do?

wifimgr.py Outdated
password = _unquote(match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!"))
except:
ssid = _unquote(match.group(1).replace("%3F", "?").replace("%21", "!"))
password = _unquote(match.group(2).replace("%3F", "?").replace("%21", "!"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. also a new diaper pattern (using a bare except:).

wifimgr.py Outdated
@@ -281,18 +291,31 @@ def start(port=80):
print('client connected from', addr)
try:
client.settimeout(5.0)

request = b""
request = bytearray(b"")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just bytearray() also creates an empty one.

wifimgr.py Outdated

while content_length_remaining > 0:
chunk = client.recv(512)
request.extend(chunk)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you already have the starting of the content in content, wouldn't you rather like to append the rest of the content to that instead of to request?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or at least remember that position of \r\n\r\n (end of headers) for later, so the body can be extracted more easily.

@TilCreator
Copy link
Contributor Author

TilCreator commented May 30, 2018

Just had a look at the issues: This pulls should fix #8 and #9 and the unquote_plus ( and a quote_plus ) function could be a solution for #6.

wifimgr.py Outdated

ssid = _unquote(match.group(1).decode("utf-8"))
password = _unquote(match.group(2).decode("utf-8"))
except:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you didn't fix this one yet.

except: is the worst (google for "diaper pattern") and every linter and also human python coders might puke on it. :-)

except Exception: is a little better, at least some linters will shut up. But still, it is not as specific as usually desired and might catch some unintended exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't fixed it jet, because it's from the old / original code, but I will fix it. Let me just check which exception gets triggered.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code (as seen in the diff) had except Exception:, so by changing it to except: you made it slightly worse.

Thanks for checking what exceptions actually should get catched there, this is what one really wants to do. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups, I changed it in #14 and then falsely merged it, my bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants