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