-
-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HttpClientConnection class #1624
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/anakod/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* HttpClientConnection.cpp | ||
* | ||
****/ | ||
|
||
#include "HttpClientConnection.h" | ||
#include "Data/Stream/FileStream.h" | ||
|
||
HttpClientConnection::~HttpClientConnection() | ||
{ | ||
// ObjectQueue doesn't own its objects | ||
while(requestQueue.count() != 0) { | ||
delete requestQueue.dequeue(); | ||
} | ||
#ifdef ENABLE_SSL | ||
delete sslSessionId; | ||
#endif | ||
} | ||
|
||
bool HttpClientConnection::send(HttpRequest* request) | ||
{ | ||
if(!requestQueue.enqueue(request)) { | ||
// the queue is full and we cannot add more requests at the time. | ||
debug_e("The request queue is full at the moment"); | ||
delete request; | ||
return false; | ||
} | ||
|
||
bool useSsl = (request->uri.Protocol == HTTPS_URL_PROTOCOL); | ||
|
||
#ifdef ENABLE_SSL | ||
// Based on the URL decide if we should reuse the SSL and TCP pool | ||
if(useSsl) { | ||
if(sslSessionId == nullptr) { | ||
sslSessionId = new SslSessionId; | ||
} | ||
addSslOptions(request->getSslOptions()); | ||
pinCertificate(request->sslFingerprints); | ||
setSslKeyCert(request->sslKeyCertPair); | ||
} | ||
#endif | ||
|
||
return connect(request->uri.Host, request->uri.Port, useSsl); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/anakod/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* HttpClientConnection.h | ||
* | ||
****/ | ||
|
||
/** @defgroup HTTP client connection | ||
* @brief Provides HTTP/S client connection | ||
* @ingroup http | ||
* @{ | ||
*/ | ||
|
||
#ifndef _SMING_CORE_NETWORK_HTTP_HTTP_CLIENT_CONNECTION_H_ | ||
#define _SMING_CORE_NETWORK_HTTP_HTTP_CLIENT_CONNECTION_H_ | ||
|
||
#include "HttpConnection.h" | ||
|
||
class HttpClientConnection : public HttpConnection | ||
{ | ||
public: | ||
HttpClientConnection() : HttpConnection(&requestQueue) | ||
{ | ||
} | ||
|
||
virtual ~HttpClientConnection(); | ||
|
||
/** @brief Queue a request | ||
* @param request | ||
* @retval bool true on success | ||
* @note we take ownership of the request. On error, it is destroyed before returning. | ||
*/ | ||
bool send(HttpRequest* request); | ||
|
||
private: | ||
RequestQueue requestQueue; | ||
}; | ||
|
||
/** @} */ | ||
#endif /* _SMING_CORE_NETWORK_HTTP_HTTP_CLIENT_CONNECTION_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,39 +22,43 @@ | |
#include "TcpClient.h" | ||
#include "Http/HttpCommon.h" | ||
#include "Http/HttpRequest.h" | ||
#include "Http/HttpConnection.h" | ||
#include "Http/HttpClientConnection.h" | ||
|
||
class HttpClient | ||
{ | ||
public: | ||
/* High-Level Method */ | ||
/* High-Level Methods */ | ||
|
||
__forceinline bool sendRequest(const String& url, RequestCompletedDelegate requestComplete) | ||
bool sendRequest(const String& url, RequestCompletedDelegate requestComplete) | ||
{ | ||
return send(request(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete)); | ||
return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete)); | ||
} | ||
|
||
__forceinline bool sendRequest(const HttpMethod method, const String& url, const HttpHeaders& headers, | ||
RequestCompletedDelegate requestComplete) | ||
bool sendRequest(const HttpMethod method, const String& url, const HttpHeaders& headers, | ||
RequestCompletedDelegate requestComplete) | ||
{ | ||
return send(request(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete)); | ||
return send(createRequest(url)->setMethod(method)->setHeaders(headers)->onRequestComplete(requestComplete)); | ||
} | ||
|
||
__forceinline bool sendRequest(const HttpMethod method, const String& url, const HttpHeaders& headers, | ||
const String& body, RequestCompletedDelegate requestComplete) | ||
bool sendRequest(const HttpMethod method, const String& url, const HttpHeaders& headers, const String& body, | ||
RequestCompletedDelegate requestComplete) | ||
{ | ||
return send( | ||
request(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete(requestComplete)); | ||
return send(createRequest(url)->setMethod(method)->setHeaders(headers)->setBody(body)->onRequestComplete( | ||
requestComplete)); | ||
} | ||
|
||
bool downloadString(const String& url, RequestCompletedDelegate requestComplete); | ||
bool downloadString(const String& url, RequestCompletedDelegate requestComplete) | ||
{ | ||
return send(createRequest(url)->setMethod(HTTP_GET)->onRequestComplete(requestComplete)); | ||
} | ||
|
||
__forceinline bool downloadFile(const String& url, RequestCompletedDelegate requestComplete = NULL) | ||
bool downloadFile(const String& url, RequestCompletedDelegate requestComplete = nullptr) | ||
{ | ||
return downloadFile(url, "", requestComplete); | ||
return downloadFile(url, nullptr, requestComplete); | ||
} | ||
|
||
bool downloadFile(const String& url, const String& saveFileName, RequestCompletedDelegate requestComplete = NULL); | ||
bool downloadFile(const String& url, const String& saveFileName, | ||
RequestCompletedDelegate requestComplete = nullptr); | ||
|
||
/* Low Level Methods */ | ||
|
||
|
@@ -68,13 +72,15 @@ class HttpClient | |
*/ | ||
bool send(HttpRequest* request); | ||
|
||
HttpRequest* request(const String& url); | ||
HttpRequest* request(const String& url) __attribute__((deprecated)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, put here in doc comments also |
||
{ | ||
return createRequest(url); | ||
} | ||
|
||
#ifdef ENABLE_SSL | ||
static void freeSslSessionPool(); | ||
#endif | ||
static void freeHttpConnectionPool(); | ||
static void freeRequestQueue(); | ||
HttpRequest* createRequest(const String& url) | ||
{ | ||
return new HttpRequest(URL(url)); | ||
} | ||
|
||
/** | ||
* Use this method to clean all request queues and object pools | ||
|
@@ -87,12 +93,7 @@ class HttpClient | |
String getCacheKey(URL url); | ||
|
||
protected: | ||
static HashMap<String, HttpConnection*> httpConnectionPool; | ||
static HashMap<String, RequestQueue*> queue; | ||
|
||
#ifdef ENABLE_SSL | ||
static HashMap<String, SslSessionId*> sslSessionIdPool; | ||
#endif | ||
static HashMap<String, HttpClientConnection*> httpConnectionPool; | ||
}; | ||
|
||
/** @} */ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the FileStream include needed here?