jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
662 stars 97 forks source link

Fix a bug in XAsyncSockets.py #98

Closed ekondayan closed 11 months ago

ekondayan commented 1 year ago

The bug is in function IsSSL where the _socket object is tested to be an instance of SSLSocket. The correct is to test if the object is of class SSLContext.

https://github.com/jczic/MicroWebSrv2/issues/95

The SSLSocket attribute is not available in micropython for port esp32 and when initiating a simple GET crashes the server.

Unhandled exception in thread started by Traceback (most recent call last): File "MicroWebSrv2/libs/XAsyncSockets.py", line 131, in _processWaitEvents File "MicroWebSrv2/libs/XAsyncSockets.py", line 587, in OnReadyForReading File "MicroWebSrv2/libs/XAsyncSockets.py", line 830, in IsSSL AttributeError: 'module' object has no attribute 'SSLSocket'

DracoTomes commented 9 months ago

This is still not entirely fixed, I ran into issues on my ESP32 because SSLSocket is still used a few lines below it. https://github.com/jczic/MicroWebSrv2/blob/7646c7535a4d1236ac98210a04cbb6ffba1c31bf/MicroWebSrv2/libs/XAsyncSockets.py#L835-L838 Replacing that will SSLContext as well (as actually checked in the function) works for me.

jczic commented 9 months ago

Ok @DracoTomes, that means that in MicroPython you have "SSLContext" as a socket attribute, otherwise the IF wouldn't go any further. However, self._socket is in no way part of the SSLContext class. I will therefore also test "SSLSocket" as an attribute.

I know that the latest version of MicroPython implements SSL (TLS) with context and asynchronously, because this didn't exist before. But I haven't checked yet.

So I guess you don't use SSL in MicroPython at all?

jczic commented 9 months ago

https://github.com/jczic/MicroWebSrv2/commit/b6f0799d07b36c9ec1b7273908642c77680f2c1f

The line "hasattr(ssl, 'SSLContext')" indicates that the system manages SSL "contexts". I know this worked on MicroPython but there must have been a change in some versions.

DracoTomes commented 9 months ago

I'm running the newest 1.22.1 so it's probably something to do with the changes in newer version. I have not used SSL yet (except for outgoing requests but I don't think that should matter).

jczic commented 9 months ago

@DracoTomes : But it should work now, shouldn't it?

DracoTomes commented 9 months ago

Oh yeah my bad. After implementing that change everything worked.

jczic commented 9 months ago

👍🏻