28
28
#include " emonesp.h"
29
29
#include " http.h"
30
30
31
- WiFiClientSecure client; // Create class for HTTPS TCP connections get_https()
32
- HTTPClient http; // Create class for HTTP TCP connections get_http()
31
+ #define HTTP_TIMEOUT 4
32
+
33
+ WiFiClient client; // Create class for HTTP TCP connections get_http()
34
+ WiFiClientSecure client_ssl; // Create class for HTTPS TCP connections get_https()
35
+
36
+ static char request[MAX_DATA_LEN+100 ];
33
37
34
38
// -------------------------------------------------------------------
35
- // HTTPS SECURE GET Request
39
+ // HTTP or HTTPS GET Request
36
40
// url: N/A
37
41
// -------------------------------------------------------------------
38
42
39
- String
40
- get_https (const char *fingerprint, const char *host, String url,
41
- int httpsPort) {
43
+ String get_http (const char * host, const char * url, int port, const char * fingerprint) {
44
+ WiFiClientSecure * http;
45
+
46
+ if (fingerprint) {
47
+ http = &client_ssl;
48
+ } else {
49
+ http = (WiFiClientSecure *) &client;
50
+ }
51
+
42
52
// Use WiFiClient class to create TCP connections
43
- if (!client. connect (host, httpsPort )) {
44
- DBUGS.print ( host + httpsPort ); // debug
53
+ if (!http-> connect (host, port, HTTP_TIMEOUT* 1000 )) {
54
+ DBUGS.printf ( " %s:%d \n " , host, port ); // debug
45
55
return (" Connection error" );
46
56
}
57
+ http->setTimeout (HTTP_TIMEOUT);
47
58
#ifndef ESP32
48
59
#warning HTTPS verification not enabled
49
- if (client. verify (fingerprint, host)) {
60
+ if (!fingerprint || http-> verify (fingerprint, host)) {
50
61
#endif
51
- client. print ( String ( " GET " ) + url + " HTTP/1.1\r\n " + " Host: " + host +
52
- " \r\n " + " Connection: close \r\n\r\n " );
62
+ sprintf (request, " GET %s HTTP/1.1\r\n Host: %s \r\n Connection: close \r\n\r\n " , url, host);
63
+ http-> print (request );
53
64
// Handle wait for reply and timeout
54
65
unsigned long timeout = millis ();
55
- while (client. available () == 0 ) {
56
- if (millis () - timeout > 5000 ) {
57
- client. stop ();
66
+ while (http-> available () == 0 ) {
67
+ if (millis () - timeout > (HTTP_TIMEOUT* 1000 ) ) {
68
+ http-> stop ();
58
69
return (" Client Timeout" );
59
70
}
71
+ #ifdef ENABLE_WDT
72
+ feedLoopWDT ();
73
+ #endif
60
74
}
61
75
// Handle message receive
62
- while (client. available ()) {
63
- String line = client. readStringUntil (' \r ' );
76
+ while (http-> available ()) {
77
+ String line = http-> readStringUntil (' \r ' );
64
78
DBUGS.println (line); // debug
65
79
if (line.startsWith (" HTTP/1.1 200 OK" )) {
66
80
return (" ok" );
@@ -73,22 +87,3 @@ get_https(const char *fingerprint, const char *host, String url,
73
87
#endif
74
88
return (" error " + String (host));
75
89
}
76
-
77
- // -------------------------------------------------------------------
78
- // HTTP GET Request
79
- // url: N/A
80
- // -------------------------------------------------------------------
81
- String
82
- get_http (const char *host, String url) {
83
- http.begin (String (" http://" ) + host + String (url));
84
- int httpCode = http.GET ();
85
- if ((httpCode > 0 ) && (httpCode == HTTP_CODE_OK)) {
86
- String payload = http.getString ();
87
- DBUGS.println (payload);
88
- http.end ();
89
- return (payload);
90
- } else {
91
- http.end ();
92
- return (" server error: " + String (httpCode));
93
- }
94
- } // end http_get
0 commit comments