Skip to content

Commit 873876d

Browse files
committed
Fix peek, read and available logic in WiFiClientSecure
1 parent 0586452 commit 873876d

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+22-10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ void WiFiClientSecure::stop()
8181
close(sslclient->socket);
8282
sslclient->socket = -1;
8383
_connected = false;
84+
_peek = -1;
8485
}
8586
stop_ssl_socket(sslclient, _CA_cert, _cert, _private_key);
8687
}
@@ -105,7 +106,7 @@ int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_c
105106
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key);
106107
_lastError = ret;
107108
if (ret < 0) {
108-
log_e("lwip_connect_r: %d", errno);
109+
log_e("start_ssl_client: %d", ret);
109110
stop();
110111
return 0;
111112
}
@@ -117,7 +118,7 @@ int WiFiClientSecure::peek(){
117118
if(_peek >= 0){
118119
return _peek;
119120
}
120-
_peek = read();
121+
_peek = timedRead();
121122
return _peek;
122123
}
123124

@@ -158,21 +159,30 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size)
158159

159160
int WiFiClientSecure::read(uint8_t *buf, size_t size)
160161
{
162+
int peeked = 0;
163+
if ((!buf && size) || (_peek < 0 && !available())) {
164+
return -1;
165+
}
166+
if(!size){
167+
return 0;
168+
}
161169
if(_peek >= 0){
162-
uint8_t data = -1;
163-
data = _peek;
170+
buf[0] = _peek;
164171
_peek = -1;
165-
return data;
172+
size--;
173+
if(!size || !available()){
174+
return 1;
175+
}
176+
buf++;
177+
peeked = 1;
166178
}
167179

168-
if (!available()) {
169-
return -1;
170-
}
171180
int res = get_ssl_receive(sslclient, buf, size);
172181
if (res < 0) {
173182
stop();
183+
return res;
174184
}
175-
return res;
185+
return res + peeked;
176186
}
177187

178188
int WiFiClientSecure::available()
@@ -183,7 +193,9 @@ int WiFiClientSecure::available()
183193
int res = data_to_read(sslclient);
184194
if (res < 0 ) {
185195
stop();
186-
}
196+
} else if(_peek >= 0) {
197+
res += 1;
198+
}
187199
return res;
188200
}
189201

0 commit comments

Comments
 (0)