diff options
author | George Hazan <george.hazan@gmail.com> | 2024-01-05 15:54:03 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2024-01-05 15:54:03 +0300 |
commit | 14c4e44a0a91e1ad701d4ae3c58185d25118e64e (patch) | |
tree | 50f36035466f355c74373e757bc00b6610ce6267 /plugins/SendScreenshotPlus/src | |
parent | 94667140aeb3886d22e4c1301423fe99aaf3fba4 (diff) |
Netlib:
- NETLIBHTTPHEADER & NETLIBHTTPREQUEST obsoleted;
- NETLIBHTTPREQUEST divided into MHttpRequest & MHttpResponse;
- MHttpHeaders now manager headers both for MHttpRequest & MHttpResponse;
Diffstat (limited to 'plugins/SendScreenshotPlus/src')
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSend.cpp | 175 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSend.h | 3 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp | 12 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h | 2 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp | 11 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_imgur.h | 2 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp | 12 | ||||
-rw-r--r-- | plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h | 2 |
8 files changed, 74 insertions, 145 deletions
diff --git a/plugins/SendScreenshotPlus/src/CSend.cpp b/plugins/SendScreenshotPlus/src/CSend.cpp index 9290e8d923..23f2c19c5c 100644 --- a/plugins/SendScreenshotPlus/src/CSend.cpp +++ b/plugins/SendScreenshotPlus/src/CSend.cpp @@ -422,36 +422,10 @@ const char* CSend::GetHTMLContent(char* str, const char* startTag, const char* e return begin;
}
-static void HTTPFormAppendData(NETLIBHTTPREQUEST* nlhr, size_t* dataMax, char** dataPos, const char* data, size_t len)
-{
- nlhr->dataLength = (*dataPos - nlhr->pData);
- if (nlhr->dataLength + len >= *dataMax) {
- *dataPos = nlhr->pData;
- *dataMax += 0x1000 + 0x1000 * (len >> 12);
- nlhr->pData = (char*)mir_realloc(nlhr->pData, *dataMax);
- if (!nlhr->pData) mir_free(*dataPos);
- *dataPos = nlhr->pData;
- if (!*dataPos)
- return;
- *dataPos += nlhr->dataLength;
- }
- if (data) {
- memcpy(*dataPos, data, sizeof(char)*len); *dataPos += len;
- nlhr->dataLength += (int)len; // not necessary
- }
-}
-
-void CSend::HTTPFormDestroy(NETLIBHTTPREQUEST* nlhr)
-{
- mir_free(nlhr->headers[0].szValue), nlhr->headers[0].szValue = nullptr;
- mir_free(nlhr->headers), nlhr->headers = nullptr;
- mir_free(nlhr->pData), nlhr->pData = nullptr;
-}
-
-int CSend::HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum)
+int CSend::HTTPFormCreate(MHttpRequest* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum)
{
char boundary[16];
- memcpy(boundary, "--M461C/", 8);
+ strcpy(boundary, "--M461C/");
{
union
{
@@ -467,57 +441,40 @@ int CSend::HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* }
nlhr->requestType = requestType;
nlhr->flags = NLHRF_HTTP11;
- if (!strncmp(url, "https://", 8)) nlhr->flags |= NLHRF_SSL;
- nlhr->szUrl = (char*)url;
- nlhr->headersCount = 3;
+ if (!strncmp(url, "https://", 8))
+ nlhr->flags |= NLHRF_SSL;
+ nlhr->m_szUrl = url;
+ nlhr->AddHeader("Content-Type", CMStringA("multipart/form-data; boundary=") + boundary);
+ nlhr->AddHeader("User-Agent", __USER_AGENT_STRING);
+ nlhr->AddHeader("Accept-Language", "en-us,en;q=0.8");
for (HTTPFormData* iter = frm, *end = frm + frmNum; iter != end; ++iter) {
- if (!(iter->flags&HTTPFF_HEADER)) break;
- ++nlhr->headersCount;
- }
- nlhr->headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*nlhr->headersCount);
- {
- char* contenttype = (char*)mir_alloc(sizeof(char)*(30 + 1 + sizeof(boundary)));
- memcpy(contenttype, "multipart/form-data; boundary=", 30);
- memcpy(contenttype + 30, boundary, sizeof(boundary));
- contenttype[30 + sizeof(boundary)] = '\0';
- nlhr->headers[0].szName = "Content-Type";
- nlhr->headers[0].szValue = contenttype;
- nlhr->headers[1].szName = "User-Agent";
- nlhr->headers[1].szValue = __USER_AGENT_STRING;
- nlhr->headers[2].szName = "Accept-Language";
- nlhr->headers[2].szValue = "en-us,en;q=0.8";
- int i = 3;
- for (HTTPFormData* iter = frm, *end = frm + frmNum; iter != end; ++iter) {
- if (!(iter->flags&HTTPFF_HEADER)) break;
- nlhr->headers[i].szName = (char*)iter->name;
- nlhr->headers[i++].szValue = (char*)iter->value_str;
- }
+ if (!(iter->flags & HTTPFF_HEADER))
+ break;
+ nlhr->AddHeader(iter->name, iter->value_str);
}
- char* dataPos = nlhr->pData;
- size_t dataMax = 0;
- for (HTTPFormData* iter = frm, *end = frm + frmNum; iter != end; ++iter) {
- if (iter->flags&HTTPFF_HEADER) continue;
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, 2 + sizeof(boundary) + 40);
- memset(dataPos, '-', 2); dataPos += 2;
- memcpy(dataPos, boundary, sizeof(boundary)); dataPos += sizeof(boundary);
- memcpy(dataPos, "\r\nContent-Disposition: form-data; name=\"", 40); dataPos += 40;
- size_t namelen = mir_strlen(iter->name), valuelen = 0;
- if (!(iter->flags&HTTPFF_INT))
- valuelen = mir_strlen(iter->value_str);
- if (iter->flags&HTTPFF_FILE) {
- const char* filename = strrchr(iter->value_str, '\\');
+
+ auto &str = nlhr->m_szParam;
+ for (HTTPFormData *iter = frm, *end = frm + frmNum; iter != end; ++iter) {
+ if (iter->flags & HTTPFF_HEADER)
+ continue;
+
+ str.AppendFormat("--%s", boundary);
+ str.Append("\r\nContent-Disposition: form-data; name=\"");
+
+ if (iter->flags & HTTPFF_FILE) {
+ const char *filename = strrchr(iter->value_str, '\\');
if (!filename) filename = strrchr(iter->value_str, '/');
if (!filename) filename = iter->value_str;
else ++filename;
- valuelen = mir_strlen(filename);
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 13 + valuelen + 17);
- memcpy(dataPos, iter->name, namelen); dataPos += namelen;
- memcpy(dataPos, "\"; filename=\"", 13); dataPos += 13;
- memcpy(dataPos, filename, valuelen); dataPos += valuelen;
- memcpy(dataPos, "\"\r\nContent-Type: ", 17); dataPos += 17;
+
+ str.Append(iter->name);
+ str.Append("\"; filename=\"");
+ str.Append(filename);
+ str.Append("\"\r\nContent-Type: ");
+
/// add mime type
- const char* mime = "application/octet-stream";
- const char* fileext = strrchr(filename, '.');
+ const char *mime = "application/octet-stream";
+ const char *fileext = strrchr(filename, '.');
if (fileext) {
if (!mir_strcmp(fileext, ".jpg") || !mir_strcmp(fileext, ".jpeg") || !mir_strcmp(fileext, ".jpe"))
mime = "image/jpeg";
@@ -530,71 +487,49 @@ int CSend::HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* else if (!mir_strcmp(fileext, ".tif") || !mir_strcmp(fileext, ".tiff"))
mime = "image/tiff";
}
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, mime, mir_strlen(mime));
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, "\r\n\r\n", 4);
+ str.Append(mime);
+ str.Append("\r\n\r\n");
+
/// add file content
size_t filesize = 0;
- FILE* fp = fopen(iter->value_str, "rb");
+ FILE *fp = fopen(iter->value_str, "rb");
if (fp) {
fseek(fp, 0, SEEK_END);
filesize = ftell(fp); fseek(fp, 0, SEEK_SET);
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, filesize + 2);
- if (fread(dataPos, 1, filesize, fp) != filesize) {
+ ptrA buf((char *)mir_alloc(filesize));
+ if (fread(buf, 1, filesize, fp) != filesize) {
+ str.Append(buf, filesize);
fclose(fp), fp = nullptr;
}
}
if (!fp) {
- HTTPFormDestroy(nlhr);
Error(L"Error occurred when opening local file.\nAborting file upload...");
Exit(ACKRESULT_FAILED);
return 1;
}
- else
- fclose(fp);
- dataPos += filesize;
- memcpy(dataPos, "\r\n", 2); dataPos += 2;
+ fclose(fp);
+ str.Append("\r\n");
}
- else if (iter->flags&HTTPFF_8BIT) {
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 38 + valuelen + 2);
- memcpy(dataPos, iter->name, namelen); dataPos += namelen;
- memcpy(dataPos, "\"\r\nContent-Transfer-Encoding: 8bit\r\n\r\n", 38); dataPos += 38;
- memcpy(dataPos, iter->value_str, valuelen); dataPos += valuelen;
- memcpy(dataPos, "\r\n", 2); dataPos += 2;
+ else if (iter->flags & HTTPFF_8BIT) {
+ str.Append(iter->name);
+ str.Append("\"\r\nContent-Transfer-Encoding: 8bit\r\n\r\n");
+ str.Append(iter->value_str);
+ str.Append("\r\n");
}
- else if (iter->flags&HTTPFF_INT) {
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 5 + 17/*max numbers*/ + 2);
- memcpy(dataPos, iter->name, namelen); dataPos += namelen;
- memcpy(dataPos, "\"\r\n\r\n", 5); dataPos += 5;
- int ret = snprintf(dataPos, 17, "%Id", iter->value_int);
- if (ret < 17 && ret>0) dataPos += ret;
- memcpy(dataPos, "\r\n", 2); dataPos += 2;
+ else if (iter->flags & HTTPFF_INT) {
+ str.Append(iter->name);
+ str.Append("\"\r\n\r\n");
+ str.AppendFormat("%Id", iter->value_int);
+ str.Append("\r\n");
}
else {
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, namelen + 5 + valuelen + 2);
- memcpy(dataPos, iter->name, namelen); dataPos += namelen;
- memcpy(dataPos, "\"\r\n\r\n", 5); dataPos += 5;
- memcpy(dataPos, iter->value_str, valuelen); dataPos += valuelen;
- memcpy(dataPos, "\r\n", 2); dataPos += 2;
+ str.Append(iter->name);
+ str.Append("\"\r\n\r\n");
+ str.Append(iter->value_str);
+ str.Append("\r\n");
}
}
- HTTPFormAppendData(nlhr, &dataMax, &dataPos, nullptr, 2 + sizeof(boundary) + 4);
- memset(dataPos, '-', 2); dataPos += 2;
- memcpy(dataPos, boundary, sizeof(boundary)); dataPos += sizeof(boundary);
- memcpy(dataPos, "--\r\n", 4); dataPos += 4;
- nlhr->dataLength = dataPos - nlhr->pData;
-#ifdef _DEBUG /// print request content to "_sendss_tmp" file for debugging
- {
- FILE* fp = fopen("_sendss_tmp", "wb");
- if (fp) {
- fprintf(fp, "--Target-- %s\n", nlhr->szUrl);
- for (int i = 0; i < nlhr->headersCount; ++i) {
- fprintf(fp, "%s: %s\n", nlhr->headers[i].szName, nlhr->headers[i].szValue);
- }
- fprintf(fp, "\n\n");
- fwrite(nlhr->pData, 1, nlhr->dataLength, fp);
- fclose(fp);
- }
- }
-#endif // _DEBUG
+
+ str.AppendFormat("--%s--\r\n", boundary);
return 0;
}
diff --git a/plugins/SendScreenshotPlus/src/CSend.h b/plugins/SendScreenshotPlus/src/CSend.h index 9301811f17..c694167eae 100644 --- a/plugins/SendScreenshotPlus/src/CSend.h +++ b/plugins/SendScreenshotPlus/src/CSend.h @@ -130,8 +130,7 @@ protected: };
static const char* GetHTMLContent(char* str, const char* startTag, const char* endTag); // changes "str", can be successfully used only once
- void HTTPFormDestroy(NETLIBHTTPREQUEST* nlhr); // use to free data inside "nlhr" created by HTTPFormCreate
- int HTTPFormCreate(NETLIBHTTPREQUEST* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum); // returns "0" on success, Exit() will be called on failure (stop processing)
+ int HTTPFormCreate(MHttpRequest* nlhr, int requestType, const char* url, HTTPFormData* frm, size_t frmNum); // returns "0" on success, Exit() will be called on failure (stop processing)
};
#endif
diff --git a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp index 5a2a4d8a34..6b604c33ba 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.cpp @@ -76,12 +76,10 @@ void CSendHost_ImageShack::SendThread() {
// send DATA and wait for m_nlreply
NLHR_PTR reply(Netlib_HttpTransaction(g_hNetlibUser, &m_nlhr));
- HTTPFormDestroy(&m_nlhr);
if (reply) {
- if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->dataLength) {
- reply->pData[reply->dataLength - 1] = '\0'; // make sure its null terminated
+ if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->body.GetLength()) {
const char* url = nullptr;
- url = GetHTMLContent(reply->pData, "<image_link>", "</image_link>");
+ url = GetHTMLContent(reply->body.GetBuffer(), "<image_link>", "</image_link>");
if (url && *url) {
m_URLthumb = m_URL = url;
@@ -95,19 +93,19 @@ void CSendHost_ImageShack::SendThread() return;
}
- url = GetHTMLContent(reply->pData, "<error ", "</error>");
+ url = GetHTMLContent(reply->body.GetBuffer(), "<error ", "</error>");
wchar_t* err = nullptr;
if (url) err = mir_a2u(url);
if (!err || !*err) { // fallback to server response mess
mir_free(err);
- err = mir_a2u(reply->pData);
+ err = mir_a2u(reply->body);
}
Error(L"%s", err);
mir_free(err);
}
else Error(SS_ERR_RESPONSE, m_pszSendTyp, reply->resultCode);
}
- else Error(SS_ERR_NORESPONSE, m_pszSendTyp, m_nlhr.resultCode);
+ else Error(SS_ERR_NORESPONSE, m_pszSendTyp, 500);
Exit(ACKRESULT_FAILED);
}
diff --git a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h index 7c58094aac..5197e8718a 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h +++ b/plugins/SendScreenshotPlus/src/CSendHost_ImageShack.h @@ -40,7 +40,7 @@ public: int Send() override;
protected:
- NETLIBHTTPREQUEST m_nlhr;
+ MHttpRequest m_nlhr;
void SendThread();
static void SendThreadWrapper(void * Obj);
diff --git a/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp b/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp index bde9f158bb..c4bb3897f7 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_imgur.cpp @@ -15,8 +15,8 @@ */
#include "stdafx.h"
-CSendHost_Imgur::CSendHost_Imgur(HWND Owner, MCONTACT hContact, bool bAsync)
- : CSend(Owner, hContact, bAsync)
+CSendHost_Imgur::CSendHost_Imgur(HWND Owner, MCONTACT hContact, bool bAsync) :
+ CSend(Owner, hContact, bAsync)
{
m_EnableItem = SS_DLG_DESCRIPTION | SS_DLG_AUTOSEND | SS_DLG_DELETEAFTERSSEND;
m_pszSendTyp = LPGENW("Image upload");
@@ -60,10 +60,9 @@ void CSendHost_Imgur::SendThread(void* obj) CSendHost_Imgur *self = (CSendHost_Imgur*)obj;
// send DATA and wait for m_nlreply
NLHR_PTR reply(Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr));
- self->HTTPFormDestroy(&self->m_nlhr);
if (reply) {
- if (reply->dataLength) {
- JSONROOT root(reply->pData);
+ if (reply->body.GetLength()) {
+ JSONROOT root(reply->body);
if (root) {
if ((*root)["success"].as_bool()) {
self->m_URL = (*root)["data"]["link"].as_mstring();
@@ -80,7 +79,7 @@ void CSendHost_Imgur::SendThread(void* obj) }
else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode);
}
- else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, self->m_nlhr.resultCode);
+ else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, 500);
self->Exit(ACKRESULT_FAILED);
}
diff --git a/plugins/SendScreenshotPlus/src/CSendHost_imgur.h b/plugins/SendScreenshotPlus/src/CSendHost_imgur.h index 544afecd53..fee457d00a 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_imgur.h +++ b/plugins/SendScreenshotPlus/src/CSendHost_imgur.h @@ -24,7 +24,7 @@ class CSendHost_Imgur : public CSend { int Send() override;
protected:
- NETLIBHTTPREQUEST m_nlhr;
+ MHttpRequest m_nlhr;
static void SendThread(void* obj);
};
#endif
diff --git a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp index 58651ae85d..4fcf8f7d8b 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp +++ b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp @@ -65,11 +65,9 @@ void CSendHost_UploadPie::SendThread(void* obj) CSendHost_UploadPie* self = (CSendHost_UploadPie*)obj;
// send DATA and wait for m_nlreply
NLHR_PTR reply(Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr));
- self->HTTPFormDestroy(&self->m_nlhr);
if (reply) {
- if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->dataLength) {
- reply->pData[reply->dataLength - 1] = '\0'; // make sure its null terminated
- char* url = reply->pData;
+ if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->body.GetLength()) {
+ char* url = reply->body.GetBuffer();
do {
char* pos;
if ((url = strstr(url, kHostURL))) {
@@ -89,17 +87,17 @@ void CSendHost_UploadPie::SendThread(void* obj) self->svcSendMsgExit(url); return;
}
else { // check error mess from server
- const char* err = GetHTMLContent(reply->pData, "<p id=\"error\"", "</p>");
+ const char* err = GetHTMLContent(reply->body.GetBuffer(), "<p id=\"error\"", "</p>");
wchar_t* werr;
if (err) werr = mir_a2u(err);
- else werr = mir_a2u(reply->pData);
+ else werr = mir_a2u(reply->body);
self->Error(L"%s", werr);
mir_free(werr);
}
}
else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode);
}
- else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, self->m_nlhr.resultCode);
+ else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, 500);
self->Exit(ACKRESULT_FAILED);
}
diff --git a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h index 94cfdf1fbc..8a4abd350f 100644 --- a/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h +++ b/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.h @@ -24,7 +24,7 @@ class CSendHost_UploadPie : public CSend { protected:
int m_expire;
- NETLIBHTTPREQUEST m_nlhr;
+ MHttpRequest m_nlhr;
static void SendThread(void* obj);
};
#endif
|