@@ -70,9 +70,7 @@ def ioctl(self, kind, arg):
7070 def close (self ):
7171 self .sock .close ()
7272
73-
74- def server_handshake (cl ):
75- req = cl .makefile ("rwb" , 0 )
73+ def server_handshake (req ):
7674 # Skip HTTP GET line.
7775 l = req .readline ()
7876 if DEBUG :
@@ -114,28 +112,28 @@ def server_handshake(cl):
114112 if DEBUG :
115113 print ("respkey:" , respkey )
116114
117- cl . send (
115+ req . write (
118116 b"""\
119117 HTTP/1.1 101 Switching Protocols\r
120118Upgrade: websocket\r
121119Connection: Upgrade\r
122120Sec-WebSocket-Accept: """
123121 )
124- cl . send (respkey )
125- cl . send ("\r \n \r \n " )
122+ req . write (respkey )
123+ req . write ("\r \n \r \n " )
126124
127125 return True
128126
129127
130128def send_html (cl ):
131- cl .send (
129+ cl .write (
132130 b"""\
133131 HTTP/1.0 200 OK\r
134132\r
135133<base href=\" """
136134 )
137- cl .send (static_host )
138- cl .send (
135+ cl .write (static_host )
136+ cl .write (
139137 b"""\" ></base>\r
140138<script src="webreplv2_content.js"></script>\r
141139"""
@@ -160,11 +158,16 @@ def setup_conn(port, accept_handler):
160158
161159
162160def accept_conn (listen_sock ):
163- global client_s
161+ global client_s , webrepl_ssl_context
164162 cl , remote_addr = listen_sock .accept ()
163+ req = cl .makefile ("rwb" , 0 )
164+ sock = cl
165+ if webrepl_ssl_context is not None :
166+ sock = webrepl_ssl_context .wrap_socket (sock )
167+ req = sock
165168
166- if not server_handshake (cl ):
167- send_html (cl )
169+ if not server_handshake (req ):
170+ send_html (sock )
168171 return False
169172
170173 prev = os .dupterm (None )
@@ -176,13 +179,13 @@ def accept_conn(listen_sock):
176179 print ("\n WebREPL connection from:" , remote_addr )
177180 client_s = cl
178181
179- ws = websocket .websocket (cl , True )
180- ws = WebreplWrapper (ws )
182+ sock = websocket .websocket (sock )
183+ sock = WebreplWrapper (sock )
181184 cl .setblocking (False )
182185 # notify REPL on socket incoming data (ESP32/ESP8266-only)
183186 if hasattr (os , "dupterm_notify" ):
184187 cl .setsockopt (socket .SOL_SOCKET , 20 , os .dupterm_notify )
185- os .dupterm (ws )
188+ os .dupterm (sock )
186189
187190 return True
188191
@@ -196,9 +199,10 @@ def stop():
196199 listen_s .close ()
197200
198201
199- def start (port = 8266 , password = None , accept_handler = accept_conn ):
200- global static_host , webrepl_pass
202+ def start (port = 8266 , password = None , ssl_context = None , accept_handler = accept_conn ):
203+ global static_host , webrepl_pass , webrepl_ssl_context
201204 stop ()
205+ webrepl_ssl_context = ssl_context
202206 webrepl_pass = password
203207 if password is None :
204208 try :
0 commit comments