From 5e2198310a3ea15e8ffc7cae8606877d7c4f5713 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Fri, 8 Mar 2019 00:12:58 +0100 Subject: [PATCH 01/35] Using legacy ssl api before I start work on migrating --- RestClient.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/RestClient.h b/RestClient.h index 176a13b..76cc701 100644 --- a/RestClient.h +++ b/RestClient.h @@ -2,8 +2,13 @@ #define RestClient_h #include + +#define USING_AXTLS #include -#include +//#include +#include "WiFiClientSecureAxTLS.h" +using namespace axTLS; + class RestClient { From af92d7e9509f30b2e9f78d6b581af5a719faf68d Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 16:05:29 +0100 Subject: [PATCH 02/35] Create library.json --- library.json | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 library.json diff --git a/library.json b/library.json new file mode 100644 index 0000000..66496ad --- /dev/null +++ b/library.json @@ -0,0 +1,11 @@ +{ + "name":"esp8266 restclient", + "version":"1.0.0", + "keywords":"rest", + "description":"A restclient for the esp8266 that supports SSL", + "repository": + { + "type":"git", + "url":"https://github.com/DaKaZ/esp8266-restclient.git" + } +} From 22cd1443adb9f3a30ca4b29fdf52ff004a1dec1f Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 16:25:24 +0100 Subject: [PATCH 03/35] Changed name --- library.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.json b/library.json index 66496ad..906f6ed 100644 --- a/library.json +++ b/library.json @@ -1,5 +1,5 @@ { - "name":"esp8266 restclient", + "name":"esp8266 restclient ssl", "version":"1.0.0", "keywords":"rest", "description":"A restclient for the esp8266 that supports SSL", From b28260aab281bed20e57404b70cf1e4fdeb6896b Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 16:26:13 +0100 Subject: [PATCH 04/35] started work on moving to bearssl API --- library.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.json b/library.json index 906f6ed..d52d6d3 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name":"esp8266 restclient ssl", - "version":"1.0.0", + "version":"2.0.0", "keywords":"rest", "description":"A restclient for the esp8266 that supports SSL", "repository": From 0627605f0d97e723ec06a41812291e8177120ada Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 18:38:15 +0100 Subject: [PATCH 05/35] Started work on moving to bearssl --- RestClient.cpp | 18 +++++++++--------- RestClient.h | 7 +------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index a289639..827b4ba 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -158,19 +158,19 @@ int RestClient::request(const char* method, const char* path, HTTP_DEBUG_PRINT("HTTP: connect\n"); if (ssl) { + if (fingerprint) + { + sslClient.setFingerprint(fingerprint); + } + else + { + sslClient.setInsecure(); + } + if(!sslClient.connect(host, port)){ HTTP_DEBUG_PRINT("HTTPS Connection failed\n"); return 0; } - if (fingerprint) { - HTTP_DEBUG_PRINT("Verifiying SSL certificate\n"); - if (sslClient.verify(fingerprint, host)) { - HTTP_DEBUG_PRINT("SSL certificate matches\n"); - } else { - HTTP_DEBUG_PRINT("SSL certificate does not match\n"); - return 0; - } - } } else { if(!client.connect(host, port)){ HTTP_DEBUG_PRINT("HTTP Connection failed\n"); diff --git a/RestClient.h b/RestClient.h index 76cc701..dc16d0c 100644 --- a/RestClient.h +++ b/RestClient.h @@ -3,12 +3,7 @@ #include -#define USING_AXTLS -#include -//#include -#include "WiFiClientSecureAxTLS.h" -using namespace axTLS; - +#include class RestClient { From 5167b952c5bf797312cadc8ea6e9ac8a4d0d7c03 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 18:40:12 +0100 Subject: [PATCH 06/35] changed name --- library.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.json b/library.json index d52d6d3..91c36f8 100644 --- a/library.json +++ b/library.json @@ -1,5 +1,5 @@ { - "name":"esp8266 restclient ssl", + "name":"ESP8266_REST_client_SSL", "version":"2.0.0", "keywords":"rest", "description":"A restclient for the esp8266 that supports SSL", From 52a30f591753e2151f675b31105ab6a56628de34 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 18:40:28 +0100 Subject: [PATCH 07/35] changed name --- library.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.json b/library.json index 906f6ed..c3c2be3 100644 --- a/library.json +++ b/library.json @@ -1,5 +1,5 @@ { - "name":"esp8266 restclient ssl", + "name":"ESP8266_REST_client_SSL", "version":"1.0.0", "keywords":"rest", "description":"A restclient for the esp8266 that supports SSL", From bb8f270b39b41263e66e4a441560d1e210faf723 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 18:41:46 +0100 Subject: [PATCH 08/35] Fixed git url --- library.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.json b/library.json index c3c2be3..283e12b 100644 --- a/library.json +++ b/library.json @@ -6,6 +6,6 @@ "repository": { "type":"git", - "url":"https://github.com/DaKaZ/esp8266-restclient.git" + "url":"https://github.com/Hal9k-dk/esp8266-restclient.git" } } From 515a74216fdbfa9c176faa5f192b0837161ffeaf Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Sun, 10 Mar 2019 18:42:33 +0100 Subject: [PATCH 09/35] Fixed git url --- library.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.json b/library.json index 91c36f8..b0b7c2b 100644 --- a/library.json +++ b/library.json @@ -6,6 +6,6 @@ "repository": { "type":"git", - "url":"https://github.com/DaKaZ/esp8266-restclient.git" + "url":"https://github.com/Hal9k-dk/esp8266-restclient.git" } -} +} \ No newline at end of file From 16408139f55ef15eee4e0ffcbb9716baee840a34 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 00:30:58 +0100 Subject: [PATCH 10/35] Removed line --- .gitignore | 1 + RestClient.h | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/RestClient.h b/RestClient.h index 76cc701..ae3aea8 100644 --- a/RestClient.h +++ b/RestClient.h @@ -5,8 +5,7 @@ #define USING_AXTLS #include -//#include -#include "WiFiClientSecureAxTLS.h" +#include using namespace axTLS; From d789a866ea77b64885dc15341caca4c00ba8bf14 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 00:50:33 +0100 Subject: [PATCH 11/35] Used library.properties instead, to be arduino compatible --- README.md | 5 ++++- library.json | 11 ----------- library.properties | 9 +++++++++ 3 files changed, 13 insertions(+), 12 deletions(-) delete mode 100644 library.json create mode 100644 library.properties diff --git a/README.md b/README.md index d280edd..9528b7a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,10 @@ HTTP Request library for Arduino and the ESP8266 WiFi SOC modules -This library now supports SSL! To use with SSL, you need to include the SHA1 fingerprint of the certificate of the site you are connecting to. You can get this by using a desktop browser and inspecting the SSL cert used at the site. Please note: this is FRAGILE, if the site updates their SSL, your code will break. But, there is not enough memory on the ESP8266 to store all the rool certs, so this is a working method. Se the example below. +This library supports SSL! +To use with SSL either include the SHA1 fingerprint of the certificate of the site you are connecting to, or use force the library to use ssl insecurely (don't veryify the server is who it says it is). + +You can get the SHA1 fingerprint by using a desktop browser and inspecting the SSL cert used at the site. Please note: this is FRAGILE, if the site updates their SSL, your code will break. But, there is not enough memory on the ESP8266 to store all the rool certs, so this is a working method. Se the example below. This library is derived almost entirely from the great work done here: https://github.com/csquared/arduino-restclient diff --git a/library.json b/library.json deleted file mode 100644 index b0b7c2b..0000000 --- a/library.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name":"ESP8266_REST_client_SSL", - "version":"2.0.0", - "keywords":"rest", - "description":"A restclient for the esp8266 that supports SSL", - "repository": - { - "type":"git", - "url":"https://github.com/Hal9k-dk/esp8266-restclient.git" - } -} \ No newline at end of file diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..ea2ccf1 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=ESP8266_REST_client_SSL +version=2.0.0 +author=Hal9k,DaKaz,csquared,ricardochimal,theycallmeswift +maintainer=x +sentence=A restclient for the esp8266 that supports SSL +paragraph=A restclient for the esp8266 that supports SSL +category=Communication +url=https://github.com/Hal9k-dk/esp8266-restclient +architectures=esp8266 \ No newline at end of file From 9a7ad3789cda8ea2aa56f98740fa05264fb1324e Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 01:32:33 +0100 Subject: [PATCH 12/35] Updated readme --- README.md | 68 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 9528b7a..d61f11f 100644 --- a/README.md +++ b/README.md @@ -29,24 +29,24 @@ You need to have the `ESP8266` board support already included. Constructor to create an RestClient object to make requests against. -Use domain name and default to port 80: +Use a domain name and default to port 80: ```c++ RestClient client = RestClient("arduino-http-lib-test.herokuapp.com"); ``` Use a local IP and an explicit port: ```c++ -RestClient client = RestClient("192.168.1.50",5000); +RestClient client = RestClient("192.168.1.50", 5000); ``` -Use a local IP, an explicit port to an SSL site and (must include the 1 to turn on SSL): +Use a domain name, an explicit port to an SSL site and verify the certificate with its fingerprint: ```c++ -RestClient client = RestClient("www.kudoso.com",443, 1); +RestClient client = RestClient("www.kudoso.com", 443, "EE 16 77 79 55 58 92 46 FB 18 40 99 2E 17 7E AB 32 0A 4A 88"); ``` -Use a local IP, an explicit port to an SSL site and verify the certificate with its fingerprint: +Use a domain name, an explicit port to an SSL site and force insecure SSL with no ssl verification: ```c++ -RestClient client = RestClient("www.kudoso.com",443, "EE 16 77 79 55 58 92 46 FB 18 40 99 2E 17 7E AB 32 0A 4A 88"); +RestClient client = RestClient("www.kudoso.com", 443, 1); ``` ### dhcp() @@ -63,48 +63,56 @@ All methods return an HTTP status code or 0 if there was an error. Start making requests! +## GET +Getting the response is optional: ```c++ -int statusCode = client.get("/")); -``` - -Pass in a string *by reference* for the response: -``` String response = ""; int statusCode = client.get("/", &response); ``` -### post(const char* path, const char* body) -### post(const char* path, String* response) -### post(const char* path, const char* body, String* response) +## POST +There are three different overloads of post: +```c++ +post(const char* path, const char* body) +post(const char* path, String* response) +post(const char* path, const char* body, String* response) ``` + +Examples: +```c++ String response = ""; int statusCode = client.post("/", &response); -statusCode = client.post("/", "foo=bar"); -response = ""; -statusCode = client.post("/", "foo=bar", &response); ``` -### put(const char* path, const char* body) -### put(const char* path, String* response) -### put(const char* path, const char* body, String* response) +```c++ +const char* post_body = "{foo: 'bar', bob: 'alice'}"; +int statusCode = client.post("/", post_body); ``` + +```c++ String response = ""; -int statusCode = client.put("/", &response); -statusCode = client.put("/", "foo=bar"); -response = ""; -statusCode = client.put("/", "foo=bar", &response); +const char* post_body = "{foo: 'bar', bob: 'alice'}"; + +int statusCode = client.post("/", post_body, &response); ``` -### del(const char* path) -### del(const char* path, const char* body) -### del(const char* path, String* response) -### del(const char* path, const char* body, String* response) +## PUT +```c++ +put(const char* path, const char* body) +put(const char* path, String* response) +put(const char* path, const char* body, String* response) ``` -String response = ""; -int statusCode = client.del("/", &response); + +## DEL + +```c++ +del(const char* path) +del(const char* path, const char* body) +del(const char* path, String* response) +del(const char* path, const char* body, String* response) ``` ## Full Example From cda6e935ca5d3e471b717c2388bc69915d9179d0 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 09:18:09 +0100 Subject: [PATCH 13/35] Update README.md --- README.md | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d61f11f..a094d9d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ HTTP Request library for Arduino and the ESP8266 WiFi SOC modules This library supports SSL! -To use with SSL either include the SHA1 fingerprint of the certificate of the site you are connecting to, or use force the library to use ssl insecurely (don't veryify the server is who it says it is). +To use with SSL either include the SHA1 fingerprint of the certificate of the site you are connecting to, or force the library to use ssl insecurely (don't veryify the server is who it says it is). You can get the SHA1 fingerprint by using a desktop browser and inspecting the SSL cert used at the site. Please note: this is FRAGILE, if the site updates their SSL, your code will break. But, there is not enough memory on the ESP8266 to store all the rool certs, so this is a working method. Se the example below. @@ -19,15 +19,16 @@ where `~/Documents/Arduino` is your sketchbook directory. > cd libraries > git clone https://github.com/dakaz/esp8266-restclient.git RestClient -# Usage - -### Include +# Dependencies -You need to have the `ESP8266` board support already included. +You need to have the `ESP8266` board support already installed. -### RestClient(host/ip, [port]) +# Usage Constructor to create an RestClient object to make requests against. +```c++ +RestClient(const char* host/ip, [int port], [bool force / const char* fingerprint]); +``` Use a domain name and default to port 80: ```c++ @@ -44,7 +45,7 @@ Use a domain name, an explicit port to an SSL site and verify the certificate wi RestClient client = RestClient("www.kudoso.com", 443, "EE 16 77 79 55 58 92 46 FB 18 40 99 2E 17 7E AB 32 0A 4A 88"); ``` -Use a domain name, an explicit port to an SSL site and force insecure SSL with no ssl verification: +Use a domain name, an explicit port to an SSL site and force insecure SSL with no certificate verification: ```c++ RestClient client = RestClient("www.kudoso.com", 443, 1); ``` @@ -52,7 +53,7 @@ RestClient client = RestClient("www.kudoso.com", 443, 1); ### dhcp() Sets up `EthernetClient` with a mac address of `DEADBEEFFEED`. Returns `true` or `false` to indicate if setting up DHCP -was successful or not +was successful or not. Not used on the ESP. ## RESTful methods @@ -64,7 +65,16 @@ All methods return an HTTP status code or 0 if there was an error. Start making requests! ## GET -Getting the response is optional: + +```c++ +get(const char* path, [String* response]); +``` + +Examples: +```c++ +int statusCode = client.get("/"); +``` + ```c++ String response = ""; int statusCode = client.get("/", &response); From c7d03a7dd108063702938dacf598f788fb1138d7 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 13:35:58 +0100 Subject: [PATCH 14/35] Added test example for esp --- .../full_test_suite_esp.ino | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 examples/full_test_suite_esp/full_test_suite_esp.ino diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino new file mode 100644 index 0000000..1376056 --- /dev/null +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -0,0 +1,247 @@ +/* RestClient full test suite + * + * Every REST method is called. + * + * by Mikkel Jeppesen (Duckle) + * + * Meant to call the API provided by: https://github.com/Hal9k-dk/REST-test + */ + + +#include "RestClient.h" + +int test_delay = 250; //so we don't spam the API +boolean describe_tests = true; + +RestClient client = RestClient("esp-rest-test.herokuapp.com"); +//RestClient client = RestClient("10.0.1.47",5000); + + +//Setup +void setup() +{ + Serial.begin(76800); +} + +void test_status(int statusCode) +{ + delay(test_delay); + if(statusCode == 200) + { + Serial.print(" OK ("); + Serial.print(statusCode); + Serial.println(")"); + } + else + { + Serial.print(" FAIL ("); + Serial.print(statusCode); + Serial.println(")"); + } +} + +String response; +void test_response(bool has_body=false, int headers=0) +{ + String body, header1, header2; + bool ok = response.startsWith(String("OK")); + + if(ok) + { + response.remove(0,2); + + if(has_body) + { + body = response.substring(0,4); + response.remove(0,4); + } + + if(headers > 0) + { + header1 = response.substring(0,2); + response.remove(0,2); + } + + if(headers > 1) + { + header2 = response.substring(0,2); + response.remove(0,2); + } + + if (has_body) + { + String out = (body == "test") ? " Body(OK): " : " Body(FAIL): "; + Serial.print(out); Serial.println(body); + } + + if(headers) + { + String out = (header1 == "h1") ? " H1(OK): " : " H1(FAIL): "; + Serial.print(out); Serial.println(header1); + if(headers > 1) + { + String out = (header2 == "h2") ? " H2(OK): " : " H2(FAIL): "; + Serial.print(out); Serial.println(header2); + } + } + } + else + { + Serial.print(' '); Serial.println(response); + } + response = ""; +} + +void describe(char * description) +{ + if(describe_tests) + { + Serial.println(description); + } +} + +//reusable test variables +char* post_body = "{\"test\":\"test\"}"; + +void GET_tests() +{ + describe("GET"); + test_status(client.get("/")); + + describe("GET w. response"); + test_status(client.get("/", &response)); + test_response(); + + describe("GET w. header"); + client.setHeader("X-Test-Header1: one"); + test_status(client.get("/")); + + describe("GET w. header and response"); + client.setHeader("X-Test-Header1: one"); + test_status(client.get("/", &response)); + test_response(false, 1); + + describe("GET w. 2 headers and response"); + client.setHeader("X-Test-Header1: one"); + client.setHeader("X-Test-Header2: two"); + test_status(client.get("/", &response)); + test_response(false, 2); +} + +void POST_tests() +{ + // POST TESTS + describe("POST w. body"); + client.setContentType("application/json"); + test_status(client.post("/", post_body)); + + describe("POST w. body and response"); + client.setContentType("application/json"); + test_status(client.post("/", post_body, &response)); + test_response(true); + + describe("POST w. body and header"); + client.setContentType("application/json"); + client.setHeader("X-Test-Header1: one"); + test_status(client.post("/", post_body)); + + describe("POST w. body, header and response"); + client.setContentType("application/json"); + client.setHeader("X-Test-Header1: one"); + test_status(client.post("/", post_body, &response)); + test_response(true, 1); + + describe("POST w. body, 2 headers and response"); + client.setContentType("application/json"); + client.setHeader("X-Test-Header1: one"); + client.setHeader("X-Test-Header2: two"); + test_status(client.post("/", post_body, &response)); + test_response(true, 2); +} + +void PUT_tests() +{ + describe("PUT w. body"); + client.setContentType("application/json"); + test_status(client.put("/", post_body)); + + describe("PUT w. body and response"); + client.setContentType("application/json"); + test_status(client.put("/", post_body, &response)); + test_response(true); + + describe("PUT w. body and header"); + client.setHeader("X-Test-Header1: one"); + test_status(client.put("/", post_body)); + + describe("PUT w. body, header and response"); + client.setHeader("X-Test-Header1: one"); + test_status(client.put("/", post_body, &response)); + test_response(true, 1); + + describe("PUT w. body, 2 headers and response"); + client.setHeader("X-Test-Header1: one"); + client.setHeader("X-Test-Header2: two"); + test_status(client.put("/", post_body, &response)); + test_response(true, 2); +} + +void DELETE_tests() +{ + describe("DELETE"); + test_status(client.del("/")); + + describe("DELETE w. body"); + client.setContentType("application/json"); + test_status(client.del("/", post_body)); + + describe("DELETE w. body and response"); + client.setContentType("application/json"); + test_status(client.del("/", post_body, &response)); + test_response(true); + + describe("DELETE w. body and header"); + client.setContentType("application/json"); + client.setHeader("X-Test-Header1: one"); + test_status(client.del("/", post_body)); + + describe("DELETE w. body, header and response"); + client.setContentType("application/json"); + client.setHeader("X-Test-Header1: one"); + test_status(client.del("/", post_body, &response)); + test_response(true, 1); + + describe("DELETE w. body, 2 headers and response"); + client.setContentType("application/json"); + client.setHeader("X-Test-Header1: one"); + client.setHeader("X-Test-Header2: two"); + test_status(client.del("/data-headers", post_body, &response)); + test_response(true, 2); +} + + +// Run the tests! +void loop() +{ + Serial.println("\nplain tests:"); + client.setSSL(0); + GET_tests(); + POST_tests(); + PUT_tests(); + DELETE_tests(); + + Serial.println("\nSSL tests:"); + client.setSSL(1); + GET_tests(); + POST_tests(); + PUT_tests(); + DELETE_tests(); + + Serial.println("\n send any-key to repeat tests"); + while(Serial.available() == 0) + {;} + while(Serial.available()) + { + Serial.read(); + } +} From 5d41d3ba1861145226d8a1e084da340be9fa4fac Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 13:39:18 +0100 Subject: [PATCH 15/35] Added wifi connection to example --- .../full_test_suite_esp.ino | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 1376056..2157983 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -17,10 +17,29 @@ RestClient client = RestClient("esp-rest-test.herokuapp.com"); //RestClient client = RestClient("10.0.1.47",5000); +const char ssid[] = "SSID"; +const char pass[] = "PASS"; + //Setup void setup() { Serial.begin(76800); + delay(10); + + // Connect to Wi-Fi network + Serial.println(); + Serial.println(); + Serial.print("Connecting to..."); + Serial.println(ssid); + + WiFi.begin(ssid, pass); + + while (WiFi.status() != WL_CONNECTED) + { + delay(500); + Serial.print("."); + } + Serial.println("\nWi-Fi connected successfully\n"); } void test_status(int statusCode) From 31eb7859f37ca60aa35509ff598702f5bb14c148 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:21:19 +0100 Subject: [PATCH 16/35] Actually include wifi library --- examples/full_test_suite_esp/full_test_suite_esp.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 2157983..6f5ac4b 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -7,7 +7,7 @@ * Meant to call the API provided by: https://github.com/Hal9k-dk/REST-test */ - +#include #include "RestClient.h" int test_delay = 250; //so we don't spam the API From 1cc9d3b510513ab67c424f8cfc1074745d3c386c Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:21:49 +0100 Subject: [PATCH 17/35] Actually include wifi library --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index ea2ccf1..fcf7e4b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP8266_REST_client_SSL -version=2.0.0 +version=2.0.1 author=Hal9k,DaKaz,csquared,ricardochimal,theycallmeswift maintainer=x sentence=A restclient for the esp8266 that supports SSL From 704de90e239f62105feccedc493e08c5195b057f Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:26:05 +0100 Subject: [PATCH 18/35] changed baud-rate --- examples/full_test_suite_esp/full_test_suite_esp.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 6f5ac4b..767fd59 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -23,7 +23,7 @@ const char pass[] = "PASS"; //Setup void setup() { - Serial.begin(76800); + Serial.begin(74880); delay(10); // Connect to Wi-Fi network From 11a1ceec3d5d68ec0233fa87b5934565c89b28ef Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:29:42 +0100 Subject: [PATCH 19/35] fixed path --- examples/full_test_suite_esp/full_test_suite_esp.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 767fd59..77d668e 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -10,7 +10,7 @@ #include #include "RestClient.h" -int test_delay = 250; //so we don't spam the API +int test_delay = 1000; //so we don't spam the API boolean describe_tests = true; RestClient client = RestClient("esp-rest-test.herokuapp.com"); @@ -234,7 +234,7 @@ void DELETE_tests() client.setContentType("application/json"); client.setHeader("X-Test-Header1: one"); client.setHeader("X-Test-Header2: two"); - test_status(client.del("/data-headers", post_body, &response)); + test_status(client.del("/", post_body, &response)); test_response(true, 2); } From 80772df0cff13f3c45e0a2f66a526afe3bd13d09 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:38:00 +0100 Subject: [PATCH 20/35] Added port setter function --- RestClient.cpp | 4 ++++ RestClient.h | 2 ++ examples/full_test_suite_esp/full_test_suite_esp.ino | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/RestClient.cpp b/RestClient.cpp index 827b4ba..f9b2561 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -150,6 +150,10 @@ void RestClient::setSSL(int _ssl){ ssl = (_ssl) ? 1 : 0; } +void RestClient::setPort(int _port){ + port = _port; +} + // The mother- generic request method. // int RestClient::request(const char* method, const char* path, diff --git a/RestClient.h b/RestClient.h index ce5a765..8de4726 100644 --- a/RestClient.h +++ b/RestClient.h @@ -27,6 +27,8 @@ class RestClient { void setContentType(const char*); // Set SSL support on(1) or off(0) void setSSL(int); + // Set port used + void setPort(int); // GET path int get(const char*); diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 77d668e..ce33965 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -244,13 +244,18 @@ void loop() { Serial.println("\nplain tests:"); client.setSSL(0); + client.setPort(80); + GET_tests(); POST_tests(); PUT_tests(); DELETE_tests(); + Serial.println("\nSSL tests:"); client.setSSL(1); + client.setPort(443); + GET_tests(); POST_tests(); PUT_tests(); From cda4fffd5472d14e7f99425801fead45237496c4 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:45:08 +0100 Subject: [PATCH 21/35] Increased test delay --- examples/full_test_suite_esp/full_test_suite_esp.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index ce33965..699c255 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -10,7 +10,7 @@ #include #include "RestClient.h" -int test_delay = 1000; //so we don't spam the API +int test_delay = 1500; //so we don't spam the API boolean describe_tests = true; RestClient client = RestClient("esp-rest-test.herokuapp.com"); From bbfe5db920fb8c1bfe3492b372ac666fe4fc91cc Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:45:23 +0100 Subject: [PATCH 22/35] Increased test delay --- examples/full_test_suite_esp/full_test_suite_esp.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 699c255..74ab4bf 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -10,7 +10,7 @@ #include #include "RestClient.h" -int test_delay = 1500; //so we don't spam the API +int test_delay = 2000; //so we don't spam the API boolean describe_tests = true; RestClient client = RestClient("esp-rest-test.herokuapp.com"); From d7da72e9ba313f55818556eb80240578fb86e71d Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 15:53:48 +0100 Subject: [PATCH 23/35] Sprinkled some delay(0) to avoid WDT resets --- RestClient.cpp | 6 +++--- examples/full_test_suite_esp/full_test_suite_esp.ino | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index f9b2561..221e593 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -205,7 +205,7 @@ int RestClient::request(const char* method, const char* path, request += String(body); request += "\r\n\r\n"; } - + delay(0); write(request.c_str()); HTTP_DEBUG_PRINT("\nEND REQUEST\n"); @@ -257,7 +257,7 @@ int RestClient::readResponse(String* response) { if (sslClient.available()) { HTTP_DEBUG_PRINT(","); - + delay(0); char c = sslClient.read(); HTTP_DEBUG_PRINT(c); @@ -302,7 +302,7 @@ int RestClient::readResponse(String* response) { if (client.available()) { HTTP_DEBUG_PRINT(","); - + delay(0); char c = client.read(); HTTP_DEBUG_PRINT(c); diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index 74ab4bf..ce33965 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -10,7 +10,7 @@ #include #include "RestClient.h" -int test_delay = 2000; //so we don't spam the API +int test_delay = 1000; //so we don't spam the API boolean describe_tests = true; RestClient client = RestClient("esp-rest-test.herokuapp.com"); From 3c817150e4d616eefdd960b96842377c4ac624fd Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Mon, 11 Mar 2019 16:05:53 +0100 Subject: [PATCH 24/35] Sprinkled some delay(0) to avoid WDT resets --- RestClient.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RestClient.cpp b/RestClient.cpp index 221e593..41bb249 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -254,6 +254,7 @@ int RestClient::readResponse(String* response) { HTTP_DEBUG_PRINT("HTTP: Connect: " + String(sslClient.connected()) + " Available: " + String(sslClient.available()) + "\n"); while (sslClient.connected()) { HTTP_DEBUG_PRINT("."); + delay(0); if (sslClient.available()) { HTTP_DEBUG_PRINT(","); @@ -299,6 +300,7 @@ int RestClient::readResponse(String* response) { }else { while (client.connected()) { HTTP_DEBUG_PRINT("."); + delay(0); if (client.available()) { HTTP_DEBUG_PRINT(","); From ee38245a99da884bd65baadc5a06554a52b45d84 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Tue, 12 Mar 2019 09:20:22 +0100 Subject: [PATCH 25/35] Set myself as maintainer --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index fcf7e4b..29b6959 100644 --- a/library.properties +++ b/library.properties @@ -1,7 +1,7 @@ name=ESP8266_REST_client_SSL version=2.0.1 author=Hal9k,DaKaz,csquared,ricardochimal,theycallmeswift -maintainer=x +maintainer=Duckle29 sentence=A restclient for the esp8266 that supports SSL paragraph=A restclient for the esp8266 that supports SSL category=Communication From 8763151cb32aeb8798e84a452b61ca6bb3407920 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Tue, 12 Mar 2019 09:46:42 +0100 Subject: [PATCH 26/35] Updated readme --- README.md | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a094d9d..de70c9c 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ RestClient(const char* host/ip, [int port], [bool force / const char* fingerprin Use a domain name and default to port 80: ```c++ -RestClient client = RestClient("arduino-http-lib-test.herokuapp.com"); +RestClient client = RestClient("esp-rest-test.herokuapp.com"); ``` Use a local IP and an explicit port: @@ -59,9 +59,6 @@ was successful or not. Not used on the ESP. All methods return an HTTP status code or 0 if there was an error. -### `get(const char* path)` -### `get(const char* path, String* response)` - Start making requests! ## GET @@ -84,9 +81,9 @@ int statusCode = client.get("/", &response); There are three different overloads of post: ```c++ -post(const char* path, const char* body) -post(const char* path, String* response) -post(const char* path, const char* body, String* response) +post(const char* path, const char* body); +post(const char* path, String* response); +post(const char* path, const char* body, String* response); ``` Examples: @@ -111,18 +108,18 @@ int statusCode = client.post("/", post_body, &response); ## PUT ```c++ -put(const char* path, const char* body) -put(const char* path, String* response) -put(const char* path, const char* body, String* response) +put(const char* path, const char* body); +put(const char* path, String* response); +put(const char* path, const char* body, String* response); ``` ## DEL ```c++ -del(const char* path) -del(const char* path, const char* body) -del(const char* path, String* response) -del(const char* path, const char* body, String* response) +del(const char* path); +del(const char* path, const char* body); +del(const char* path, String* response); +del(const char* path, const char* body, String* response); ``` ## Full Example From ad7a014ba2a1ed1cd798fe2766d5b774c6865285 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Tue, 12 Mar 2019 13:41:59 +0100 Subject: [PATCH 27/35] Changed name --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index fcf7e4b..155da5e 100644 --- a/library.properties +++ b/library.properties @@ -1,4 +1,4 @@ -name=ESP8266_REST_client_SSL +name=ESP8266 REST client SSL version=2.0.1 author=Hal9k,DaKaz,csquared,ricardochimal,theycallmeswift maintainer=x From ce9f8e413c6b17362d5bbff097124a0a441eed3e Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Tue, 12 Mar 2019 15:40:44 +0100 Subject: [PATCH 28/35] Changed baudrate as arduino had issues with it --- examples/full_test_suite_esp/full_test_suite_esp.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/full_test_suite_esp/full_test_suite_esp.ino b/examples/full_test_suite_esp/full_test_suite_esp.ino index ce33965..c6cd1b5 100644 --- a/examples/full_test_suite_esp/full_test_suite_esp.ino +++ b/examples/full_test_suite_esp/full_test_suite_esp.ino @@ -23,7 +23,7 @@ const char pass[] = "PASS"; //Setup void setup() { - Serial.begin(74880); + Serial.begin(115200); delay(10); // Connect to Wi-Fi network From 1a2486c25a941d1b2cbf43fe48992411eef8cadd Mon Sep 17 00:00:00 2001 From: Torsten Martinsen Date: Sat, 7 Sep 2019 22:08:24 +0200 Subject: [PATCH 29/35] Initialize member variables. --- RestClient.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/RestClient.h b/RestClient.h index 8de4726..9f7e895 100644 --- a/RestClient.h +++ b/RestClient.h @@ -14,11 +14,11 @@ class RestClient { // set fingerprint if using SSL, stores the SHA1 fingerprint of the remote site, implicity sets ssl to on RestClient(const char* _host, int _port, const char* _fingerprint); - //Client Setup + // Client Setup bool dhcp(); int begin(byte*); - //Generic HTTP Request + // Generic HTTP Request int request(const char* method, const char* path, const char* body, String* response); // Set a Request Header @@ -64,13 +64,13 @@ class RestClient { WiFiClientSecure sslClient; int readResponse(String*); void write(const char*); - const char* host; - int port; - int num_headers; - const char* headers[10]; - const char* contentType; - const char* fingerprint; - int ssl; + const char* host = nullptr; + int port = 0; + int num_headers = 0; + const char* headers[10] = { nullptr }; + const char* contentType = nullptr; + const char* fingerprint = nullptr; + bool ssl = 0; }; #endif From 1e2604d190e81389d97121307755bdfda843b447 Mon Sep 17 00:00:00 2001 From: Torsten Martinsen Date: Sat, 7 Sep 2019 22:13:29 +0200 Subject: [PATCH 30/35] Change SSL argument to bool. Fix bogus maybe-initialization of contenttype. --- RestClient.cpp | 37 +++++++++++-------------------------- RestClient.h | 14 +++++++------- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 41bb249..f9148cc 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -11,23 +11,15 @@ RestClient::RestClient(const char* _host){ host = _host; port = 80; - ssl = 0; - fingerprint = NULL; - num_headers = 0; - if (!contentType) { - contentType = "application/x-www-form-urlencoded"; // default - } + ssl = false; + contentType = "application/x-www-form-urlencoded"; } RestClient::RestClient(const char* _host, int _port){ host = _host; port = _port; - ssl = 0; - fingerprint = NULL; - num_headers = 0; - if (!contentType) { - contentType = "application/x-www-form-urlencoded"; // default - } + ssl = false; + contentType = "application/x-www-form-urlencoded"; } bool RestClient::dhcp(){ @@ -45,23 +37,16 @@ bool RestClient::dhcp(){ RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ host = _host; port = _port; - ssl = 1; + ssl = true; fingerprint = _fingerprint; - num_headers = 0; - if (!contentType) { - contentType = "application/x-www-form-urlencoded"; // default - } + contentType = "application/x-www-form-urlencoded"; } -RestClient::RestClient(const char* _host, int _port, int _ssl) { +RestClient::RestClient(const char* _host, int _port, bool _ssl) { host = _host; port = _port; - ssl = (_ssl) ? 1 : 0; - fingerprint = NULL; - num_headers = 0; - if (!contentType) { - contentType = "application/x-www-form-urlencoded"; // default - } + ssl = _ssl; + contentType = "application/x-www-form-urlencoded"; } // GET path @@ -146,8 +131,8 @@ void RestClient::setContentType(const char* contentTypeValue){ contentType = contentTypeValue; } -void RestClient::setSSL(int _ssl){ - ssl = (_ssl) ? 1 : 0; +void RestClient::setSSL(bool _ssl){ + ssl = _ssl; } void RestClient::setPort(int _port){ diff --git a/RestClient.h b/RestClient.h index 9f7e895..862157d 100644 --- a/RestClient.h +++ b/RestClient.h @@ -7,12 +7,12 @@ class RestClient { public: - RestClient(const char* host); - RestClient(const char* _host, int _port); + explicit RestClient(const char* host); + explicit RestClient(const char* _host, int _port); // set ssl to on but do not verify server identity with fingerprint - RestClient(const char* _host, int _port, int _ssl); + explicit RestClient(const char* _host, int _port, bool _ssl); // set fingerprint if using SSL, stores the SHA1 fingerprint of the remote site, implicity sets ssl to on - RestClient(const char* _host, int _port, const char* _fingerprint); + explicit RestClient(const char* _host, int _port, const char* _fingerprint); // Client Setup bool dhcp(); @@ -25,8 +25,8 @@ class RestClient { void setHeader(const char*); // Set Content-Type Header void setContentType(const char*); - // Set SSL support on(1) or off(0) - void setSSL(int); + // Set SSL support on or off + void setSSL(bool); // Set port used void setPort(int); @@ -70,7 +70,7 @@ class RestClient { const char* headers[10] = { nullptr }; const char* contentType = nullptr; const char* fingerprint = nullptr; - bool ssl = 0; + bool ssl = false; }; #endif From ebd3b5a824fee1f7fe4a9afdee4d4c79f4a7f65b Mon Sep 17 00:00:00 2001 From: Torsten Martinsen Date: Sat, 7 Sep 2019 22:17:22 +0200 Subject: [PATCH 31/35] Fix bug where last part of response could be lost. --- RestClient.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index f9148cc..4c3219b 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -271,7 +271,7 @@ int RestClient::readResponse(String* response) { } if (c == '\n') { - // you're starting a new line + // your starting a new line currentLineIsBlank = true; } else if (c != '\r') { @@ -307,7 +307,7 @@ int RestClient::readResponse(String* response) { } if(httpBody){ - //only write response if its not null + // only write response if its not null if(response != NULL) response->concat(c); } else @@ -317,7 +317,7 @@ int RestClient::readResponse(String* response) { } if (c == '\n') { - // you're starting a new line + // your starting a new line currentLineIsBlank = true; } else if (c != '\r') { @@ -327,6 +327,12 @@ int RestClient::readResponse(String* response) { } } } + while (client.available()) { + char c = client.read(); + if (httpBody) + if (response) + response->concat(c); + } } HTTP_DEBUG_PRINT("HTTP: return readResponse3\n"); From f8195f1cc138ca13e12d57263324f1087d95023b Mon Sep 17 00:00:00 2001 From: Torsten Martinsen Date: Thu, 12 Sep 2019 19:42:10 +0200 Subject: [PATCH 32/35] Ensure that we run the state machine for bytes received after the connection is closed. --- RestClient.cpp | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 4c3219b..d52e264 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -2,9 +2,7 @@ #ifdef HTTP_DEBUG #define HTTP_DEBUG_PRINT(string) (Serial.print(string)) -#endif - -#ifndef HTTP_DEBUG +#else #define HTTP_DEBUG_PRINT(string) #endif @@ -227,13 +225,6 @@ int RestClient::readResponse(String* response) { int i = 0; int code = 0; - if(response == NULL){ - HTTP_DEBUG_PRINT("HTTP: NULL RESPONSE POINTER: \n"); - }else{ - HTTP_DEBUG_PRINT("HTTP: NON-NULL RESPONSE POINTER: \n"); - } - - HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n"); void* http_client; if(ssl) { HTTP_DEBUG_PRINT("HTTP: Connect: " + String(sslClient.connected()) + " Available: " + String(sslClient.available()) + "\n"); @@ -283,15 +274,12 @@ int RestClient::readResponse(String* response) { } HTTP_DEBUG_PRINT("HTTPS client closed \n"); }else { - while (client.connected()) { - HTTP_DEBUG_PRINT("."); + while (client.connected() || client.available()) { delay(0); if (client.available()) { - HTTP_DEBUG_PRINT(","); delay(0); char c = client.read(); - HTTP_DEBUG_PRINT(c); if(c == ' ' && !inStatus){ inStatus = true; From abdae4aaa8a58b8c0f6f76a89de7c49a1cd1c689 Mon Sep 17 00:00:00 2001 From: Mikkel Jeppesen <2756925+Duckle29@users.noreply.github.com> Date: Wed, 16 Oct 2019 08:19:43 +0200 Subject: [PATCH 33/35] Update library.properties --- library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.properties b/library.properties index b3261ec..90e00c6 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=ESP8266 REST client SSL -version=2.0.1 +version=2.1.0 author=Hal9k,DaKaz,csquared,ricardochimal,theycallmeswift maintainer=Duckle29 sentence=A restclient for the esp8266 that supports SSL paragraph=A restclient for the esp8266 that supports SSL category=Communication url=https://github.com/Hal9k-dk/esp8266-restclient -architectures=esp8266 \ No newline at end of file +architectures=esp8266 From 898a677f2e157d2dd3633bdd8020fce0a780715e Mon Sep 17 00:00:00 2001 From: Szymon Dudzik Date: Sat, 14 Dec 2019 15:45:57 +0100 Subject: [PATCH 34/35] New methods to access raw response and specific response header, --- README.md | 22 ++++++++++++++++++++++ RestClient.cpp | 19 +++++++++++++++++++ RestClient.h | 6 ++++++ 3 files changed, 47 insertions(+) diff --git a/README.md b/README.md index de70c9c..550eb07 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ You can get the SHA1 fingerprint by using a desktop browser and inspecting the S This library is derived almost entirely from the great work done here: https://github.com/csquared/arduino-restclient +# Fork note + +API I used returns token inside response header, there was no way to access it, so I've managed to prepare two more functions. One for full access to raw response, and the second to fetch specific header value in simplest possible way. +I have no time to prepare full test suites, but it should work on any environments. + # Install Clone (or download and unzip) the repository to `~/Documents/Arduino/libraries` @@ -122,6 +127,23 @@ del(const char* path, String* response); del(const char* path, const char* body, String* response); ``` +## Additional function implemented in this fork + +```c++ +// Get raw response (without body) +getRawLastResponse(); +// Get response header value by key +getResponseHeader(const char* key, String*); +``` + +Examples: + +```c++ +String rawResponse = client.getRawLastResponse(); +String headerValue; +client.getResponseHeader("Token", &headerValue); +``` + ## Full Example I test every way of calling the library (against a public heroku app)[https://github.com/csquared/arduino-http-test]. diff --git a/RestClient.cpp b/RestClient.cpp index d52e264..583ca81 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -11,6 +11,7 @@ RestClient::RestClient(const char* _host){ port = 80; ssl = false; contentType = "application/x-www-form-urlencoded"; + rawLastResponse = ""; } RestClient::RestClient(const char* _host, int _port){ @@ -221,6 +222,7 @@ int RestClient::readResponse(String* response) { boolean httpBody = false; boolean inStatus = false; + String rawLastResponse = ""; char statusCode[4]; int i = 0; int code = 0; @@ -269,6 +271,8 @@ int RestClient::readResponse(String* response) { // you've gotten a character on the current line currentLineIsBlank = false; } + + rawLastResponse = rawLastResponse + c; } } } @@ -312,6 +316,8 @@ int RestClient::readResponse(String* response) { // you've gotten a character on the current line currentLineIsBlank = false; } + + rawLastResponse = rawLastResponse + c; } } } @@ -326,3 +332,16 @@ int RestClient::readResponse(String* response) { HTTP_DEBUG_PRINT("HTTP: return readResponse3\n"); return code; } + +String RestClient::getRawLastResponse() { + return rawLastResponse; +} + +void RestClient::getResponseHeader(const char* key, String* value) { + String fullLengthPrefix = (String) key + ": "; + int start = rawLastResponse.indexOf(fullLengthPrefix); + int end = rawLastResponse.indexOf("\n", start); + if(start && end) { + value->concat(rawLastResponse.substring(start + 7,end)); + } +} diff --git a/RestClient.h b/RestClient.h index 862157d..5c63c7b 100644 --- a/RestClient.h +++ b/RestClient.h @@ -59,9 +59,15 @@ class RestClient { // DELETE path and body and response int del(const char*, const char*, String*); + // Get raw last response + String getRawLastResponse(); + // Get response header + void getResponseHeader(const char* key, String*); + private: WiFiClient client; WiFiClientSecure sslClient; + String rawLastResponse; int readResponse(String*); void write(const char*); const char* host = nullptr; From e346ff6996c1551e9d7fc7bd71ced3e14c64ce26 Mon Sep 17 00:00:00 2001 From: Szymon Dudzik Date: Sat, 14 Dec 2019 20:36:30 +0100 Subject: [PATCH 35/35] Fixed position and added debugging lines. --- RestClient.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 583ca81..92305dd 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -222,7 +222,6 @@ int RestClient::readResponse(String* response) { boolean httpBody = false; boolean inStatus = false; - String rawLastResponse = ""; char statusCode[4]; int i = 0; int code = 0; @@ -231,11 +230,11 @@ int RestClient::readResponse(String* response) { if(ssl) { HTTP_DEBUG_PRINT("HTTP: Connect: " + String(sslClient.connected()) + " Available: " + String(sslClient.available()) + "\n"); while (sslClient.connected()) { - HTTP_DEBUG_PRINT("."); + // HTTP_DEBUG_PRINT("."); delay(0); if (sslClient.available()) { - HTTP_DEBUG_PRINT(","); + // HTTP_DEBUG_PRINT(","); delay(0); char c = sslClient.read(); HTTP_DEBUG_PRINT(c); @@ -338,10 +337,14 @@ String RestClient::getRawLastResponse() { } void RestClient::getResponseHeader(const char* key, String* value) { + HTTP_DEBUG_PRINT(rawLastResponse); String fullLengthPrefix = (String) key + ": "; + HTTP_DEBUG_PRINT(fullLengthPrefix); int start = rawLastResponse.indexOf(fullLengthPrefix); int end = rawLastResponse.indexOf("\n", start); + HTTP_DEBUG_PRINT(start); + HTTP_DEBUG_PRINT(end); if(start && end) { - value->concat(rawLastResponse.substring(start + 7,end)); + value->concat(rawLastResponse.substring(start + fullLengthPrefix.length(), end-1)); } }