summaryrefslogtreecommitdiff
path: root/protocols/CloudFile
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2024-01-05 15:54:03 +0300
committerGeorge Hazan <george.hazan@gmail.com>2024-01-05 15:54:03 +0300
commit14c4e44a0a91e1ad701d4ae3c58185d25118e64e (patch)
tree50f36035466f355c74373e757bc00b6610ce6267 /protocols/CloudFile
parent94667140aeb3886d22e4c1301423fe99aaf3fba4 (diff)
Netlib:
- NETLIBHTTPHEADER & NETLIBHTTPREQUEST obsoleted; - NETLIBHTTPREQUEST divided into MHttpRequest & MHttpResponse; - MHttpHeaders now manager headers both for MHttpRequest & MHttpResponse;
Diffstat (limited to 'protocols/CloudFile')
-rw-r--r--protocols/CloudFile/src/Services/dropbox_service.cpp4
-rw-r--r--protocols/CloudFile/src/Services/google_service.cpp8
-rw-r--r--protocols/CloudFile/src/Services/microsoft_service.cpp6
-rw-r--r--protocols/CloudFile/src/Services/yandex_service.cpp6
-rw-r--r--protocols/CloudFile/src/cloud_file.cpp12
-rw-r--r--protocols/CloudFile/src/cloud_file.h6
-rw-r--r--protocols/CloudFile/src/http_request.h82
7 files changed, 27 insertions, 97 deletions
diff --git a/protocols/CloudFile/src/Services/dropbox_service.cpp b/protocols/CloudFile/src/Services/dropbox_service.cpp
index 805a7f2800..92b00fc493 100644
--- a/protocols/CloudFile/src/Services/dropbox_service.cpp
+++ b/protocols/CloudFile/src/Services/dropbox_service.cpp
@@ -85,7 +85,7 @@ void CDropboxService::RequestAccessTokenThread(void *param)
return;
}
- JSONNode root = JSONNode::parse(response->pData);
+ JSONNode root = JSONNode::parse(response->body);
if (root.empty()) {
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode));
ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR);
@@ -206,7 +206,7 @@ auto CDropboxService::CreateSharedLink(const std::string &path)
if (!response || response->resultCode != HTTP_CODE_CONFLICT)
HttpResponseToError(response);
- JSONNode root = JSONNode::parse(response->pData);
+ JSONNode root = JSONNode::parse(response->body);
if (root.isnull())
throw Exception(HttpStatusToError());
diff --git a/protocols/CloudFile/src/Services/google_service.cpp b/protocols/CloudFile/src/Services/google_service.cpp
index 58b32eaa7f..fea2c3b2b4 100644
--- a/protocols/CloudFile/src/Services/google_service.cpp
+++ b/protocols/CloudFile/src/Services/google_service.cpp
@@ -100,8 +100,8 @@ void CGDriveService::RequestAccessTokenThread(void *param)
NLHR_PTR response(request.Send(m_hConnection));
if (response == nullptr || response->resultCode != HTTP_CODE_OK) {
- const char *error = response && response->dataLength
- ? response->pData
+ const char *error = response && response->body.GetLength()
+ ? response->body
: HttpStatusToError(response ? response->resultCode : 0);
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), error);
@@ -110,7 +110,7 @@ void CGDriveService::RequestAccessTokenThread(void *param)
return;
}
- JSONNode root = JSONNode::parse(response->pData);
+ JSONNode root = JSONNode::parse(response->body);
if (root.empty()) {
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode));
ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR);
@@ -180,7 +180,7 @@ auto CGDriveService::CreateUploadSession(const std::string &parentId, const std:
HandleHttpError(response);
if (HTTP_CODE_SUCCESS(response->resultCode))
- if (auto *pszHdr = Netlib_GetHeader(response, "Location"))
+ if (auto *pszHdr = response->FindHeader("Location"))
return std::string(pszHdr);
HttpResponseToError(response);
diff --git a/protocols/CloudFile/src/Services/microsoft_service.cpp b/protocols/CloudFile/src/Services/microsoft_service.cpp
index 8d63f4a23f..17115235cd 100644
--- a/protocols/CloudFile/src/Services/microsoft_service.cpp
+++ b/protocols/CloudFile/src/Services/microsoft_service.cpp
@@ -100,8 +100,8 @@ void COneDriveService::RequestAccessTokenThread(void *param)
NLHR_PTR response(request.Send(m_hConnection));
if (response == nullptr || response->resultCode != HTTP_CODE_OK) {
- const char *error = response->dataLength
- ? response->pData
+ const char *error = response->body.GetLength()
+ ? response->body
: HttpStatusToError(response->resultCode);
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), error);
@@ -110,7 +110,7 @@ void COneDriveService::RequestAccessTokenThread(void *param)
return;
}
- JSONNode root = JSONNode::parse(response->pData);
+ JSONNode root = JSONNode::parse(response->body);
if (root.empty()) {
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode));
ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR);
diff --git a/protocols/CloudFile/src/Services/yandex_service.cpp b/protocols/CloudFile/src/Services/yandex_service.cpp
index f21e9ef2e0..892821e0c4 100644
--- a/protocols/CloudFile/src/Services/yandex_service.cpp
+++ b/protocols/CloudFile/src/Services/yandex_service.cpp
@@ -102,8 +102,8 @@ void CYandexService::RequestAccessTokenThread(void *param)
NLHR_PTR response(request.Send(m_hConnection));
if (response == nullptr || response->resultCode != HTTP_CODE_OK) {
- const char *error = response->dataLength
- ? response->pData
+ const char *error = response->body.GetLength()
+ ? response->body
: HttpStatusToError(response->resultCode);
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), error);
@@ -112,7 +112,7 @@ void CYandexService::RequestAccessTokenThread(void *param)
return;
}
- JSONNode root = JSONNode::parse(response->pData);
+ JSONNode root = JSONNode::parse(response->body);
if (root.empty()) {
Netlib_Logf(m_hConnection, "%s: %s", GetAccountName(), HttpStatusToError(response->resultCode));
ShowNotification(TranslateT("Server does not respond"), MB_ICONERROR);
diff --git a/protocols/CloudFile/src/cloud_file.cpp b/protocols/CloudFile/src/cloud_file.cpp
index 9f66caaf82..772b42ae36 100644
--- a/protocols/CloudFile/src/cloud_file.cpp
+++ b/protocols/CloudFile/src/cloud_file.cpp
@@ -129,16 +129,16 @@ char* CCloudService::HttpStatusToError(int status)
return "Unknown error";
}
-void CCloudService::HttpResponseToError(NETLIBHTTPREQUEST *response)
+void CCloudService::HttpResponseToError(MHttpResponse *response)
{
if (response == nullptr)
throw Exception(HttpStatusToError());
- if (response->dataLength)
- throw Exception(response->pData);
+ if (response->body.GetLength())
+ throw Exception(response->body.c_str());
throw Exception(HttpStatusToError(response->resultCode));
}
-void CCloudService::HandleHttpError(NETLIBHTTPREQUEST *response)
+void CCloudService::HandleHttpError(MHttpResponse *response)
{
if (response == nullptr)
throw Exception(HttpStatusToError());
@@ -152,11 +152,11 @@ void CCloudService::HandleHttpError(NETLIBHTTPREQUEST *response)
HttpResponseToError(response);
}
-JSONNode CCloudService::GetJsonResponse(NETLIBHTTPREQUEST *response)
+JSONNode CCloudService::GetJsonResponse(MHttpResponse *response)
{
HandleHttpError(response);
- JSONNode root = JSONNode::parse(response->pData);
+ JSONNode root = JSONNode::parse(response->body);
if (root.isnull())
throw Exception(HttpStatusToError());
diff --git a/protocols/CloudFile/src/cloud_file.h b/protocols/CloudFile/src/cloud_file.h
index de053af431..4a5b91da72 100644
--- a/protocols/CloudFile/src/cloud_file.h
+++ b/protocols/CloudFile/src/cloud_file.h
@@ -18,15 +18,15 @@ protected:
std::string PreparePath(const std::string &path) const;
virtual char* HttpStatusToError(int status = 0);
- virtual void HttpResponseToError(NETLIBHTTPREQUEST *response);
- virtual void HandleHttpError(NETLIBHTTPREQUEST *response);
+ virtual void HttpResponseToError(MHttpResponse *response);
+ virtual void HandleHttpError(MHttpResponse *response);
virtual void HandleJsonError(JSONNode &node) = 0;
// events
void OnModulesLoaded() override;
MWindow OnCreateAccMgrUI(MWindow) override;
- JSONNode GetJsonResponse(NETLIBHTTPREQUEST *response);
+ JSONNode GetJsonResponse(MHttpResponse *response);
virtual void Upload(FileTransferParam *ftp) = 0;
diff --git a/protocols/CloudFile/src/http_request.h b/protocols/CloudFile/src/http_request.h
index 68be404921..214ecea761 100644
--- a/protocols/CloudFile/src/http_request.h
+++ b/protocols/CloudFile/src/http_request.h
@@ -17,7 +17,7 @@ public:
}
};
-class HttpRequest : protected NETLIBHTTPREQUEST
+class HttpRequest : public MHttpRequest
{
private:
CMStringA m_szUrl;
@@ -26,28 +26,11 @@ private:
{
requestType = type;
flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMP;
- szUrl = NULL;
- headers = NULL;
- headersCount = 0;
- pData = NULL;
- dataLength = 0;
- resultCode = 0;
- szResultDescr = NULL;
nlc = NULL;
timeout = 0;
}
protected:
- enum HttpRequestUrlFormat { FORMAT };
-
- void AddHeader(LPCSTR szName, LPCSTR szValue)
- {
- headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER) * (headersCount + 1));
- headers[headersCount].szName = mir_strdup(szName);
- headers[headersCount].szValue = mir_strdup(szValue);
- headersCount++;
- }
-
void AddBasicAuthHeader(LPCSTR szLogin, LPCSTR szPassword)
{
size_t length = mir_strlen(szLogin) + mir_strlen(szPassword) + 1;
@@ -60,47 +43,17 @@ protected:
szPassword);
ptrA ePair(mir_base64_encode(cPair, length));
-
- length = mir_strlen(ePair) + 7;
- char *value = (char*)mir_calloc(length + 1);
- mir_snprintf(value, length, "Basic %s", ePair.get());
-
- headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1));
- headers[headersCount].szName = mir_strdup("Authorization");
- headers[headersCount].szValue = value;
- headersCount++;
+ AddHeader("Authorization", CMStringA(FORMAT, "Basic %s", ePair.get()));
}
void AddBearerAuthHeader(LPCSTR szValue)
{
- size_t length = mir_strlen(szValue) + 8;
- char *value = (char*)mir_calloc(length + 1);
- mir_snprintf(
- value,
- length,
- "Bearer %s",
- szValue);
-
- headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1));
- headers[headersCount].szName = mir_strdup("Authorization");
- headers[headersCount].szValue = value;
- headersCount++;
+ AddHeader("Authorization", CMStringA(FORMAT, "Bearer %s", szValue));
}
void AddOAuthHeader(LPCSTR szValue)
{
- size_t length = mir_strlen(szValue) + 7;
- char *value = (char*)mir_calloc(length + 1);
- mir_snprintf(
- value,
- length,
- "OAuth %s",
- szValue);
-
- headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1));
- headers[headersCount].szName = mir_strdup("Authorization");
- headers[headersCount].szValue = value;
- headersCount++;
+ AddHeader("Authorization", CMStringA(FORMAT, "OAuth %s", szValue));
}
void AddUrlParameter(const char *urlFormat, ...)
@@ -124,16 +77,6 @@ protected:
va_end(valueArgs);
}
- void SetData(const char *data, size_t size)
- {
- if (pData != NULL)
- mir_free(pData);
-
- dataLength = (int)size;
- pData = (char*)mir_alloc(size);
- memcpy(pData, data, size);
- }
-
public:
HttpRequest(int type, LPCSTR url)
{
@@ -142,7 +85,7 @@ public:
m_szUrl = url;
}
- HttpRequest(int type, HttpRequestUrlFormat, LPCSTR urlFormat, ...)
+ HttpRequest(int type, CMStringDataFormat, LPCSTR urlFormat, ...)
{
Init(type);
@@ -152,22 +95,9 @@ public:
va_end(formatArgs);
}
- ~HttpRequest()
- {
- for (int i = 0; i < headersCount; i++)
- {
- mir_free(headers[i].szName);
- mir_free(headers[i].szValue);
- }
- mir_free(headers);
- if (pData)
- mir_free(pData);
- }
-
- NETLIBHTTPREQUEST* Send(HNETLIBUSER hConnection)
+ MHttpResponse* Send(HNETLIBUSER hConnection)
{
m_szUrl.Replace('\\', '/');
- szUrl = m_szUrl.GetBuffer();
return Netlib_HttpTransaction(hConnection, this);
}
};