@@ -70,9 +70,7 @@ def ioctl(self, kind, arg):
70
70
def close (self ):
71
71
self .sock .close ()
72
72
73
-
74
- def server_handshake (cl ):
75
- req = cl .makefile ("rwb" , 0 )
73
+ def server_handshake (req ):
76
74
# Skip HTTP GET line.
77
75
l = req .readline ()
78
76
if DEBUG :
@@ -114,28 +112,28 @@ def server_handshake(cl):
114
112
if DEBUG :
115
113
print ("respkey:" , respkey )
116
114
117
- cl . send (
115
+ req . write (
118
116
b"""\
119
117
HTTP/1.1 101 Switching Protocols\r
120
118
Upgrade: websocket\r
121
119
Connection: Upgrade\r
122
120
Sec-WebSocket-Accept: """
123
121
)
124
- cl . send (respkey )
125
- cl . send ("\r \n \r \n " )
122
+ req . write (respkey )
123
+ req . write ("\r \n \r \n " )
126
124
127
125
return True
128
126
129
127
130
128
def send_html (cl ):
131
- cl .send (
129
+ cl .write (
132
130
b"""\
133
131
HTTP/1.0 200 OK\r
134
132
\r
135
133
<base href=\" """
136
134
)
137
- cl .send (static_host )
138
- cl .send (
135
+ cl .write (static_host )
136
+ cl .write (
139
137
b"""\" ></base>\r
140
138
<script src="webreplv2_content.js"></script>\r
141
139
"""
@@ -160,11 +158,16 @@ def setup_conn(port, accept_handler):
160
158
161
159
162
160
def accept_conn (listen_sock ):
163
- global client_s
161
+ global client_s , webrepl_ssl_context
164
162
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
165
168
166
- if not server_handshake (cl ):
167
- send_html (cl )
169
+ if not server_handshake (req ):
170
+ send_html (sock )
168
171
return False
169
172
170
173
prev = os .dupterm (None )
@@ -176,13 +179,13 @@ def accept_conn(listen_sock):
176
179
print ("\n WebREPL connection from:" , remote_addr )
177
180
client_s = cl
178
181
179
- ws = websocket .websocket (cl , True )
180
- ws = WebreplWrapper (ws )
182
+ sock = websocket .websocket (sock )
183
+ sock = WebreplWrapper (sock )
181
184
cl .setblocking (False )
182
185
# notify REPL on socket incoming data (ESP32/ESP8266-only)
183
186
if hasattr (os , "dupterm_notify" ):
184
187
cl .setsockopt (socket .SOL_SOCKET , 20 , os .dupterm_notify )
185
- os .dupterm (ws )
188
+ os .dupterm (sock )
186
189
187
190
return True
188
191
@@ -196,9 +199,10 @@ def stop():
196
199
listen_s .close ()
197
200
198
201
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
201
204
stop ()
205
+ webrepl_ssl_context = ssl_context
202
206
webrepl_pass = password
203
207
if password is None :
204
208
try :
0 commit comments