Skip to content

Commit e72895b

Browse files
committed
Quiet SSL and HTTPClient debug
1 parent 7aa1913 commit e72895b

File tree

4 files changed

+70
-71
lines changed

4 files changed

+70
-71
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

+44-48
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ bool HTTPClient::begin(String url, const char* CAcert)
110110
if (!beginInternal(url, "https")) {
111111
return false;
112112
}
113+
_secure = true;
113114
_transportTraits = TransportTraitsPtr(new TLSTraits(CAcert));
114115
return true;
115116
}
@@ -121,34 +122,30 @@ bool HTTPClient::begin(String url, const char* CAcert)
121122
bool HTTPClient::begin(String url)
122123
{
123124

124-
if (beginInternal(url, "https")) {
125-
return begin(url, (const char*)NULL);
126-
}
127125
_transportTraits.reset(nullptr);
128126
_port = 80;
129127
if (!beginInternal(url, "http")) {
130-
return false;
128+
return begin(url, (const char*)NULL);
131129
}
132130
_transportTraits = TransportTraitsPtr(new TransportTraits());
133131
return true;
134132
}
135133

136134
bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
137135
{
138-
log_d("[HTTP-Client][begin] url: %s", url.c_str());
139-
bool hasPort = false;
136+
log_v("url: %s", url.c_str());
140137
clear();
141138

142139
// check for : (http: or https:
143140
int index = url.indexOf(':');
144141
if(index < 0) {
145-
log_d("[HTTP-Client][begin] failed to parse protocol");
142+
log_e("failed to parse protocol");
146143
return false;
147144
}
148145

149146
_protocol = url.substring(0, index);
150147
if (_protocol != expectedProtocol) {
151-
log_d("[HTTP-Client][begin] unexpected protocol: %s, expected %s", _protocol.c_str(), expectedProtocol);
148+
log_w("unexpected protocol: %s, expected %s", _protocol.c_str(), expectedProtocol);
152149
return false;
153150
}
154151

@@ -177,7 +174,7 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
177174
_host = host;
178175
}
179176
_uri = url;
180-
log_d("[HTTP-Client][begin] host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str());
177+
log_d("host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str());
181178
return true;
182179
}
183180

@@ -188,7 +185,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri)
188185
_port = port;
189186
_uri = uri;
190187
_transportTraits = TransportTraitsPtr(new TransportTraits());
191-
log_d("[HTTP-Client][begin] host: %s port: %d uri: %s", host.c_str(), port, uri.c_str());
188+
log_d("host: %s port: %d uri: %s", host.c_str(), port, uri.c_str());
192189
return true;
193190
}
194191

@@ -203,7 +200,6 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcer
203200
return false;
204201
}
205202
_transportTraits = TransportTraitsPtr(new TLSTraits(CAcert));
206-
//log_d("[HTTP-Client][begin] host: %s port: %d url: %s httpsFingerprint: %s", host.c_str(), port, uri.c_str(), httpsFingerprint.c_str());
207203
return true;
208204
}
209205

@@ -215,19 +211,19 @@ void HTTPClient::end(void)
215211
{
216212
if(connected()) {
217213
if(_tcp->available() > 0) {
218-
log_d("[HTTP-Client][end] still data in buffer (%d), clean up.", _tcp->available());
214+
log_d("still data in buffer (%d), clean up.", _tcp->available());
219215
while(_tcp->available() > 0) {
220216
_tcp->read();
221217
}
222218
}
223219
if(_reuse && _canReuse) {
224-
log_d("[HTTP-Client][end] tcp keep open for reuse");
220+
log_d("tcp keep open for reuse");
225221
} else {
226-
log_d("[HTTP-Client][end] tcp stop");
222+
log_d("tcp stop");
227223
_tcp->stop();
228224
}
229225
} else {
230-
log_d("[HTTP-Client][end] tcp is closed");
226+
log_v("tcp is closed");
231227
}
232228
}
233229

@@ -295,7 +291,7 @@ void HTTPClient::setAuthorization(const char * auth)
295291
void HTTPClient::setTimeout(uint16_t timeout)
296292
{
297293
_tcpTimeout = timeout;
298-
if(connected()) {
294+
if(connected() && !_secure) {
299295
_tcp->setTimeout(timeout);
300296
}
301297
}
@@ -468,11 +464,11 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
468464

469465
// are all Bytes a writen to stream ?
470466
if(bytesWrite != bytesRead) {
471-
log_d("[HTTP-Client][sendRequest] short write, asked for %d but got %d retry...", bytesRead, bytesWrite);
467+
log_d("short write, asked for %d but got %d retry...", bytesRead, bytesWrite);
472468

473469
// check for write error
474470
if(_tcp->getWriteError()) {
475-
log_d("[HTTP-Client][sendRequest] stream write error %d", _tcp->getWriteError());
471+
log_d("stream write error %d", _tcp->getWriteError());
476472

477473
//reset write error for retry
478474
_tcp->clearWriteError();
@@ -489,15 +485,15 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
489485

490486
if(bytesWrite != leftBytes) {
491487
// failed again
492-
log_d("[HTTP-Client][sendRequest] short write, asked for %d but got %d failed.", leftBytes, bytesWrite);
488+
log_d("short write, asked for %d but got %d failed.", leftBytes, bytesWrite);
493489
free(buff);
494490
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
495491
}
496492
}
497493

498494
// check for write error
499495
if(_tcp->getWriteError()) {
500-
log_d("[HTTP-Client][sendRequest] stream write error %d", _tcp->getWriteError());
496+
log_d("stream write error %d", _tcp->getWriteError());
501497
free(buff);
502498
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
503499
}
@@ -516,15 +512,15 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
516512
free(buff);
517513

518514
if(size && (int) size != bytesWritten) {
519-
log_d("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.", bytesWritten, size);
520-
log_d("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
515+
log_d("Stream payload bytesWritten %d and size %d mismatch!.", bytesWritten, size);
516+
log_d("ERROR SEND PAYLOAD FAILED!");
521517
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
522518
} else {
523-
log_d("[HTTP-Client][sendRequest] Stream payload written: %d", bytesWritten);
519+
log_d("Stream payload written: %d", bytesWritten);
524520
}
525521

526522
} else {
527-
log_d("[HTTP-Client][sendRequest] too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
523+
log_d("too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
528524
return returnError(HTTPC_ERROR_TOO_LESS_RAM);
529525
}
530526

@@ -551,7 +547,7 @@ WiFiClient& HTTPClient::getStream(void)
551547
return *_tcp;
552548
}
553549

554-
log_d("[HTTP-Client] getStream: not connected");
550+
log_d("getStream: not connected");
555551
static WiFiClient empty;
556552
return empty;
557553
}
@@ -566,7 +562,7 @@ WiFiClient* HTTPClient::getStreamPtr(void)
566562
return _tcp.get();
567563
}
568564

569-
log_d("[HTTP-Client] getStreamPtr: not connected");
565+
log_d("getStreamPtr: not connected");
570566
return nullptr;
571567
}
572568

@@ -614,7 +610,7 @@ int HTTPClient::writeToStream(Stream * stream)
614610
// read size of chunk
615611
len = (uint32_t) strtol((const char *) chunkHeader.c_str(), NULL, 16);
616612
size += len;
617-
log_d("[HTTP-Client] read chunk len: %d", len);
613+
log_d(" read chunk len: %d", len);
618614

619615
// data left?
620616
if(len > 0) {
@@ -666,7 +662,7 @@ String HTTPClient::getString(void)
666662
if(_size) {
667663
// try to reserve needed memmory
668664
if(!sstring.reserve((_size + 1))) {
669-
log_d("[HTTP-Client][getString] not enough memory to reserve a string! need: %d", (_size + 1));
665+
log_d("not enough memory to reserve a string! need: %d", (_size + 1));
670666
return "";
671667
}
672668
}
@@ -807,36 +803,36 @@ bool HTTPClient::connect(void)
807803
{
808804

809805
if(connected()) {
810-
log_d("[HTTP-Client] connect. already connected, try reuse!");
806+
log_d("already connected, try reuse!");
811807
while(_tcp->available() > 0) {
812808
_tcp->read();
813809
}
814810
return true;
815811
}
816812

817813
if (!_transportTraits) {
818-
log_d("[HTTP-Client] connect: HTTPClient::begin was not called or returned error");
814+
log_d("HTTPClient::begin was not called or returned error");
819815
return false;
820816
}
821817

822818
_tcp = _transportTraits->create();
823819

824820

825821
if (!_transportTraits->verify(*_tcp, _host.c_str())) {
826-
log_d("[HTTP-Client] transport level verify failed");
822+
log_d("transport level verify failed");
827823
_tcp->stop();
828824
return false;
829825
}
830826

831827
if(!_tcp->connect(_host.c_str(), _port)) {
832-
log_d("[HTTP-Client] failed connect to %s:%u", _host.c_str(), _port);
828+
log_d("failed connect to %s:%u", _host.c_str(), _port);
833829
return false;
834830
}
835831

836-
log_d("[HTTP-Client] connected to %s:%u", _host.c_str(), _port);
832+
log_d(" connected to %s:%u", _host.c_str(), _port);
837833

838834
// set Timeout for readBytesUntil and readStringUntil
839-
_tcp->setTimeout(_tcpTimeout);
835+
setTimeout(_tcpTimeout);
840836
/*
841837
#ifdef ESP8266
842838
_tcp->setNoDelay(true);
@@ -921,7 +917,7 @@ int HTTPClient::handleHeaderResponse()
921917

922918
lastDataTime = millis();
923919

924-
log_d("[HTTP-Client][handleHeaderResponse] RX: '%s'", headerLine.c_str());
920+
log_v("RX: '%s'", headerLine.c_str());
925921

926922
if(headerLine.startsWith("HTTP/1.")) {
927923
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
@@ -951,14 +947,14 @@ int HTTPClient::handleHeaderResponse()
951947
}
952948

953949
if(headerLine == "") {
954-
log_d("[HTTP-Client][handleHeaderResponse] code: %d", _returnCode);
950+
log_d("code: %d", _returnCode);
955951

956952
if(_size > 0) {
957-
log_d("[HTTP-Client][handleHeaderResponse] size: %d", _size);
953+
log_d("size: %d", _size);
958954
}
959955

960956
if(transferEncoding.length() > 0) {
961-
log_d("[HTTP-Client][handleHeaderResponse] Transfer-Encoding: %s", transferEncoding.c_str());
957+
log_d("Transfer-Encoding: %s", transferEncoding.c_str());
962958
if(transferEncoding.equalsIgnoreCase("chunked")) {
963959
_transferEncoding = HTTPC_TE_CHUNKED;
964960
} else {
@@ -971,7 +967,7 @@ int HTTPClient::handleHeaderResponse()
971967
if(_returnCode) {
972968
return _returnCode;
973969
} else {
974-
log_d("[HTTP-Client][handleHeaderResponse] Remote host is not an HTTP Server!");
970+
log_d("Remote host is not an HTTP Server!");
975971
return HTTPC_ERROR_NO_HTTP_SERVER;
976972
}
977973
}
@@ -1037,11 +1033,11 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
10371033

10381034
// are all Bytes a writen to stream ?
10391035
if(bytesWrite != bytesRead) {
1040-
log_d("[HTTP-Client][writeToStream] short write asked for %d but got %d retry...", bytesRead, bytesWrite);
1036+
log_d("short write asked for %d but got %d retry...", bytesRead, bytesWrite);
10411037

10421038
// check for write error
10431039
if(stream->getWriteError()) {
1044-
log_d("[HTTP-Client][writeToStreamDataBlock] stream write error %d", stream->getWriteError());
1040+
log_d("stream write error %d", stream->getWriteError());
10451041

10461042
//reset write error for retry
10471043
stream->clearWriteError();
@@ -1058,15 +1054,15 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
10581054

10591055
if(bytesWrite != leftBytes) {
10601056
// failed again
1061-
log_d("[HTTP-Client][writeToStream] short write asked for %d but got %d failed.", leftBytes, bytesWrite);
1057+
log_w("short write asked for %d but got %d failed.", leftBytes, bytesWrite);
10621058
free(buff);
10631059
return HTTPC_ERROR_STREAM_WRITE;
10641060
}
10651061
}
10661062

10671063
// check for write error
10681064
if(stream->getWriteError()) {
1069-
log_d("[HTTP-Client][writeToStreamDataBlock] stream write error %d", stream->getWriteError());
1065+
log_w("stream write error %d", stream->getWriteError());
10701066
free(buff);
10711067
return HTTPC_ERROR_STREAM_WRITE;
10721068
}
@@ -1084,15 +1080,15 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
10841080

10851081
free(buff);
10861082

1087-
log_d("[HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: %d).", bytesWritten);
1083+
log_d("connection closed or file end (written: %d).", bytesWritten);
10881084

10891085
if((size > 0) && (size != bytesWritten)) {
1090-
log_d("[HTTP-Client][writeToStreamDataBlock] bytesWritten %d and size %d mismatch!.", bytesWritten, size);
1086+
log_d("bytesWritten %d and size %d mismatch!.", bytesWritten, size);
10911087
return HTTPC_ERROR_STREAM_WRITE;
10921088
}
10931089

10941090
} else {
1095-
log_d("[HTTP-Client][writeToStreamDataBlock] too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
1091+
log_w("too less ram! need %d", HTTP_TCP_BUFFER_SIZE);
10961092
return HTTPC_ERROR_TOO_LESS_RAM;
10971093
}
10981094

@@ -1107,9 +1103,9 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
11071103
int HTTPClient::returnError(int error)
11081104
{
11091105
if(error < 0) {
1110-
log_d("[HTTP-Client][returnError] error(%d): %s", error, errorToString(error).c_str());
1106+
log_w("error(%d): %s", error, errorToString(error).c_str());
11111107
if(connected()) {
1112-
log_d("[HTTP-Client][returnError] tcp stop");
1108+
log_d("tcp stop");
11131109
_tcp->stop();
11141110
}
11151111
}

libraries/HTTPClient/src/HTTPClient.h

+1
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class HTTPClient
196196
bool _reuse = false;
197197
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
198198
bool _useHTTP10 = false;
199+
bool _secure = false;
199200

200201
String _uri;
201202
String _protocol;

libraries/WiFi/src/WiFiClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
118118
{
119119
int res = setsockopt(fd(), SOL_SOCKET, option, value, len);
120120
if(res < 0) {
121-
log_e("%d", errno);
121+
log_e("%X : %d", option, errno);
122122
}
123123
return res;
124124
}

0 commit comments

Comments
 (0)