Skip to content

Commit ca1e049

Browse files
committed
Fixed hosting getapic.me, fixed memory leaks
Fixed file handle leak in mmap function Cleanup XdgMime library data on exit Servers Checker: Fixed mime content type check in
1 parent b0e0a35 commit ca1e049

File tree

13 files changed

+63
-20
lines changed

13 files changed

+63
-20
lines changed

Data/Scripts/dosya.nut

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ function UploadFile(FileName, options) {
6666
local action = form.attr("action");
6767
nm.setUrl(action);
6868
form.find("textarea").each(function(index, elem) {
69-
nm.addQueryParam(elem.attr("name"), elem.text());
69+
nm.addQueryParam(elem.attr("name"), strip(elem.text()));
7070
});
7171
nm.doPost("");
7272

7373
if (nm.responseCode() == 200) {
7474
local doc3 = Document(nm.responseBody());
75-
local link = doc3.find("textarea#ic0-").text();
75+
local link = strip(doc3.find("textarea#ic0-").text());
7676
options.setViewUrl(link);
7777
if (link != "") {
7878
return 1;

Data/servers.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,9 @@
402402
<Call Function="json" Arg1="result.success" AssignVars="Success:0;"/>
403403
<RegExp Data="$(Success)" Pattern="True" AssignVars=""/>
404404
</Action>
405-
<Action Type="get" Url="https://getapic.me/" RegExp="session\]'\)\.val\('(.+?)'" AssignVars="Session:0" OnlyOnce="0">
406-
<RegExp Pattern="suid\]'\)\.val\('(.+?)'" AssignVars="Suid:0"/>
405+
<Action Type="get" Url="https://getapic.me/" CustomHeaders="X-Requested-With:XMLHttpRequest">
406+
<Call Function="json" Arg1="result.data.suid" AssignVars="Suid:0;"/>
407+
<Call Function="json" Arg1="result.data.session" AssignVars="Session:0;"/>
407408
</Action>
408409
<Action Type="upload" Url="https://getapic.me/upload" CustomHeaders="X-Requested-With:XMLHttpRequest" Referer="https://getapic.me/"
409410
PostParams="getpreviewsize=$(_THUMBWIDTH);getpreviewalt=;getreduceimage=320;needreduce=0;upload_quality=100;upload_angle=0;upload_resizeside=width;gettypeofdownload=N;session=$(Session);suid=$(Suid);file[]=%filename%;"

Source/Core/3rdpart/xdgmime/ports/mman.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,19 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
8484
if (flags & MAP_PRIVATE)
8585
dwDesiredAccess |= FILE_MAP_COPY;
8686
void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);
87+
88+
// Mapped views of a file mapping object maintain internal references to the object, and a
89+
// file mapping object does not close until all references to it are released. Therefore,
90+
// to fully close a file mapping object, an application must unmap all mapped views of the
91+
// file mapping object by calling UnmapViewOfFile and close the file mapping object handle
92+
// by calling CloseHandle. These functions can be called in any order.
93+
//
94+
// Although an application may close the file handle used to create a file mapping object,
95+
// the system holds the corresponding file open until the last view of the file is unmapped.
96+
// Files for which the last view has not yet been unmapped are held open with no sharing
97+
// restrictions.
98+
CloseHandle(h);
8799
if (ret == NULL) {
88-
CloseHandle(h);
89100
ret = MAP_FAILED;
90101
}
91102
return ret;
@@ -94,8 +105,7 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
94105
static void munmap(void *addr, size_t length)
95106
{
96107
UnmapViewOfFile(addr);
97-
/* ruh-ro, we leaked handle from CreateFileMapping() ... */
98108
}
99109

100110
#undef DWORD_HI
101-
#undef DWORD_LO
111+
#undef DWORD_LO

Source/Core/Network/INetworkClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class INetworkClient {
102102
virtual std::string urlDecode(const std::string& str){ return std::string(); }
103103
virtual void setMaxUploadSpeed(uint64_t speed){}
104104
virtual void setMaxDownloadSpeed(uint64_t speed) {}
105+
virtual void cleanupAfter() {}
105106
};
106107

107108
class INetworkClientFactory {
@@ -110,4 +111,4 @@ class INetworkClientFactory {
110111
virtual ~INetworkClientFactory() = default;
111112
};
112113

113-
#endif
114+
#endif

Source/Core/Network/NetworkClient.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ bool NetworkClient::doUploadMultipartData()
468468
bool NetworkClient::private_on_finish_request()
469469
{
470470
private_checkResponse();
471-
private_cleanup_after();
471+
cleanupAfter();
472472
private_parse_headers();
473473
if (curl_result != CURLE_OK)
474474
{
@@ -700,7 +700,7 @@ void NetworkClient::private_cleanup_before()
700700
curl_easy_setopt(curl_handle, CURLOPT_SEEKFUNCTION, nullptr);
701701
}
702702

703-
void NetworkClient::private_cleanup_after()
703+
void NetworkClient::cleanupAfter()
704704
{
705705
m_currentActionType = ActionType::atNone;
706706
m_QueryHeaders.clear();
@@ -884,7 +884,7 @@ bool NetworkClient::private_apply_method()
884884

885885
void NetworkClient::setReferer(const std::string &str)
886886
{
887-
curl_easy_setopt(curl_handle, CURLOPT_REFERER, str.c_str());
887+
curl_easy_setopt(curl_handle, CURLOPT_REFERER, !str.empty() ? str.c_str() : nullptr);
888888
}
889889

890890
int NetworkClient::getCurlResult()

Source/Core/Network/NetworkClient.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ class NetworkClient: public INetworkClient
239239
ActionType currrentActionType() const;
240240

241241
static void clearThreadData();
242+
243+
void cleanupAfter() override;
242244
/*! @endcond */
243245
private:
244246

@@ -280,7 +282,6 @@ class NetworkClient: public INetworkClient
280282
bool private_apply_method();
281283
void private_parse_headers();
282284
void private_cleanup_before();
283-
void private_cleanup_after();
284285
bool private_on_finish_request();
285286
void private_init_transfer();
286287
void private_checkResponse();

Source/Core/Upload/DefaultUploadEngine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ bool CDefaultUploadEngine::DoAction(UploadAction& Action)
406406
}
407407
}
408408

409+
defer<void>([this] {
410+
m_NetworkClient->setReferer({});
411+
m_NetworkClient->cleanupAfter();
412+
});
413+
409414
if (!Action.Description.empty())
410415
SetStatus(stUserDescription, Action.Description);
411416
else

Source/Core/Upload/Tests/DefaultUploadEngineTest.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ TEST_F(DefaultUploadEngineTest, login)
134134
ON_CALL(networkClient, doGet(_)).WillByDefault(Return(true));
135135
ON_CALL(networkClient, doPost(_)).WillByDefault(Return(true));
136136
ON_CALL(networkClient, responseCode()).WillByDefault(Return(200));
137-
{
138-
testing::InSequence dummy;
139-
EXPECT_CALL(networkClient, setReferer(action1.Referer));
137+
//{
138+
//testing::InSequence dummy;
139+
EXPECT_CALL(networkClient, setReferer(action1.Referer)).Times(AtLeast(1));
140140
EXPECT_CALL(networkClient, addQueryParam("password", "qwerty"));
141141
EXPECT_CALL(networkClient, addQueryParam("login", "username"));
142142
EXPECT_CALL(networkClient, addQueryParam("submit", "1"));
143143
EXPECT_CALL(networkClient, doPost("")).WillOnce(Return(true));
144144
EXPECT_CALL(networkClient, responseBody()).WillOnce(Return("<result>success</result>"));
145-
}
145+
EXPECT_CALL(networkClient, setReferer("")).Times(AtLeast(1));
146+
//}
146147
std::string displayName = IuCoreUtils::ExtractFileName(constSizeFileName);
147148
auto fileTask = std::make_shared<FileUploadTask>(constSizeFileName, displayName);
148149
ServerSettingsStruct serverSettings;
@@ -197,6 +198,7 @@ TEST_F(DefaultUploadEngineTest, shortenUrl)
197198
ON_CALL(networkClient, urlEncode(_)).WillByDefault(Return("http%3A%2F%2Fexample.com%2Fhello%3Fsomeparam%3D1"));
198199

199200
EXPECT_CALL(networkClient, setReferer(action1.Referer));
201+
EXPECT_CALL(networkClient, setReferer(""));
200202
EXPECT_CALL(networkClient, setUrl("https://example.com/shorten?url=http%3A%2F%2Fexample.com%2Fhello%3Fsomeparam%3D1"));
201203
{
202204
testing::InSequence dummy;
@@ -267,6 +269,7 @@ TEST_F(DefaultUploadEngineTest, json)
267269
ON_CALL(networkClient, urlEncode(_)).WillByDefault(Return("http%3A%2F%2Fexample.com%2Fhello%3Fsomeparam%3D1"));
268270

269271
EXPECT_CALL(networkClient, setReferer(action1.Referer));
272+
EXPECT_CALL(networkClient, setReferer(""));
270273
EXPECT_CALL(networkClient, setUrl("https://example.com/shorten?url=http%3A%2F%2Fexample.com%2Fhello%3Fsomeparam%3D1"));
271274
{
272275
testing::InSequence dummy;

Source/Core/Utils/CoreUtils.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,24 @@ std::string GetFileMimeTypeByName(const std::string& fileName) {
553553
return mime;
554554
}
555555

556+
std::string GetFileMimeTypeByContents(const std::string& fileName)
557+
{
558+
const std::string DefaultMimeType = "application/octet-stream";
559+
FILE* f = FopenUtf8(fileName.c_str(), "rb");
560+
if (!f) {
561+
return DefaultMimeType;
562+
}
563+
char buffer[256] {};
564+
size_t readBytes = fread(buffer, 1, sizeof(buffer), f);
565+
fclose(f);
566+
int resultPrio = 0;
567+
568+
auto* mime = xdg_mime_get_mime_type_for_data(buffer, readBytes, &resultPrio);
569+
570+
if (!mime) {
571+
return DefaultMimeType;
572+
}
573+
return mime;
574+
}
575+
556576
} // end of namespace IuCoreUtils

Source/Core/Utils/CoreUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ namespace IuCoreUtils
9494
int64_t StringToInt64(const std::string& str);
9595
std::string GetFileMimeType(const std::string&);
9696
std::string GetFileMimeTypeByName(const std::string& fileName);
97+
std::string GetFileMimeTypeByContents(const std::string& fileName);
9798
std::string GetDefaultExtensionForMimeType(const std::string&);
9899
std::string StrReplace(std::string text, std::string s, std::string d);
99100
std::string ConvertToUtf8(const std::string &text, const std::string& codePage);

0 commit comments

Comments
 (0)