From d0317db9162f600ec4eff202534fdc485adc5feb Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Tue, 30 Nov 2021 00:10:59 +0100 Subject: [PATCH] Apply formatting and some cleanup --- BridgeHttpClient.cpp | 41 ++++--- BridgeHttpClient.h | 260 +++++++++++++++++++++---------------------- library.properties | 2 +- 3 files changed, 155 insertions(+), 148 deletions(-) diff --git a/BridgeHttpClient.cpp b/BridgeHttpClient.cpp index a5be1d9..d647dd2 100644 --- a/BridgeHttpClient.cpp +++ b/BridgeHttpClient.cpp @@ -1,25 +1,32 @@ /* - Copyright (c) 2016 Imre Horvath. All rights reserved. + Copyright (c) 2016, 2021 Imre Horvath. All rights reserved. - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "BridgeHttpClient.h" +BridgeHttpClient::BridgeHttpClient() + : _isInsecureEnabled(false), + _user(NULL), + _passwd(NULL), + _extraHeaderIdx(0) { +} + void BridgeHttpClient::get(const char *url) { - request("GET", url, NULL); + request("GET", url, NULL); } void BridgeHttpClient::post(const char *url, const char *data) { @@ -35,7 +42,7 @@ void BridgeHttpClient::del(const char *url) { } void BridgeHttpClient::getAsync(const char *url) { - request("GET", url, NULL, true); + request("GET", url, NULL, true); } void BridgeHttpClient::postAsync(const char *url, const char *data) { @@ -80,7 +87,7 @@ void BridgeHttpClient::enableInsecure() { } int BridgeHttpClient::addHeader(const char *header) { - if (_extraHeaderIdx < EXTRA_HEADERS_MAX) { + if (_extraHeaderIdx < maxNumberOfExtraHeaders) { _extraHeaders[_extraHeaderIdx++] = header; return 0; } @@ -115,7 +122,7 @@ bool BridgeHttpClient::getResponseHeaderValue(const String& header, String& valu // Get the header value int startOfValue = _cachedRespHeaders.indexOf(':', startOfHeader) + 1; String respValue = _cachedRespHeaders.substring(startOfValue, - _cachedRespHeaders.indexOf('\n', startOfValue)); + _cachedRespHeaders.indexOf('\n', startOfValue)); respValue.trim(); value = respValue; diff --git a/BridgeHttpClient.h b/BridgeHttpClient.h index 1025b37..1eab25b 100644 --- a/BridgeHttpClient.h +++ b/BridgeHttpClient.h @@ -1,20 +1,20 @@ /* - Copyright (c) 2016 Imre Horvath. All rights reserved. + Copyright (c) 2016, 2021 Imre Horvath. All rights reserved. - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #ifndef _BRIDGE_HTTP_CLIENT_H_ #define _BRIDGE_HTTP_CLIENT_H_ @@ -23,123 +23,123 @@ class BridgeHttpClient : public Process { - public: - // The library supports up to 16 user addable request headers - static const size_t EXTRA_HEADERS_MAX = 16; - - BridgeHttpClient() : _isInsecureEnabled(false), - _user(NULL), _passwd(NULL), - _extraHeaderIdx(0) {} - - /* - * Synchronous methods - */ - void get(const char *url); - void post(const char *url, const char *data); - void put(const char *url, const char *data); - void del(const char *url); - - /* - * Asynchronous methods - */ - void getAsync(const char *url); - void postAsync(const char *url, const char *data); - void putAsync(const char *url, const char *data); - void delAsync(const char *url); - - /* - * Method to check, if the async request is still running or has finished. - */ - bool finished(); - - /* - * Call this method after the request has finished, - * to get the HTTP response code from the server. - * - * Returns the response code eg. 200 - */ - unsigned int getResponseCode(); - - /* - * Call this method after the request has finished, - * to get the response headers received from the server. - * - * Returns a String object with all the response headers included. - */ - String getResponseHeaders(); - - /* - * Call this method before issuing the requests, - * to allows "insecure" SSL. - * - * Useful in the following case for example: - * Certificate cannot be authenticated with known CA certificates. - */ - void enableInsecure(); - - /* - * Call this method before issuing the request, - * to add an extra header to the request. - * - * Returns 0 if the header fits into the array of headers. -1 otherwise. - */ - int addHeader(const char *header); - - /* - * Call this method before issuing the request, - * to include basic authorization into the request. - */ - void basicAuth(const char *user, const char *passwd); - - /* - * Call this method between the different request calls on the same client, - * to clear/setup the request headers for the next call. - */ - void clearHeaders(); - - /* - * Call this method between the different request calls on the same client, - * to clear the previously set basic authorization for the subsequent call. - */ - void clearAuth(); - - /* - * Call this method after the request has finished, - * to get a particular response header value. - * - * Returns true and sets the value parameter if found, - * otherwise return false and doesn't set value parameter at all. - */ - bool getResponseHeaderValue(const String& header, String& value); - - /* - * Reset client request settings. - * Clears the request headers, basic auth. settings and the insecure-enabled flag. - */ - void startRequest(); - - private: - String _tempFileName; - - bool _isInsecureEnabled; - - const char *_user; - const char *_passwd; - - const char *_extraHeaders[EXTRA_HEADERS_MAX]; - int _extraHeaderIdx; - - String _cachedRespHeaders; - - /* - * Sends the actual request via cURL on the Linux side - */ - void request(const char *verb, const char *url, const char *data, bool async=false); - - /* - * Clears the cached response headers between subsequent requests - */ - void clearCachedRespHeaders(); +public: + /* + * Constructor + */ + BridgeHttpClient(); + + /* + * Synchronous methods + */ + void get(const char *url); + void post(const char *url, const char *data); + void put(const char *url, const char *data); + void del(const char *url); + + /* + * Asynchronous methods + */ + void getAsync(const char *url); + void postAsync(const char *url, const char *data); + void putAsync(const char *url, const char *data); + void delAsync(const char *url); + + /* + * Method to check, if the async request is still running or has finished. + */ + bool finished(); + + /* + * Call this method after the request has finished, + * to get the HTTP response code from the server. + * + * Returns the response code eg. 200 + */ + unsigned int getResponseCode(); + + /* + * Call this method after the request has finished, + * to get the response headers received from the server. + * + * Returns a String object with all the response headers included. + */ + String getResponseHeaders(); + + /* + * Call this method before issuing the requests, + * to allows "insecure" SSL. + * + * Useful in the following case for example: + * Certificate cannot be authenticated with known CA certificates. + */ + void enableInsecure(); + + /* + * Call this method before issuing the request, + * to add an extra header to the request. + * + * Returns 0 if the header fits into the array of headers. -1 otherwise. + */ + int addHeader(const char *header); + + /* + * Call this method before issuing the request, + * to include basic authorization into the request. + */ + void basicAuth(const char *user, const char *passwd); + + /* + * Call this method between the different request calls on the same client, + * to clear/setup the request headers for the next call. + */ + void clearHeaders(); + + /* + * Call this method between the different request calls on the same client, + * to clear the previously set basic authorization for the subsequent call. + */ + void clearAuth(); + + /* + * Call this method after the request has finished, + * to get a particular response header value. + * + * Returns true and sets the value parameter if found, + * otherwise return false and doesn't set value parameter at all. + */ + bool getResponseHeaderValue(const String& header, String& value); + + /* + * Reset client request settings. + * Clears the request headers, basic auth. settings and the insecure-enabled flag. + */ + void startRequest(); + +private: + String _tempFileName; + + bool _isInsecureEnabled; + + const char *_user; + const char *_passwd; + + // The library supports up to 16 user addable request headers + static const unsigned maxNumberOfExtraHeaders = 16; + const char *_extraHeaders[maxNumberOfExtraHeaders]; + int _extraHeaderIdx; + + String _cachedRespHeaders; + + /* + * Sends the actual request via cURL on the Linux side + */ + void request(const char *verb, const char *url, const char *data, bool async=false); + + /* + * Clears the cached response headers between subsequent requests + */ + void clearCachedRespHeaders(); }; #endif diff --git a/library.properties b/library.properties index 3963ab3..8535d83 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=BridgeHttpClient -version=3.1.0 +version=3.1.1 author=Imre Horvath maintainer=Imre Horvath sentence=A practical and easy to use generic HTTP client library for the Yun.