Skip to content

Commit

Permalink
Fixed hosting getapic.me, fixed memory leaks
Browse files Browse the repository at this point in the history
Fixed file handle leak in mmap function
Cleanup XdgMime library data on exit
Servers Checker: Fixed mime content type check in
  • Loading branch information
zenden2k committed Oct 24, 2024
1 parent b0e0a35 commit ca1e049
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Data/Scripts/dosya.nut
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ function UploadFile(FileName, options) {
local action = form.attr("action");
nm.setUrl(action);
form.find("textarea").each(function(index, elem) {
nm.addQueryParam(elem.attr("name"), elem.text());
nm.addQueryParam(elem.attr("name"), strip(elem.text()));
});
nm.doPost("");

if (nm.responseCode() == 200) {
local doc3 = Document(nm.responseBody());
local link = doc3.find("textarea#ic0-").text();
local link = strip(doc3.find("textarea#ic0-").text());
options.setViewUrl(link);
if (link != "") {
return 1;
Expand Down
5 changes: 3 additions & 2 deletions Data/servers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,9 @@
<Call Function="json" Arg1="result.success" AssignVars="Success:0;"/>
<RegExp Data="$(Success)" Pattern="True" AssignVars=""/>
</Action>
<Action Type="get" Url="https://getapic.me/" RegExp="session\]'\)\.val\('(.+?)'" AssignVars="Session:0" OnlyOnce="0">
<RegExp Pattern="suid\]'\)\.val\('(.+?)'" AssignVars="Suid:0"/>
<Action Type="get" Url="https://getapic.me/" CustomHeaders="X-Requested-With:XMLHttpRequest">
<Call Function="json" Arg1="result.data.suid" AssignVars="Suid:0;"/>
<Call Function="json" Arg1="result.data.session" AssignVars="Session:0;"/>
</Action>
<Action Type="upload" Url="https://getapic.me/upload" CustomHeaders="X-Requested-With:XMLHttpRequest" Referer="https://getapic.me/"
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%;"
Expand Down
16 changes: 13 additions & 3 deletions Source/Core/3rdpart/xdgmime/ports/mman.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,19 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
if (flags & MAP_PRIVATE)
dwDesiredAccess |= FILE_MAP_COPY;
void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length);

// Mapped views of a file mapping object maintain internal references to the object, and a
// file mapping object does not close until all references to it are released. Therefore,
// to fully close a file mapping object, an application must unmap all mapped views of the
// file mapping object by calling UnmapViewOfFile and close the file mapping object handle
// by calling CloseHandle. These functions can be called in any order.
//
// Although an application may close the file handle used to create a file mapping object,
// the system holds the corresponding file open until the last view of the file is unmapped.
// Files for which the last view has not yet been unmapped are held open with no sharing
// restrictions.
CloseHandle(h);
if (ret == NULL) {
CloseHandle(h);
ret = MAP_FAILED;
}
return ret;
Expand All @@ -94,8 +105,7 @@ static void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t
static void munmap(void *addr, size_t length)
{
UnmapViewOfFile(addr);
/* ruh-ro, we leaked handle from CreateFileMapping() ... */
}

#undef DWORD_HI
#undef DWORD_LO
#undef DWORD_LO
3 changes: 2 additions & 1 deletion Source/Core/Network/INetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class INetworkClient {
virtual std::string urlDecode(const std::string& str){ return std::string(); }
virtual void setMaxUploadSpeed(uint64_t speed){}
virtual void setMaxDownloadSpeed(uint64_t speed) {}
virtual void cleanupAfter() {}
};

class INetworkClientFactory {
Expand All @@ -110,4 +111,4 @@ class INetworkClientFactory {
virtual ~INetworkClientFactory() = default;
};

#endif
#endif
6 changes: 3 additions & 3 deletions Source/Core/Network/NetworkClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ bool NetworkClient::doUploadMultipartData()
bool NetworkClient::private_on_finish_request()
{
private_checkResponse();
private_cleanup_after();
cleanupAfter();
private_parse_headers();
if (curl_result != CURLE_OK)
{
Expand Down Expand Up @@ -700,7 +700,7 @@ void NetworkClient::private_cleanup_before()
curl_easy_setopt(curl_handle, CURLOPT_SEEKFUNCTION, nullptr);
}

void NetworkClient::private_cleanup_after()
void NetworkClient::cleanupAfter()
{
m_currentActionType = ActionType::atNone;
m_QueryHeaders.clear();
Expand Down Expand Up @@ -884,7 +884,7 @@ bool NetworkClient::private_apply_method()

void NetworkClient::setReferer(const std::string &str)
{
curl_easy_setopt(curl_handle, CURLOPT_REFERER, str.c_str());
curl_easy_setopt(curl_handle, CURLOPT_REFERER, !str.empty() ? str.c_str() : nullptr);
}

int NetworkClient::getCurlResult()
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Network/NetworkClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ class NetworkClient: public INetworkClient
ActionType currrentActionType() const;

static void clearThreadData();

void cleanupAfter() override;
/*! @endcond */
private:

Expand Down Expand Up @@ -280,7 +282,6 @@ class NetworkClient: public INetworkClient
bool private_apply_method();
void private_parse_headers();
void private_cleanup_before();
void private_cleanup_after();
bool private_on_finish_request();
void private_init_transfer();
void private_checkResponse();
Expand Down
5 changes: 5 additions & 0 deletions Source/Core/Upload/DefaultUploadEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ bool CDefaultUploadEngine::DoAction(UploadAction& Action)
}
}

defer<void>([this] {
m_NetworkClient->setReferer({});
m_NetworkClient->cleanupAfter();
});

if (!Action.Description.empty())
SetStatus(stUserDescription, Action.Description);
else
Expand Down
11 changes: 7 additions & 4 deletions Source/Core/Upload/Tests/DefaultUploadEngineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,16 @@ TEST_F(DefaultUploadEngineTest, login)
ON_CALL(networkClient, doGet(_)).WillByDefault(Return(true));
ON_CALL(networkClient, doPost(_)).WillByDefault(Return(true));
ON_CALL(networkClient, responseCode()).WillByDefault(Return(200));
{
testing::InSequence dummy;
EXPECT_CALL(networkClient, setReferer(action1.Referer));
//{
//testing::InSequence dummy;
EXPECT_CALL(networkClient, setReferer(action1.Referer)).Times(AtLeast(1));
EXPECT_CALL(networkClient, addQueryParam("password", "qwerty"));
EXPECT_CALL(networkClient, addQueryParam("login", "username"));
EXPECT_CALL(networkClient, addQueryParam("submit", "1"));
EXPECT_CALL(networkClient, doPost("")).WillOnce(Return(true));
EXPECT_CALL(networkClient, responseBody()).WillOnce(Return("<result>success</result>"));
}
EXPECT_CALL(networkClient, setReferer("")).Times(AtLeast(1));
//}
std::string displayName = IuCoreUtils::ExtractFileName(constSizeFileName);
auto fileTask = std::make_shared<FileUploadTask>(constSizeFileName, displayName);
ServerSettingsStruct serverSettings;
Expand Down Expand Up @@ -197,6 +198,7 @@ TEST_F(DefaultUploadEngineTest, shortenUrl)
ON_CALL(networkClient, urlEncode(_)).WillByDefault(Return("http%3A%2F%2Fexample.com%2Fhello%3Fsomeparam%3D1"));

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

EXPECT_CALL(networkClient, setReferer(action1.Referer));
EXPECT_CALL(networkClient, setReferer(""));
EXPECT_CALL(networkClient, setUrl("https://example.com/shorten?url=http%3A%2F%2Fexample.com%2Fhello%3Fsomeparam%3D1"));
{
testing::InSequence dummy;
Expand Down
20 changes: 20 additions & 0 deletions Source/Core/Utils/CoreUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,24 @@ std::string GetFileMimeTypeByName(const std::string& fileName) {
return mime;
}

std::string GetFileMimeTypeByContents(const std::string& fileName)
{
const std::string DefaultMimeType = "application/octet-stream";
FILE* f = FopenUtf8(fileName.c_str(), "rb");
if (!f) {
return DefaultMimeType;
}
char buffer[256] {};
size_t readBytes = fread(buffer, 1, sizeof(buffer), f);
fclose(f);
int resultPrio = 0;

auto* mime = xdg_mime_get_mime_type_for_data(buffer, readBytes, &resultPrio);

if (!mime) {
return DefaultMimeType;
}
return mime;
}

} // end of namespace IuCoreUtils
1 change: 1 addition & 0 deletions Source/Core/Utils/CoreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ namespace IuCoreUtils
int64_t StringToInt64(const std::string& str);
std::string GetFileMimeType(const std::string&);
std::string GetFileMimeTypeByName(const std::string& fileName);
std::string GetFileMimeTypeByContents(const std::string& fileName);
std::string GetDefaultExtensionForMimeType(const std::string&);
std::string StrReplace(std::string text, std::string s, std::string d);
std::string ConvertToUtf8(const std::string &text, const std::string& codePage);
Expand Down
4 changes: 3 additions & 1 deletion Source/Image Uploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class Application {
IuCommonFunctions::ClearTempFolder(commonTempFolder_ + _T("\\") + folder);
}

xdg_mime_set_dirs(nullptr);
xdg_mime_shutdown();
// deletes empty temp directory
RemoveDirectory(commonTempFolder_);
}
Expand Down Expand Up @@ -134,7 +136,7 @@ class Application {
if (dataFolder.Right(1) == "\\") {
dataFolder.Truncate(dataFolder.GetLength() - 1);
}
std::string dir = WinUtils::wstostr(dataFolder.GetString(), CP_ACP);
std::string dir = W2U(dataFolder);
char* cacheDir = strdup(dir.c_str());
if (cacheDir) {
const char* dirs[2]
Expand Down
2 changes: 1 addition & 1 deletion Source/ServerListTool/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CString GetFileInfo(CString fileName, MyFileInfo* mfi)
{
int64_t fileSize = IuCoreUtils::GetFileSize(W2U(fileName));
CString result = MyBytesToString(fileSize) + _T("(") + std::to_wstring(fileSize).c_str() + _T(" bytes);");
CString mimeType = IuCoreUtils::GetFileMimeType(W2U(fileName)).c_str();
CString mimeType = IuCoreUtils::GetFileMimeTypeByContents(W2U(fileName)).c_str();
result += mimeType + _T(";");
if (mfi) mfi->mimeType = mimeType;
if (mimeType.Find(_T("image/")) >= 0) {
Expand Down
3 changes: 1 addition & 2 deletions Source/ServerListTool/ServersChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,9 @@ void ServersChecker::onFileFinished(bool ok, int /*statusCode*/, CFileDownloader

CString report = GetFileInfo(fileName, &mfi);

CString mimeType = Utf8ToWCstring(IuCoreUtils::GetFileMimeType(WCstringToUtf8((fileName))));
if (fileId < 2) // is thumb or image
{
if (mimeType.Find(_T("image/")) >= 0) {
if (mfi.mimeType.Find(_T("image/")) >= 0) {
if (fileId == 0 && (mfi.width != m_sourceFileInfo.width || mfi.height != m_sourceFileInfo.height))
serverData.stars[fileId] = 0;
else
Expand Down

0 comments on commit ca1e049

Please sign in to comment.