From b544acde216c835da0262b20909f474ced56c71a Mon Sep 17 00:00:00 2001 From: Michael Kazmier Date: Tue, 25 Aug 2015 12:51:35 -0600 Subject: [PATCH 01/10] working rest library for ESP8266 --- README.md | 59 +++++--------------------------------------------- RestClient.cpp | 15 ------------- RestClient.h | 7 ++---- 3 files changed, 7 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 23dee66..241ac26 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ -# RestClient for Arduino +# RestClient for Arduino ESP8266 WiFi modules -HTTP Request library for Arduino and the Ethernet shield. +HTTP Request library for Arduino and the ESP8266 WiFi SOC modules +This library is derived almost entirely from the great work done here: https://github.com/csquared/arduino-restclient # Install @@ -10,19 +11,13 @@ where `~/Documents/Arduino` is your sketchbook directory. > cd ~/Documents/Arduino > mkdir libraries > cd libraries - > git clone https://github.com/csquared/arduino-restclient.git RestClient + > git clone https://github.com/dakaz/esp8266-restclient.git RestClient # Usage ### Include -You need to have the `Ethernet` library already included. - -```c++ -#include -#include -#include "RestClient.h" -``` +You need to have the `ESP8266` board support already included. ### RestClient(host/ip, [port]) @@ -38,50 +33,6 @@ Use a local IP and an explicit port: RestClient client = RestClient("192.168.1.50",5000); ``` -### dhcp() - -Sets up `EthernetClient` with a mac address of `DEADBEEFFEED` - -```c++ - client.dhcp() -``` - -Note: you can have multiple RestClient objects but only need to call -this once. - -Note: if you have multiple Arduinos on the same network, you'll need -to give each one a different mac address. - -### begin(byte mac[]) - -It just wraps the `EthernetClient` call to `begin` and DHCPs. -Use this if you need to explicitly set the mac address. - -```c++ - byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; - if (client.begin(mac) == 0) { - Serial.println("Failed to configure Ethernet using DHCP"); - } -``` - -### Manual Ethernet Setup - -You can skip the above methods and just configure the EthernetClient yourself: - -```c++ - byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; - //the IP address for the shield: - byte ip[] = { 192, 168, 2, 11 }; - Ethernet.begin(mac,ip); -``` - -```c++ - byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; - Ethernet.begin(mac); -``` - -This is especially useful for debugging network connection issues. - ## RESTful methods All methods return an HTTP status code or 0 if there was an error. diff --git a/RestClient.cpp b/RestClient.cpp index d1dcbd7..f2283da 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -22,21 +22,6 @@ RestClient::RestClient(const char* _host, int _port){ contentType = "x-www-form-urlencoded"; // default } -void RestClient::dhcp(){ - byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; - if (begin(mac) == 0) { - Serial.println("Failed to configure Ethernet using DHCP"); - } - //give it time to initialize - delay(1000); -} - -int RestClient::begin(byte mac[]){ - return Ethernet.begin(mac); - //give it time to initialize - delay(1000); -} - // GET path int RestClient::get(const char* path){ return request("GET", path, NULL, NULL); diff --git a/RestClient.h b/RestClient.h index 637e0ac..314c774 100644 --- a/RestClient.h +++ b/RestClient.h @@ -1,7 +1,4 @@ -#include -#include -#include - +#include class RestClient { public: @@ -45,7 +42,7 @@ class RestClient { int del(const char*, const char*, String*); private: - EthernetClient client; + WiFiClient client; int readResponse(String*); void write(const char*); const char* host; From 643ea6ec14138b3d6ef2daedf8ac236168a0047c Mon Sep 17 00:00:00 2001 From: Mike Kazmier Date: Fri, 6 May 2016 08:36:40 -0600 Subject: [PATCH 02/10] updated to use SSL with the new WiFiClientSecure --- README.md | 8 ++ RestClient.cpp | 330 ++++++++++++++++++++++++++++++------------------- RestClient.h | 32 +++-- 3 files changed, 232 insertions(+), 138 deletions(-) diff --git a/README.md b/README.md index 241ac26..71c9614 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,9 @@ # RestClient for Arduino ESP8266 WiFi modules 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 is derived almost entirely from the great work done here: https://github.com/csquared/arduino-restclient # Install @@ -33,6 +36,11 @@ Use a local IP and an explicit port: RestClient client = RestClient("192.168.1.50",5000); ``` +Use a local IP and an explicit port to an SSL site:: +```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"); +``` + ## RESTful methods All methods return an HTTP status code or 0 if there was an error. diff --git a/RestClient.cpp b/RestClient.cpp index f2283da..61ce836 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -9,208 +9,288 @@ #endif RestClient::RestClient(const char* _host){ - host = _host; - port = 80; - num_headers = 0; - contentType = "x-www-form-urlencoded"; // default + host = _host; + port = 80; + fingerprint = NULL; + num_headers = 0; + contentType = "x-www-form-urlencoded"; // default } RestClient::RestClient(const char* _host, int _port){ - host = _host; - port = _port; - num_headers = 0; - contentType = "x-www-form-urlencoded"; // default + host = _host; + port = _port; + fingerprint = NULL; + num_headers = 0; + contentType = "x-www-form-urlencoded"; // default +} + +RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ + host = _host; + port = _port; + fingerprint = _fingerprint; + num_headers = 0; + contentType = "x-www-form-urlencoded"; // default } // GET path int RestClient::get(const char* path){ - return request("GET", path, NULL, NULL); + return request("GET", path, NULL, NULL); } //GET path with response int RestClient::get(const char* path, String* response){ - return request("GET", path, NULL, response); + return request("GET", path, NULL, response); } // POST path and body int RestClient::post(const char* path, const char* body){ - return request("POST", path, body, NULL); + return request("POST", path, body, NULL); } // POST path and body with response int RestClient::post(const char* path, const char* body, String* response){ - return request("POST", path, body, response); + return request("POST", path, body, response); } // PUT path and body int RestClient::put(const char* path, const char* body){ - return request("PUT", path, body, NULL); + return request("PUT", path, body, NULL); } // PUT path and body with response int RestClient::put(const char* path, const char* body, String* response){ - return request("PUT", path, body, response); + return request("PUT", path, body, response); } // DELETE path int RestClient::del(const char* path){ - return request("DELETE", path, NULL, NULL); + return request("DELETE", path, NULL, NULL); } // DELETE path and response int RestClient::del(const char* path, String* response){ - return request("DELETE", path, NULL, response); + return request("DELETE", path, NULL, response); } // DELETE path and body int RestClient::del(const char* path, const char* body ){ - return request("DELETE", path, body, NULL); + return request("DELETE", path, body, NULL); } // DELETE path and body with response int RestClient::del(const char* path, const char* body, String* response){ - return request("DELETE", path, body, response); + return request("DELETE", path, body, response); } void RestClient::write(const char* string){ - HTTP_DEBUG_PRINT(string); - client.print(string); + + if(fingerprint) { + HTTP_DEBUG_PRINT("\nSSL Print: "); + HTTP_DEBUG_PRINT(string); + sslClient.print(string); + } else { + HTTP_DEBUG_PRINT("\nHTTP Print: "); + HTTP_DEBUG_PRINT(string); + client.print(string); + } } void RestClient::setHeader(const char* header){ - headers[num_headers] = header; - num_headers++; + headers[num_headers] = header; + num_headers++; } void RestClient::setContentType(const char* contentTypeValue){ - contentType = contentTypeValue; + contentType = contentTypeValue; } // The mother- generic request method. // int RestClient::request(const char* method, const char* path, - const char* body, String* response){ - - HTTP_DEBUG_PRINT("HTTP: connect\n"); - - if(client.connect(host, port)){ + const char* body, String* response){ + + HTTP_DEBUG_PRINT("HTTP: connect\n"); + + if (fingerprint) { + if(!sslClient.connect(host, port)){ + HTTP_DEBUG_PRINT("HTTPS Connection failed\n"); + return 0; + } + 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"); + return 0; + } + } + + HTTP_DEBUG_PRINT("HTTP: connected\n"); HTTP_DEBUG_PRINT("REQUEST: \n"); - // Make a HTTP request line: - write(method); - write(" "); - write(path); - write(" HTTP/1.1\r\n"); + + String request = String(method) + " " + String(path) + " HTTP/1.1\r\n"; for(int i=0; iconcat(c); - } - else - { - if (c == '\n' && currentLineIsBlank) { - httpBody = true; - } - - if (c == '\n') { - // you're starting a new line - currentLineIsBlank = true; - } - else if (c != '\r') { - // you've gotten a character on the current line - currentLineIsBlank = false; - } - } + + // an http request ends with a blank line + boolean currentLineIsBlank = true; + boolean httpBody = false; + boolean inStatus = false; + + char statusCode[4]; + 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: return readResponse3\n"); - return code; -} + + HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n"); + void* http_client; + if(fingerprint) { + HTTP_DEBUG_PRINT("HTTP: Connect: " + String(sslClient.connected()) + " Available: " + String(sslClient.available()) + "\n"); + while (sslClient.connected()) { + HTTP_DEBUG_PRINT("."); + + if (sslClient.available()) { + HTTP_DEBUG_PRINT(","); + + char c = sslClient.read(); + HTTP_DEBUG_PRINT(c); + + if(c == ' ' && !inStatus){ + inStatus = true; + } + + if(inStatus && i < 3 && c != ' '){ + statusCode[i] = c; + i++; + } + if(i == 3){ + statusCode[i] = '\0'; + code = atoi(statusCode); + } + + if(httpBody){ + //only write response if its not null + if(response != NULL) response->concat(c); + } + else + { + if (c == '\n' && currentLineIsBlank) { + httpBody = true; + } + + if (c == '\n') { + // you're starting a new line + currentLineIsBlank = true; + } + else if (c != '\r') { + // you've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + } + HTTP_DEBUG_PRINT("HTTPS client closed \n"); + }else { + while (client.connected()) { + HTTP_DEBUG_PRINT("."); + + if (client.available()) { + HTTP_DEBUG_PRINT(","); + + char c = client.read(); + HTTP_DEBUG_PRINT(c); + + if(c == ' ' && !inStatus){ + inStatus = true; + } + + if(inStatus && i < 3 && c != ' '){ + statusCode[i] = c; + i++; + } + if(i == 3){ + statusCode[i] = '\0'; + code = atoi(statusCode); + } + + if(httpBody){ + //only write response if its not null + if(response != NULL) response->concat(c); + } + else + { + if (c == '\n' && currentLineIsBlank) { + httpBody = true; + } + + if (c == '\n') { + // you're starting a new line + currentLineIsBlank = true; + } + else if (c != '\r') { + // you've gotten a character on the current line + currentLineIsBlank = false; + } + } + } + } + } + + HTTP_DEBUG_PRINT("HTTP: return readResponse3\n"); + return code; +} \ No newline at end of file diff --git a/RestClient.h b/RestClient.h index 314c774..cd37f4b 100644 --- a/RestClient.h +++ b/RestClient.h @@ -1,14 +1,18 @@ -#include -class RestClient { +#include +#include - public: +class RestClient { + +public: RestClient(const char* host); RestClient(const char* _host, int _port); - + // set fingerprint if using SSL, stores the SHA1 fingerprint of the remote site + RestClient(const char* _host, int _port, const char* _fingerprint); + //Client Setup void dhcp(); int begin(byte*); - + //Generic HTTP Request int request(const char* method, const char* path, const char* body, String* response); @@ -16,22 +20,22 @@ class RestClient { void setHeader(const char*); // Set Content-Type Header void setContentType(const char*); - + // GET path int get(const char*); // GET path and response int get(const char*, String*); - + // POST path and body int post(const char* path, const char* body); // POST path and body and response int post(const char* path, const char* body, String*); - + // PUT path and body int put(const char* path, const char* body); // PUT path and body and response int put(const char* path, const char* body, String*); - + // DELETE path int del(const char*); // DELETE path and body @@ -40,14 +44,16 @@ class RestClient { int del(const char*, String*); // DELETE path and body and response int del(const char*, const char*, String*); - - private: + +private: WiFiClient client; + 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* contentType; + const char* fingerprint; +}; \ No newline at end of file From aa91b66e0825f42f15a7bfc2db8f26d63d121a7b Mon Sep 17 00:00:00 2001 From: Mike Kazmier Date: Thu, 6 Apr 2017 09:55:05 -0600 Subject: [PATCH 03/10] add support for connecting to a site via SSL but not verifying the certificate with the fingerprint --- README.md | 7 ++++++- RestClient.cpp | 39 ++++++++++++++++++++++++++++----------- RestClient.h | 25 +++++++++++++++---------- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ae95303..d280edd 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,12 @@ Use a local IP and an explicit port: RestClient client = RestClient("192.168.1.50",5000); ``` -Use a local IP and an explicit port to an SSL site:: +Use a local IP, an explicit port to an SSL site and (must include the 1 to turn on SSL): +```c++ +RestClient client = RestClient("www.kudoso.com",443, 1); +``` + +Use a local IP, an explicit port to an SSL site and verify the certificate with its fingerprint: ```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"); ``` diff --git a/RestClient.cpp b/RestClient.cpp index 860154a..cf83b1c 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -11,6 +11,7 @@ RestClient::RestClient(const char* _host){ host = _host; port = 80; + ssl = 0; fingerprint = NULL; num_headers = 0; contentType = "application/x-www-form-urlencoded"; // default @@ -19,6 +20,7 @@ RestClient::RestClient(const char* _host){ RestClient::RestClient(const char* _host, int _port){ host = _host; port = _port; + ssl = 0; fingerprint = NULL; num_headers = 0; contentType = "application/x-www-form-urlencoded"; // default @@ -39,11 +41,21 @@ bool RestClient::dhcp(){ RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ host = _host; port = _port; + ssl = 1; fingerprint = _fingerprint; num_headers = 0; contentType = "x-www-form-urlencoded"; // default } +RestClient::RestClient(const char* _host, int _port, int _ssl) { + host = _host; + port = _port; + ssl = (_ssl) ? 1 : 0; + fingerprint = NULL; + num_headers = 0; + contentType = "x-www-form-urlencoded"; // default +} + // GET path int RestClient::get(const char* path){ return request("GET", path, NULL, NULL); @@ -96,7 +108,7 @@ int RestClient::del(const char* path, const char* body, String* response){ void RestClient::write(const char* string){ - if(fingerprint) { + if(ssl) { HTTP_DEBUG_PRINT("\nSSL Print: "); HTTP_DEBUG_PRINT(string); sslClient.print(string); @@ -116,6 +128,10 @@ void RestClient::setContentType(const char* contentTypeValue){ contentType = contentTypeValue; } +void RestClient::setSSL(int _ssl){ + ssl = (_ssl) ? 1 : 0; +} + // The mother- generic request method. // int RestClient::request(const char* method, const char* path, @@ -123,19 +139,20 @@ int RestClient::request(const char* method, const char* path, HTTP_DEBUG_PRINT("HTTP: connect\n"); - if (fingerprint) { + if (ssl) { if(!sslClient.connect(host, port)){ HTTP_DEBUG_PRINT("HTTPS Connection failed\n"); return 0; } - 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; + 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"); @@ -180,7 +197,7 @@ int RestClient::request(const char* method, const char* path, //cleanup HTTP_DEBUG_PRINT("HTTP: stop client\n"); num_headers = 0; - if(fingerprint){ + if(ssl){ sslClient.stop(); } else { client.stop(); @@ -211,7 +228,7 @@ int RestClient::readResponse(String* response) { HTTP_DEBUG_PRINT("HTTP: RESPONSE: \n"); void* http_client; - if(fingerprint) { + if(ssl) { HTTP_DEBUG_PRINT("HTTP: Connect: " + String(sslClient.connected()) + " Available: " + String(sslClient.available()) + "\n"); while (sslClient.connected()) { HTTP_DEBUG_PRINT("."); diff --git a/RestClient.h b/RestClient.h index 8982e78..90a45e7 100644 --- a/RestClient.h +++ b/RestClient.h @@ -2,17 +2,19 @@ #include class RestClient { - + public: RestClient(const char* host); RestClient(const char* _host, int _port); - // set fingerprint if using SSL, stores the SHA1 fingerprint of the remote site + // set ssl to on but do not verify server identity with fingerprint + RestClient(const char* _host, int _port, int _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); - + //Client Setup bool dhcp(); int begin(byte*); - + //Generic HTTP Request int request(const char* method, const char* path, const char* body, String* response); @@ -20,22 +22,24 @@ 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); + // GET path int get(const char*); // GET path and response int get(const char*, String*); - + // POST path and body int post(const char* path, const char* body); // POST path and body and response int post(const char* path, const char* body, String*); - + // PUT path and body int put(const char* path, const char* body); // PUT path and body and response int put(const char* path, const char* body, String*); - + // DELETE path int del(const char*); // DELETE path and body @@ -44,7 +48,7 @@ class RestClient { int del(const char*, String*); // DELETE path and body and response int del(const char*, const char*, String*); - + private: WiFiClient client; WiFiClientSecure sslClient; @@ -56,4 +60,5 @@ class RestClient { const char* headers[10]; const char* contentType; const char* fingerprint; -}; \ No newline at end of file + int ssl; +}; From 3cb244aaebfe67d70530bed6705e075b6d1b7397 Mon Sep 17 00:00:00 2001 From: Mike Kazmier Date: Mon, 1 May 2017 10:13:31 -0600 Subject: [PATCH 04/10] wrap header in define for portability --- RestClient.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/RestClient.h b/RestClient.h index 90a45e7..064069e 100644 --- a/RestClient.h +++ b/RestClient.h @@ -1,3 +1,7 @@ +#ifndef RestClient_h +#define RestClient_h + +#include #include #include @@ -62,3 +66,5 @@ class RestClient { const char* fingerprint; int ssl; }; + +#endif From e7b696c1b19f9a2d9dd1df4a0acca694bf4859d1 Mon Sep 17 00:00:00 2001 From: cookys Date: Thu, 18 May 2017 20:54:10 +0800 Subject: [PATCH 05/10] * add patch function --- RestClient.cpp | 10 ++++++++++ RestClient.h | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/RestClient.cpp b/RestClient.cpp index cf83b1c..cc5dbd4 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -76,6 +76,16 @@ int RestClient::post(const char* path, const char* body, String* response){ return request("POST", path, body, response); } +// PATCH path and body +int RestClient::patch(const char* path, const char* body){ + return request("PATCH", path, body, NULL); +} + +// PATCH path and body with response +int RestClient::patch(const char* path, const char* body, String* response){ + return request("PATCH", path, body, response); +} + // PUT path and body int RestClient::put(const char* path, const char* body){ return request("PUT", path, body, NULL); diff --git a/RestClient.h b/RestClient.h index 064069e..176a13b 100644 --- a/RestClient.h +++ b/RestClient.h @@ -39,6 +39,11 @@ class RestClient { // POST path and body and response int post(const char* path, const char* body, String*); + // PATCH path and body + int patch(const char* path, const char* body); + // PATCH path and body and response + int patch(const char* path, const char* body, String*); + // PUT path and body int put(const char* path, const char* body); // PUT path and body and response From 4cb9e7c997c9e2d4206c603db79acf9a7b0e484f Mon Sep 17 00:00:00 2001 From: mikaelgu80 Date: Thu, 6 Jul 2017 15:27:21 +0300 Subject: [PATCH 06/10] Update RestClient.cpp --- RestClient.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index cc5dbd4..6a7a226 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -14,7 +14,9 @@ RestClient::RestClient(const char* _host){ ssl = 0; fingerprint = NULL; num_headers = 0; - contentType = "application/x-www-form-urlencoded"; // default + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } RestClient::RestClient(const char* _host, int _port){ @@ -23,7 +25,9 @@ RestClient::RestClient(const char* _host, int _port){ ssl = 0; fingerprint = NULL; num_headers = 0; - contentType = "application/x-www-form-urlencoded"; // default + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } bool RestClient::dhcp(){ @@ -44,7 +48,9 @@ RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ ssl = 1; fingerprint = _fingerprint; num_headers = 0; - contentType = "x-www-form-urlencoded"; // default + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } RestClient::RestClient(const char* _host, int _port, int _ssl) { @@ -53,7 +59,9 @@ RestClient::RestClient(const char* _host, int _port, int _ssl) { ssl = (_ssl) ? 1 : 0; fingerprint = NULL; num_headers = 0; - contentType = "x-www-form-urlencoded"; // default + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } // GET path @@ -178,7 +186,7 @@ int RestClient::request(const char* method, const char* path, for(int i=0; i Date: Thu, 6 Jul 2017 15:55:13 +0300 Subject: [PATCH 07/10] Update RestClient.cpp Fixed tabs. --- RestClient.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 6a7a226..5b20c49 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -14,8 +14,8 @@ RestClient::RestClient(const char* _host){ ssl = 0; fingerprint = NULL; num_headers = 0; - if (contentType != NULL) { - contentType = "application/x-www-form-urlencoded"; // default + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default } } @@ -25,9 +25,9 @@ RestClient::RestClient(const char* _host, int _port){ ssl = 0; fingerprint = NULL; num_headers = 0; - if (contentType != NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } bool RestClient::dhcp(){ @@ -48,9 +48,9 @@ RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ ssl = 1; fingerprint = _fingerprint; num_headers = 0; - if (contentType != NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } RestClient::RestClient(const char* _host, int _port, int _ssl) { @@ -59,9 +59,9 @@ RestClient::RestClient(const char* _host, int _port, int _ssl) { ssl = (_ssl) ? 1 : 0; fingerprint = NULL; num_headers = 0; - if (contentType != NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (contentType != NULL) { + contentType = "application/x-www-form-urlencoded"; // default + } } // GET path @@ -195,9 +195,9 @@ int RestClient::request(const char* method, const char* path, request += "Content-Type: " + String(contentType) + "\r\n"; } - if(method == "GET"){ - request += "\r\n"; - } + if(method == "GET"){ + request += "\r\n"; + } if(body != NULL){ request += String(body); From 4e2069f837328d4647b6cbab88f1adc7e0e03367 Mon Sep 17 00:00:00 2001 From: DaKaZ Date: Mon, 28 Aug 2017 07:49:49 -0600 Subject: [PATCH 08/10] Revert bug in overwriting Content-Type headers --- RestClient.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 5b20c49..46e791b 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -14,7 +14,7 @@ RestClient::RestClient(const char* _host){ ssl = 0; fingerprint = NULL; num_headers = 0; - if (contentType != NULL) { + if (contentType == NULL) { contentType = "application/x-www-form-urlencoded"; // default } } @@ -25,7 +25,7 @@ RestClient::RestClient(const char* _host, int _port){ ssl = 0; fingerprint = NULL; num_headers = 0; - if (contentType != NULL) { + if (contentType == NULL) { contentType = "application/x-www-form-urlencoded"; // default } } @@ -48,7 +48,7 @@ RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ ssl = 1; fingerprint = _fingerprint; num_headers = 0; - if (contentType != NULL) { + if (contentType == NULL) { contentType = "application/x-www-form-urlencoded"; // default } } @@ -59,7 +59,7 @@ RestClient::RestClient(const char* _host, int _port, int _ssl) { ssl = (_ssl) ? 1 : 0; fingerprint = NULL; num_headers = 0; - if (contentType != NULL) { + if (contentType == NULL) { contentType = "application/x-www-form-urlencoded"; // default } } From 6d40ad901c4cdd1f98af1606165b7a27ff3552c0 Mon Sep 17 00:00:00 2001 From: DaKaZ Date: Tue, 26 Dec 2017 08:33:21 -0700 Subject: [PATCH 09/10] Ensure blank line at end of headers Fixes issue #4 --- RestClient.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 46e791b..4d6cb5f 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -195,9 +195,7 @@ int RestClient::request(const char* method, const char* path, request += "Content-Type: " + String(contentType) + "\r\n"; } - if(method == "GET"){ - request += "\r\n"; - } + request += "\r\n"; if(body != NULL){ request += String(body); From 60c519110423c0343b787c7ab8f2000bc2881bc7 Mon Sep 17 00:00:00 2001 From: Mike Kazmier Date: Tue, 4 Dec 2018 15:53:42 -0700 Subject: [PATCH 10/10] fixes issue #8 - ensures we look for undefined/null/nullptr --- RestClient.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/RestClient.cpp b/RestClient.cpp index 4d6cb5f..a289639 100644 --- a/RestClient.cpp +++ b/RestClient.cpp @@ -14,9 +14,9 @@ RestClient::RestClient(const char* _host){ ssl = 0; fingerprint = NULL; num_headers = 0; - if (contentType == NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (!contentType) { + contentType = "application/x-www-form-urlencoded"; // default + } } RestClient::RestClient(const char* _host, int _port){ @@ -25,9 +25,9 @@ RestClient::RestClient(const char* _host, int _port){ ssl = 0; fingerprint = NULL; num_headers = 0; - if (contentType == NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (!contentType) { + contentType = "application/x-www-form-urlencoded"; // default + } } bool RestClient::dhcp(){ @@ -48,9 +48,9 @@ RestClient::RestClient(const char* _host, int _port, const char* _fingerprint){ ssl = 1; fingerprint = _fingerprint; num_headers = 0; - if (contentType == NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (!contentType) { + contentType = "application/x-www-form-urlencoded"; // default + } } RestClient::RestClient(const char* _host, int _port, int _ssl) { @@ -59,9 +59,9 @@ RestClient::RestClient(const char* _host, int _port, int _ssl) { ssl = (_ssl) ? 1 : 0; fingerprint = NULL; num_headers = 0; - if (contentType == NULL) { - contentType = "application/x-www-form-urlencoded"; // default - } + if (!contentType) { + contentType = "application/x-www-form-urlencoded"; // default + } } // GET path