1
1
#include " Fetch.h"
2
2
#include < WiFiClientSecure.h>
3
3
4
- // Fetch fetch;
5
-
6
4
Response fetch (const char * url, RequestOptions options) {
7
5
// Parsing URL.
8
6
Url parsedUrl = parseUrl (url);
@@ -14,10 +12,11 @@ Response fetch(const char* url, RequestOptions options) {
14
12
// Set fingerprint if https.
15
13
if (parsedUrl.scheme == " https" ) {
16
14
#ifdef ESP8266
17
- if (options.fingerprint == " " ) {
18
- Serial.println (" [INFO] No fingerprint is provided. Using the INSECURE mode for connection!" );
15
+ if (options.fingerprint == " " && options. caCert == " " ) {
16
+ Serial.println (" [INFO] No fingerprint or caCert is provided. Using the INSECURE mode for connection!" );
19
17
client.setInsecure ();
20
18
}
19
+ else if (options.caCert != " " ) client.setTrustAnchors (new X509List (options.caCert .c_str ()));
21
20
else client.setFingerprint (options.fingerprint .c_str ());
22
21
#elif defined(ESP32)
23
22
if (options.caCert == " " ) {
@@ -31,7 +30,7 @@ Response fetch(const char* url, RequestOptions options) {
31
30
// Connecting to server.
32
31
while (!client.connect (parsedUrl.host .c_str (), parsedUrl.port )) {
33
32
delay (1000 );
34
- // Serial.print(".");
33
+ Serial.print (" ." );
35
34
}
36
35
37
36
// Forming request.
@@ -43,8 +42,8 @@ Response fetch(const char* url, RequestOptions options) {
43
42
" Connection: " + options.headers .connection + " \r\n\r\n " +
44
43
options.body + " \r\n\r\n " ;
45
44
46
- // Serial.println("Request is: ");
47
- // Serial.println(request);
45
+ Serial.println (" Request is: " );
46
+ Serial.println (request);
48
47
49
48
// Sending request.
50
49
client.print (request);
@@ -67,9 +66,9 @@ Response fetch(const char* url, RequestOptions options) {
67
66
if (line == " \r " ) break ;
68
67
}
69
68
70
- // Serial.println("-----HEADERS START-----");
71
- // Serial.println(response.headers.text);
72
- // Serial.println("-----HEADERS END-----");
69
+ Serial.println (" -----HEADERS START-----" );
70
+ Serial.println (response.headers .text );
71
+ Serial.println (" -----HEADERS END-----" );
73
72
74
73
// Getting response body.
75
74
while (client.available ()) {
@@ -108,9 +107,6 @@ String operator+(String str, Body body) {
108
107
return str + body.text ();
109
108
}
110
109
111
- // Fetch::Fetch(const char* url, RequestOptions options) :
112
- // _url(url), _options(options) {}
113
-
114
110
Response::Response (): ok(false ), status(200 ), statusText(" OK" ),
115
111
redirected(false ), type(" " ), headers(ResponseHeaders()), body(" " ) {}
116
112
0 commit comments